Source code for pycif.plugins.datastreams.fields.oldlmdz_photochem.utils
import datetime
import glob
import os
import calendar
import numpy as np
[docs]
def find_valid_file(ref_dir, file_format, dd):
#print(' Get all files and dates matching the file and format')
list_files_orig = os.listdir(ref_dir)
list_files_avail = [] #list available files
# if dd first hour of the month, needs file of the month before
for f in list_files_orig:
try:
check = datetime.datetime.strptime(f, file_format)
# use small list
#if -62 < (check - dd).days < 62:
# list_files_avail.append(f)
if check.year == dd.year and check.month == dd.month:
list_files_avail.append(f)
if dd.hour == 0:
if -1 > (check - dd).days >= -31:
list_files_avail.append(f)
except:
continue
#print('lllllllllllllll',list_files_avail)
list_files = []
list_dates = []
# actually in each file, information every hour
# but at half hours
delta_t = 1
for f in list_files_avail:
date_beg = datetime.datetime.strptime(f, file_format) + datetime.timedelta(hours = 0.5)
date_end = date_beg + datetime.timedelta( days = calendar.mdays[date_beg.month] + calendar.isleap(date_beg.year))
nb_deltas = int((date_end - date_beg).days * 24 / delta_t)
#print(nb_deltas)
list_dates_covered = [ date_beg + ( k + 1 ) * datetime.timedelta( hours = delta_t ) for k in range(nb_deltas) ]
#print(list_dates_covered)
for d in list_dates_covered:
list_files.append(f)
list_dates.append(d)
list_files = np.array(list_files)
list_dates = np.array(list_dates)
# Sorting along dates
isort = np.argsort(list_dates)
list_dates = list_dates[isort]
list_files = list_files[isort]
if list_files == []:
raise Exception("Did not find any valid CAMS files in {} "
"with format {}. Please check your yml file"
.format(ref_dir, file_format))
# Compute deltas
mask = (list_dates - dd) <= datetime.timedelta(0)
#print(mask)
file_ref1 = ref_dir + list_files[mask][np.argmax(list_dates[mask])]
date_ref1 = list_dates[mask].max()
#print(file_ref1, date_ref1,dd)
mask = (list_dates - dd) >= datetime.timedelta(0)
#print('mmmm',mask)
file_ref2 = ref_dir + list_files[mask][np.argmin(list_dates[mask])]
date_ref2 = list_dates[mask].min()
#print(file_ref2, date_ref2,dd)
return [file_ref1, file_ref2], [date_ref1, date_ref2]