From 08a3b5d070ebf80ca15321b42c3c4519bc8f8704 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 19 Dec 2018 16:44:15 -0800 Subject: [PATCH] Fixes for changes to downloadable data and "calendar" missing Downloaded runoff (and other) data are now missing the "calendar" attribute for the time variable. This fix uses the default calendar specified in the config file if that's missing and emits a warning. Also adds mm/day as an acceptable unit. Both were causing crashes in current use. --- rvic/convolution.py | 3 ++- rvic/core/read_forcing.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/rvic/convolution.py b/rvic/convolution.py index fee3b93..afa013e 100644 --- a/rvic/convolution.py +++ b/rvic/convolution.py @@ -137,7 +137,8 @@ def convolution_init(config): forcings['LATITUDE_VAR'], forcings['DATL_LIQ_FLDS'], forcings['START'], - forcings['END']) + forcings['END'], + options,) # ---------------------------------------------------------------- # # ---------------------------------------------------------------- # diff --git a/rvic/core/read_forcing.py b/rvic/core/read_forcing.py index 70417df..ab3351e 100644 --- a/rvic/core/read_forcing.py +++ b/rvic/core/read_forcing.py @@ -28,7 +28,7 @@ class DataModel(object): # ---------------------------------------------------------------- # # Initialize def __init__(self, path, file_str, time_fld, lat_fld, liq_flds, - start, end): + start, end, options): self.path = path self.time_fld = time_fld @@ -125,7 +125,15 @@ def __init__(self, path, file_str, time_fld, lat_fld, liq_flds, if i == 0: # get the calendar and units information - self.calendar = f.variables[self.time_fld].calendar + if "calendar" in f.variables[self.time_fld].ncattrs(): + self.calendar = f.variables[self.time_fld].calendar + use_default_calendar = False + else: + self.calendar = options["CALENDAR"] + use_default_calendar = True # this is a fix for missing calendar attributes in netCDF files. + # We'll ignore the future checks for same calendar + log.warning('Initial file has no calendar information - using default') + self.time_units = f.variables[self.time_fld].units time_series = f.variables[self.time_fld][:] @@ -144,11 +152,11 @@ def __init__(self, path, file_str, time_fld, lat_fld, liq_flds, # check that the units match the first file if f.variables[self.time_fld].units != self.time_units: raise ValueError('Units do not match in input files') - if f.variables[self.time_fld].calendar != self.calendar: + if not use_default_calendar and f.variables[self.time_fld].calendar != self.calendar: + # only check if it's a mismatch if we're not using the default calendar raise ValueError('Calendars do not match in input files') - time_series = np.append(time_series, - f.variables[self.time_fld][:]) + time_series = np.append(time_series, f.variables[self.time_fld][:]) f.close() @@ -212,7 +220,7 @@ def start(self, timestamp, rout_var): if units in ['kg/m2*s', 'kg m-2 s-1', 'kg m^-2 s^-1', 'kg*m-2*s-1', 'kg s-1 m-2']: self.fld_mult[fld] = 1.0 - elif units in ['mm', 'MM', 'milimeters', 'Milimeters']: + elif units in ['mm', 'MM', 'milimeters', 'Milimeters', 'mm/day']: self.fld_mult[fld] = WATERDENSITY / MMPERMETER / self.secs_per_step elif units in ['m', 'M', 'meters', 'Meters']: self.fld_mult[fld] = WATERDENSITY / self.secs_per_step