#!/usr/bin/env python # version 1.1 March 2013 # Written by Luke Madaus # Will output the appropriately-timed namelist files for operation from datetime import datetime, timedelta from optparse import OptionParser import os # This is where we get the variables we need from the command line parser = OptionParser() parser.add_option('-i','--inittime',dest='init_hour',help='Specify start hour (today is assumed)',default=None) parser.add_option('-d','--startdate',dest='start_date',help='Specify start date (YYMMDDHH)',default=datetime.utcnow().strftime('%Y%m%d')+'12') parser.add_option('-l','--runlength',dest='run_length',help='Run length in hours',default='48') # Set the file names to read from standard_wps = "namelist.wps" standard_input = "namelist.input" # Actaully parse the command line options here (opts,args) = parser.parse_args() # Set variables from the command line options try: starttime = datetime.strptime(opts.start_date, '%Y%m%d%H') except: print "Couldn't format the time %s!" % (opts.start_date) exit(1) if opts.init_hour != None: starttime = starttime.replace(hour=int(opts.init_hour)) # Runlength becomes a timedelta try: runlength = timedelta(hours = int(opts.run_length)) except: print "Unable to parse runlength %s!" % (opts.run_length) exit(1) # Compute the end time from starttime and runlength endtime = starttime + runlength # Now we have our start time and length. # We have two possible namelists to edit. # Check for namelist.wps in this directory first. if os.path.exists('namelist.wps'): # If it exists, open it for reading wpsfile = open('namelist.wps','r') # Open a blank new outfile to write to wpsout = open('new_namelist.wps','w') # Now read each line of the wps file. wpslines = wpsfile.readlines() # Loop through the lines and edit the ones that need to be changed. for line in wpslines: # strip each line to remove whitespace from beginning and newlines from the end. linestrip = line.strip() # Now we start checking. First see if this is the start_date line if linestrip.startswith('start_date'): # If it is, rewrite the line with the the new starttime # (the backslash means continue this command on the next line print >> wpsout, " start_date = '%s', '%s'," \ % (starttime.strftime('%Y-%m-%d_%H:%M:%S'),starttime.strftime('%Y-%m-%d_%H:%M:%S')) # Now check if this is the enddate line elif linestrip.startswith('end_date'): # Same as before print >> wpsout, " end_date = '%s', '%s'," \ % (endtime.strftime('%Y-%m-%d_%H:%M:%S'),endtime.strftime('%Y-%m-%d_%H:%M:%S')) # If the line starts with neither of these two things, then we don't # care about changing this line. Just print it back out. else: print >> wpsout, " " + linestrip # And that's it! Close both the wpsfile and wpsout file. Then, move the new_namelist.wps to # overwrite namelist.wps wpsfile.close() wpsout.close() os.system('mv -f new_namelist.wps namelist.wps') # Now check to see if namelist.input is in this directory. if os.path.exists('namelist.input'): # If it exists, open it for reading. namefile = open('namelist.input','r') # Also open a new output file nameout = open('new_namelist.input','w') # Read each line of the namefile for line in namefile.readlines(): # Strip the line of leading whitespace and trailing newlines linestrip = line.strip() # Now start checking for various lines # We only really need to change run_days and # run seconds if linestrip.startswith('run_days'): print >> nameout, " run_days = %d," % runlength.days elif linestrip.startswith('run_seconds'): print >> nameout, " run_seconds = %d," % runlength.seconds # Now the long process of writing the start and end dates individually elif linestrip.startswith('start_year'): print >> nameout, " start_year = %d, %d," % (starttime.year, starttime.year) elif linestrip.startswith('start_month'): print >> nameout, " start_month = %02d, %02d," % (starttime.month, starttime.month) elif linestrip.startswith('start_day'): print >> nameout, " start_day = %02d, %02d," % (starttime.day, starttime.day) elif linestrip.startswith('start_hour'): print >> nameout, " start_hour = %02d, %02d," % (starttime.hour, starttime.hour) elif linestrip.startswith('start_minute'): print >> nameout, " start_minute = %02d, %02d," % (starttime.minute, starttime.minute) elif linestrip.startswith('start_second'): print >> nameout, " start_second = %02d, %02d," % (starttime.second, starttime.second) elif linestrip.startswith('end_year'): print >> nameout, " end_year = %d, %d," % (endtime.year, endtime.year) elif linestrip.startswith('end_month'): print >> nameout, " end_month = %02d, %02d," % (endtime.month, endtime.month) elif linestrip.startswith('end_day'): print >> nameout, " end_day = %02d, %02d," % (endtime.day, endtime.day) elif linestrip.startswith('end_hour'): print >> nameout, " end_hour = %02d, %02d," % (endtime.hour, endtime.hour) elif linestrip.startswith('end_minute'): print >> nameout, " end_minute = %02d, %02d," % (endtime.minute, endtime.minute) elif linestrip.startswith('end_second'): print >> nameout, " end_second = %02d, %02d," % (endtime.second, endtime.second) # And if the line is anything else, we don't care. # Just get the justification right for & and / lines elif linestrip.startswith('&') or linestrip.startswith('/'): print >> nameout, linestrip else: print >> nameout, " " + linestrip # Close the files and move the new one into place namefile.close() nameout.close() os.system('mv -f new_namelist.input namelist.input')