-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmes_validate.py
executable file
·68 lines (64 loc) · 2.17 KB
/
mmes_validate.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
#!/usr/bin/env python3
import os
import re
from _datetime import datetime, timedelta
from cdo import Cdo
# set general objects
cdo = Cdo()
# check time function
def check_time(file, date_start, shape):
"""
Check a netCdf or Grib forecast file against a required time interval
:param file: filename (netcdf or grib) to evaluate
:param date_start: desired start date of file YYYMMDD format
:param shape: number of hourly time steps to have a valid file
:return: true if the file is valid
"""
# check if file exists
if not os.path.isfile(file):
return False
#check if file is empty
if os.stat(file).st_size == 0:
return False
# calculate date_start and date end
startdate = datetime.strptime(date_start, "%Y%m%d")
enddate = startdate + timedelta(hours=int(shape)-1)
try:
# get info from cdo sinfon
info = cdo.sinfon(input=file)
except:
print('error opening ' + file)
return False
# set empty variables
first_date = last_date = ''
timesteps = []
for i in info:
# match reftime date (always lower then first date
m1 = re.match(r'RefTime.*(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})', i)
if m1:
try:
reftime = datetime.strptime(m1.group(1), "%Y-%m-%d %H:%M:%S")
except ValueError:
print('Error while finding reftime')
continue # next loop
# match all dates in array
m2 = re.findall(r'(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})', i)
if m2:
try:
# add all dates to array
timesteps += [datetime.strptime(d, "%Y-%m-%d %H:%M:%S") for d in m2]
except ValueError:
print('Error while finding last date')
except:
print('Error in time dimension')
# if no date are present abord
if len(timesteps)==0:
return False
# get first date and last date
first_date = min(timesteps)
last_date = max(timesteps)
# check if date interval include our interval of interest
if first_date <= startdate and last_date >= enddate:
return True
else:
return False