U  g%@sdZdddddgZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddlmZmZmZmZmZmZmZdd lmZdd lmZmZmZdd l mZmZmZd Zd ZdZeeBeBZGddde j j!Z"dS)zG!Post-processes GSI input and output for data assimilation diagnostics.GSIPostF_ORGF_GESF_ANLF_ALLN)runcheckrunaliasmpimpirunexebigexe)NamedDir) deliver_file make_symlink remove_file) EGRIB1Product check_post link_post_fixcsJeZdZdZfddZd ddZddZd d Zddefd d Z Z S)ra!Post-processes GSI inputs and outputs. Runs the hwrf_post on GSI inputs and outputs from one or more of three stages. * original data (F_ORG) - the original, downscaled, parent model data * first guess (F_GES) - the original data, with the vortex relocated * analysis (F_ANL) - the final data input to the forecast This class is compatible with the hwrf.regrib and hwrf.gribtask, and can be used in place of an hwrf.post.PostManyWRF. c s\tt|j|||f|t|_t|_t|_t|_t|_ t|_ t|_ t|_ dS)a'!Constructor for GSIPost @param dstore the produtil.datastore.Datastore database to use @param conf the hwrf.config.HWRFConfig configuration information @param section the section to use in conf @param kwargs keyword arguments passed to the superclass constructorN) superr__init__list_domainsdict_wrfout_wrforg_wrfges_wrfanl _org_products _ges_products _anl_products)selfdstoreconfsectionkwargs __class__9/lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/hwrf/gsipost.pyr+szGSIPost.__init__NcCs|dkr|dkr|dkrt|dk s(t|dk s4t|j|||j|<|dk r||j|<t|j|j|jd|j d|j |<|dk r||j |<t|j|j|jd|j d|j |<|dk r||j |<t|j|j|jd|j d|j|<dS)aT!Adds another case to the list of those to post-process @param domain a WRFDomain to be processed @param wrfout a wrfout file for internal use. It will be copied and its fields will e replaced with the ges and/or analysis file as needed @param org Optional: original down-scaled parent domain data @param ges Optional: a first guess wrfinput or restart file, generally after vortex relocation @param anl Optional: an analysis (GSI output) wrfinput or restart file @note At least one of org, ges or anl must be given.NZ_org_)categoryprodnameZ_ges_Z_anl_)AssertionErrorrappendrrrr%tasknamenamer.r!rr"r r#)r$domainwrfoutorggesanlr+r+r,add_case<s4          zGSIPost.add_casec Cs>|}d}|jD]}|dt|f|j|}||jkrt|dt|f||j||d||j|n|dt|f||jkr|dt|f||j ||d||j|n|dt|f||j kr|d t|f||j ||d ||j |q|d t|fq|d d S)z4!Executes the GSI post for all cases on all domains.Tz%s: process this domainz%s: process orgr5z%s: has no org dataz%s: process gesr6z%s: has no ges dataz%s: process anlr7z%s: has no anl datazDone processing domains.N) logrinfostrrr!_processrr"rr#r )r$loggerZgoodr3r4r+r+r,rbsB        z GSIPost.runc Cs|dk s t|dk st|dk s$t|dk s0t|}|jsBt|j}|sPttj|sntjd|f|js| d|j fn|j s| d|j |jf|j d|d|j }tj|j|} tj| r| d| ft| t| |j |dtt|d} tt|d } t|d d d t|jd d d| d} t| dk|d| d} t| dk|dtdD]} tjj| |dq|j j!"d}t#dd}|$d|fW5QRXt%| d ddk} |&dd}| dt'|(d||d|)d}| d |ftjj|d!d |d"| d#t*| |d}t+|||\}}}} |sxtj,d$|tj|j-|}|j.|||| d%|dW5QRXd S)&z!Internal function that implements most of run() Do not call directly. This is the implementation of self.run: it runs the post for one case, on one domain.Nz%s: is empty or nonexistentz$%s: not available (location unknown)z,%s (%s): not available according to database_z%s: exists; will delete)keepr= hwrf_3dvarpost postinputT)r?ghost)force)storm_relocaterCflnm3new_ghtzstorm_relocate.log)r=) 3dvar_updaterBrGz3dvar_update.logzfort.*z%Y-%m-%d_%H:%M:%Sitagwtzpostinput netcdf %s NMM NEST )allranksz vpost.logneedcrtmFzlink post fix filesFIXhwrfcontrolz&%s: use this control file for gsi postzfort.14)rDr=zRun post, log to vpost.log.z GSI post on )CENLACENLOfromloc)/r/r9locationprodutilfileop isnonemptyhwrf exceptionsPostHasNoInputr:did availabler2r.ospathjoinworkdirexistsshutilrmtreerscrubr r getexer rrrglobrr&cyclestrftimeopenwriter confboolrgetdirconfstrrr PostFailedoutdirdeliver)r$inprodr3whyr4outprodr=Zwrfthere shortnamer^ZdiffwrfrAcmdfilename datestamprIrLZfort14retokcenlacenlotolocr+r+r,r<s~                 zGSIPost._processcks|dkr|dk r|g}n|dkr&|j}|D]^}|t@rL||jkrL|j|V|t@rj||jkrj|j|V|t@r*||jkr*|j|Vq*dS)aD!Iterates over EGRIB1Product objects produced by this task. @param domains a list of WRFDomain objects. Only these will be iterated. @param domain a single WRFDomain; only its products will be iterated @param which_step which steps are of interest: * F_ORG - original parent model data * F_GES - first guess (vortex-relocated parent model data) * F_ANL - analysis, the final input to the forecast * any integer or of the above * F_ALL - all products, the default @param kwargs ignoredN)rrr!rr#rr")r$domainsr3Z which_stepr(dr+r+r,productss   zGSIPost.products)NNN) __name__ __module__ __qualname____doc__rr8rr<rr} __classcell__r+r+r)r,r!s   &J)#r__all__r[rdr`produtil.fileoprS produtil.run produtil.cdhwrf.gsirV hwrf.hwrftask hwrf.posthwrf.exceptionsrrr r r r r rrrrrrrrrrrhwrftaskHWRFTaskrr+r+r+r,s $