def main(): # prepare logger and its directory log_file_location = output['folder'] + "/log/" try: os.makedirs(log_file_location) except: cmd = 'rm -r ' + log_file_location + "/*" os.system(cmd) pass vos.initialize_logging(log_file_location) # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate, nrOfTimeSteps) calculationModel = CalcFramework(cloneMapFileName,\ pcraster_files, \ modelTime, \ output, inputEPSG, outputEPSG, resample_method) dynamic_framework = DynamicFramework(calculationModel, modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def initialize_config(self, fileName): logger.info("PCRGlobWB: initialize_config") try: self.configuration = Configuration(fileName, relative_ini_meteo_paths=True) pcr.setclone(self.configuration.cloneMap) # set start and end time based on configuration self.model_time = ModelTime() self.model_time.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.model_time.update(0) self.shape = self.calculate_shape() logger.info("Shape of maps is %s", str(self.shape)) self.model = None except: import traceback traceback.print_exc() raise
def initialize(self, config_file_location=None): """ Initializes the model: read config file, load variables, get timestep information, etc. """ self.configuration = Configuration(config_file_location) self.initial_state = None self.currTimeStep = ModelTime( ) # timeStep info: year, month, day, doy, hour, etc self.currTimeStep.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.deterministic_runner = DeterministicRunner( self.configuration, self.currTimeStep, self.initial_state) self.dynamic_framework = DynamicFramework(self.deterministic_runner, 1) self.dynamic_framework.setQuiet(True) self.dynamic_framework._runInitial() self.dynamic_framework._runResume() # set timestep (to allow updates on a per-timestep-basis) self.currenttimestep = 0 logger.info('Model initialized. Spin-up might be required.')
def main(): # get the full path of configuration/ini file given in the system argument iniFileName = os.path.abspath(sys.argv[1]) # debug option debug_mode = False if len(sys.argv) > 2: if sys.argv[2] == "debug": debug_mode = True # object to handle configuration/ini file configuration = Configuration(iniFileName = iniFileName, \ debug_mode = debug_mode) # timeStep info: year, month, day, doy, hour, etc currTimeStep = ModelTime() # Running the deterministic_runner currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) logger.info('Model run starts.') deterministic_runner = DeterministicRunner(configuration, currTimeStep) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def spinup(self): """ Spin-up the model. This is required to obtain realistic starting conditions for the model run. It runs on a yearly basis until the required convergence or max. allowed spin-up runs is reached. """ spin_up = SpinUp(self.configuration) # object for spin_up self.currTimeStep = ModelTime( ) # timeStep info: year, month, day, doy, hour, etc # spin-up noSpinUps = int(self.configuration.globalOptions['maxSpinUpsInYears']) if noSpinUps > 0: logger.info('Spin-Up #Total Years: ' + str(noSpinUps)) spinUpRun = 0 has_converged = False while spinUpRun < noSpinUps and has_converged == False: spinUpRun += 1 self.currTimeStep.getStartEndTimeStepsForSpinUp( self.configuration.globalOptions['startTime'], spinUpRun, noSpinUps) logger.info('Spin-Up Run No. ' + str(spinUpRun)) deterministic_runner = DeterministicRunner( self.configuration, self.currTimeStep, self.initial_state) all_state_begin = deterministic_runner.model.getAllState() self.dynamic_framework = DynamicFramework( deterministic_runner, self.currTimeStep.nrOfTimeSteps) self.dynamic_framework.setQuiet(True) self.dynamic_framework.run() all_state_end = deterministic_runner.model.getAllState() has_converged = spin_up.checkConvergence( all_state_begin, all_state_end, spinUpRun, deterministic_runner.model.routing.cellArea) self.initial_state = deterministic_runner.model.getState() # setting model ready after spin-up self.currTimeStep.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.deterministic_runner = DeterministicRunner( self.configuration, self.currTimeStep, self.initial_state) logger.info( 'End of spin-up. Model is ready for transient simulation run.')
def main(): # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate,endDate,nrOfTimeSteps) calculationModel = CalcFramework(cloneMapFileName,\ pcraster_files, \ output, \ modelTime) dynamic_framework = DynamicFramework(calculationModel,modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): startDate = "2003-01-01" #YYYY-MM-DD endDate = "2010-12-31" #YYYY-MM-DD # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) # # upscaling the PCR-GLOBWB results from five minute to one degree: upscalingModel = UpscalingFramework(input_files,\ output_files,\ modelTime,\ tmpDir) dynamic_framework = DynamicFramework(upscalingModel,\ modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run() # reset time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) # # evaluate model results to grace product graceEvaluation = GraceEvaluation(input_files,\ output_files,\ modelTime,\ tmpDir) dynamic_framework = DynamicFramework(graceEvaluation,\ modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate, nrOfTimeSteps) calculationModel = CalcFramework(cloneMapFileName,\ pcraster_files, \ output, \ modelTime) dynamic_framework = DynamicFramework(calculationModel, modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) # converting model convertModel = ConvertVolumeToHeightFramework(input_netcdf, \ output_netcdf, \ modelTime, \ tmpDir) dynamic_framework = DynamicFramework(convertModel, modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) # resample netcdf resampleModel = ResampleFramework(input_netcdf,\ output_netcdf,\ modelTime,\ tmpDir) dynamic_framework = DynamicFramework(resampleModel, modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # input_file input_file = "/projects/0/dfguu/users/edwin/pcr-globwb-aqueduct/historical/1958-2001_watch/" input_file = sys.argv[1] # netcdf variable name input_variable_name = sys.argv[2] # timeStep info: year, month, day, doy, hour, etc start_date = "2015-01-01" end_date = "2015-12-31" start_date = sys.argv[3] end_date = sys.argv[4] # currTimeStep = ModelTime() currTimeStep.getStartEndTimeSteps(start_date, end_date) # output folder from this calculation output_folder = "/scratch-shared/edwin/mekong_basin_temperature/test/" output_folder = sys.argv[5] # - if exists, cleaning the previous output directory: if os.path.isdir(output_folder): shutil.rmtree(output_folder) # - making the output folder os.makedirs(output_folder) # output file, variable name and unit output_file = output_folder + "/" + sys.argv[6] variable_name = sys.argv[7] variable_unit = sys.argv[8] # logger # - making a log directory log_file_directory = output_folder + "/" + "log/" os.makedirs(log_file_directory) # - initialize logging vos.initialize_logging(log_file_directory) # Running the deterministic_runner logger.info('Starting the calculation.') deterministic_runner = DeterministicRunner(currTimeStep, input_file, input_variable_name, output_file, variable_name, variable_unit) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # prepare the output directory try: os.makedirs(pcraster_output['output_folder']) except: os.system('rm -r ' + str(pcraster_output['output_folder'])) pass # - making the directory for storing global extent output files os.makedirs(pcraster_output['output_folder'] + "/global/") # - making the directory for storing regional extent output files os.makedirs(pcraster_output['output_folder'] + "/regional/") # prepare logger and its directory log_file_location = pcraster_output['output_folder'] + "/log/" try: os.makedirs(log_file_location) except: pass vos.initialize_logging(log_file_location) # prepare a temporary folder tmp_file_location = pcraster_output['output_folder'] + "/tmp/" try: os.makedirs(tmp_file_location) except: pass # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) calculationModel = CalcFramework(globeCloneMapFileName, localCloneMapFileName, \ netcdf_input, \ pcraster_output, \ modelTime, \ inputEPSG, outputEPSG, resample_method) dynamic_framework = DynamicFramework(calculationModel, modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # prepare logger and its directory log_file_location = output['folder'] + "/log/" try: os.makedirs(log_file_location) except: pass vos.initialize_logging(log_file_location) # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) calculationModel = CalcFramework(cloneMapFileName,\ input_files, \ modelTime, \ output) dynamic_framework = DynamicFramework(calculationModel, modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # - prepare the output folder if os.path.exists(output_folder): shutil.rmtree(output_folder) os.makedirs(output_folder) # prepare logger and its directory log_file_location = output_folder + "/log/" os.makedirs(log_file_location) vos.initialize_logging(log_file_location) # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate, endDate) #~ print modelTime.nrOfTimeSteps # calculation model/framework calculationModel = AreaOperationNetcdfToPCRasterTSS(netcdf_input_file = netcdf_input_file, \ areaMapFileName = areaMapFileName, \ areaPointMapFileName = areaPointMapFileName, \ netcdf_input_clone_map_file = netcdf_input_clone_map_file, \ output_folder = output_folder, \ unit_conversion_factor = unit_conversion_factor, \ unit_conversion_offset = unit_conversion_offset, \ modelTime = modelTime, \ inputProjection = inputProjection, \ outputProjection = outputProjection, \ resample_method = resample_method, \ tss_daily_output_file = tss_daily_output_file, \ tss_10day_output_file = tss_10day_output_file, \ report_10day_pcr_files = True ) number_of_time_steps = modelTime.nrOfTimeSteps #~ number_of_time_steps = 100 dynamic_framework = DynamicFramework(calculationModel, number_of_time_steps) dynamic_framework.setQuiet(True) # - start the calculation dynamic_framework.run()
def main(): # the output folder from this calculation output_folder = "/scratch-shared/edwinvua/data_for_diede/netcdf_process/climatology_average/" output_folder = sys.argv[1] # totat_runoff_input_file totat_runoff_input_file = "/scratch-shared/edwinvua/data_for_diede/flow_scenarios/climatology_average_totalRunoff_monthTot_output_1979-2015.nc" totat_runoff_input_file = sys.argv[2] # timeStep info: year, month, day, doy, hour, etc start_date = "2015-01-01" end_date = "2015-12-31" start_date = sys.argv[3] end_date = sys.argv[4] # currTimeStep = ModelTime() currTimeStep.getStartEndTimeSteps(start_date, end_date) # - if exists, cleaning the previous output directory: if os.path.isdir(output_folder): shutil.rmtree(output_folder) # - making the output folder os.makedirs(output_folder) # logger # - making a log directory log_file_directory = output_folder + "/" + "log/" os.makedirs(log_file_directory) # - initialize logging vos.initialize_logging(log_file_directory) # Running the deterministic_runner logger.info('Starting the calculation.') deterministic_runner = DeterministicRunner(currTimeStep, output_folder, totat_runoff_input_file) dynamic_framework = DynamicFramework(deterministic_runner,currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # print disclaimer disclaimer.print_disclaimer() # get the full path of configuration/ini file given in the system argument iniFileName = os.path.abspath(sys.argv[1]) # debug option debug_mode = False if len(sys.argv) > 2: if sys.argv[2] == "debug" or sys.argv[2] == "debug_parallel": debug_mode = True # options to perform steady state calculation steady_state_only = False if len(sys.argv) > 3: if sys.argv[3] == "steady-state-only": steady_state_only = True # object to handle configuration/ini file configuration = Configuration(iniFileName = iniFileName, \ debug_mode = debug_mode, \ steady_state_only = steady_state_only) # timeStep info: year, month, day, doy, hour, etc currTimeStep = ModelTime() # Running the deterministic_runner currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) logger.info('Model run starts.') deterministic_runner = DeterministicRunner(configuration, currTimeStep) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # prepare logger and its directory log_file_location = output['folder']+"/log/" try: os.makedirs(log_file_location) except: cmd = 'rm -r '+log_file_location+"/*" os.system(cmd) pass vos.initialize_logging(log_file_location) # time object modelTime = ModelTime() # timeStep info: year, month, day, doy, hour, etc modelTime.getStartEndTimeSteps(startDate,endDate,nrOfTimeSteps) calculationModel = CalcFramework(cloneMapFileName,\ pcraster_files, \ modelTime, \ output, inputEPSG, outputEPSG, resample_method) dynamic_framework = DynamicFramework(calculationModel,modelTime.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): initial_state = None configuration = Configuration() spin_up = SpinUp(configuration) # object for spin_up currTimeStep = ModelTime( ) # timeStep info: year, month, day, doy, hour, etc # spinningUp noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears']) if noSpinUps > 0: logger.info('Spin-Up #Total Years: ' + str(noSpinUps)) spinUpRun = 0 has_converged = False while spinUpRun < noSpinUps and has_converged == False: spinUpRun += 1 currTimeStep.getStartEndTimeStepsForSpinUp( configuration.globalOptions['startTime'], spinUpRun, noSpinUps) logger.info('Spin-Up Run No. ' + str(spinUpRun)) deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state) all_state_begin = deterministic_runner.model.getAllState() dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run() all_state_end = deterministic_runner.model.getAllState() has_converged = spin_up.checkConvergence( all_state_begin, all_state_end, spinUpRun, deterministic_runner.model.routing.cellArea) initial_state = deterministic_runner.model.getState() # # Running the deterministic_runner (excluding DA scheme) currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) logger.info('Transient simulation run started.') deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # get the full path of configuration/ini file given in the system argument iniFileName = os.path.abspath(sys.argv[1]) # debug option debug_mode = False if len(sys.argv) > 2: if sys.argv[2] == "debug": debug_mode = True # object to handle configuration/ini file configuration = Configuration(iniFileName = iniFileName, \ debug_mode = debug_mode) # timeStep info: year, month, day, doy, hour, etc currTimeStep = ModelTime() # object for spin_up spin_up = SpinUp(configuration) # spinningUp noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears']) initial_state = None if noSpinUps > 0: logger.info('Spin-Up #Total Years: ' + str(noSpinUps)) spinUpRun = 0 has_converged = False while spinUpRun < noSpinUps and has_converged == False: spinUpRun += 1 currTimeStep.getStartEndTimeStepsForSpinUp( configuration.globalOptions['startTime'], spinUpRun, noSpinUps) logger.info('Spin-Up Run No. ' + str(spinUpRun)) deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state) all_state_begin = deterministic_runner.model.getAllState() dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run() all_state_end = deterministic_runner.model.getAllState() has_converged = spin_up.checkConvergence( all_state_begin, all_state_end, spinUpRun, deterministic_runner.model.routing.cellArea) initial_state = deterministic_runner.model.getState() # # Running the deterministic_runner (excluding DA scheme) currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) logger.info('Transient simulation run started.') deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
def main(): # print disclaimer disclaimer.print_disclaimer() # get the full path of configuration/ini file given in the system argument iniFileName = os.path.abspath(sys.argv[1]) # debug option debug_mode = False if len(sys.argv) > 2: if sys.argv[2] == "debug": debug_mode = True # object to handle configuration/ini file configuration = Configuration(iniFileName = iniFileName, \ debug_mode = debug_mode) # timeStep info: year, month, day, doy, hour, etc currTimeStep = ModelTime() # object for spin_up spin_up = SpinUp(configuration) # spinningUp noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears']) initial_state = None if noSpinUps > 0: logger.info('Spin-Up #Total Years: ' + str(noSpinUps)) spinUpRun = 0 has_converged = False while spinUpRun < noSpinUps and has_converged == False: spinUpRun += 1 currTimeStep.getStartEndTimeStepsForSpinUp( configuration.globalOptions['startTime'], spinUpRun, noSpinUps) logger.info('Spin-Up Run No. ' + str(spinUpRun)) deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state) all_state_begin = deterministic_runner.model.getAllState() dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run() all_state_end = deterministic_runner.model.getAllState() has_converged = spin_up.checkConvergence( all_state_begin, all_state_end, spinUpRun, deterministic_runner.model.routing.cellArea) initial_state = deterministic_runner.model.getState() # Running the deterministic_runner (excluding DA scheme) currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) logger.info('Transient simulation run started.') deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run() # for debugging to PCR-GLOBWB version one if configuration.debug_to_version_one: logger.info('\n\n\n\n\n' + 'Executing PCR-GLOBWB version 1.' + '\n\n\n\n\n') # reset modelTime object currTimeStep = None currTimeStep = ModelTime() currTimeStep.getStartEndTimeSteps( configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) # execute PCR-GLOBWB version 1 # - including comparing model outputs (from versions one and two) pcrglobwb_one = oldcalc_framework.PCRGlobWBVersionOne(configuration, \ currTimeStep, \ deterministic_runner.model.routing.landmask, \ deterministic_runner.model.routing.cellArea) dynamic_framework = DynamicFramework(pcrglobwb_one, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
class BmiPCRGlobWB(EBmi): #we use the same epoch as pcrglobwb netcdf reporting def days_since_industry_epoch(self, modeltime): return (modeltime - datetime.date(1901, 1, 1)).days def in_modeltime(self, days_since_industry_epoch): return (datetime.datetime(1901, 1, 1) + datetime.timedelta(days=days_since_industry_epoch)).date() def calculate_shape(self): # return pcr.pcr2numpy(self.model.landmask, 1e20).shape return (pcr.clone().nrRows(), pcr.clone().nrCols()) #BMI initialize (as a single step) def initialize(self, fileName): self.initialize_config(fileName) self.initialize_model() #EBMI initialize (first step of two) def initialize_config(self, fileName): logger.info("PCRGlobWB: initialize_config") try: self.configuration = Configuration(fileName, relative_ini_meteo_paths=True) pcr.setclone(self.configuration.cloneMap) # set start and end time based on configuration self.model_time = ModelTime() self.model_time.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.model_time.update(0) self.shape = self.calculate_shape() logger.info("Shape of maps is %s", str(self.shape)) self.model = None except: import traceback traceback.print_exc() raise #EBMI initialize (second step of two) def initialize_model(self): if self.model is not None: #already initialized return try: logger.info("PCRGlobWB: initialize_model") initial_state = None self.model = PCRGlobWB(self.configuration, self.model_time, initial_state) self.reporting = Reporting(self.configuration, self.model, self.model_time) logger.info("Shape of maps is %s", str(self.shape)) logger.info("PCRGlobWB Initialized") except: import traceback traceback.print_exc() raise def update(self): timestep = self.model_time.timeStepPCR self.model_time.update(timestep + 1) self.model.read_forcings() self.model.update(report_water_balance=True) self.reporting.report() # #numpy = pcr.pcr2numpy(self.model.landSurface.satDegUpp000005, 1e20) # numpy = pcr.pcr2numpy(self.model.landSurface.satDegUpp000005, np.NaN) # print numpy.shape # print numpy def update_until(self, time): while self.get_current_time() + 0.001 < time: self.update() def update_frac(self, time_frac): raise NotImplementedError def finalize(self): pass def get_component_name(self): return "pcrglobwb" def get_input_var_names(self): return ["top_layer_soil_saturation"] def get_output_var_names(self): return ["top_layer_soil_saturation"] def get_var_type(self, long_var_name): return 'float64' def get_var_units(self, long_var_name): #TODO: this is not a proper unit return '1' def get_var_rank(self, long_var_name): return 0 def get_var_size(self, long_var_name): return np.prod(self.get_grid_shape(long_var_name)) def get_var_nbytes(self, long_var_name): return self.get_var_size(long_var_name) * np.float64.itemsize def get_start_time(self): return self.days_since_industry_epoch(self.model_time.startTime) def get_current_time(self): return self.days_since_industry_epoch(self.model_time.currTime) def get_end_time(self): return self.days_since_industry_epoch(self.model_time.endTime) def get_time_step(self): return 1 def get_time_units(self): return "Days since 1901-01-01" def get_value(self, long_var_name): logger.info("getting value for var %s", long_var_name) if (long_var_name == "top_layer_soil_saturation"): if self.model is not None and hasattr(self.model.landSurface, 'satDegUpp000005'): #first make all NanS into 0.0 with cover, then cut out the model using the landmask. # This should not actually make a difference. remasked = pcr.ifthen( self.model.landmask, pcr.cover(self.model.landSurface.satDegUpp000005, 0.0)) pcr.report(self.model.landSurface.satDegUpp000005, "value.map") pcr.report(remasked, "remasked.map") value = pcr.pcr2numpy(remasked, np.NaN) else: logger.info( "model has not run yet, returning empty state for top_layer_soil_saturation" ) value = pcr.pcr2numpy(pcr.scalar(0.0), np.NaN) # print "getting var", value # sys.stdout.flush() doubles = value.astype(np.float64) # print "getting var as doubles!!!!", doubles result = np.flipud(doubles) # print "getting var as doubles flipped!!!!", result # sys.stdout.flush() return result else: raise Exception("unknown var name" + long_var_name) def get_value_at_indices(self, long_var_name, inds): raise NotImplementedError # def get_satDegUpp000005_from_observation(self): # # # assumption for observation values # # - this should be replaced by values from the ECV soil moisture value (sattelite data) # # - uncertainty should be included here # # - note that the value should be between 0.0 and 1.0 # observed_satDegUpp000005 = pcr.min(1.0,\ # pcr.max(0.0,\ # pcr.normal(pcr.boolean(1)) + 1.0)) # return observed_satDegUpp000005 def set_satDegUpp000005(self, src): mask = np.isnan(src) src[mask] = 1e20 observed_satDegUpp000005 = pcr.numpy2pcr(pcr.Scalar, src, 1e20) pcr.report(observed_satDegUpp000005, "observed.map") constrained_satDegUpp000005 = pcr.min( 1.0, pcr.max(0.0, observed_satDegUpp000005)) pcr.report(constrained_satDegUpp000005, "constrained.map") pcr.report(self.model.landSurface.satDegUpp000005, "origmap.map") diffmap = constrained_satDegUpp000005 - self.model.landSurface.satDegUpp000005 pcr.report(diffmap, "diffmap.map") # ratio between observation and model ratio_between_observation_and_model = pcr.ifthenelse(self.model.landSurface.satDegUpp000005 > 0.0, constrained_satDegUpp000005 / \ self.model.landSurface.satDegUpp000005, 0.0) # updating upper soil states for all lad cover types for coverType in self.model.landSurface.coverTypes: # correcting upper soil state (storUpp000005) self.model.landSurface.landCoverObj[ coverType].storUpp000005 *= ratio_between_observation_and_model # if model value = 0.0, storUpp000005 is calculated based on storage capacity (model parameter) and observed saturation degree self.model.landSurface.landCoverObj[coverType].storUpp000005 = pcr.ifthenelse( self.model.landSurface.satDegUpp000005 > 0.0, \ self.model.landSurface.landCoverObj[coverType].storUpp000005, \ constrained_satDegUpp000005 * self.model.landSurface.parameters.storCapUpp000005) # correct for any scaling issues (value < 0 or > 1 do not make sense self.model.landSurface.landCoverObj[ coverType].storUpp000005 = pcr.min( 1.0, pcr.max( 0.0, self.model.landSurface.landCoverObj[coverType]. storUpp000005)) def set_value(self, long_var_name, src): if self.model is None or not hasattr(self.model.landSurface, 'satDegUpp000005'): logger.info("cannot set value for %s, as model has not run yet.", long_var_name) return logger.info("setting value for %s", long_var_name) # logger.info("dumping state to %s", self.configuration.endStateDir) # self.model.dumpStateDir(self.configuration.endStateDir + "/pre/") # print "got value to set", src # make sure the raster is the right side up src = np.flipud(src) # print "flipped", src # cast to pcraster precision src = src.astype(np.float32) # print "as float 32", src sys.stdout.flush() logger.info("setting value shape %s", src.shape) if (long_var_name == "top_layer_soil_saturation"): self.set_satDegUpp000005(src) else: raise Exception("unknown var name" + long_var_name) # write state here to facilitate restarting tomorrow # logger.info("dumping state to %s", self.configuration.endStateDir) # self.model.dumpStateDir(self.configuration.endStateDir + "/post/") def set_value_at_indices(self, long_var_name, inds, src): raise NotImplementedError def get_grid_type(self, long_var_name): return BmiGridType.UNIFORM def get_grid_shape(self, long_var_name): return def get_grid_shape(self, long_var_name): return self.shape def get_grid_spacing(self, long_var_name): cellsize = pcr.clone().cellSize() return np.array([cellsize, cellsize]) def get_grid_origin(self, long_var_name): north = pcr.clone().north() cellSize = pcr.clone().cellSize() nrRows = pcr.clone().nrRows() south = north - (cellSize * nrRows) west = pcr.clone().west() return np.array([south, west]) def get_grid_x(self, long_var_name): raise ValueError def get_grid_y(self, long_var_name): raise ValueError def get_grid_z(self, long_var_name): raise ValueError def get_grid_connectivity(self, long_var_name): raise ValueError def get_grid_offset(self, long_var_name): raise ValueError #EBMI functions def set_start_time(self, start_time): self.model_time.setStartTime(self.in_modeltime(start_time)) def set_end_time(self, end_time): self.model_time.setEndTime(self.in_modeltime(end_time)) def get_attribute_names(self): raise NotImplementedError def get_attribute_value(self, attribute_name): raise NotImplementedError def set_attribute_value(self, attribute_name, attribute_value): raise NotImplementedError def save_state(self, destination_directory): logger.info("saving state to %s", destination_directory) self.model.dumpStateDir(destination_directory) def load_state(self, source_directory): raise NotImplementedError
def main(): # get the full path of configuration/ini file given in the system argument iniFileName = os.path.abspath(sys.argv[1]) # debug option debug_mode = False if len(sys.argv) > 2: if sys.argv[2] == "debug" or sys.argv[ 2] == "debug_parallel" or sys.argv[2] == "debug-parallel": debug_mode = True # object to handle configuration/ini file configuration = Configuration(iniFileName = iniFileName, \ debug_mode = debug_mode, \ no_modification = False) # parallel option this_run_is_part_of_a_set_of_parallel_run = False if len(sys.argv) > 2: if sys.argv[2] == "parallel" or sys.argv[ 2] == "debug_parallel" or sys.argv[2] == "debug-parallel": this_run_is_part_of_a_set_of_parallel_run = True # for a non parallel run (usually 30min), a specific directory given in the system argument (sys.argv[3]) will be assigned for a given parameter combination: if this_run_is_part_of_a_set_of_parallel_run == False: # modfiying 'outputDir' (based on the given system argument) configuration.globalOptions['outputDir'] += "/" + str( sys.argv[3]) + "/" # for a parallel run (usually 5min), we assign a specific directory based on the clone number/code: if this_run_is_part_of_a_set_of_parallel_run: # modfiying outputDir, clone-map and landmask (based on the given system arguments) clone_code = str(sys.argv[3]) configuration.globalOptions['outputDir'] += "/" + clone_code + "/" configuration.globalOptions['cloneMap'] = configuration.globalOptions[ 'cloneMap'] % (clone_code) configuration.globalOptions['landmask'] = configuration.globalOptions[ 'landmask'] % (clone_code) # set configuration configuration.set_configuration(system_arguments=sys.argv) # timeStep info: year, month, day, doy, hour, etc currTimeStep = ModelTime() # object for spin_up spin_up = SpinUp(configuration) # spinning-up noSpinUps = int(configuration.globalOptions['maxSpinUpsInYears']) initial_state = None if noSpinUps > 0: logger.info('Spin-Up #Total Years: ' + str(noSpinUps)) spinUpRun = 0 has_converged = False while spinUpRun < noSpinUps and has_converged == False: spinUpRun += 1 currTimeStep.getStartEndTimeStepsForSpinUp( configuration.globalOptions['startTime'], spinUpRun, noSpinUps) logger.info('Spin-Up Run No. ' + str(spinUpRun)) deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state, sys.argv) all_state_begin = deterministic_runner.model.getAllState() dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run() all_state_end = deterministic_runner.model.getAllState() has_converged = spin_up.checkConvergence( all_state_begin, all_state_end, spinUpRun, deterministic_runner.model.routing.cellArea) initial_state = deterministic_runner.model.getState() # # Running the deterministic_runner (excluding DA scheme) currTimeStep.getStartEndTimeSteps(configuration.globalOptions['startTime'], configuration.globalOptions['endTime']) logger.info('Transient simulation run started.') deterministic_runner = DeterministicRunner(configuration, currTimeStep, initial_state, sys.argv) dynamic_framework = DynamicFramework(deterministic_runner, currTimeStep.nrOfTimeSteps) dynamic_framework.setQuiet(True) dynamic_framework.run()
class pcrglobwbBMI(object): def initialize(self, config_file_location=None): """ Initializes the model: read config file, load variables, get timestep information, etc. """ self.configuration = Configuration(config_file_location) self.initial_state = None self.currTimeStep = ModelTime( ) # timeStep info: year, month, day, doy, hour, etc self.currTimeStep.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.deterministic_runner = DeterministicRunner( self.configuration, self.currTimeStep, self.initial_state) self.dynamic_framework = DynamicFramework(self.deterministic_runner, 1) self.dynamic_framework.setQuiet(True) self.dynamic_framework._runInitial() self.dynamic_framework._runResume() # set timestep (to allow updates on a per-timestep-basis) self.currenttimestep = 0 logger.info('Model initialized. Spin-up might be required.') def finalize(self): """ Finalizes the model: shut down the model run, clean up resources, etc. """ self.dynamic_framework._runSuspend() #dynamic_framework._wf_shutdown() # commented out, special function from wflow Dynamic Framework def spinup(self): """ Spin-up the model. This is required to obtain realistic starting conditions for the model run. It runs on a yearly basis until the required convergence or max. allowed spin-up runs is reached. """ spin_up = SpinUp(self.configuration) # object for spin_up self.currTimeStep = ModelTime( ) # timeStep info: year, month, day, doy, hour, etc # spin-up noSpinUps = int(self.configuration.globalOptions['maxSpinUpsInYears']) if noSpinUps > 0: logger.info('Spin-Up #Total Years: ' + str(noSpinUps)) spinUpRun = 0 has_converged = False while spinUpRun < noSpinUps and has_converged == False: spinUpRun += 1 self.currTimeStep.getStartEndTimeStepsForSpinUp( self.configuration.globalOptions['startTime'], spinUpRun, noSpinUps) logger.info('Spin-Up Run No. ' + str(spinUpRun)) deterministic_runner = DeterministicRunner( self.configuration, self.currTimeStep, self.initial_state) all_state_begin = deterministic_runner.model.getAllState() self.dynamic_framework = DynamicFramework( deterministic_runner, self.currTimeStep.nrOfTimeSteps) self.dynamic_framework.setQuiet(True) self.dynamic_framework.run() all_state_end = deterministic_runner.model.getAllState() has_converged = spin_up.checkConvergence( all_state_begin, all_state_end, spinUpRun, deterministic_runner.model.routing.cellArea) self.initial_state = deterministic_runner.model.getState() # setting model ready after spin-up self.currTimeStep.getStartEndTimeSteps( self.configuration.globalOptions['startTime'], self.configuration.globalOptions['endTime']) self.deterministic_runner = DeterministicRunner( self.configuration, self.currTimeStep, self.initial_state) logger.info( 'End of spin-up. Model is ready for transient simulation run.') def update(self, dt=-1): """ Updates the model a number of timesteps, dependent on specified dt: dt = -1 -> runs the entire model from start time to end time dt = 1 -> updates the model 1 timestep (1 day) dt > 1 -> updates the model a number of timesteps (dt days) NOTE: the model can only run on a daily timestep! """ if dt == 1: # update timestep self.currenttimestep += 1 self.currTimeStep.update(self.currenttimestep) # commented out, already stated at initialization and at end of spin-up, not required at every timestep? #deterministic_runner = DeterministicRunner(self.configuration, self.currTimeStep, self.initial_state) # update model self.dynamic_framework = DynamicFramework( self.deterministic_runner, self.currenttimestep, self.currenttimestep) self.dynamic_framework.setQuiet(True) self.dynamic_framework.run() # update states (commented out, not required?) #self.initial_state = deterministic_runner.model.getState() elif dt == -1: # commented out, already stated at initialization and at end of spin-up, not required here as well? #deterministic_runner = DeterministicRunner(self.configuration, self.currTimeStep, self.initial_state) self.dynamic_framework = DynamicFramework( self.deterministic_runner, self.currTimeStep.nrOfTimeSteps) self.dynamic_framework.setQuiet(True) self.dynamic_framework.run() else: # update timestep self.currenttimestep += 1 self.currTimeStep.update(self.currenttimestep) # update model self.dynamic_framework = DynamicFramework( self.deterministic_runner, self.currenttimestep + (dt - 1), self.currenttimestep) self.dynamic_framework.setQuiet(True) self.dynamic_framework.run() # update time self.currenttimestep += (dt - 1) self.currTimeStep.update(self.currenttimestep) def get_start_time(self): """ Returns model start time Input: - Output: time as datetime (YYYY,MM,DD) """ return self.currTimeStep.startTime def get_end_time(self): """ Returns model end time Input: - Output: time as datetime (YYYY,MM,DD) """ return self.currTimeStep.endTime def get_current_time(self): """ Returns current model time Input: - Output: time as datetime (YYYY,MM,DD) """ return self.currTimeStep.currTime def get_time_step(self): """ Return current model timestep Input: - Output: timestep as int """ return self.currTimeStep.timeStepPCR def get_var(self, name, missingValues=-999): """ Returns a numpy array from model library Input: variable/map name (string) Output: numpy array or single variable, depending on input NOTE1: to get a variable from a specific landCover type, a tuple containing two strings should be used, with: - string 1 = name of landCover type - string 2 = name of variable NOTE2: there are two options to create a numpy array: - pcr2numpy -> requires a value for MV (optional, default = -999) - pcr_as_numpy -> automatically sets nan for all MV Currently using pcr2numpy! """ # check size of name input if numpy.size(name) == 1: # check for 'name' in the different sections of the model if hasattr(self.deterministic_runner.model.landSurface, name): pcrmap = getattr(self.deterministic_runner.model.landSurface, name) elif hasattr(self.deterministic_runner.model.routing, name): pcrmap = getattr(self.deterministic_runner.model.routing, name) elif hasattr(self.deterministic_runner.model.meteo, name): pcrmap = getattr(self.deterministic_runner.model.meteo, name) elif hasattr(self.deterministic_runner.model.groundwater, name): pcrmap = getattr(self.deterministic_runner.model.groundwater, name) else: logger.warn( name + " cannot be found in the model, returning empty list!") else: # first check if a specific model section was used as input if name[0] == 'landSurface': if hasattr(self.deterministic_runner.model.landSurface, name[1]): pcrmap = getattr( self.deterministic_runner.model.landSurface, name[1]) elif name[0] == 'routing': if hasattr(self.deterministic_runner.model.routing, name[1]): pcrmap = getattr(self.deterministic_runner.model.routing, name[1]) elif name[0] == 'WaterBodies': if hasattr(self.deterministic_runner.model.routing.WaterBodies, name[1]): pcrmap = getattr( self.deterministic_runner.model.routing.WaterBodies, name[1]) elif name[0] == 'pcrglobwb': if hasattr(self.deterministic_runner.model, name[1]): pcrmap = getattr(self.deterministic_runner.model, name[1]) # otherwise check if it is a variable from a landCover type else: # use the first entry of 'name' to find correct landCover type, second entry to find variable try: if hasattr( self.deterministic_runner.model.landSurface. landCoverObj[name[0]], name[1]): pcrmap = getattr( self.deterministic_runner.model.landSurface. landCoverObj[name[0]], name[1]) else: logger.warn( '(' + name[0] + ', ' + name[1] + ") cannot be found in the model, returning empty list!" ) except: logger.warn( '(' + name[0] + ', ' + name[1] + ") cannot be found in the model, returning empty list!" ) # attempt to create a numpy array, otherwise try to give the single value, or return empty list if this is both not possible try: return_value = pcr2numpy(pcrmap, missingValues) #return_value = pcr_as_numpy(pcrmap) except: try: return_value = pcrmap except: return [] return return_value def set_var(self, name, var, missingValues=-999): """ Sets a pcr map with values from a numpy array. Input: variable/map name (string), values (numpy array or single value), missing values (optional, default = -999) Output: - NOTE: to set a variable from a specific landCover type, a tuple containing two strings should be used, with: - string 1 = name of landCover type - string 2 = name of variable """ # try to create a pcr map from numpy array, otherwise just use the single value try: pcrmap = numpy2pcr(Scalar, var, missingValues) except: pcrmap = var # check if LDD (requires additional step) if 'lddMap' in name: pcrmap = ldd(pcrmap) # check size of name input if numpy.size(name) == 1: # update pcr map used in model with set values if hasattr(self.deterministic_runner.model.groundwater, name): exec "self.deterministic_runner.model.groundwater." + name + " = pcrmap" elif hasattr(self.deterministic_runner.model.landSurface, name): exec "self.deterministic_runner.model.landSurface." + name + " = pcrmap" elif hasattr(self.deterministic_runner.model.meteo, name): exec "self.deterministic_runner.model.meteo." + name + " = pcrmap" elif hasattr(self.deterministic_runner.model.routing, name): exec "self.deterministic_runner.model.routing." + name + " = pcrmap" else: logger.warn(name + " is not defined in the model, doing nothing!") else: # first check if a specific model section was used as input if name[0] == 'landSurface': if hasattr(self.deterministic_runner.model.landSurface, name[1]): exec "self.deterministic_runner.model.landSurface." + name[ 1] + " = pcrmap" elif name[0] == 'routing': if hasattr(self.deterministic_runner.model.routing, name[1]): exec "self.deterministic_runner.model.routing." + name[ 1] + " = pcrmap" elif name[0] == 'WaterBodies': if hasattr(self.deterministic_runner.model.routing.WaterBodies, name[1]): exec "self.deterministic_runner.model.routing.WaterBodies." + name[ 1] + " = pcrmap" elif name[0] == 'pcrglobwb': if hasattr(self.deterministic_runner.model, name[1]): exec "self.deterministic_runner.model." + name[ 1] + " = pcrmap" # otherwise check if it is a variable from a landCover type else: try: if hasattr( self.deterministic_runner.model.landSurface. landCoverObj[name[0]], name[1]): exec "self.deterministic_runner.model.landSurface.landCoverObj['" + name[ 0] + "']." + name[1] + " = pcrmap" else: logger.warn( '(' + name[0] + ', ' + name[1] + + ") is not defined in the model, doing nothing!") except: logger.warn( '(' + name[0] + ', ' + name[1] + + ") is not defined in the model, doing nothing!")