def update_namelist_wps(template, target, max_dom, init_time, fcst_hours, bdy_interval, geo_em_dir, met_em_dir, geogrid_tbl, metgrid_tbl, prefix, constants_name=None): """ Updates the namelist.wps to reflect updated settings in config Arguments: expand -- function for exanding any strings template -- base namelist_wps file to alter target -- name of modified file to write to """ # consider refactoring this method, perhaps relacing it with a more general namelist updating function, # and pushing some of the logic into the configuration file? logger = shared.get_logger() logger.debug('*** UPDATING namelist.wps ***') # function to expand any date/time placeholders expand = lambda s : substitute.sub_date(s, init_time=init_time) #wps_run_dir= expand(target) bdy_times = shared.get_bdy_times(init_time, fcst_hours, bdy_interval) met_em_dir = expand(met_em_dir) geo_em_dir = expand(geo_em_dir) interval_seconds = bdy_interval * 60 * 60 logger.debug('reading namelist.wps <--------- %s' % template) namelist = shared.read_namelist(template) # # Update some options based on the forecast config file # namelist.update('max_dom', max_dom) namelist.update('opt_output_from_geogrid_path', geo_em_dir, section='share') namelist.update('opt_output_from_metgrid_path', met_em_dir, section='metgrid') namelist.update('opt_geogrid_tbl_path', expand(geogrid_tbl), section='geogrid') namelist.update('opt_metgrid_tbl_path', expand(metgrid_tbl), section='metgrid') namelist.update('interval_seconds', [interval_seconds]) # # Generate formatted strings for inclusion in the namelist.wps file # start_str = bdy_times[0].strftime("%Y-%m-%d_%H:%M:%S") end_str = bdy_times[-1].strftime("%Y-%m-%d_%H:%M:%S") logger.debug("Updating namelist.wps start and end") namelist.update('start_date', [start_str]*max_dom) namelist.update('end_date', [end_str]*max_dom) if constants_name: namelist.update('constants_name', constants_name, section='metgrid') namelist.to_file(target) logger.debug('*** FINISHED UPDATING namelist.wps ***')
def update_namelist_input(template, target, max_dom, init_time, fcst_hours, history_interval, interval_seconds, metadata=None): """ Updates the namelist.input file to reflect updated settings in config. Adds a non-standard &metadata section to give a name to the model run Arguments: config -- dictionary containing various configuration options """ logger =shared.get_logger() logger.debug('*** UPDATING namelist.input ***') logger.debug(target) logger.debug(fcst_hours) namelist = shared.read_namelist(template) if metadata: for key,value in metadata.items(): namelist.update(key, value, section='metadata') fcst_times = shared.get_interval_times(start=init_time, count=fcst_hours+1, freq=rrule.HOURLY) start = fcst_times[0] end = fcst_times[-1] # # I'm still not sure how the WRF namelist works between # the start and end settings and the run_XX settings. # I think we can just keep days as zero, and work entirely # in hours # namelist.update('start_year', [start.year] * max_dom) namelist.update('start_month',[start.month]* max_dom) namelist.update('start_day', [start.day] * max_dom) namelist.update('start_hour', [start.hour] * max_dom) namelist.update('end_year', [end.year] * max_dom) namelist.update('end_month', [end.month] * max_dom) namelist.update('end_day', [end.day] * max_dom) namelist.update('end_hour', [end.hour] * max_dom) namelist.update('run_days', [0]) namelist.update('run_hours', [fcst_hours] ) #namelist.update('run_minutes',[0]) namelist.update('history_interval', [history_interval] * max_dom) namelist.update('interval_seconds', [interval_seconds]) # # If DFI is being used, update DFI settings # From user guide: # "For time specification, it typically needs to integrate # backward for 0.5 to 1 hour, and integrate forward for half of the time." # # should we just write this everytime into the file and rely of dfi_opt # as the on/off switch? # hour = datetime.timedelta(0, 60*60) minute = datetime.timedelta(0, 60) #dfi_bck = config['dfi_bck'] * minute #dfi_fwd = config['dfi_fwd'] * minute #dfi_bckstop = start - dfi_bck #dfi_fwdstop = start + dfi_fwd #namelist.update('dfi_bckstop_year', dfi_bckstop.year, 'dfi_control') #namelist.update('dfi_bckstop_month', dfi_bckstop.month, 'dfi_control') #namelist.update('dfi_bckstop_day', dfi_bckstop.day, 'dfi_control') #namelist.update('dfi_bckstop_hour', dfi_bckstop.hour, 'dfi_control') #namelist.update('dfi_bckstop_minute', dfi_bckstop.minute, 'dfi_control') #namelist.update('dfi_bckstop_second', dfi_bckstop.second, 'dfi_control') #namelist.update('dfi_fwdstop_year', dfi_fwdstop.year, 'dfi_control') #namelist.update('dfi_fwdstop_month', dfi_fwdstop.month, 'dfi_control') #namelist.update('dfi_fwdstop_day', dfi_fwdstop.day, 'dfi_control') #namelist.update('dfi_fwdstop_hour', dfi_fwdstop.hour, 'dfi_control') #namelist.update('dfi_fwdstop_minute', dfi_fwdstop.minute, 'dfi_control') #namelist.update('dfi_fwdstop_second', dfi_fwdstop.second, 'dfi_control') namelist.to_file(target) logger.debug('*** FINISHED UPDATING namelist.input ***')