def wps2wrf( namelist_wps, namelist_input, sdate, edate, maxdom, chunk_is_restart, timestep_dxfactor='6') : nmlw = fn.FortranNamelist( namelist_wps ) nmli = fn.WrfNamelist( namelist_input ) nmli.setValue("max_dom", maxdom) for var in ["run_days", "run_hours", "run_minutes", "run_seconds"]: nmli.setValue(var, 0) nmli.setMaxDomValue("start_year", sdate.year) nmli.setMaxDomValue("start_month", sdate.month) nmli.setMaxDomValue("start_day", sdate.day) nmli.setMaxDomValue("start_hour", sdate.hour) nmli.setMaxDomValue("end_year", edate.year) nmli.setMaxDomValue("end_month", edate.month) nmli.setMaxDomValue("end_day", edate.day) nmli.setMaxDomValue("end_hour", edate.hour) for var in [ "parent_grid_ratio", "i_parent_start", "j_parent_start", "e_we", "e_sn"]: nmli.setValue(var, nmlw.getValue(var)) nmli.setValue("parent_time_step_ratio", nmlw.getValue("parent_grid_ratio")) if exists("met_em.d01.%s.nc" % datetime2datewrf( sdate ) ): # If there are met_em files, we need to run real.exe. Otherwise, we # cannot get enough info (num_metgrid_*levels) to run real.exe nmli.setValue("num_metgrid_levels", get_num_metgrid_levels()) nmli.setValue("num_metgrid_soil_levels", get_num_metgrid_soil_levels()) # # Compute the grid spacings. Read them from met_em files if the projection is lat-lon. # nmli.setValue("grid_id", list(range(1, maxdom+1))) # Update parant_id in the namelist nmli.setValue("parent_id", nmlw.getValue("parent_id")) alldx = [] for idom in range(1,maxdom + 1): thisdx = get_latlon_dx(sdate, "d0%i" % idom) alldx.append(thisdx) nmli.setValue("dx", alldx) nmli.setValue("dy", alldx) # May be an issue for global WRF # # Compute the time step. # if timestep_dxfactor.startswith("manual:"): nmli.setValue("time_step", int(timestep_dxfactor[7:])) elif timestep_dxfactor.startswith("adaptive:"): nmli.setValue("use_adaptive_time_step", ".true.", "domains") else: nmli.setValue("time_step", get_time_step(nmli.getValue("dx")[0], eval(timestep_dxfactor))) nmli.setValue("restart", chunk_is_restart) # # Trim, check, overwrite the file and ... we are done! # #nmli.trimMaxDom() nmli.wrfCheck() #nmli.extendMaxDomVariables() nmli.overWriteNamelist()
def fix_ptop( namelist_input ): top_press = get_ptop() nmli = fn.WrfNamelist( namelist_input ) try : specp_top = int( nmli.getValue( "p_top_requested" )[0] ) except Exception : if top_press > 5000 : logging.info( "Default p_top is 5000., but your input files only reach %d. Fixing..." % top_press ) nmli.setValue( "p_top_requested ", top_press, "domains" ) else : logging.info( "Default p_top will be 5000." ) nmli.setValue( "p_top_requested ", 5000, "domains" ) else : if specp_top < top_press : logging.info( "Specified p_top is %d, but your input files only reach %d. Fixing..." % ( specp_top, top_press ) ) nmli.setValue( "p_top_requested", top_press ) nmli.wrfCheck() nmli.overWriteNamelist()
def update_namelist(self, namelist_input, section): """ Update namelist values """ nmli = fn.WrfNamelist(namelist_input) logging.debug("Updating parameter 'max_dom' in the namelist") nmli.setValue("max_dom", int(self.cfg[section]['max_dom'])) max_dom = single = False for mnl_variable, mnl_values in self.cfg[section][ 'namelist_values'].items(): # Update the namelist per each combination logging.debug("Updating parameter '%s' in the namelist" % mnl_variable) # Modify the namelist with the parameters available in the namelist description if mnl_variable.startswith("max_dom:"): mnl_variable = mnl_variable[8:] max_dom = True elif mnl_variable.startswith("single:"): mnl_variable = mnl_variable[7:] single = True if '.' in mnl_variable: nml_section, val = mnl_variable.split('.') else: nml_section, val = "", mnl_variable if max_dom and not val in nmli.MAX_DOM_VARIABLES: nmli.MAX_DOM_VARIABLES.extend(val) if single and val in nmli.MAX_DOM_VARIABLES: nmli.MAX_DOM_VARIABLES.remove(val) try: nmli.setValue( val, coerce_value_list(mnl_values.strip(',').split(',')), nml_section) except IndexError: raise Exception( "'%s' does not have values for all namelist combinations." % mnl_variable) except Exception as err: raise Exception(err) nmli.trimMaxDom() nmli.extendMaxDomVariables() if nmli.wrfCheck(): raise Exception("Please review 'namelist_values' variable.") if not self.dryrun: nmli.overWriteNamelist()
def cycle_time(self, namelist_input, section): """ Clycle realization time """ realization_name = self.name + '-' + section.split("/")[1] for (start_date, end_date, simult_interval, simult_length, chunk_size, restart_interval) in self.cfg[section]['date_time']: # Update restart_interval in the namelist logging.debug( "Updating parameter 'restart_interval' in the namelist") nmli = fn.WrfNamelist(namelist_input) nmli.setValue("restart_interval", restart_interval) if not self.dryrun: nmli.overWriteNamelist() # Define which calendar is going to be used exp_calendar = Calendar(self.cfg[section]['calendar']) rea_start_date = start_date while rea_start_date < end_date: rea_end_date = exp_calendar.add(rea_start_date, simult_length) if rea_end_date > end_date: rea_end_date = end_date rea_name = "%s-%s" % (realization_name, rea_start_date.strftime("%Y%m%dT%H%M%S")) logging.info("---> Realization %s: start date %s end date %s" % (rea_name, rea_start_date, rea_end_date)) # Check realization on the database rea = self.check_db(name=rea_name, start_date=rea_start_date, end_date=rea_end_date, cfg=self.cfg[section]) # Create chunks only if end date has been modified if rea: rea.cycle_chunks() elif not rea: # If there is not runtime we have to add the start month of the realization if ( 'preprocessor_optargs' in self.cfg[ section ] ) and \ ( 'member' in self.cfg[ section ] [ 'preprocessor_optargs' ] ) and \ ( not 'runtime' in self.cfg[ section ] [ 'preprocessor_optargs' ] ) : self._update_member(rea_start_date) # Create a realization rea = Realization() rea.name = rea_name rea.start_date = rea_start_date rea.end_date = rea_end_date rea.chunk_size = chunk_size rea.current_date = rea_start_date rea.status = Realization.Status.PREPARED rea.current_chunk = 1 rea.cfg = self.cfg[section] # Add realization to the experiment self.realization.append(rea) # Create chunk for the realization rea.cycle_chunks() # Check storage if not self.dryrun: # Default section will be the current section realization_cfg = dict() realization_cfg['ensemble/default'] = copy.deepcopy( self.cfg[section]) for key, val in self.cfg[section].items(): if key.startswith('resource'): realization_cfg[key] = copy.deepcopy(val) save_json(realization_cfg, self.home_directory, "realization.json") rea._prepare_sub_files() rea_start_date = exp_calendar.add(rea_start_date, simult_interval)