Skip to content

Commit

Permalink
added fix such that the use of a maximum lead time for models (max_lt…
Browse files Browse the repository at this point in the history
…) works and can loosely be added as part of kwargs
  • Loading branch information
bohlinger committed Aug 29, 2024
1 parent eda577b commit 1fdd00e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
21 changes: 12 additions & 9 deletions wavy/collocation_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def collocation_fct(obs_lons, obs_lats, model_lons, model_lats):
return index_array_2d, distance_array, valid_output_index


def get_model_filename(nID, d, leadtime):
def get_model_filename(nID, d, leadtime, **kwargs):
mco = mc(nID=nID, sd=d, ed=d, leadtime=leadtime)
return mco._make_model_filename_wrapper(parse_date(str(d)), leadtime)
return mco._make_model_filename_wrapper(parse_date(str(d)),
leadtime, **kwargs)


def find_valid_fc_dates_for_model_and_leadtime(fc_dates, model, leadtime):
def find_valid_fc_dates_for_model_and_leadtime(fc_dates, model,
leadtime, **kwargs):
'''
Finds valid dates that are close to desired dates at a precision
of complete hours
Expand All @@ -84,13 +86,13 @@ def find_valid_fc_dates_for_model_and_leadtime(fc_dates, model, leadtime):
# pass
#else:
fc_dates_new = [d for d in fc_dates_new
if get_model_filename(model, d, leadtime) is not None]
if get_model_filename(model, d, leadtime, **kwargs)
is not None]
return fc_dates_new


def check_if_file_is_valid(fc_date, model, leadtime, max_lt=None):
fname = get_model_filename(
model, fc_date, leadtime, max_lt=max_lt)
def check_if_file_is_valid(fc_date, model, leadtime, **kwargs):
fname = get_model_filename(model, fc_date, leadtime, **kwargs)
print('Check if requested file:\n', fname, '\nis available and valid')
try:
nc = netCDF4.Dataset(fname, mode='r')
Expand Down Expand Up @@ -133,7 +135,7 @@ class collocation_class(qls):
'''

def __init__(self, oco=None, model=None, poi=None,
distlim=None, leadtime=None, max_lt=None, **kwargs):
distlim=None, leadtime=None, **kwargs):
print('# ----- ')
print(" ### Initializing collocation_class object ###")
print(" ")
Expand Down Expand Up @@ -312,7 +314,8 @@ def _collocate_track(self, **kwargs):
ndt_datetime = [parse_date(str(d)) for d in ndt]

ndt_valid = find_valid_fc_dates_for_model_and_leadtime(
ndt, self.model, self.leadtime)
ndt, self.model, self.leadtime,
**kwargs)

ndt_valid = np.unique(ndt_valid)

Expand Down
17 changes: 8 additions & 9 deletions wavy/model_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,14 @@ def _make_model_filename(self, fc_date, leadtime):
.replace(">", "\\>")
return filename

def _make_model_filename_wrapper(
self, fc_date, leadtime, max_lt=None, **kwargs):
def _make_model_filename_wrapper(self, fc_date, leadtime, **kwargs):
"""
Wrapper function of make_model_filename. Organizes various cases.
param:
model - modelname type(str)
fc_date - datetime object
leadtime - integer in hours
max_lt - maximum lead time allowed
return:
filename
Expand Down Expand Up @@ -362,10 +360,12 @@ def _make_model_filename_wrapper(
+ " with extended leadtime")
leadtime = (leadtime
+ vars(self.cfg)['misc']['init_step'])
if max_lt is not None and leadtime > max_lt:
if (kwargs.get('max_lt') is not None
and leadtime > kwargs.get('max_lt')):
print("Leadtime:", leadtime,
"is greater as maximum allowed leadtime:",
max_lt)
str(kwargs.get('max_lt')))
break
else:
filename = None
elif (isinstance(fc_date, list) and isinstance(leadtime, int)):
Expand All @@ -380,15 +380,14 @@ def _make_model_filename_wrapper(

return filename

def _make_list_of_model_filenames(self, fc_dates, lt):
def _make_list_of_model_filenames(self, fc_dates, lt, **kwargs):
"""
return: flst - list of model files to be opened
dlst - list of dates to be chosen within each file
"""
#fn = make_model_filename_wrapper(datetime(2021,1,1,1),1)
flst = []
for d in fc_dates:
fn = self._make_model_filename_wrapper(d, lt)
fn = self._make_model_filename_wrapper(d, lt, **kwargs)
if fn is not None:
flst.append(fn)
return flst
Expand Down Expand Up @@ -493,7 +492,7 @@ def list_input_files(self, show=False, **kwargs):
self.cfg.misc['date_incr'])

pathlst = self._make_list_of_model_filenames(
fc_dates, self.leadtime)
fc_dates, self.leadtime, **kwargs)
else:
# if defined path local
print(" ## Find and list files ...")
Expand Down

0 comments on commit 1fdd00e

Please sign in to comment.