-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathera5_year_checker.py
126 lines (89 loc) · 3.55 KB
/
era5_year_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/local/apps/python/2.7.3-02/bin/python
import subprocess
import calendar
import datetime
import os
import sys
import submit_job_rsync_netcdf
from elasticsearch import Elasticsearch
import requests
query = {
"query": {"match_all": {}}
}
'''
es.search(index="ceda-fbi", body=query)
es.indices.get_mapping(index="ceda-fbi")
'''
es_url = 'https://jasmin-es1.ceda.ac.uk/ceda-fbi/_search'
def poll_fbi(dir_path):
query = {
"query": {
"match_phrase_prefix": {
"info.directory.analyzed":dir_path
}
},
"size": 40
}
resp = requests.post(es_url, json=query)
return resp
starting_dir = '/badc/ecmwf-era5/data/'
next_dir_map = {'era5-s-nc': 'oper/an_sfc',
'era5-m-nc': 'oper/an_ml',
'era5-s-fc-nc': 'oper/fc_sfc',
'era5-s-en-nc': 'enda/an_sfc',
'era5-s-en-mean': 'enda/em_sfc',
'era5-s-en-sd' : 'enda/es_sfc'
}
target_dir_mapping = {'era5-s-nc': 'ecmwf-era5',
'era5-m-nc': 'ecmwf-era5-model',
'era5-s-fc-nc': 'ecmwf-era5-fc',
'era5-s-en-nc': 'ecmwf-era5-en-mem',
'era5-s-en-mean': 'ecmwf-era5-en-mean',
'era5-s-en-sd' : 'ecmwf-era5-en-mean'
}
def check_dir(selected_stream, year):
year_dir = os.path.join(starting_dir, next_dir_map[selected_stream],year)
for month_check in range(1,13):
no_of_days = calendar.monthrange(int(year),month_check)[1]
if month_check < 10:
month_check = '0' + str(month_check)
else:
month_check = str(month_check)
missing_days = []
for day_of_month in range(1,no_of_days+1):
if day_of_month < 10:
day_of_month = '0' + str(day_of_month)
else:
day_of_month = str(day_of_month)
date_to_check = os.path.join(year_dir,month_check, day_of_month)
resp = poll_fbi(date_to_check)
if not resp.json()['hits']['hits']:
missing_days.append('%s%s%s'% (year,month_check,day_of_month))
if missing_days:
return missing_days
if __name__ == "__main__":
stream = sys.argv[1]
for year in range(2019, 1979,-1):
missing_days = check_dir(stream, str(year))
if missing_days:
missing_days[0], missing_days[-1]
keywords = {'start': missing_days[0],
'end': missing_days[-1],
'target_dir': target_dir_mapping[stream],
'qos' : 'normal'
}
if stream == 'era5-m-nc':
keywords['qos'] = 'long'
if stream == 'era5-s-en-nc':
keywords['qos'] = 'large'
print keywords, stream
backfill_file = 'era5_control/%s-backfill.txt'%stream
f = open(backfill_file, 'r')
extracting_month = missing_days[0][0:6]
lines = f.read()
answer = lines.find('string')
if not answer:
with open(backfill_file,'w') as backfill_log:
backfill_log.write('%s|%s|%s'% (datetime.datetime.now().strftime('%Y%m%d'), keywords['start'], keywords['end']))
#x = submit_job_rsync_netcdf.MarsJob(stream, keywords)
break