U #gQ#@sdZddlZddlZddlZddlZddlZddlZddlZddZddZ d"ddZ d d Z d d Z d#ddZ d$ddZddZddZddZddZddZGdddZddZejjZd d!ZdS)%) ysplitter trailzeroNcCs|dd}|dd}|dd}|dd}|dkrXt|dkrRt|d}qd}nN|d krt|dkrrd }qt|d}n&t|dkrt|d}n t|d}|d krd t|}nt|}|||||fS) aV!Splits a string with a ten digit YYYYMMDDHH into components @param ymdh A string "YYYYMMDDHH" containing a date and hour. @returns a tuple (y4,mm,mm1str,dd,hh) where y4 is the year, mm is the month, dd is the day of the month and hh is the hour. The mmn1 is the nearest month start/end boundary considering the day of monthr Z1201 0)intstr)ymdhy4mmddhhZmmn1Zmm1strr6/lfs/h1/ops/prod/packages/hwrf.v13.2.9/ush/pom/util.pyrs&        rcCs"dtdt|t||dS)z!Returns a string of length n or greater, that consists of n-len(astr) zeros followed by astr @param n,astr the strings to considerr rN)maxrlen)Zastrnrrrr:src CsP|dk r|d|ft|d$}|D]}|||dq(W5QRXdS)z!Creates an input file from a list of strings @param filename the input file to create @param lst the list of strings @param logger a logging.Logger for log messagesNz%s: creating input filew )infoopenwrite)filenamelstloggerZfidZmemrrrinpfile?s   r"cCsHtdkstdkrdStjtrDzttWntk rBYnXdS)zQ!Deletes the specified file. Ignores errors. @param file The file to remove.N)argospathexistsunlinkEnvironmentError)filerrrremoveJs r+cGs|D] }t|qdS)zd!Deletes a list of files. @param args All positional arguments contain names of files to remove.N)r+)argsr$rrrrmallTsr-TcCsztj|r|rt|Wntjk r4YnXz*tj|r^t|jdkr^t|Wntjk rvYnXzDtj|stj|rt||qt d|n t d|Wntjk rYnXdS)z!Copies the source to the destination without retaining permissions our group IDs. Errors are ignored. @param src,dest Source and destination filenames. @param force If True, the destination file is replaced if it already exists.rz%s does NOT exists NOT copiedz%s does exists NOT copiedN) r%r&r'r+errorstatst_sizeshutilcopyprintsrcdestforcerrrr2Ys$  r2cCsztj|r|rt|Wntjk r4YnXz*tj|r^t|jdkr^t|Wntjk rvYnXzDtj|stj|rt||qt d|n t d|Wntjk rYnXdS)z!Renames the source file to the destination @param src,dest The source and destination filenames. @param force if True, the destination is replaced if it already exists.rz%s does NOT exists NOT movedz%s does exists NOT movedN) r%r&r'r+r.r/r0r1mover3r4rrrr8ts$  r8cCsxztj|rt|Wntjk r0YnXz*tj|rNt||n td|Wntjk rrYnXdS)zd!Links the source file to the destination. @param src,dest The source and destination filenames.z%s does NOT exists NOT linkedN)r%r&r'r+r.symlinkr3)r5r6rrrlinks  r:c CsNtj|std|dSt|tj|sBtd|dS|dkrtj|rbt|d}nd}t|dd<}ztj|||d}Wn tk rtd |YnXW5QRX|St|ddp}d t |d |}t|ztj||d d }Wn8t k r2} ztd|t | fW5d} ~ XYnXW5QRX| |SdS)a!Runs an executable. Will only run on the NOAA Jet cluster. @warning Do not use this function. It only runs on NOAA Jet. It lacks error checking other than returning the exit code and there is no logging. Use the produtil.run module instead. @param nproc Number of processors to use @param rundir Directory in which to run. @param exefile Executable to run. @param stdin,stdout Input and output files. z%s does NOT exists ir rNz/std.outr)stdinstdoutz Failed: (%s)z/apps/local/bin/mpiexec -np z T)r=shellzFailed: (%s): %s) r%r&r'r3chdirr subprocesscallOSErrorrr)close) nprocrundirZexefiler<r=finfoutretcoderunstrerrrrunexes8        rKc CslzFi}t|d,}|D] }td|}|d||d<qW5QRX|WStk rftd|YnXdS)az!Reads the contents of the file for name,value pairs and returns a dict with the contents. Reads the file line-by-line. Searches for pairs of strings "string1,string2" separated by spaces, colons, semicolons or commas. Creates a dict mapping from the first string to the second. Any strings past the second are ignored. @param filename the file to read.rtz [;,:\s]\s*rr zcan NOT open (%s)N)rresplitIOErrorr3)rinputflinerrr read_inputs   rScsfdd}|S)zW!Wrapper for making the POM namelist. @param f the return value from pom.nml.make()c s|d}d}t|d`}d}|d|tt|jD]*}|d|j|||j|j|fq8|ddW5QRXS)Nz/pom.nmlz = zw+z&pom_nmlz%s z %s %s %s /)rrrangerkeysnamelist)selfDESTrZasgnr*r$krQrr decoratens  zmakenwrap..decoratenr)rQr\rr[r makenwraps r]c Cst|dkrzt|trzt|trztt|ddt|ddt|ddt|dd}|tj|d7}|dStd||fd S) zx!Adds a given number of hours to the date. @param ymdh A string "YYYYMMDDHH" @param hours number of hours to addrrrrr)hoursz%Y%m%d%Hz&InputErr: ymdh hours in dateplushoursN)r isinstancerrdatetime timedeltastrftimer3)rr^trrr dateplushourss. rdc@s,eZdZdZdZddZddZddZd S) counterz !A simple integer counter class.rcCs ||_dS)z>!Sets the counter value @param x the new counter valueNvalue)rXxrrrsetsz counter.setcCs|jd|_dS)z!Increments the counter by 1.r NrfrXrrrupsz counter.upcCs|jd|_dS)z!Decrements the counter by 1.r Nrfrjrrrdownsz counter.downN)__name__ __module__ __qualname____doc__rgrirkrlrrrrres recCs$t|tdkst|rdSdSdS)zZ!Turns a bool to an int, returning 1 for True or 0 for False. @param l the bool value.Tr rN)typeAssertionError)lrrrlogi2intsrtc CsztdWn4tk rB}ztd|WYdSd}~XYnXztdWn4tk r}ztd|WYdSd}~XYnXdS)z7!Prints a message about not exiting, and returns False.r zcaught exit(%s)FN)sysexit SystemExitr3r%_exitrfrrrvetos  rz)N)T)T)__all__r%rMrvr1os.pathr`r@rrr"r+r-r2r8r:rKrSr]rdrertr&joinjn2rrzrrrrs.     )