16 __all__ =
'ysplitter',
'trailzero',
27 """!Splits a string with a ten digit YYYYMMDDHH into components
29 @param ymdh A string "YYYYMMDDHH" containing a date and hour.
30 @returns a tuple (y4,mm,mm1str,dd,hh) where y4 is the year, mm is
31 the month, dd is the day of the month and hh is the hour. The mmn1 is
32 the nearest month start/end boundary considering the day of month"""
45 return (y4,mm, mm1str,dd,hh)
47 """!Returns a string of length n or greater, that consists of
48 n-len(astr) zeros followed by astr
49 @param n,astr the strings to consider"""
50 (
'0'*max(0,int(n)-len(astr)) ) + astr
52 """!Creates an input file from a list of strings
53 @param filename the input file to create
54 @param lst the list of strings
55 @param logger a logging.Logger for log messages"""
56 if logger
is not None:
57 logger.info(
'%s: creating input file'%(filename,))
58 with open(filename,
"w")
as fid:
63 """!Deletes the specified file. Ignores errors.
64 @param file The file to remove."""
65 if arg
is None or arg==
'':
67 if os.path.exists(arg):
70 except EnvironmentError:
73 """!Deletes a list of files.
74 @param args All positional arguments contain names of files to remove."""
77 def copy(src, dest, force=True):
78 """!Copies the source to the destination without retaining permissions our group IDs.
81 @param src,dest Source and destination filenames.
82 @param force If True, the destination file is replaced if it already exists."""
84 if os.path.exists(dest)
and force:
89 if os.path.exists(dest)
and os.stat(dest).st_size==0:
94 if not os.path.exists(dest):
95 if os.path.exists(src):
96 shutil.copy(src, dest)
98 print(
"%s does NOT exists NOT copied" %(src))
100 print(
"%s does exists NOT copied" %(dest))
104 def move(src, dest, force=True):
105 """!Renames the source file to the destination
106 @param src,dest The source and destination filenames.
107 @param force if True, the destination is replaced if it already exists."""
109 if os.path.exists(dest)
and force:
114 if os.path.exists(dest)
and os.stat(dest).st_size==0:
119 if not os.path.exists(dest):
120 if os.path.exists(src):
121 shutil.move(src, dest)
123 print(
"%s does NOT exists NOT moved" %(src))
125 print(
"%s does exists NOT moved" %(dest))
129 """!Links the source file to the destination.
130 @param src,dest The source and destination filenames."""
132 if os.path.exists(dest):
137 if os.path.exists(src):
138 os.symlink(src, dest)
140 print(
"%s does NOT exists NOT linked" %(src))
144 def runexe(nproc, rundir, exefile, stdin, stdout):
145 """!Runs an executable. Will only run on the NOAA Jet cluster.
147 @warning Do not use this function. It only runs on NOAA Jet. It
148 lacks error checking other than returning the exit code and
149 there is no logging. Use the produtil.run module instead.
150 @param nproc Number of processors to use
151 @param rundir Directory in which to run.
152 @param exefile Executable to run.
153 @param stdin,stdout Input and output files.
155 if not os.path.exists(rundir):
156 print(
"%s does NOT exists " %(rundir))
159 if not os.path.exists(exefile):
160 print(
"%s does NOT exists " %(exefile))
163 if os.path.exists(stdin):
164 fin = open(stdin,
'r')
167 with open(rundir+
"/std.out",
'w')
as fout:
169 retcode=subprocess.call(exefile, stdin=fin,stdout=fout)
171 print(
"Failed: (%s)" % exefile )
175 with open(rundir+
"/std.out",
'w')
as fout:
176 runstr=
"/apps/local/bin/mpiexec -np " + str(nproc) +
" " + exefile
179 retcode=subprocess.call(runstr, stdout=fout, shell=
True)
180 except EnvironmentError
as e:
181 print(
"Failed: (%s): %s" %(exefile, str(e)))
186 """!Reads the contents of the file for name,value pairs and returns a dict with the contents.
188 Reads the file line-by-line. Searches for pairs of strings
189 "string1,string2" separated by spaces, colons, semicolons or
190 commas. Creates a dict mapping from the first string to the
191 second. Any strings past the second are ignored.
193 @param filename the file to read."""
196 with open(filename,
'rt')
as f:
198 line=re.split(
r'[;,:\s]\s*',line)
199 input[line[1]]=line[0]
202 print(
"can NOT open (%s)" %filename)
205 """!Wrapper for making the POM namelist.
206 @param f the return value from pom.nml.make()"""
207 def decoraten(self,DEST):
208 filename = DEST+
"/pom.nml"
210 with open(filename,
"w+")
as file:
212 file.write(
"%s\n" % (arg))
213 for k
in range(len(self.keys)):
214 file.write(
"\t %s %s %s\n" % (self.keys[k], asgn, \
215 self.namelist[self.keys[k]]))
216 file.write(
"%s\n" %(
'/'))
220 """!Adds a given number of hours to the date.
221 @param ymdh A string "YYYYMMDDHH"
222 @param hours number of hours to add"""
223 if len(ymdh) == 10
and isinstance(ymdh, str) \
224 and isinstance(hours, int):
225 t=datetime.datetime(int(ymdh[0:4]),int(ymdh[4:6]),int(ymdh[6:8]),
227 t+=datetime.timedelta(hours=hours)
228 return t.strftime(
'%Y%m%d%H')
230 print(
"InputErr: ymdh hours in dateplushours", ymdh, hours)
233 """!A simple integer counter class."""
237 """!Sets the counter value
238 @param x the new counter value"""
243 """!Increments the counter by 1."""
246 """!Decrements the counter by 1."""
251 """!Turns a bool to an int, returning 1 for True or 0 for False.
252 @param l the bool value."""
253 assert(type(l) == type(
True))
264 """!Prints a message about not exiting, and returns False."""
267 except SystemExit, value:
268 print(
"caught exit(%s)" % value)
272 except SystemExit, value:
273 print(
"caught exit(%s)" % value)
def set(self, x)
Sets the counter value.
def up(self)
Increments the counter by 1.
def runexe(nproc, rundir, exefile, stdin, stdout)
Runs an executable.
def rmall(args)
Deletes a list of files.
def move
Renames the source file to the destination.
def link(src, dest)
Links the source file to the destination.
int value
The current counter value.
def trailzero(astr, n)
Returns a string of length n or greater, that consists of n-len(astr) zeros followed by astr...
def logi2int(l)
Turns a bool to an int, returning 1 for True or 0 for False.
def read_input(filename)
Reads the contents of the file for name,value pairs and returns a dict with the contents.
def remove(file)
Deletes the specified file.
def makenwrap(f)
Wrapper for making the POM namelist.
def down(self)
Decrements the counter by 1.
def ysplitter(ymdh)
Splits a string with a ten digit YYYYMMDDHH into components.
A simple integer counter class.
def inpfile
Creates an input file from a list of strings.
def veto()
Prints a message about not exiting, and returns False.
def dateplushours(ymdh, hours)
Adds a given number of hours to the date.
def copy
Copies the source to the destination without retaining permissions our group IDs. ...