U ����� g�[����������������������@���s.��d�dl�Z�d�dlZd�dlZd�dlZd�dlZd�dlZd�dlZd�dl m Z mZmZm Z mZmZmZ�d�dlmZ�d�dlmZ�d�dlZd�dlmZ�d�dlT�d�dlT�d�dlT�d�dlT�ddd gZG�d d��dejj�ZG�dd��dejj �Z!G�d d ��d ej"j#�Z$G�dd��dej%j�Z&G�dd��dej"j'�Z(G�dd��dej)j*�Z+dS�)�����N)� COMPLETED�RUNNING� UNSTARTED�FAILED�Product�FileProduct�UpstreamFile)� isnonempty)�WorkPool)�HWRFTask)�*�ForecastWatcher�FakeInit�RealInitc�����������������������s*���e�Zd�ZdZ��fdd�Zddd�Z���ZS�) �Input2FinalMergez�Links real_nmm wrfinput_d01 file or returns the FileProduct, regardless of the time and domain, in the current directory for a final merge.c��������������������s"���t�t|���||��||�_||�_d�S��N)�superr����__init__�domain�atime)�self�stormid�srcr���r����� __class__���</lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/hwrf/multistorm.pyr������s����zInput2FinalMerge.__init__FNc�����������������K���s����|d�k r2||�j�kr2|d�k r.|�d||�j�f���dS�|�jd�k r~|�j}|�j�|�j|�j�}|rz|jsf|����|�j |t |||d�S�dS�d�S�)Nz7Wrong stormid requested: %s instead of %s, cannot link.F)�target� just_check)r����errorr���Ztgtname�wrfinput_at_timer���r���� available�check�link_product�WRFInputMissing)r����loggerr���r����kwargs�tgt�pr���r���r���� get_inputs���s&������ � �zInput2FinalMerge.get_inputs)FN)�__name__� __module__�__qualname__�__doc__r���r)���� __classcell__r���r���r���r���r������s���r���c�����������������������sN���e�Zd�Z��fdd�Zedd���Zedd���Zdd��Zd d ��Zddd �Z ���Z S�)r ���c����������� ��� ������sf���|j�dd||d�}d�|�_d�|�_|����6} tt|��j|||||||d��||�_||�_||�_ W�5�Q�R�X�d�S�)N�dirz{realstormwork}/{taskname})� realstorm�taskname)r1����workdir�outdir) � strinterp�_d02map�_d03map�transactionr���r ���r����locationr2���r3���) r����ds�confZfakeid�section�wrfr1���r&���r2����txr���r���r���r���4���s&������ ������zForecastWatcher.__init__c�����������������C���s���|�j�S�)z)The outer domain value for a multistorm..)r5����r���r���r���r����d02remapB���s����zForecastWatcher.d02remapc�����������������C���s���|�j�S�)z(The inner domain value for a multitorm .)r6���r>���r���r���r����d03remapG���s����zForecastWatcher.d03remapc�����������������C���sH���|�j�rD|�jsDd|�krD|�j�dd�}|�j�dd�}|�d��||�|�d<�d�S�)Nr8����config�fakestormid�STID)�ismultistorm�isfakestormr:����getstr�replace)r���rB���� this_stidr���r���r����change_locationL���s ����zForecastWatcher.change_locationc�����������������C���s���dS�)z�!Placeholder to allow a ForecastWatcher to look like an hwrf.fcsttask.WRFAtmos to the scripts.exhwrf_check_init job. This allows the fake storms to run the init checking job.Tr���r>���r���r���r����check_all_inputsY���s����z ForecastWatcher.check_all_inputsFc�������������� ���C���s���|���|�}|dk r|S�|���}|�d�}|dk s2t�|�j�rD|�j�sD|�j�dd�}|�j�dd����}|�jj }|� |�} |�jd|�j�dd�d�} tj� |����}||k�r4|������d k�r4|������d kr�d| d �d ��|�_d|ks�t�|�d|�j�}|������d k�r4d| d �d ��|�_d|k�s&t�|�d|�j�}tj�| |�}ntj�|tj� |�����}|�j����6} t|�j|�j||d�}d|d<�|���|d<�||d<�W�5�Q�R�X�|�r�||_|��||�S�)z"Converts a WRFOutput to a Product.Nr3���rA����multistorm_priority_sid�multistorm_sids�{realstormwork}/runwrfrB����r0�����������zd%02dZd02�����d03)�category�prodnamer8����K����minage�streamr8���)� _get_cache�path�AssertionErrorrD���rE���r:���rF����split� storminfo�stormid3�index� confstrinterp�os�basenamer����get_grid_idr5���rG���r6����join�dstorer7���r���r1���rW���r8���� _set_cache)r����wrfout�relocate�cached�relr3���Z priority_stidrL���rH���Z this_stid_idx�fake_outdir�wrfprod�loc�t�ufr���r���r���� as_product`���sJ���� � � ���zForecastWatcher.as_product)F)r*���r+���r,���r����propertyr?���r@���rI���rJ���ro���r.���r���r���r���r���r ���3���s��� c�����������������������sZ���e�Zd�Zdd��Zdd��Zdd��Zd��fdd � Z��fd d�Zdd ��Zdd��Z dd��Z ���ZS�)r���c�����������������O���s���d�S�r���r����r����argsr&���r���r���r����run_exe���������zRealInit.run_exec�����������������O���s���d�S�r���r���rq���r���r���r����link_fix����rt���zRealInit.link_fixc�����������������O���s���d�S�r���r���rq���r���r���r���� make_namelist����rt���zRealInit.make_namelistFc��������������������sZ���d}d|�j�kr(|o$|�jd|�jj|d�}nd}|�����|��t|��|oTtt|��� |�}|S�)NT�merge)r���r���z%Input type "merge" was not specified.) �inputs� link_inputr\���r]����log�criticalr$���r���r����link_all_inputs)r���r����okay�msgr���r���r���r|�������s���� ��zRealInit.link_all_inputsc��������������������s���t�t|���|��d�S�r���)r���r����add_wrfinput�r����rr���r���r����add_orig_wrfinput����s����zRealInit.add_orig_wrfinputc�����������������C���s$���|���dt|�jj||�j���|�jj��S�)a2��Adds an input source (via self.add_input) that will provide the wrfinput output file from gdas_merge, or whatever the last step in the real storm initialization may have been. The given object must have a get_merge(stormid) function that returns a Product for a given stormrw���)� add_inputr���r\���r]����sim�get_moadr:����cycler����r���r���r���r�������s��������zRealInit.add_wrfinputc�����������������C���s����|�j�jrdnd}d|�d�|�d�}d|�d�|�d�}d}|�jj}ddd d |�ddd d|�d|�d|�d�g }td��b}|D�]N}|��||��} |��|�} tj�| �s�t j �d| | f���|�|�j | | g��q~|����W�5�Q�R�X�d�S�)N�_�:�{year}-{month}-{day}_{HH}�00�00.ncz({com}/{vit[stormid3]}.{YMDH}.multistorm.�geo_nmm.d01.nc�geo_nmm_nest.l01.nc�geo_nmm_nest.l02.nc�met_nmm.d01.�wrfinput_d01� wrfbdy_d01zfort.65Zwrfanl_d02_Zwrfanl_d03_Z wrfinput_d01_� gdas_merge.nc����z1%s: does not exist or is empty, cannot copy to %s)r�����nocolonsr\���r]���r ����icstr�produtil�fileopr ����hwrf� exceptions�ForecastInputError�add_work� _copy_one�barrier)r����cr����met_nmm_time�prefixr����files�workers�f�comfileZ localfiler���r���r����deliver_products����s:���� � ���zRealInit.deliver_productsc�����������������C���s���t�jj||d|����d��d�S�)NF)�moveokr%���)r����r�����deliver_filerz���)r���ZfromfZtofr���r���r���r��������s�����zRealInit._copy_one)F)r*���r+���r,���rs���ru���rv���r|���r����r���r����r����r.���r���r���r���r���r�������s��� c�����������������������s|���e�Zd�Z��fdd�Zedd���Zdd��Zddd �Zd d��Zdd ��Z dd��Z dd��Zdd��Zdd��Z dd��Zddd�Z���ZS�)r���c����������� ��� ������sd���t�t|��j|||f|��||�_|���r*dnd|�_||�|�_|�_||�_|� ���} |�� ���W�5�Q�R�X�d�S�)Nr����r����)r���r���r���r0����get_nocolons�colon�stormNinner�stormNouter�_FakeInit__wrfr7����define_locations_from_com) r���r9���r:���r;���r<���r0���r����r����r&���r=���r���r���r���r�������s���� zFakeInit.__init__c�����������������C���s���|�j�S��z.!Returns the hwrf.wrf.WRFSimulation being run.)r����r>���r���r���r���r��������s����zFakeInit.simc�����������������C���sb��|�j�jrdnd}d|�d�|�d�}d|�d�|�d�}|��dd�|��dd �|��d d�d�|�_|��d |�d|��|�_|��d�|�_|��d�|�_|��d�|�_|��d�|�_t ��|�_ t ��|�_|�j� ddd����}tt|��D�]�}|d�}||�}|�jd|�d|�|d�|�j |�jd|��<�|�jd|�d|�|d�|�j |�jd|��<�|�jd|�d�d|f�|d�|�j|<�q�d�S�) Nr����r����r����r����r����z;{realstormcom}/{realstorm}.{YMDH}.multistorm.geo_nmm.d01.ncr����z@{realstormcom}/{realstorm}.{YMDH}.multistorm.geo_nmm_nest.l01.ncr����z@{realstormcom}/{realstorm}.{YMDH}.multistorm.geo_nmm_nest.l02.ncr����)rO���rQ���� ���z9{realstormcom}/{realstorm}.{YMDH}.multistorm.met_nmm.d01.r����z9{realstormcom}/{realstorm}.{YMDH}.multistorm.wrfinput_d01z4{realstormcom}/{realstorm}.{YMDH}.multistorm.fort.65z7{realstormcom}/{realstorm}.{YMDH}.multistorm.wrfbdy_d01rA���rL����nosidsrO���z8{realstormcom}/{realstorm}.{YMDH}.multistorm.wrfanl_d03_zstorm%sinner_wrfanl)r���zstorm%sinnerz8{realstormcom}/{realstorm}.{YMDH}.multistorm.wrfanl_d02_zstorm%souter_wrfanlzstorm%souterz:{realstormcom}/{realstorm}.{YMDH}.multistorm.wrfinput_d01_r����zmerge_%s)r����r����� productify�geofile�metfile� real_init�fort65�wrfbdy�wrfinput_final�dict� wrfanl_for� merge_forr:���rF���r[����range�lenr����r����)r���r����r���r����rL���Zi1�ir���r���r���r���r��������sZ�������� ���� ��� ������z"FakeInit.define_locations_from_comNc�����������������C���sj���|d�kr|�j�}|�j||d�}tj�|�}|d�kr6|}n|��|�}|}t|�j||�j|d�}||_||d<�|S�)NrN���)r8���ra���) r0���r_���r`���rY���ra���r���rd���r1���r8���)r����filenamerT���r���rl���ra���rn���r���r���r���r������s����� zFakeInit.productifyc�����������������C���s���||�j�kr|�j�|�S�d�S�r���)r����)r���r���r���r���r���� get_merge��s���� zFakeInit.get_mergec�����������������C���s���|�j�S�)zBReturns the wrfinput file regardless of the time or domain)r����r>���r���r���r����get_wrfinput_gfsinit_realinit&��s����z&FakeInit.get_wrfinput_gfsinit_realinitc�����������������C���s���||�j�kr|�j�|�S�d�S�r���)r�����r���r���r���r���r���r����wrfanl_at_time+��s���� zFakeInit.wrfanl_at_timec�����������������C���s���|�j�S�)z(Makes add_wrfinput work with this class.)r����r����r���r���r���r ���0��s����zFakeInit.wrfinput_at_timec�����������������C���s���|�j�S�)z&Makes add_fort65 work with this class.)r����r����r���r���r����fort65_at_time4��s����zFakeInit.fort65_at_timec�����������������C���s���|�j�S�)z&Makes add_wrfbdy work with this class.)r����r����r���r���r����wrfbdy_at_time8��s����zFakeInit.wrfbdy_at_timec�����������������C���s$���|�j�dk st�||�jjkr |�j�S�dS�)z'Makes add_metgrid work with this class.N)r����rZ���r:���r����)r����ftimer���r���r����met_at_time<��s����zFakeInit.met_at_timec�������������� ���C���sz���|����}|dkr|����}z|�j|�W�S��tk rt�}�z2|jdt|�|t|�j�t|�f�dd��W�Y��dS�d}~X�Y�nX�dS�)z�!Makes add_geogrid work with this class. @param domain the domain of interest @bug this only works for nesting ratios that are a power of 3NzYBLAH BLAH Requesting domain %s at nest level %d which we do not have from geogrid: %s: %sT)�exc_info)� moad_ratiorz���r�����KeyErrorr����str)r���r���r%����ratio�ker���r���r����geodatC��s�������zFakeInit.geodat)NN)N)r*���r+���r,���r���rp���r����r����r����r����r����r����r ���r����r����r����r����r.���r���r���r���r���r�������s��� " c�����������������������s(���e�Zd�Zd ��fdd� Zd dd�Z���ZS�)�WRFAtmosMultiStormT�auxhist1c��������������������s(���t�t|��j||||f||d�|���d�S�)N)�keeprun�wrfdiag_stream)r���r����r���)r���rd���r:���r;���r<���r����r����r&���r���r���r���r���W��s�������zWRFAtmosMultiStorm.__init__�namelist.inputNc�������������� ���C���s����|dkr|�����}|�j�dd�}|�j�dd�}|�jrT|�����|��d�|�jj|||�}n|�����|��d�|�j |||�}t |d��}|�|��W�5�Q�R�X�dS�)a#��Runs set_ij_start (swcorner_dynamic) to generate the i & j start locations for domain 2, then generates the namelist. For the fakestorm of multistorm run, it generates the i & j start locations for each of the outer storms domain then generates the namelist.NrA����domlat�domlon�swcorner_dynamic�wt)rz���r:����getfloatrE���r<����swcorner_dynamic_multistorm�getexe�syndat_multistormr����r\����open�write)r���r����r%���r����r�����s�nlinr���r���r���rv���]��s(�������������z WRFAtmosMultiStorm.make_namelist)Tr����)r����N)r*���r+���r,���r���rv���r.���r���r���r���r���r����U��s������r����c�����������������������s@���e�Zd�ZdZ��fdd�Zedd���Zddd�Zdd d �Z���Z S�) � WRFCopyTaskMSz�This is a Task that copies WRF input and output files from the WRF FAKE Storm run directory to the COM directory,for a Multi Storm Basin Scale.c�������������� ������s*���t�t|���||d||�dd���||�_d�S�)N�copywrfrA���� out_prefix)r���r����r���rF����_WRFCopyTaskMS__wrftask)r���rd���r:���r;����wrftaskr����r&���r���r���r���r���v��s���� ���� �zWRFCopyTaskMS.__init__c�����������������C���s���|�j�S�r����)r����r>���r���r���r����wrf_watcher~��s����zWRFCopyTaskMS.wrf_watcherNc�����������������C���s����t�|t�r$|���}tj�t|��}ntj�t|��}t�dd|�}t�dd|�}|dkrhd|�j |f�}n|�j ||||d�}tj�|�j|�|fS�)z�Generates a full path to the delivery location of the specified source file. Returns the full path and the basename in a tuple.zq([0-9][0-9][0-9][0-9])[_.-]([0-9][0-9])[_.-]([0-9][0-9])[_.-]([0-9][0-9])[_.-]([0-9][0-9])[_.-]([0-9][0-9])[^/]*$z\1-\2-\3_\4:\5:\6z\1-\2-\3_\4:\5:00Nz%s.%s)�inname�inname_colon�inname_colon_s00) � isinstancer����getprodnamer`���rY���ra���r�����re�subr����r_���rc���r3���)r����orig�destnameZthisprodname�bn�bn_colon�bn_colon_s00�fullbnr���r���r���r�������s*���� �����zWRFCopyTaskMS.comfilec�����������������C���s��|�j�||d�\}}t|t�r.|}|dkr�d}n�|�jr�|�jrNtj�|�jj |�}q�|�j d|�j�dd�d�} |dkr�|�j jr�|�d |�j j�} tj�| | �}q�tj�| |�}ntj�|�jj|�}t|�j|d |�jf�|d�}||_d}t|�j||�j|d�}|�||t|�f��|�S�) z�Do not call this function directly. It is the internal implementation of d_initial and d_final. Call those functions instead.)r����NFrM���rA���rB���rN���ztrack_d03.patcfrR���z%s-upstream)rd���rT���rS���r8���T)r����r����r���rD���rE���r`���rY���rc����_wrftaskr2���r_���r:���rF���r����r@���rG���r8���r���rd���r1���r����append�bool)r����group�inprodr"���r����r�����combn�upstream�wrffilerj���Zinprod_d03remap�productr���r���r����_deliver_to_group���s<���� � � �����zWRFCopyTaskMS._deliver_to_group)N)NN) r*���r+���r,���r-���r���rp���r����r����r����r.���r���r���r���r���r����s��s��� 3r����),� fractions�mathr�����datetimer`����produtil.fileopr�����hwrf.wrfr�����produtil.datastorer���r���r���r���r���r���r���r ����produtil.workpoolr ����hwrf.finalmergetask� hwrf.hwrftaskr���� hwrf.numerics� hwrf.namelist�hwrf.exceptions�hwrf.wrfbase�__all__�finalmergetaskZMerge2FinalMerger���r<����ExternalWRFTaskr ����fcsttask�WRFTaskBaser����hwrftaskr����WRFAtmosr����r�����WRFCopyTaskr����r���r���r���r����<module>���s$���($ j@x