def __init__(self, datestart, dateend): config.__init__(self) self.startdate = datestart self.enddate = dateend # read WRF namelist in WRF work_dir wrf_nml = f90nml.read(self.config['options_wrf']['namelist.input']) # get number of domains self.ndoms = wrf_nml['domains']['max_dom'] self.rundir = self.config['filesystem']['wrf_run_dir'] # archive in subdir per year self.archivedir = os.path.join( self.config['filesystem']['archive_dir'], str(self.startdate.year)) utils._create_directory(self.archivedir) # define static variables self.define_vars_static() # define variables that need to be stored hourly self.define_vars_hourly() # define variables that need to be stored every minute for the inner # domain, hourly for the other domains self.define_vars_minute() self.define_vars_deac() # define variables to be deaccumulated self.archive() # archive "normal" variables self.archive_wrfvar_input() # archive wrfvar_input files # get start_date from config.json start_date = utils.return_validate( self.config['options_general']['date_start']) if (start_date == datestart): # very first timestep self.archive_static() # archive static variables self.cleanup()
def __init__(self): config.__init__(self) rundir = self.config['filesystem']['wrf_run_dir'] wpsdir = os.path.join(self.config['filesystem']['work_dir'], 'wps') ## wrf run dir # cleanup old met_em files # create list of files to remove #files = [glob.glob(os.path.join(rundir, ext)) # for ext in ['met_em*']] # flatten list #files_flat = [item for sublist in files for item in sublist] # remove files silently #[ utils.silentremove(filename) for filename in files_flat ] # copy new met_em files # create list of files to copy files = [glob.glob(os.path.join(wpsdir, ext)) for ext in ['met_em*']] # flatten list files_flat = [item for sublist in files for item in sublist] [ shutil.copyfile(filename, os.path.join(rundir, os.path.basename(filename))) for filename in files_flat ] ## wps workdir # create list of files to remove files = [ glob.glob(os.path.join(wpsdir, ext)) for ext in ['met_em*', 'FILE*', 'PFILE*', 'GRIBFILE*'] ] # flatten list files_flat = [item for sublist in files for item in sublist] # remove files silently [utils.silentremove(filename) for filename in files_flat]
def __init__(self, namelist): config.__init__(self) self.namelist = namelist self.read_namelist() self.create_namelist_copies() self.modify_coarse_namelist() self.modify_fine_namelist()
def __init__(self, itype='rural'): if itype not in ['rural', 'urban', 'both']: raise Exception( 'Unknown itype, should be one of rural, urban, both') config.__init__(self) # read WRF namelist in WRF work_dir wrf_nml = f90nml.read(self.config['options_wrf']['namelist.input']) self.wrfda_workdir = os.path.join( self.config['filesystem']['work_dir'], 'wrfda') self.wrf_rundir = self.config['filesystem']['work_dir'] # get number of domains ndoms = wrf_nml['domains']['max_dom'] # check if ndoms is an integer and >0 if not (isinstance(ndoms, int) and ndoms > 0): raise ValueError("'domains_max_dom' namelist variable should be an " \ "integer>0") doms = range(2, ndoms + 1) for dom in doms: pdomain = 1 self.read_init(dom, pdomain) if ((itype == 'rural') or (itype == 'both')): self.fix_2d_field('ALBBCK', 'CANWAT', 'MU', 'PSFC', 'SST', 'TMN', 'TSK', 'T2') self.fix_3d_field('P', 'PH', 'SH2O', 'SMOIS', 'T', 'W', 'QVAPOR') self.fix_3d_field_uv(self.XLAT_U_p, self.XLONG_U_p, self.XLAT_U_c, self.XLONG_U_c, 'U') self.fix_3d_field_uv(self.XLAT_V_p, self.XLONG_V_p, self.XLAT_V_c, self.XLONG_V_c, 'V') if ndoms > 1: self.cleanup(dom)
def __init__(self, filename): config.__init__(self) self.wrfda_workdir = os.path.join(self.config['filesystem']['work_dir'], 'wrfda') # verify input self.verify_input(filename) # fix urban temperatures in outer domain domain = 1 self.fix_temp(domain)
def __init__(self, dtobj, infile): config.__init__(self) if self.config['options_urbantemps']['ah.csv']: ahcsv = self.config['options_urbantemps']['ah.csv'] self.read_ah_csv(ahcsv, dtobj) self.options = self.read_tbl(infile) self.change_AH() self.write_tbl()
def __init__(self): config.__init__(self) # load config self.wrf_run_dir = self.config['filesystem']['wrf_run_dir'] self._cli_parser() self.define_retry_values() self.load_nml() self.change_namelist() self.write_namelist() self.run_wrf()
def _create_cylc_config(self, suitename, basedir): ''' Create cylc suite.rc configuration file based on config.json ''' config.__init__(self, os.path.join(basedir, suitename, 'config.json')) self.incr_hour = self.config['options_general']['run_hours'] self.wps_interval_hours = self.config['options_wps']['run_hours'] suiterc = self._header() suiterc += self._scheduling() suiterc += self._runtime() suiterc += self._visualization() self._write(suiterc, os.path.join(basedir, suitename, 'suite.rc'))
def __init__(self, low_only=False): config.__init__(self) # load config self.low_only = low_only self.rundir = self.config['filesystem']['wrf_run_dir'] self.wrfda_workdir = os.path.join( self.config['filesystem']['work_dir'], 'wrfda') self.max_dom = utils.get_max_dom( self.config['options_wrf']['namelist.input']) # copy default 3dvar obsproc namelist to namelist.obsproc self.obsproc_dir = os.path.join(self.config['filesystem']['wrfda_dir'], 'var/obsproc') # get dictionary with workdir/obs filename per domain self.obs = self.get_obsproc_dirs()
def __init__(self, datestring, interval): config.__init__(self) dt = utils.convert_cylc_time2(datestring) prevtime = dt - datetime.timedelta(hours=interval) wrfout_time = datetime.datetime.strftime(prevtime, '%Y-%m-%d_%H:%M:%S') max_dom = utils.get_max_dom() rundir = self.config['filesystem']['wrf_run_dir'] for dom in range(1, max_dom + 1): outfile = os.path.join(rundir, 'wrfout_d0' + str(dom) + '_' + wrfout_time) infile = os.path.join(rundir, 'wrfinput_d0' + str(dom)) os.system( '/home/haren/cylc-suites/forecast/bin/copy_urb_init.sh ' + outfile + ' ' + infile)
def __init__(self, args): config.__init__(self) obsDir = self.config['filesystem']['obs_dir'] obsFilename = self.config['filesystem']['obs_filename'] outputFile = os.path.join(obsDir, obsFilename) dt = utils.convert_cylc_time(args.datestring) # startdate dt1 = datetime.datetime(dt.year, dt.month, 1) dt1s = dt1.strftime('%Y%m%d') # convert to string inputdir = os.path.join(args.inputdir, dt1s) inputFile = os.path.join(inputdir, args.inputfile) # remove existing file utils.silentremove(outputFile) # copy inputfile to location specified in config.json shutil.copyfile(inputFile, outputFile)
def __init__(self, datestring): config.__init__(self) datestart = utils.convert_cylc_time(datestring) # initialize WRFDA object WRFDA = wrfda(datestart) WRFDA.prepare_updatebc(datestart) # update lower boundary conditions for domain in range(1, WRFDA.max_dom + 1): WRFDA.updatebc_run(domain) # run da_updatebc.exe # copy radar data into WRFDA workdir if available try: radarFile = self.config['filesystem']['radar_filepath'] radarTarget = os.path.join(self.config['filesystem']['work_dir'], 'wrfda', 'd01', 'ob.radar') shutil.copyfile(radarFile, radarTarget) except (KeyError, IOError): pass # prepare for running da_wrfvar.exe WRFDA.prepare_wrfda() # run da_wrfvar.exe WRFDA.wrfvar_run(1) # interpolate rural variables from wrfda wrfda_interpolate(itype='rural') try: urbanData = self.config['options_urbantemps']['urban_stations'] except KeyError: urbanData = False if urbanData: bskin = bumpskin(urbanData, dstationtypes=['davis', 'vp2', 'vantage']) # update URBPARM.TBL with anthropogenic heat factors try: urbparmFile = self.config['options_wrf']['urbparm.tbl'] except KeyError: urbparmFile = False if urbparmFile: urbparm(datestart, urbparmFile) # update lateral boundary conditions WRFDA.prepare_updatebc_type('lateral', datestart, 1) WRFDA.updatebc_run(1) # copy files over to WRF run_dir WRFDA.wrfda_post(datestart)
def __init__(self, datestring): config.__init__(self) dt = utils.convert_cylc_time(datestring) postprocess = upp() # construct wrfout name for domain 1 dt_str = dt.strftime('%Y-%m-%d_%H:%M:%S') wrfout_name = 'wrfout_d01_' + dt_str wrfout_file = os.path.join(self.config['filesystem']['wrf_run_dir'], wrfout_name) start_date = utils.return_validate( postprocess.config['options_general']['date_start']) upp_interval = postprocess.config['options_upp']['upp_interval'] if (start_date == dt): # very first timestep postprocess.run_unipost_file(wrfout_file, frequency=upp_interval, use_t0=True) else: postprocess.run_unipost_file(wrfout_file, frequency=upp_interval, use_t0=False)
def _create_directory_structure(self, suitename, basedir=None): ''' Create directory structure for the Cylc configuration ''' # set basedir to users home directory if not supplied if not basedir: basedir = os.path.join(os.path.expanduser("~"), 'cylc-suites') # subdirectories to create subdirs = ['bin', 'control', 'doc', 'inc'] # create subdirectories [ utils._create_directory(os.path.join(basedir, suitename, subdir)) for subdir in subdirs ] # copy over helper scripts for cylc cylcDir = pkg_resources.resource_filename('wrfpy', 'cylc/') targetDir = os.path.join(basedir, suitename, 'bin') copy_tree(cylcDir, targetDir) # create empty json config file in suite directory # this does not overwrite an existing config file config.__init__(self, os.path.join(basedir, suitename, 'config.json'))
def __init__(self, datestring, cylc_suite_def_path): config.__init__(self) dt = utils.convert_cylc_time(datestring) wrfout_time = datetime.datetime.strftime(dt, '%Y-%m-%d_%H:%M:%S') nml = self.config['options_wrf']['namelist.input'] max_dom = utils.get_max_dom(nml) rundir = self.config['filesystem']['wrf_run_dir'] archivedir = self.config['filesystem']['archive_dir'] gis_archive = os.path.join(archivedir, 'gis', wrfout_time) utils._create_directory(gis_archive) for dom in range(1, max_dom + 1): wrfout = os.path.join(rundir, 'wrfout_d0' + str(dom) + '_' + wrfout_time) archived = os.path.join(archivedir, 'wrfout_d0' + str(dom) + '_' + wrfout_time) utils.silentremove(archived) os.system('nc3tonc4 ' + wrfout + ' ' + archived) try: gis_out = os.path.join( gis_archive, 'meteo_gis_d0' + str(dom) + '_' + wrfout_time) os.system('cdo -f nc4c -z zip_4 selvar,Q2,T2,U10,V10 ' + wrfout + ' ' + gis_out) except Exception: pass plot_archive = os.path.join(archivedir, 'plot', wrfout_time) utils._create_directory(plot_archive) wrfncl = os.path.join(cylc_suite_def_path, 'bin', 'wrf_Surface3.ncl') os.system('ncl ' + wrfncl + ' inputfile=' + r'\"' + archived + r'\" outputfile=\"' + plot_archive + r'/surface_d0' + str(dom) + '.png' + r'\"') plot_latest = os.path.join(archivedir, 'plot', 'latest') try: os.symlink(plot_archive, plot_latest) except OSError, e: if e.errno == errno.EEXIST: os.remove(plot_latest) os.symlink(plot_archive, plot_latest)
def __init__(self): config.__init__(self) # read WRF namelist in WRF work_dir wrf_nml = f90nml.read(self.config['options_wrf']['namelist.input']) self.wrfda_workdir = os.path.join( self.config['filesystem']['work_dir'], 'wrfda') # get number of domains ndoms = wrf_nml['domains']['max_dom'] # check if ndoms is an integer and >0 if not (isinstance(ndoms, int) and ndoms > 0): raise ValueError("'domains_max_dom' namelist variable should be an " \ "integer>0") doms = range(2, ndoms + 1) for dom in doms: pdomain = 1 self.read_init(dom, pdomain) self.fix_2d_field('ALBBCK', 'CANWAT', 'MU', 'PSFC', 'SST', 'TMN', 'TSK') self.fix_3d_field('P', 'PH', 'SH2O', 'SMOIS', 'T', 'TSLB', 'W', 'QVAPOR') try: if self.config['options_general']['fix_urban_temps']: print("incrementing urban temperatures") # calculate 0/1 matrix for urban cells self.urb = np.zeros( np.shape(self.wrfinput_c.variables['LU_INDEX'][:])) self.urb[self.wrfinput_c.variables['LU_INDEX'] == 1] = 1 self.fix_2d_field('TC_URB', 'TR_URB', 'TB_URB', 'TG_URB', 'TS_URB') self.fix_3d_field('TRL_URB', 'TBL_URB', 'TGL_URB', 'TSLB') except KeyError: pass self.fix_3d_field_uv(self.XLAT_U_p, self.XLONG_U_p, self.XLAT_U_c, self.XLONG_U_c, 'U') self.fix_3d_field_uv(self.XLAT_V_p, self.XLONG_V_p, self.XLAT_V_c, self.XLONG_V_c, 'V') if ndoms > 1: self.cleanup()
def __init__(self, filename, nstationtypes=None, dstationtypes=None): config.__init__(self) # optional define station types to be used self.nstationtypes = nstationtypes # stationtypes at night self.dstationtypes = dstationtypes # stationtypes during daytime self.wrfda_workdir = os.path.join( self.config['filesystem']['work_dir'], 'wrfda') self.wrf_rundir = self.config['filesystem']['work_dir'] # verify input self.verify_input(filename) # get number of domains wrf_nml = f90nml.read(self.config['options_wrf']['namelist.input']) ndoms = wrf_nml['domains']['max_dom'] # check if ndoms is an integer and >0 if not (isinstance(ndoms, int) and ndoms > 0): raise ValueError("'domains_max_dom' namelist variable should be an" " integer>0") try: (lat, lon, diffT) = self.findDiffT(1) for domain in range(1, ndoms + 1): self.applyToGrid(lat, lon, diffT, domain) except TypeError: pass
def __init__(self, dtobj, nstationtypes=None, dstationtypes=None): config.__init__(self) # optional define station types to be used self.nstationtypes = nstationtypes # stationtypes at night self.dstationtypes = dstationtypes # stationtypes during daytime # define datestr datestr = datetime.strftime(dtobj, '%Y-%m-%d_%H:%M:%S') # define name of csv file self.wrf_rundir = self.config['filesystem']['work_dir'] fname = 'obs_stations_' + datestr + '.csv' self.csvfile = os.path.join(self.wrf_rundir, fname) try: # try to read an existing csv file self.read_csv(datestr) except IOError: if self.config['options_urbantemps']['urban_stations']: # reading existing csv file failed, start from scratch self.urbStations = self.config['options_urbantemps']['urban_stations'] self.verify_input() self.obs_temp_p(dtobj) self.write_csv(datestr) else: raise
def __init__(self): config.__init__(self) self._set_variables() self._initialize() self._prepare_post_dir() self._set_environment_variables()
def __init__(self): config.__init__(self) self.wrf_rundir = self.config['filesystem']['wrf_run_dir']
def __init__(self): config.__init__(self) # load config wps_namelist = self.config['options_wps']['namelist.wps'] split_nml_shared.__init__(self, wps_namelist) self._modify_fine_namelist_wps() self._save_namelists()
def __init__(self): config.__init__(self) # load config # define and create wps working directory self.wps_workdir = os.path.join(self.config['filesystem']['work_dir'], 'wps') utils._create_directory(self.wps_workdir)
def __init__(self): config.__init__(self) # load config wrf_namelist = self.config['options_wrf']['namelist.input'] split_nml_shared.__init__(self, wrf_namelist) self._save_namelists()
def __init__(self, datestart, dateend): config.__init__(self) # TODO: wrf_run_dir should be flexible if running in UPP mode self.wrf_run_dir = self.config['filesystem']['wrf_run_dir'] self.cleanup_previous_wrf_run() self.prepare_wrf_config(datestart, dateend)