U  g$@sdZddddgZddlZddlZddlZddlZddlZddlZddl Zddl Z ddl Z ddl Z ddlZ ddlZ ddlZ ddlZ ddlZ ddlmZmZmZmZmZddlmZmZmZdd lmZdd lmZmZmZmZm Z m!Z!dd l m"Z"m#Z#m$Z$m%Z%dd lm&Z&m'Z'd dddddZ(Gddde j)j*Z+Gddde j,j-Z.d$ddZ/d%ddZ0Gddde j,j1Z2dd Z3Gd!dde j4j5Z6Gd"dde j7j8Z9Gd#dde j)j*Z:dS)&z8This module handles WW3 related scripts for HWRF system.WW3Init WRFWW3POMWW3Post WRFWW3HYCOMN) FileProductRUNNING COMPLETEDFAILED UpstreamFile) make_symlink deliver_filewait_for_files)NamedDir)runrunstrcheckrunexebigexealias) to_datetimeto_datetime_rel to_fraction to_timedelta)WaveInitFailed WW3InputError)z ./mod_def.ww3z{com}/{out_prefix}.mod_def.ww3)z ./wind.ww3z{com}/{out_prefix}.wind.ww3)z ./current.ww3z{com}/{out_prefix}.current.ww3)z ./restart.ww3z#{com}/{out_prefix}.restart_init.ww3)z./ww3_shel.inpz{com}/{out_prefix}.ww3_shel.inp)Zmod_defwindcurrentrestartww3_shelcsXeZdZdfdd ZddZddd Zd d Zd d ZddZddZ ddZ Z S)rN~`Tc  sTtt|j|||fd|i| |t||_t||_t||_t||_ dS)aCreates a WW3Init dstore - the produtil.datastore.Datastore to use conf - the HWRFConfig to use section - the section name for this task taskname - the task name. Default: section fcstlen - the forecast length in hours outstep - the output step in seconds pntstep - the pntout step in seconds rststep - the restart output step in seconds Other keyword arguments are passed to the superclass constructor.tasknameN) superr__init___make_productsfloatfcstlenintoutsteppntsteprststep) selfdstoreconfsectionr!r&r(r)r*kwargs __class__5/lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/hwrf/ww3.pyr#s    zWW3Init.__init__c Cst|_tj|jj}|jZt D]J\}}|\}}t j |j||j}|||||_||d<||f|j|<q,W5QRXdS)zCreates FileProduct objects for all output files. The outdir is the directory to which the pom package output its final files. localpathN)dict _productshwrfnumericsrr-cycler, transaction prodnamesitemsprodutil datastorerr!timestrlocation)r+atimeprodname filepathsr4compathprodr2r2r3r$-s zWW3Init._make_productscks8|jD](\}}|\}}|dks,||jkr |Vq dS)zIterate over all products.N)r6r<rB)r+namer/rBstuffrEr4r2r2r3products;szWW3Init.productsc cst|jj}td|j|}t|dd}|dd}|dd}t|d}t||}|}||krt |j ||||dVt||}q^dS) N input_stepr gfs_datasetgfsgfs_item datasetitemftimerA) rr-r9rr&rconfintconfstrrr5taskvars) r+rAetimeintervalrPrQepsilonendewhenr2r2r3 inputiterBs     zWW3Init.inputiterccsJ|}t|jj}t|jd|}t|dd}|dd}|dd}|dd}t j |j||}t |d } t| |} |} d } |d d} |d d }|dd}|dd}| | krF|j f||| |d|j}|jr(t|g|| |||d}|s(d||f}||t j||V| |d} t|| } qdS)NrIrJr rKrLrMcataloghwrfdatarNr max_grib_waitgrib_sleep_time min_grib_size min_grib_agerO)maxwait sleeptimemin_size min_mtime_agez3%s: did not exist or was too small after %d seconds)logrr-r9rr&rrSrTr7input DataCatalogrlocaterUrealtimer error exceptionsr)r+loggerrArVrWrPrQhddcrXrYrZfhourrerfrgrhthefileZwaitedmsgr2r2r3 gfsgrib2iterQsJ             zWW3Init.gfsgrib2itercCs8|}|jD] \}}|\}}|j|d|dqdS)NF)frominfokeeprp)rir6r<deliver)r+rprBrGrEr4r2r2r3deliver_productsqszWW3Init.deliver_productscs|d}|dd}|dkr&d}n&|dkr4d}nd|fd}d}z0t|_|dd}t|j|j dd}fd d }t | d d dd || dd|| dd|| dd|| ddt d}|r|dk}t |d|dkrd}tjj|dtt| dd}|D]n} d| d} t|| ddD]} td| rd| | 7} qd|| ddd|f| >} t | dq8tj|rd}nd}tjjd|f|r$t | dd dd || d!d!t d"}|r|d#k}t |dnHt | d$d%dd || d&d&t d'}|r`|d(k}t |d|rt | d)d dd || d!d!t d"}|r|d*k}t |dn d+d} d,}z\| d-}tj|rtjj |d.dd} tjjd/|fntjjd0|fWnBtk rl}z"tjjjd1|t|fdd2W5d3}~XYnX| sƈd4t | d5d6dd || d7d7t d8}|r|d9k}t |d|r|tj !|j"|j#}| d:}t$|j"j%}t&|j'd;|}t(|j)}t(|j*}|d}|,d3d?}W5QRXn.|d.linkz {grid_inp}z ww3_grid.inprxrpz {grid_bot}.z {grid_msk}z {grid_obr}Zww3_gridz ./ww3_grid ww3_grid.logrzgfs.uvgrd10m.ncwgrib2zExtracting wind at 10 m from %sz:[UV]GRD:10 m above ground:z-iz-appendz-netcdfz       rww3rstrestart%03d.ww3c CsP|jj}t||}t||}t|} | d} t| |} t} |} | | krj| |ksTt| | t| | } q@||| d}|j }| D]}|d7}d|kr||}n|}|}t j |j |}t|j|j||d}||d<||d<d|d <tt||d |d <d |d <||||qW5QRX|d|||d|dfdS)NrNrrb%)categoryrBr@streamr@HminsizerI restarttimerdminagez+Created %d output %s products from %s to %sz %Y%m%d%H%M)r-r9rrlistAssertionErrorappendadd_coupled_streamr,r:rrjoinr@r r!roundadd_coupled_productridebugr)taskZrstbegrstdtZrstendrfmtrAZbegenddtrXZeendtimesnowitimertimefilenamerBr@rEr2r2r3add_ww3_productSsL           rcs4eZdZdZd fdd ZddZd d d ZZS) WRFCoupledWW3aRuns a WRF-WW3 coupled simulation (no ocean). Most of the work of this class is done by the superclasses. This class adds code to copy the inputs needed by WW3 and the coupler. There are three critical new config section values: wm3c_ranks = number of coupler ranks. Default: 1 ww3_ranks = number of WW3 ranks. Default: 24 wrf_ranks = nubmer of WRF ranks. No default. This one is mandatory.Tauxhist1Nc  st|ts$tdt|jt|ftt|j||||||f|||_ t |} | dddd| dddd | | |j d d d } |j} | d krt||d|dd|j} | d krt|| | | ddddt||d|ddt||d|dddS)N]The ww3init argument to WRFCoupledWW3.__init__ must be a WW3Init object. You passed a %s %s.coupler hwrf_wm3c wm3c_ranksrbww3hwrf_ocean_fcst ww3_ranks0rrrrrIww3pnt out_pnt.ww3rrww3out out_grd.ww3Z ww3mdldefz mdl_def.ww3) isinstancer TypeErrortyperrr"rr#rrcouple couplewrfr-rr)rsimendr*) r+r,r-r.wrfkeeprunwrfdiag_streamrr/ ww3initerrrrr0r2r3r#s2 zWRFCoupledWW3.__init__cCs |dSNuncoupler+r2r2r3 remove_waveszWRFCoupledWW3.remove_waveFcCs t|j|Srrrr+rr2r2r3rszWRFCoupledWW3.copy_ww3_inputs)TrN)F)rrrrr#rrrr2r2r0r3r~s rcCs|jddd}|j}|dkr8t||d|dd|j}|dkr`t||||ddd d t||d|d d t||d|d ddS)z!Helper function for WRFWW3POM and WRFWW3HYCOM constructors to reduce code duplication. Adds WW3 products to the forecast output. Should be run after superclass constructor.rrrrrIr r rbrrr r  ww3moddef mod_def.ww3N)r-rr)rrr*)r+rrrrrr2r2r3 wave_setupsr!cs@eZdZdfdd ZddZdd Zd d Zdd dZZS)rTrNc  sdt|ts$tdt|jt|f||_t||_t t |j |||||||f| t |||dS)Nr) rrrrrrrrrr"rr#r!) r+r,r-r.rrrpominitrr/r0r2r3r#s   zWRFWW3POM.__init__cCs|ddS)Npomrrr2r2r3 remove_oceanszWRFWW3POM.remove_oceancCs|ddSNrrrr2r2r3rszWRFWW3POM.remove_wavecCs|dddd|jdSNrrrr rrrr2r2r3 _add_waveszWRFWW3POM._add_waveFcCs t|j|Srrrr2r2r3rszWRFWW3POM.copy_ww3_inputs)TrNN)F rrrr#r$rr(rrr2r2r0r3rs cs@eZdZdfdd ZddZdd Zd d Zdd dZZS)rTrNc sft| ts$tdt| jt| f| |_t||_t t |j ||||||||f| t || |dS)Nz[The ww3init argument to WRFWW3HYCOM.__init__ must be a WW3Init object. You passed a %s %s.) rrrrrrrrrr"rr#r!) r+r,r-r.rocstatusrr hycominitrr/r0r2r3r#s  zWRFWW3HYCOM.__init__cCs|ddS)Nhycomrrr2r2r3r$szWRFWW3HYCOM.remove_oceancCs|ddSr%rrr2r2r3rszWRFWW3HYCOM.remove_wavecCs|dddd|jdSr&r'rr2r2r3r(szWRFWW3HYCOM._add_waveFcCs t|j|Srrrr2r2r3rszWRFWW3HYCOM.copy_ww3_inputs)TrNN)Fr)r2r2r0r3rscs|eZdZdZfddZddZddZdd Zed d Z d d Z ddZ ddZ ddZ ddZddZddZZS)rzRun WW3 post-process.c s<tt|j|||f|||_||_||_|d|_dS)NF)r"rr#r(r)r make_products _ncks_path)r+dsr-r.r(r)rr/r0r2r3r#s zWW3Post.__init__cks|jV|dd}|r |jV|jV|dd}|r@|jV|jV|dd}|r`|jV|jV|dd}|r|jV|j V|dd}|r|j VdS)Nr~T) _ww3gribprodr _ww3griblog _ww3ounfprod _ww3ounflog _ww3ounpprod _ww3ounplog_ww3outpbullprod_ww3outpbulllog_ww3outpspecprod_ww3outpspeclog)r+r/r~r2r2r3rHs(     zWW3Post.productscCs|dd}tj|jd|j|_|rrr,r!r0r1r2r3r4r5r6r7r8r9)r+r~r2r2r3r-sp zWW3Post.make_productscCsD|j}|}tjj||dtt|ddd||fdk|ddS)Nrz-4z-L6z /dev/null) ncks_pathrir=rrrr)r+rtargetignorencksrpr2r2r3Z __copy_ncks-s zWW3Post.__copy_nckscCsV|jdkrP|dd}|js,tjjddd}|dksJt|trF|dksJt||_|jS)zReturns the path to ncks. Returns None if ncks cannot be found. This function will only search for ncks once, and will cache the result. Set self._ncks_path=False to force a recheck.Fr>r) raise_missingN)r.rr=rfind_exerrr)r+r>r2r2r3r;3s  zWW3Post.ncks_pathc& Cs|}|dd}t|_zt|jd|dd^}dd|jjddDd}||rh|j rh|j s|| d t |fd d|jjd dDd}||r|j r|j s| d t |ft |j d d|d|jdddd}|dkr||jdkr|t |j dd|dt |ddd|d||td}|rD|dk}t||d|jjd}|d} |jj|| |dd|jdddd} | dkr$|jdkr$t |j dd|dt |ddd|d||td}|r|dk}t||d|jjd } |d!} |jj| | ||jd|jd"ddd} | dkr|jdkrd#d|jjd$dDd}||r|j r|j s| d%t |ft |j d&d|dt |d'd'd|d||td(}|r|d)k}t||d|jjd*}|d+}|jj||||jd|jd,ddd}|jd-ddd}|jdkrd.d|jjd$dDd}||r|j r|j s| d%t |ft |j d&d|dt |d/d/d|d| |td0}|d1k}t||dd1}t!|}|"}W5QRXd2dt#|D}||dd3|d3d4}|dkrPg}g}t#|D]\}}|d3}|$d}|$d3}|$d4}|%d5||||f|&||td0}|d6|d7k}t'j((||)|d8|)|d9|)d6|d7qJtd:d;|}t||dtd<|d=?}t||dd>} |d?}!|j*j| |!|dd|dkrg}g}|jjd@}"t#|D]\}}|d3}|$d}|$d3}|$d4}|%dA||||f|+||td0}|dB|d7k}t||dt,dC|"dD|dDdE|dF|)|dD|)dB|d7qxtd:dG|}t||dtd<|dH?}t||ddI}#|dJ}$|j-j|#|$|ddW5QRXt.|_WnDt/k r}%z$t0|_|j dKt1|%fddLW5d}%~%XYnXdS)MzRun the WW3 post.r~TrcSsg|]}|qSr2r2.0pr2r2r3 IszWW3Post.run..r)rrz/%s: mod_def.ww3 not yet available from forecastcSsg|]}|qSr2r2rAr2r2r3rDNsr z/%s: out_grd.ww3 not yet available from forecastr r ww3_grib_postr|ww3post)r.r Zww3_gribz ./ww3_gribz ww3_grib.logrZgribfilez{com}/{out_prefix}.ww3.grb2N)rwr@rpcopier ww3_ounf_postZww3_ounfz ./ww3_ounfz ww3_ounf.logz ww3.%Y.ncz{com}/{out_prefix}.ww3_ounf.ncww3_ounp_spec_postcSsg|]}|qSr2r2rAr2r2r3rDwsr z/%s: out_pnt.ww3 not yet available from forecastr Zww3_ounpz ./ww3_ounpz ww3_ounp.logzww3.%Y_spec.ncz#{com}/{out_prefix}.ww3_ounp_spec.ncww3_outp_bull_postww3_outp_spec_postcSsg|]}|qSr2r2rAr2r2r3rDsZww3_outpz ./ww3_outpzww3_outp_info.logcSsg|]\}}d|kr|qS)z ----------r2)rBielemr2r2r3rDsrbz&ww3_outp_bull for buoy: %i, %s, %s, %sZww3_outp_bull_z.logz.bullz.csvtar)-cvfww3_outp_bull.tarcatzww3_outp_bull.logrQz${com}/{out_prefix}.ww3_outp_bull.tarz%y%m%d%Hz&ww3_outp_spec for buoy: %i, %s, %s, %sZww3_outp_spec_zww3.z.spcFr)rPww3_outp_spec.tarzww3_outp_spec.logrSz${com}/{out_prefix}.ww3_outp_spec.tarzWW3 post failed: %sr)2rirrrrrrrHcheckrr@rnrr rTr(r make_grib_inprrr-r9rrr0ry make_ounf_inpr2_WW3Post__copy_ncksr)make_ounp_spec_inpr4make_outp_info_inpr readlines enumeratesplitrmake_outp_bull_inpr=rrr6make_outp_spec_inpr r8rrr r)&r+rpr~rZmdprodZogprodrErZ ww3_grib_outZww3_grib_out_comrHZ ww3_ounf_outZww3_ounf_out_comrIZopprodZ ww3_ounp_outZww3_ounp_out_comrJrKfnamerZ ww3_outp_infoindicesZbuoysZfileoutZfilelogrLZbuoyipntZbuoyidZbuoylonZbuoylatZ ww3_outp_bullZww3_outp_bull_comZww3tstrZ ww3_outp_specZww3_outp_spec_comrr2r2r3rBs0                                 z WW3Post.runc Cstj|j|j}|dd}|s,|d}t|jj}t }|j | dt |j | ddt|dB}tdd,}||j|f||d |jjd |W5QRXW5QRXdS) Ngrib_inprz{PARMww3}/ww3_grib.inp_tmplr)rrrrz ww3_grib.inprTrr7rrr-r.rTrrr9r5rrr'r(rrr)r+rprrbrArrrr2r2r3rUs$      zWW3Post.make_grib_inpc Cstj|j|j}|dd}|s,|d}t|jj}t }|j | dt |j dt|dB}tdd,}||j|f||d |jjd |W5QRXW5QRXdS) Nounf_inprz{PARMww3}/ww3_ounf.inp_tmplr)rrrz ww3_ounf.inprTrrc)r+rprrdrArrrr2r2r3rVs"      zWW3Post.make_ounf_inpc Cstj|j|j}|dd}|s,|d}t|jj}t }|j | dt |j dt|dB}tdd,}||j|f||d |jjd |W5QRXW5QRXdS) N ounp_spec_inprz {PARMww3}/ww3_ounp_spec.inp_tmplr)rrrz ww3_ounp.inprTrr7rrr-r.rTrrr9r5rrr'r)rrr)r+rprrerArrrr2r2r3rXs"      zWW3Post.make_ounp_spec_inpc Cstj|j|j}|dd}|s,|d}t|jj}t }|j | dt |j d| ddt|dB}tdd ,}||j|f||d |jjd |W5QRXW5QRXdS) N outp_bull_inpr {PARMww3}/ww3_outp_bull.inp_tmplrrrrZPNT_NUMrr ww3_outp.inprTrrf)r+rprrgrArrrr2r2r3rYs&      zWW3Post.make_outp_info_inpc Cstj|j|j}|dd}|s,|d}t|jj}t }|j | dt |j t || ddt|dB}tdd,}||j|f||d |jjd |W5QRXW5QRXdS) NrgrrhrrirrjrTrrf) r+rarprrgrArrrr2r2r3r]s&      zWW3Post.make_outp_bull_inpc Cstj|j|j}|dd}|s,|d}t|jj}t }|j | dt |j t || ddt|dB}tdd,}||j|f||d |jjd |W5QRXW5QRXdS) N outp_spec_inprz {PARMww3}/ww3_outp_spec.inp_tmplrrirrjrTrrf) r+rarprrkrArrrr2r2r3r^(s&      zWW3Post.make_outp_spec_inp)rrrrr#rHr-rWpropertyr;rrUrVrXrYr]r^rr2r2r0r3rs  )F)rr);r__all__rrprodutil.datastorer=produtil.fileop produtil.cd produtil.run produtil.log hwrf.mpipomtcr7 hwrf.hwrftask hwrf.numericshwrf.exceptions hwrf.namelist hwrf.coupling hwrf.input hwrf.hycomrrrr r r r r rrrrrrrrrrrrrr;hwrftaskHWRFTaskrcouplingComponentIniterrrr CoupledWRFrr!mpipomtc WRFCoupledPOMrr,WRFCoupledHYCOMrrr2r2r2r3s8 ((     +-