U gXH@sdddlZz ddlZWn epek r4ddlZYnXddlmZddlZ ddl Z GdddZ dS)N) interpolatec@sdeZdZdddZddZddddddd d d gd d gddddd dddfd dZddZdddZdS)obc_vectvariableFcKsXd|_||_||_g|_|jd|j|j|dk rd|j||D]\}}|j|qN|jdkrdd|jd|jf|_ dd|jd|jf|_ n^|jdkrdd |jd |jd|jd|jf|_ dd |jd |jd|jd|jf|_ d |_ d |_ d |_ d |_d|_d|_||_|r>t|j|_ntj|jtjjd|_dS)a constructor of obc_variable : import from segment and adds attributes specific to this variable *** args : * segment : existing obc_segment object * variable_name_u : name of the zonal component in output file * variable_name_v : name of the meridional component in output file *** kwargs (mandatory) : * geometry : shape of the output field (line, surface) * obctype : radiation, flather,... T variable_nameNlinetimeny_nx_surfacenz__iig-C6?g333333? staggerloc)vectorvariable_name_uvariable_name_vitemsappend__dict__updategeometry segment_namedimensions_name_udimensions_name_vxmsgguessgtypenscanepsxrelc use_locstreamESMFFieldlocstream_target field_target grid_target StaggerLocCENTER)selfsegmentrrr kwargskeyvaluer-J/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/mom6_obc/lib_obc_vectvariable.py__init__s^        zobc_vectvariable.__init__cCsB|jdkr"t|j|j|jf}n|jdkr>t|j|jf}|S)Nr r)rnpemptynznynx)r(datar-r-r.allocateWs   zobc_vectvariable.allocateNTzrlonlatbilinearc% Cs|dk rN|j||| | ||d\|_}}}}|j||| |||d\|_}}}}nD|j||| | ||d\|_}}}}|j||| |||d\|_}}}}tj|||d}tj|||d}|jdkr|dd||||f}|dd||||f}t|| |j|j\|_ |_ |_ n:|||||f}|||||f}d|_ d|_ d|_ ztj || |d|_ WntdYnX|}|} tj|jtjjd}!tj|jtjjd}"|dkrtd |d krtj|!|jtjjtjjd }#n&|d krtj|!|jtjjtjjd }#n|}#|dkrztd |d krRtj|"|jtjjtjjd }$n&|d kr~tj|"|jtjjtjjd }$n|}$td|||#|!|j|j|j|_td|| |$|"|j|j|j|_|jt !|j"|j#|j$d|j%|j&df|jt '|j"|j#|j$d|j%|j&df|_(|jt !|j"|j#|j$d|j%|j&df|jt '|j"|j#|j$d|j%|j&df|_)|j*|j*|!*|"*|#|$fS)a; interpolate_from performs a serie of operation : * read input data * perform extrapolation over land if desired * read or create mask if extrapolation * call ESMF for regridding Optional arguments (=default) : * frame=None : time record from input data (e.g. 1,2,..,12) when input file contains more than one record. * drown=True : perform extrapolation of ocean values onto land * maskfile=None : to read mask from a file (else uses missing value of variable) * maskvar=None : if maskfile is defined, we need to provide name of mask array in maskfile * missing_value=None : when missing value attribute not defined in input file, this allows to pass it * use_locstream=False : interpolate from ESMF grid to ESMF locstream instead of ESMF grid, a bit faster. use only to interpolate to boundary. * from_global=True : if input file is global leave to true. If input is regional, set to False. interpolating from a regional extraction can significantly speed up processing. N)x_coordsy_coordsautocrop)framer r z!input data time variable not readr zcreate regridding for ur:)unmapped_action regrid_methodpatchzcreate regridding for vz regridding uz regridding v)+create_source_gridZ gridsrc_uZ gridsrc_vncdf read_fieldrread_vert_coordr4r3depthr2dz read_timetimesrcprintcopyr!r"r&r'Regridr$UnmappedActionIGNORE RegridMethodBILINEARPATCHperform_interpolationr%r Zdata_uZdata_vr0cosangle_dxjminjmaximinimaxsin data_u_out data_v_outdestroy)%r(filenameZ variable_uZ variable_vr>maskfilemaskvar missing_value from_global depthnametimename coord_names_u coord_names_vZ x_coords_uZ y_coords_uZ x_coords_vZ y_coords_vmethodinterpolator_uinterpolator_vr=Z imin_src_uZ imax_src_uZ jmin_src_uZ jmax_src_uZ imin_src_vZ imax_src_vZ jmin_src_vZ jmax_src_vZ datasrc_uZ datasrc_vZ dataextrap_uZ dataextrap_vZ field_src_uZ field_src_vZ regridme_uZ regridme_vr-r-r.interpolate_from_s"                0000  z!obc_vectvariable.interpolate_fromcCs|jdkr$|jdddddf}|jdkrH|jdddddf}|jdkrl|jdddddf}|jdkr|jdddddf}|}|jdkrt|jD]} || ddddf|jdd<tj |j|j|j k<|||}|j } t | } t | d} t t| d} tt| }tj |dd<t| | dkrt|t|| krtj|| | | dd}||| d| dd|| d| dd<|| d|d| d<|| d|| dd<n tj|| | | dd}||}n0t| | dkr6| | |dd<n d |dd<|r|jdkrl| || dddf<n"|jdkr| || dddf<q||j|jd|j|jdf|| ddddf<qn|jd kr|ddddf|jdd<tj |j|j|j k<|||}|j } t | } t | d} t t| d} tt| }tj |dd<t| | dkr6t|t|| krtj|| | | dd}||| d| dd|| d| dd<|| d|d| d<|| d|| dd<n tj|| | | dd}||}n0t| | dkrZ| | |dd<n d |dd<|rt||j|jf|ddddf<n8||j|jd|j|jdf|ddddf<|S) Nrr r nearest)kindr?r) orientationcoordsr6rr0aranger2 transposer5nan fill_valuerLisfinitewhereisnanr1lenminrinterp1dr4r3rVrWrXrYreshape)r( dataextrapregridme field_srcr$r%r coord_boundaryr5kzdata_with_gapsnogaps nogaps_posoknans data_interpinterpr-r-r.rSs     $   0      "   0    z&obc_vectvariable.perform_interpolationcCsj|dk r|dk r|}|}nHt||d} t||d} t| jdkrZt| | \}}n| }| }|rt|j||\} } } }|| || | f}|| || | f}|j\}}|sd} |} d} |}|r|st j t ||gdd}d|_ d|_ n t t ||g}d|_ d|_ |jt jjgd|j|jt jjddd<|j|jt jjddd<|| | | |fS)Nrr ) num_peri_dimsrkr )rDrEryshaper0meshgridlc find_subsetr%r!Gridarrayrkew add_coordsr&r'Trq)r(r^rb coord_namesr;r<r=lon_srclat_srclonslatsimin_srcimax_srcjmin_srcjmax_srcny_srcnx_srcgridsrcr-r-r.rCLs>  z#obc_vectvariable.create_source_grid)F)NNT)__name__ __module__ __qualname__r/r6rjrSrCr-r-r-r.rs6 E Ur) numpyr0esmpyr! ImportErrorModuleNotFoundErrorscipyr lib_ioncdfrD lib_commonrrr-r-r-r.s