def cm1_prep(mem, start, end, restart_name='cm1out_rst_000001.nc'): """ Function to write new namelist.input and check to be sure files are in order """ # If this is a restart, be sure we have the right file if start != 0: # Try loading the restart file with the correct name if not os.path.exists(restart_name): error_handler( 'unable to find file {:s} in member {:d} directory'.format( restart_name, mem), 'cm1_prep') # Make sure that the restart file is indeed at the correct time rstnc = Dataset(restart_name, 'r') time_sec = int(rstnc.variables['time'][0]) rstnc.close() if time_sec != start: error_handler( 'restart file {:s} for member {:d} does not match current cycle time: {:d} min.' .format(restart_name, mem, start), 'cm1_prep') # Find out how long the run should be dtime = int(end - start) # Write the namelist if not os.path.exists('namelist.input'): os.system('cp {:s}/namelist.input .'.format(dir_dom)) nmld = read_namelist('namelist.input') if start != 0: irst = 1 nmld['param2']['irst'] = 1 nmld['param2']['rstnum'] = 1 nmld['param1']['run_time'] = dtime nmld['param1']['rstfrq'] = cycle_len write_namelist(nmld, 'namelist.input') #os.system('./write_cm1_namelist.py -r {:s} -l {:d}'.format(restart_name, dtime)) else: nmld['param1']['run_time'] = dtime nmld['param1']['rstfrq'] = cycle_len write_namelist(nmld, 'namelist.input') #os.system('./write_cm1_namelist.py -l {:d}'.format(dtime)) if not os.path.exists('namelist.input'): error_handler('Unable to find namelist.input', 'wrf_prep')
def cm1_prep(mem,start,end,restart_name='cm1out_rst_000001.nc'): """ Function to write new namelist.input and check to be sure files are in order """ # If this is a restart, be sure we have the right file if start != 0: # Try loading the restart file with the correct name if not os.path.exists(restart_name): error_handler('unable to find file {:s} in member {:d} directory'.format(restart_name, mem), 'cm1_prep') # Make sure that the restart file is indeed at the correct time rstnc = Dataset(restart_name, 'r') time_sec = int(rstnc.variables['time'][0]) rstnc.close() if time_sec != start: error_handler('restart file {:s} for member {:d} does not match current cycle time: {:d} min.'.format(restart_name, mem, start), 'cm1_prep') # Find out how long the run should be dtime = int(end-start) # Write the namelist if not os.path.exists('namelist.input'): os.system('cp {:s}/namelist.input .'.format(dir_dom)) nmld = read_namelist('namelist.input') if start != 0: irst = 1 nmld['param2']['irst'] = 1 nmld['param2']['rstnum'] = 1 nmld['param1']['run_time'] = dtime nmld['param1']['rstfrq'] = cycle_len write_namelist(nmld, 'namelist.input') #os.system('./write_cm1_namelist.py -r {:s} -l {:d}'.format(restart_name, dtime)) else: nmld['param1']['run_time'] = dtime nmld['param1']['rstfrq'] = cycle_len write_namelist(nmld, 'namelist.input') #os.system('./write_cm1_namelist.py -l {:d}'.format(dtime)) if not os.path.exists('namelist.input'): error_handler('Unable to find namelist.input','wrf_prep')
parser = OptionParser(description=desc) parser.add_option('-d','--datein',dest='datein',action='store',type='string',default=cycle_len,\ help='Date of assimilation cycle (YYYYMMDDHH)') parser.add_option('-m','--mpi_procs',dest='mpi_procs',action='store',type='string',\ default=mpi_numprocs_filter, help='Number of processors to use for MPI run') (opts, args) = parser.parse_args() #datein = datetime.strptime(opts.datein,'%Y%m%d%H') #prevdate = datein - timedelta(minutes=int(fct_len)) datein = int(opts.datein) prevdate = datein - cycle_len mpi_numprocs_filter = int(opts.mpi_procs) # Get the namelist values nmld = read_namelist('input.nml') cm1nmld = read_namelist('namelist.input') # Change these values to only run certain sections of the code PRE_CLEAN = True PRE_CHECK = True RUN_FILTER = True ARCHIVE_FILES = True POST_CLEAN = True def main(): # First step--write the namelist # Need to know if we are trying to apply adaptive # inflation from the previous time or not. If we are using it,
parser = OptionParser(description=desc) parser.add_option('-d','--datein',dest='datein',action='store',type='string',default=cycle_len,\ help='Date of assimilation cycle (YYYYMMDDHH)') parser.add_option('-m','--mpi_procs',dest='mpi_procs',action='store',type='string',\ default=mpi_numprocs_filter, help='Number of processors to use for MPI run') (opts,args) = parser.parse_args() #datein = datetime.strptime(opts.datein,'%Y%m%d%H') #prevdate = datein - timedelta(minutes=int(fct_len)) datein = int(opts.datein) prevdate = datein - cycle_len mpi_numprocs_filter = int(opts.mpi_procs) # Get the namelist values nmld = read_namelist('input.nml') cm1nmld = read_namelist('namelist.input') # Change these values to only run certain sections of the code PRE_CLEAN = True PRE_CHECK = True RUN_FILTER = True ARCHIVE_FILES = True POST_CLEAN = True def main(): # First step--write the namelist # Need to know if we are trying to apply adaptive # inflation from the previous time or not. If we are using it,
obsdir, assim_target_date)): print("Could not find obs sequence file:") print("{:s}/{:%Y%m%d%H%M%S}_obs_seq.prior".format(obsdir, assim_target_date)) print("Aborting.") exit(1) os.system( '{copy} {obsdir}/{assim_target_date:%Y%m%d%H%M%S}_obs_seq.prior obs_seq.prior' .format(**locals())) # Make a subdirectory in the archive directory to hold output for this cycle os.system('mkdir {:s}/archive/{:%Y%m%d%H%M%S}'.format(centraldir, assim_target_date)) # Load the input.nml file and change it to reflect this time dartnml = read_namelist('input.nml') #> if not initializing, we need to ensure that the output inflation files from #> previous cycle are used as input for next cycle if not initialize: dartnml['filter_nml']['inf_initial_from_restart'] = [True, True] dartnml['filter_nml']['inf_sd_initial_from_restart'] = [True, True] # Change the name of the template file dartnml['model_nml']['cm1_template_file'] = restart_filename # Set the right obs sequence name dartnml['filter_nml']['obs_sequence_in_name'] = 'obs_seq.prior' # Set the number of ensemble members dartnml['filter_nml']['ens_size'] = ens_size
# set this to "true" # this script is going to determine several things by reading the input.nml # file which contains the &filter_nml namelist. make sure it exists first. #if ( ! -e input.nml ) then if not os.path.exists('input.nml'): print("ERROR - input.nml does not exist in local directory.") print("ERROR - input.nml needed to determine several settings for this script.") exit(1) # detect whether the model is supposed to run as an MPI job or not # by reading the "async = " from the &filter_nml namelist in input.nml. # some namelists contain the same string - be sure to get the filter_nml one # by grepping for lines which follow it. nmld = read_namelist('input.nml') async_type = nmld['filter_nml']['async'] if (async_type == 0) or (async_type == 2): parallel_model = False elif (async_type == 4): parallel_model = True else: print("cannot autodetect async value in the filter_nml namelist in input.nml file.") print("hardcode the parallel_model shell variable and comment out these lines.") exit(-1) parallel_model = False # Determine the number of ensemble members from input.nml, # as well as the command for advancing the model. num_ens = nmld['filter_nml']['ens_size']
def build_obs_structure(intime, rst_file, gridspace=16): """ Function to specify what variables we want and how dense they should be """ #from make_namelist_dart import set_namelist_sectors, write_namelist #from write_cm1_namelist import set_namelist_defaults from namelist_utils import read_namelist, write_dart_namelist # Generate the DART namelist structure so we # can query (and modify) it dartnml = read_namelist('input.nml') cm1nml = read_namelist('namelist.input') # Build the epoch from the cm1 namelist param11 = cm1nml['param11'] startdate = datetime(param11['year'], param11['month'],\ param11['day'], param11['hour'],\ param11['minute'], param11['second']) # Parse out the observations to assimilate use_obs = dartnml['obs_kind_nml']['assimilate_these_obs_types'] # I'm just testing this for now #use_obs = use_obs[0:2] use_obs = ['LAND_SFC_PRESSURE'] print(use_obs) # Put time as datetime intime = startdate + timedelta(seconds=intime) epoch = datetime(1601,1,1,0) print("Indate:", intime) # Figure out the grid structure dx = cm1nml['param1']['dx'] dy = cm1nml['param1']['dy'] nx = cm1nml['param0']['nx'] ny = cm1nml['param0']['ny'] # Now figure out how many obs we'll need gridspace *= 1000 obx = range(int(dx/2.),int(nx*dx),gridspace) oby = range(int(dy/2.),int(ny*dx),gridspace) #obx = [16000] #oby = [16000] total_obs = len(obx)*len(oby)*len(use_obs) print("Total number of obs:", total_obs) # Write a new dart namelist for the current time delt = intime - epoch #dartnml['perfect_model_obs_nml']['first_obs_days'] = str(delt.days) #dartnml['perfect_model_obs_nml']['first_obs_seconds'] = str(delt.seconds) #dartnml['perfect_model_obs_nml']['last_obs_days'] = str(delt.days) #dartnml['perfect_model_obs_nml']['last_obs_seconds'] = str(delt.seconds) #dartnml['perfect_model_obs_nml']['init_time_days'] = str(delt.days) #dartnml['perfect_model_obs_nml']['init_time_seconds'] = str(delt.seconds) dartnml['perfect_model_obs_nml']['obs_seq_in_file_name'] = 'obs_seq.in' dartnml['perfect_model_obs_nml']['restart_in_file_name'] = rst_file # Make sure we have the right io pattern dartnml['io_filenames_nml']['restart_in_stub'] = 'notinuse' dartnml['io_filenames_nml']['overwrite_input'] = False dartnml['io_filenames_nml']['rpointer'] = True dartnml['io_filenames_nml']['rpointer_file'] = 'input_filelist.txt' # Write the pointer file with open('input_filelist.txt', 'w') as pointerfile: pointerfile.write(rst_file) os.system('mv input_filelist.txt {:s}/'.format(dir_obs)) # Write the modified namelist write_dart_namelist(dartnml) # Set up the input file with open('obs_seq_input.txt','w') as infile: infile.write(str(total_obs)+'\n') # Total num obs infile.write('0\n') # 0 copies infile.write('0\n') # 0 QC values for obtype in use_obs: for x in obx: for y in oby: infile.write('0\n') infile.write(obtype+'\n') # Identify ob type infile.write('0\n') # Specify location infile.write(str(x)+'\n') # X coordinate infile.write(str(y)+'\n') # Y coordinate infile.write('0\n') # Z coordinate infile.write('{:d} {:d} {:d} {:d} {:d} {:d}\n'.format(\ intime.year, intime.month,\ intime.day, intime.hour,\ intime.minute, intime.second)) infile.write(str(error_var[obtype])+'\n') # Error variance infile.write('obs_seq.in\n') # Move these files to the obs dir if os.path.exists(os.path.join(dir_obs, 'input.nml')): os.system('rm -f {:s}'.format(os.path.join(dir_obs, 'input.nml'))) if os.path.exists(os.path.join(dir_obs, 'obs_seq_input.txt')): os.system('rm -f {:s}'.format(os.path.join(dir_obs, 'obs_seq_input.txt'))) os.system('mv obs_seq_input.txt {:s}'.format(dir_obs)) os.system('cp input.nml {:s}'.format(dir_obs)) return None
def build_obs_structure(intime, rst_file, gridspace=4): """ Function to specify what variables we want and how dense they should be """ #from make_namelist_dart import set_namelist_sectors, write_namelist #from write_cm1_namelist import set_namelist_defaults from namelist_utils import read_namelist, write_dart_namelist # Generate the DART namelist structure so we # can query (and modify) it dartnml = read_namelist('input.nml') cm1nml = read_namelist('namelist.input') # Build the epoch from the cm1 namelist param11 = cm1nml['param11'] startdate = datetime(param11['year'], param11['month'],\ param11['day'], param11['hour'],\ param11['minute'], param11['second']) # Parse out the observations to assimilate use_obs = dartnml['obs_kind_nml']['assimilate_these_obs_types'] # I'm just testing this for now #use_obs = use_obs[0:2] #use_obs = ['LAND_SFC_U_WIND_COMPONENT', 'LAND_SFC_V_WIND_COMPONENT', 'LAND_SFC_TEMPERATURE', # 'LAND_SFC_SPECIFIC_HUMIDITY'] use_obs = [ 'TEMPERATURE_2M', 'SURFACE_PRESSURE', 'SPECIFIC_HUMIDITY_2M', 'V_WIND_10M', 'U_WIND_10M' ] print(use_obs) # Put time as datetime intime = startdate + timedelta(seconds=intime) epoch = datetime(1601, 1, 1, 0) print("Indate:", intime) # Figure out the grid structure dx = cm1nml['param1']['dx'] dy = cm1nml['param1']['dy'] nx = cm1nml['param0']['nx'] ny = cm1nml['param0']['ny'] # Now figure out how many obs we'll need gridspace *= 1000 obx = range(int(dx / 2.), int(nx * dx), gridspace) oby = range(int(dy / 2.), int(ny * dx), gridspace) #obx = [16000] #oby = [16000] total_obs = len(obx) * len(oby) * len(use_obs) print("Total number of obs:", total_obs) # Write a new dart namelist for the current time delt = intime - epoch #dartnml['perfect_model_obs_nml']['first_obs_days'] = str(delt.days) #dartnml['perfect_model_obs_nml']['first_obs_seconds'] = str(delt.seconds) #dartnml['perfect_model_obs_nml']['last_obs_days'] = str(delt.days) #dartnml['perfect_model_obs_nml']['last_obs_seconds'] = str(delt.seconds) #dartnml['perfect_model_obs_nml']['init_time_days'] = str(delt.days) #dartnml['perfect_model_obs_nml']['init_time_seconds'] = str(delt.seconds) dartnml['perfect_model_obs_nml']['obs_seq_in_file_name'] = 'obs_seq.in' dartnml['perfect_model_obs_nml']['restart_in_file_name'] = rst_file # Make sure we have the right io pattern dartnml['io_filenames_nml']['restart_in_stub'] = 'notinuse' dartnml['io_filenames_nml']['overwrite_input'] = False dartnml['io_filenames_nml']['rpointer'] = True dartnml['io_filenames_nml']['rpointer_file'] = 'input_filelist.txt' # Write the pointer file with open('input_filelist.txt', 'w') as pointerfile: pointerfile.write(rst_file) os.system('mv input_filelist.txt {:s}/'.format(dir_obs)) # Write the modified namelist write_dart_namelist(dartnml) # Set up the input file with open('obs_seq_input.txt', 'w') as infile: infile.write(str(total_obs) + '\n') # Total num obs infile.write('0\n') # 0 copies infile.write('0\n') # 0 QC values for obtype in use_obs: for x in obx: for y in oby: infile.write('0\n') infile.write(obtype + '\n') # Identify ob type infile.write('0\n') # Specify location infile.write(str(x) + '\n') # X coordinate infile.write(str(y) + '\n') # Y coordinate infile.write(str(heights[obtype]) + '\n') # Z coordinate infile.write('{:d} {:d} {:d} {:d} {:d} {:d}\n'.format(\ intime.year, intime.month,\ intime.day, intime.hour,\ intime.minute, intime.second)) infile.write(str(error_var[obtype]) + '\n') # Error variance infile.write('obs_seq.in\n') # Move these files to the obs dir if os.path.exists(os.path.join(dir_obs, 'input.nml')): os.system('rm -f {:s}'.format(os.path.join(dir_obs, 'input.nml'))) if os.path.exists(os.path.join(dir_obs, 'obs_seq_input.txt')): os.system('rm -f {:s}'.format( os.path.join(dir_obs, 'obs_seq_input.txt'))) os.system('mv obs_seq_input.txt {:s}'.format(dir_obs)) os.system('cp input.nml {:s}'.format(dir_obs)) return None
os.system( '{copy} {truthdir}/input_sounding {dirname} || exit 2'. format(**locals())) os.system( '{copy} {truthdir}/LANDUSE.TBL {dirname} || exit 2'. format(**locals())) os.system( '{copy} {truthdir}/namelist.input {dirname}/namelist.input.template || exit 2' .format(**locals())) # Enforce certain assumptions. # 1) we are always starting from a netCDF file ... irst == 1 # 2) restart always be named cm1_out_rst_000001.nc ... rstnum == 1 # 3) the run_time value will be a dummy string # Python version --use namelist utilities nmld = read_namelist(os.path.join(dirname, 'namelist.input.template')) nmld['param2']['irst'] = 1 nmld['param2']['rstnum'] = 1 nmld['param1']['run_time'] = cm1_forecast_length nmld['param1']['rstfrq'] = cm1_forecast_length write_namelist(nmld, os.path.join(dirname, 'namelist.input.template')) os.system('{remove} input_filelist.txt'.format(**locals())) # Python -- write to input_filelist.txt in with # statement. This will automatically close # the file when we are out of the block with open('input_filelist.txt', 'w') as outfile: outfile.write(os.path.join(dirname, filename) + '\n') # When perfect_model_obs starts, it needs to read the grid information.
#!/usr/bin/env python import os, sys, getopt # Get parameters from ens_dart_param and the default cm1 namelist from ens_dart_param import * from namelist_utils import read_namelist, write_namelist cm1nml = read_namelist(os.path.join(dir_dom, 'namelist.input')) """ This is a quickly-reformatted version of the make_ensemble script from version 1 of this code that makes it compatible with the new namelist version. This script will make an ensemble (in serial) with a number of members as specified in the namelist variable "Ne" or manually specified from the command line. The script will copy in all of the necessary files. No perturbation of initial conditions is enabled at this time. LEM 09/2015 """ (opts, args) = getopt.getopt(sys.argv[1:], 'n:') for o, a in opts: if o == '-n': Ne = int(a) def main(): DOMDIR = dir_dom os.chdir(DOMDIR) """ if os.path.isdir(DOMDIR+'/mems'): remove = raw_input('Remove contents of current ensemble directory (0 or 1)?') if remove:
'{copy} {ensembledir}/input_sounding . || exit 2'.format( **locals())) os.system( '{copy} {ensembledir}/LANDUSE.TBL . || exit 2'.format( **locals())) os.system( '{copy} {ensembledir}/namelist.input . || exit 2'.format( **locals())) # Enforce certain assumptions. # 1) we are always starting from a netCDF file ... irst == 1 # 2) restart always be named cm1_out_rst_000001.nc ... rstnum == 1 # 3) the run_time value will be a dummy string # Python version --use namelist utilities # read the namelist nmld = read_namelist('namelist.input') # Check for environment variable that # contains the current forecast length # Otherwise leave as is if os.environ.get('CM1_FORECAST_LENGTH') not in [None, '']: cm1_forecast_length = float(os.envion.get('CM1_FORECAST_LENGTH')) nmld['param1']['run_time'] = cm1_forecast_length nmld['param1']['rstfrq'] = cm1_forecast_length # Set the other namelist requirements and write nmld['param2']['irst'] = 1 nmld['param2']['rstnum'] = 1 write_namelist(nmld, 'namelist.input') # Python -- write to input_filelist.txt in with
#!/usr/bin/env python import os, sys, getopt # Get parameters from ens_dart_param and the default cm1 namelist from ens_dart_param import * from namelist_utils import read_namelist, write_namelist cm1nml = read_namelist(os.path.join(dir_dom, 'namelist.input')) """ This is a quickly-reformatted version of the make_ensemble script from version 1 of this code that makes it compatible with the new namelist version. This script will make an ensemble (in serial) with a number of members as specified in the namelist variable "Ne" or manually specified from the command line. The script will copy in all of the necessary files. No perturbation of initial conditions is enabled at this time. LEM 09/2015 """ (opts,args) = getopt.getopt(sys.argv[1:],'n:') for o,a in opts: if o == '-n': Ne = int(a) def main(): DOMDIR = dir_dom os.chdir(DOMDIR) """