def initial(self): self.var.irrMgmtParameterFileNC = self.var._configuration.irrMgmtOptions[ 'irrMgmtParameterNC'] self.var.parameter_names = [ 'IrrMethod', 'IrrInterval', 'SMT1', 'SMT2', 'SMT3', 'SMT4', 'MaxIrr', 'AppEff', 'NetIrrSMT', 'WetSurf' ] for param in self.var.parameter_names: # nm = '_' + var vars(self.var)[param] = vos.netcdf2PCRobjCloneWithoutTime( self.var.irrMgmtParameterFileNC, param, cloneMapFileName=self.var.cloneMap) # check if an irrigation schedule file is required if np.sum(self.var.IrrMethod == 3) > 0: if self.var._configuration.irrMgmtOptions[ 'irrScheduleNC'] != "None": self.var._configuration.irrMgmtOptions[ 'irrScheduleNC'] = vos.getFullPath( self.var._configuration.irrMgmtOptions[item], self.var._configuration.globalOptions['inputDir']) self.var.irrScheduleFileNC = self.var._configuration.irrMgmtOptions[ 'irrScheduleNC'] else: logger.error( 'IrrMethod equals 3 in some or all places, but irrScheduleNC is not set in configuration file' ) else: self.var.irrScheduleFileNC = None
def create_output_directories(self): cleanOutputDir = False if cleanOutputDir: try: shutil.rmtree(self.FILE_PATHS['PathOut']) except: pass try: os.makedirs(self.FILE_PATHS['PathOut']) except: pass # make temp directory self.tmpDir = vos.getFullPath("tmp/", self.FILE_PATHS['PathOut']) if os.path.exists(self.tmpDir): shutil.rmtree(self.tmpDir) os.makedirs(self.tmpDir) # make netcdf directory self.outNCDir = vos.getFullPath("netcdf/", self.FILE_PATHS['PathOut']) if os.path.exists(self.outNCDir): shutil.rmtree(self.outNCDir) os.makedirs(self.outNCDir) # make and populate backup directory for Python scripts self.scriptDir = vos.getFullPath("scripts/", self.FILE_PATHS['PathOut']) if os.path.exists(self.scriptDir): shutil.rmtree(self.scriptDir) os.makedirs(self.scriptDir) # working/starting directory where all Python scripts are located path_of_this_module = os.path.abspath(os.path.dirname(__file__)) self.starting_directory = path_of_this_module all_files = glob.glob(os.path.join(path_of_this_module, '*.py')) for filename in all_files: shutil.copy(filename, self.scriptDir) # make log directory self.logFileDir = vos.getFullPath("log/", self.FILE_PATHS['PathOut']) cleanLogDir = True if os.path.exists(self.logFileDir) and cleanLogDir: shutil.rmtree(self.logFileDir) os.makedirs(self.logFileDir) # make end state directory self.endStateDir = vos.getFullPath("states/", self.FILE_PATHS['PathOut']) if os.path.exists(self.endStateDir): shutil.rmtree(self.endStateDir) os.makedirs(self.endStateDir)
def read(self): # reading canal supply: if self.include_canal_system: if self.VariableCanalSupply: if self.DailyCanalNC: # introduce this test so that we do not ask the model to read # a file from a timestep prior to the current simulation. if not (self._modelTime.isFirstTimestep() & (self.canalTimeLag > 0)): day, month, year = self._modelTime.day, self._modelTime.month, self._modelTime.year canalFileNC = self.canalFileNC.format(day=day, month=month, year=year) exists = os.path.exists(canalFileNC) max_wait_time = 60 wait_time = 0.1 total_wait_time = 0 while exists is False and total_wait_time <= max_wait_time: time.sleep(wait_time) exists = os.path.exists(canalFileNC) total_wait_time += wait_time if not exists: msg = "canal file doesn't exist and maximum wait time exceeded" raise AQError(msg) CanalSupply = vos.netcdf2PCRobjCloneWithoutTime( canalFileNC, self.canalVarName, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.CanalSupply = CanalSupply[self.landmask] else: CanalSupply = vos.netcdf2PCRobjClone( self.canalFileNC, self.canalVarName, str(currTimeStep.fulldate), useDoy=method_for_time_index, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.CanalSupply = CanalSupply[self.landmask] else: CanalSupply = vos.netcdf2PCRobjCloneWithoutTime( self.canalFileNC, self.canalVarName, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.CanalSupply = CanalSupply[self.landmask]
def initial(self): # Define initial water contents self.var.initialConditionFileNC = self.var._configuration.globalOptions[ 'initialConditionNC'] th = vos.netcdf2PCRobjCloneWithoutTime( self.var.initialConditionFileNC, 'th', cloneMapFileName=self.var.cloneMap) init_cond_interp_method = self.var._configuration.globalOptions[ 'InterpMethod'] # init_cond_type = self.var._configuration.globalOptions['initialConditionType'] if init_cond_interp_method.lower() == 'depth': depths = vos.netcdfDim2NumPy(self.var.initialConditionFileNC, 'depth') zBot = np.cumsum(self.var.dz) zTop = zBot - self.var.dz zMid = (zTop + zBot) / 2 # add zero point if depths[0] > 0: depths = np.concatenate([[0.], depths]) th = np.concatenate([th[0, ...][None, :], th], axis=0) if depths[-1] < zBot[-1]: depths = np.concatenate([depths, [zBot[-1]]]) th = np.concatenate([th, th[-1, ...][None, :]], axis=0) # now interpolate to compartments f_thini = interpolate.interp1d(depths, th, axis=0, bounds_error=False, fill_value=(th[0, ...], th[-1, ...])) th = f_thini(zMid) else: th = th[self.var.layerIndex, :, :] # NB original Matlab code also allows users to supply initial condition # as field capacity, wilting point or saturation. We do not explicitly # include this option but of course it is possible to do this by # supplying a netCDF file containing the values. However, note that in # the original version if field capacity is specified and groundwater # table is present the initial water content is set to th_fc_adj once # this variable has been computed. th = np.broadcast_to( th, (self.var.nCrop, self.var.nComp, self.var.nLat, self.var.nLon)) self.var.th = np.copy(th)
def __init__(self, configuration, modelTime, initialState=None): self._configuration = configuration self._modelTime = modelTime # clone map, land mask pcr.setclone(configuration.cloneMap) self.cloneMap = self._configuration.cloneMap self.landmask = vos.readPCRmapClone( configuration.globalOptions['landmask'], configuration.cloneMap, configuration.tmpDir, configuration.globalOptions['inputDir'], True) attr = vos.getMapAttributesALL(self.cloneMap) self.nLat = int(attr['rows']) self.nLon = int(attr['cols'])
def initial(self): self.WaterTable = bool( int(self._configuration.GROUNDWATER['WaterTable'])) self.VariableWaterTable = bool( int(self._configuration.GROUNDWATER['VariableWaterTable'])) self.DailyGroundwaterNC = bool( int(self._configuration.GROUNDWATER['DailyGroundwaterInputFile'])) zGW = np.ones_like(self.landmask) * 999. self.zGW = zGW[self.landmask] # self.zGW = np.ones((self.nCell)) * 999. if self.WaterTable: self.gwFileNC = self._configuration.GROUNDWATER[ 'groundwaterInputFile'] self.gwVarName = self._configuration.GROUNDWATER[ 'groundwaterVariableName'] self.gwTimeLag = int(self._configuration.GROUNDWATER['timeLag']) # if the program is configured to read daily groundwater files and # the time lag is positive, we also need to read an initial value # file if self.DailyGroundwaterNC & (self.gwTimeLag > 0): initialGroundwaterLevelNC = str( self._configuration. INITIAL_CONDITIONS['initialGroundwaterLevelInputFile']) zGW = vos.netcdf2PCRobjCloneWithoutTime( initialGroundwaterLevelNC, self.gwVarName, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.zGW = zGW[self.landmask]
def read_root_fraction(self): landmask = np.broadcast_to(self.var.landmask, (2, self.var.nLat, self.var.nLon)) root_fraction = vos.netcdf2PCRobjCloneWithoutTime( self.rootFractionNC, self.rootFractionVarName, cloneMapFileName=self.var.cloneMap) root_fraction = root_fraction[landmask].reshape(2, self.var.nCell) root_fraction = np.broadcast_to( root_fraction[None, None, :, :], (self.var.nFarm, self.var.nCrop, 2, self.var.nCell)) # scale root fraction - adapted from CWATM # landcoverType.py lines 311-319 # 1 - add top layer to root fraction input data root_fraction_adj = np.zeros( (self.var.nFarm, self.var.nCrop, self.var.nLayer, self.var.nCell)) top_layer_fraction = np.divide( self.var.root_depth[..., 0, :], (self.var.root_depth[..., 0, :] + self.var.root_depth[..., 1, :])) root_fraction_adj[..., 0, :] = top_layer_fraction * root_fraction[..., 0, :] root_fraction_adj[ ..., 1, :] = (1. - top_layer_fraction) * root_fraction[..., 1, :] root_fraction_adj[..., 2, :] = (1. - root_fraction[..., 1, :]) # 2 - scale so that the total root fraction sums to one root_fraction_sum = np.sum(root_fraction_adj, axis=-2) root_fraction_adj /= root_fraction_sum[..., None, :] self.var.root_fraction = root_fraction_adj.copy()
def read_potential_crop_yield(self): if self.var.AnnualChangeInPotYield: if self.var._modelTime.timeStepPCR == 1 or self.var._modelTime.doy == 1: date = '%04i-%02i-%02i' % (self.var._modelTime.year, 1, 1) self.var.Yx = vos.netcdf2PCRobjClone( self.var.PotYieldFileNC, self.var.PotYieldVarName, date, useDoy=None, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) else: if self.var._modelTime.timeStepPCR == 1: self.var.Yx = vos.netcdf2PCRobjCloneWithoutTime( self.var.PotYieldFileNC, self.var.PotYieldVarName, cloneMapFileName=self.var.cloneMap)
def set_landmask(self): self.landmask = vos.readPCRmapClone( self._configuration.globalOptions['landmask'], self._configuration.cloneMap, self._configuration.tmpDir, self._configuration.globalOptions['inputDir'], True) self.landmask = self.landmask > 0
def initial(self): self.var.fieldMgmtParameterFileNC = self.var._configuration.fieldMgmtOptions['fieldMgmtParameterNC'] self.var.parameter_names = ['Mulches','MulchPctGS','MulchPctOS','fMulch','Bunds','zBund','BundWater'] for var in self.var.parameter_names: # nm = '_' + var vars(self.var)[var] = vos.netcdf2PCRobjCloneWithoutTime( self.var.fieldMgmtParameterFileNC, var, cloneMapFileName=self.var.cloneMap)
def read(self): """Function to read crop input parameters""" if len(self.var.crop_parameters_to_read) > 0: for param in self.var.crop_parameters_to_read: # nm = '_' + param vars(self.var)[param] = vos.netcdf2PCRobjCloneWithoutTime( self.var.cropParameterFileNC, param, cloneMapFileName=self.var.cloneMap)
def read(self): relative_elevation_var_names = [ 'dzRel0001', 'dzRel0005', 'dzRel0010', 'dzRel0020', 'dzRel0030', 'dzRel0040', 'dzRel0050', 'dzRel0060', 'dzRel0070', 'dzRel0080', 'dzRel0090', 'dzRel0100' ] for var_name in relative_elevation_var_names: d = vos.netcdf2PCRobjCloneWithoutTime( str(self.lc_configuration['relativeElevationInputFile']), var_name, cloneMapFileName=self.var.cloneMap)[self.var.landmask] vars(self.var)[var_name] = d self.var.elevation_standard_deviation = vos.netcdf2PCRobjCloneWithoutTime( str(self.lc_configuration['elevationStandardDeviationInputFile']), 'dem_standard_deviation', cloneMapFileName=self.var.cloneMap)[self.var.landmask]
def read_max_root_depth(self): max_root_depth = vos.netcdf2PCRobjCloneWithoutTime( self.maxRootDepthNC, self.maxRootDepthVarName, cloneMapFileName=self.var.cloneMap) # nlat, nlon max_root_depth = max_root_depth[self.var.landmask] max_root_depth *= self.var.soildepth_factor # CALIBRATION self.var.max_root_depth = np.broadcast_to( max_root_depth[None, None, :], (self.var.nFarm, self.var.nCrop, self.var.nCell))
def read_fertiliser_app_rate(self, ncFile, ncVarName, date=None): fert_app_rate = vos.read_netCDF(ncFile, ncVarName, date, useDoy=None, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) fert_app_rate = self.reshape_fertiliser_app_rate( fert_app_rate, ncFile, ncVarName) return fert_app_rate
def read_fertiliser_price(self, ncFile, ncVarName, date=None): fert_price = vos.netcdf2PCRobjClone(ncFile, ncVarName, date, useDoy=None, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) fert_price = fert_price[self.var.landmask] return fert_price
def read_crop_area(self, date = None): if date is None: crop_area = vos.netcdf2PCRobjCloneWithoutTime( self.CropAreaFileNC, self.CropAreaVarName, cloneMapFileName = self.var.cloneMap, LatitudeLongitude = True) else: crop_area = vos.netcdf2PCRobjClone( self.CropAreaFileNC, self.CropAreaVarName, date, useDoy = None, cloneMapFileName = self.var.cloneMap, LatitudeLongitude = True) crop_area = np.reshape( crop_area[self.var.landmask_crop], (self.var.nCrop, self.var.nCell)) crop_area = np.float64(crop_area) return crop_area
def dynamic(self): # TODO: only read data if the year has changed if self.var._modelTime.timeStepPCR == 1 or self.var._modelTime.doy == 1: date = '%04i-%02i-%02i' %(self.var._modelTime.year, 1, 1) self.var.conc = vos.netcdf2PCRobjClone(self.var.co2FileNC, self.var.co2VarName, date, useDoy = None, cloneMapFileName = self.var.cloneMap, LatitudeLongitude = True)
def read(self): """Function to read crop input parameters""" if len(self.crop_parameters_to_read) > 0: for param in self.crop_parameters_to_read: d = vos.netcdf2PCRobjCloneWithoutTime( self.CropParameterNC, param, cloneMapFileName=self.var.cloneMap) d = d[self.var.landmask_crop].reshape(self.var.nCrop,self.var.nCell) vars(self.var)[param] = np.broadcast_to( d, (self.var.nFarm, self.var.nCrop, self.var.nCell))
def read_potential_crop_yield(self): start_of_model_run = (self.var._modelTime.timeStepPCR == 1) start_of_year = (self.var._modelTime.doy == 1) if self.AnnualChangeInPotYield: if start_of_model_run or start_of_year: date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0, 0) Yx = vos.netcdf2PCRobjClone( self.PotYieldNC, self.PotYieldVarName, date, useDoy = None, cloneMapFileName = self.var.cloneMap, LatitudeLongitude = True) self.var.Yx = Yx[self.var.landmask_crop].reshape(self.var.nCrop, self.var.nCell) else: if start_of_model_run: # date = datetime.datetime(self.staticLandCoverYear, 1, 1, 0, 0, 0) # ***TODO*** Yx = vos.netcdf2PCRobjCloneWithoutTime( self.PotYieldNC, self.PotYieldVarName, cloneMapFileName = self.var.cloneMap) self.var.Yx = Yx[self.var.landmask_crop].reshape(self.var.nCrop, self.var.nCell)
def set_farm_category(self): if not self.FarmCategoryFileNC == "None": start_of_model_run = (self.var._modelTime.timeStepPCR == 1) start_of_year = (self.var._modelTime.doy == 1) if self.AnnualChangeInFarmCategory: if start_of_model_run or start_of_year: date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0, 0) farm_category_new = vos.netcdf2PCRobjClone( self.FarmCategoryFileNC, self.FarmCategoryVarName, date, useDoy=None, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) mask = np.broadcast_to( self.var.landmask, (self.var.nFarm, self.var.nLat, self.var.nLon)) farm_category_new = np.reshape( farm_category_new[mask], (self.var.nFarm, self.var.nCell)) if np.any(self.var.GrowingSeasonDayOne): self.var.FarmCategory[self.var.GrowingSeasonDayOne[ 0, :, :]] = (farm_category_new[ self.var.GrowingSeasonDayOne[0, :, :]]) else: if start_of_model_run: farm_category = vos.netcdf2PCRobjCloneWithoutTime( self.FarmCategoryFileNC, self.FarmCategoryVarName, cloneMapFileName=self.var.cloneMap) mask = np.broadcast_to( self.var.landmask, (self.var.nFarm, self.var.nLat, self.var.nLon)) farm_category = np.reshape( farm_category[mask], (self.var.nFarm, self.var.nCell)) self.var.FarmCategory = farm_category.copy()
def reshape_fertiliser_app_rate(self, fert_app_rate, ncFile, ncVarName): fert_app_rate_has_farm_dimension = ( vos.check_if_nc_variable_has_dimension(ncFile, ncVarName, 'farm')) # print 'hello, world' if fert_app_rate_has_farm_dimension: fert_app_rate = np.reshape( fert_app_rate[self.var.landmask_farm_crop], (self.var.nFarm, self.var.nCrop, self.var.nCell)) else: fert_app_rate = np.reshape(fert_app_rate[self.var.landmask_crop], (self.var.nCrop, self.var.nCell)) fert_app_rate = np.broadcast_to( fert_app_rate[None, :, :], (self.var.nFarm, self.var.nCrop, self.var.nCell)) return fert_app_rate
def update_cover_fraction(self): # TODO: make flexible the day on which land cover is changed start_of_model_run = (self.var._modelTime.timeStepPCR == 1) start_of_year = (self.var._modelTime.doy == 1) if self.dynamicLandCover: if start_of_model_run or start_of_year: date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0, 0) self.var.coverFraction = vos.netcdf2PCRobjClone( self.coverFractionNC.format( day=self.var._modelTime.currTime.day, month=self.var._modelTime.currTime.month, year=self.var._modelTime.currTime.year), self.coverFractionVarName, date, # useDoy = method_for_time_index, # cloneMapAttributes = self.cloneMapAttributes, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True)[self.var.landmask] else: if start_of_model_run: date = datetime.datetime(self.staticLandCoverYear, 1, 1, 0, 0, 0) self.var.coverFraction = vos.netcdf2PCRobjClone( self.coverFractionNC.format( day=self.var._modelTime.currTime.day, month=self.var._modelTime.currTime.month, year=self.var._modelTime.currTime.year), self.coverFractionVarName, date, # useDoy = method_for_time_index, # cloneMapAttributes = self.cloneMapAttributes, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True)[self.var.landmask]
def set_diesel_price(self): start_of_model_run = (self.var._modelTime.timeStepPCR == 1) start_of_year = (self.var._modelTime.doy == 1) if not self.DieselPriceFileNC == "None": if start_of_model_run or start_of_year: date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0, 0) diesel_price = vos.netcdf2PCRobjClone( self.DieselPriceFileNC, self.DieselPriceVarName, date, useDoy=None, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) diesel_price = diesel_price[self.var.landmask] self.var.DieselPrice = diesel_price
def update_crop_coefficient(self): start_of_model_run = (self.var._modelTime.timeStepPCR == 1) if start_of_model_run or (self.var._modelTime.day in [1, 11, 21]): self.var.cropCoefficient = vos.netcdf2PCRobjClone( self.cropCoefficientNC.format( day=self.var._modelTime.currTime.day, month=self.var._modelTime.currTime.month, year=self.var._modelTime.currTime.year), self.cropCoefficientVarName, datetime.datetime(2000, self.var._modelTime.currTime.month, self.var._modelTime.currTime.day), # useDoy = method_for_time_index, # cloneMapAttributes = self.cloneMapAttributes, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True)[self.var.landmask]
def initial_tubewell_capacity(self): # TODO - this is where initial tubewell ownership is read to the model initialTubewellCapacityNC = str( self.configuration['initialTubewellOwnershipInputFile']) tubewell_capacity_varname = str( self.configuration['initialTubewellOwnershipVariableName']) tubewell_count = vos.netcdf2PCRobjCloneWithoutTime( initialTubewellCapacityNC, tubewell_capacity_varname, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) mask = np.broadcast_to(self.var.landmask, tubewell_count.shape) tubewell_count = np.reshape(tubewell_count[mask], (self.var.nFarm, self.var.nCell)) self.var.TubewellCount = tubewell_count.copy()
def update_intercept_capacity(self): if self.interceptCapNC != "None": start_of_model_run = (self.var._modelTime.timeStepPCR == 1) if start_of_model_run or (self.var._modelTime.day in [1, 11, 21]): self.var.interception_capacity = vos.netcdf2PCRobjClone( self.interceptCapNC.format( day=self.var._modelTime.currTime.day, month=self.var._modelTime.currTime.month, year=self.var._modelTime.currTime.year), self.interceptCapVarName, datetime.datetime(2000, self.var._modelTime.currTime.month, self.var._modelTime.currTime.day), # useDoy = method_for_time_index, # cloneMapAttributes = self.cloneMapAttributes, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True)[self.var.landmask] self.var.interception_capacity = self.var.interception_capacity.clip( self.var.minInterceptCap, None)
def initial_water_content(self): landmask = np.broadcast_to( self.var.landmask, (self.var.nLayer, self.var.nLat, self.var.nLon)) self.var.initialConditionFileNC = str( self.var._configuration. INITIAL_CONDITIONS['initialConditionInputFile']) self.var.initialConditionVarName = str( self.var._configuration. INITIAL_CONDITIONS['initialConditionVariableName']) th = vos.netcdf2PCRobjCloneWithoutTime( self.var.initialConditionFileNC, self.var.initialConditionVarName, cloneMapFileName=self.var.cloneMap) th = th[landmask].reshape(self.var.nLayer, self.var.nCell) self.var.th = np.broadcast_to( th[None, None, ...], (self.var.nFarm, self.var.nCrop, self.var.nLayer, self.var.nCell)).copy() self.var.wc = (self.var.th * self.var.root_depth)
def initial(self): initialCanalAccessNC = str( self.configuration['initialCanalAccessInputFile']) canal_access_varname = str( self.configuration['initialCanalAccessVariableName']) try: canal_access = vos.netcdf2PCRobjCloneWithoutTime( initialCanalAccessNC, canal_access_varname, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) except: canal_access = np.ones( (self.var.nFarm, self.var.nLat, self.var.nLon)) mask = np.broadcast_to(self.var.landmask, canal_access.shape) canal_access = np.reshape(canal_access[mask], (self.var.nFarm, self.var.nCell)) self.var.CanalAccess = canal_access.copy()
def set_crop_price(self): """Function to read crop area""" start_of_model_run = (self.var._modelTime.timeStepPCR == 1) start_of_year = (self.var._modelTime.doy == 1) if start_of_model_run or start_of_year: if not self.CropPriceFileNC == "None": date = datetime.datetime(self.var._modelTime.year, 1, 1, 0, 0, 0) crop_price = vos.netcdf2PCRobjClone( self.CropPriceFileNC, self.CropPriceVarName, date, useDoy=None, cloneMapFileName=self.var.cloneMap, LatitudeLongitude=True) crop_price = np.reshape(crop_price[self.var.landmask_crop], (self.var.nCrop, self.var.nCell)) crop_price = np.broadcast_to( crop_price[None, :, :], (self.var.nFarm, self.var.nCrop, self.var.nCell)).copy() self.var.CropPrice = crop_price
def read(self): # method for finding time indexes in the groundwater netdf file: # - the default one method_for_time_index = None # - based on the ini/configuration file (if given) # if 'time_index_method_for_groundwater_netcdf' in self._configuration.GROUNDWATER.keys() and\ # self._configuration.GROUNDWATER['time_index_method_for_groundwater_netcdf'] != "None": # method_for_time_index = self._configuration.GROUNDWATER['time_index_method_for_groundwater_netcdf'] # reading groundwater: if self.WaterTable: if self.VariableWaterTable: # DailyGroundwaterNC is a logical indicating whether a separate # netCDF is used for each time step - use this for coupling if self.DailyGroundwaterNC: # introduce this test so that we do not ask the model to read # a file from a timestep prior to the current simulation. if not (self._modelTime.isFirstTimestep() & (self.gwTimeLag > 0)): tm = self._modelTime.currTime - datetime.timedelta( self.gwTimeLag) day, month, year = tm.day, tm.month, tm.year # Fill named placeholders (NB we have already checked that # the specified filename contains these placeholders) gwFileNC = self.gwFileNC.format(day=day, month=month, year=year) # Check whether the file is present in the filesystem; if # it doesn't, enter a while loop which periodically checks # whether the file exists. We specify a maximum wait time # in order to prevent the model hanging if the file never # materialises. exists = os.path.exists(gwFileNC) max_wait_time = 60 wait_time = 0.1 total_wait_time = 0 while exists is False and total_wait_time <= max_wait_time: time.sleep(wait_time) exists = os.path.exists(gwFileNC) total_wait_time += wait_time if not exists: msg = "groundwater file doesn't exist and maximum wait time exceeded" raise AQError(msg) zGW = vos.netcdf2PCRobjCloneWithoutTime( gwFileNC, self.gwVarName, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.zGW = zGW[self.landmask] else: zGW = vos.netcdf2PCRobjClone( self.gwFileNC, self.gwVarName, str(currTimeStep.fulldate), useDoy=method_for_time_index, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.zGW = zGW[self.landmask] else: zGW = vos.netcdf2PCRobjCloneWithoutTime( self.gwFileNC, self.gwVarName, cloneMapFileName=self.cloneMap, LatitudeLongitude=True) self.zGW = zGW[self.landmask]