Changeset 2272 in lmdz_wrf for trunk


Ignore:
Timestamp:
Dec 27, 2018, 10:05:14 PM (7 years ago)
Author:
lfita
Message:

Adding realistic example on `area_weighted'
Adding:

  • `compute_WRFbnds': Function to compute vertices of the cells using WRF 'XLONG_[U/V]', 'XLAT_[U/V]'
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r2271 r2272  
    6868## e.g. # nc_var.py -o dimrename -f /media/lluis/ExtDiskC_ext3/DATA/estudios/FPS_Alps/additional/IOP/select/out_irene/simcdx_vars_cape_120lev_cdxwrf2.nc -S Time -v time0
    6969## e.g. # nc_var.py -o area_weighted -f 'reference_data.nc:lon;lon;lon_bnds;-1;lat;lat;lat_bnds;-1,get_data.nc:lon;lon;lon_bnds;-1;lat;lat;lat_bnds;-1' -S 'yes:min,max,mean,stddev,count' -v ct_values,xband_values,box_values,mosaic_values
     70## e.g. # nc_var.py -o area_weighted -f '/media/lluis/ExtDiskC_ext4/bkup/llamp/estudios/dominios/SA150k/geo_em.d01.nc:west_east;XLONG_M;WRFxbnds;-1;south_north;XLAT_M;WRFybnds;-1,/media/lluis/ExtDiskC_ext4/bkup/llamp/estudios/dominios/SA50k/geo_em.d01.nc:west_east;XLONG_M;WRFxbnds;-1;south_north;XLAT_M;WRFybnds;-1' -S 'no:mean' -v HGT_M
    7071
    7172from optparse import OptionParser
  • trunk/tools/nc_var_tools.py

    r2271 r2272  
    2717927179    return
    2718027180
     27181def compute_WRFbnds(lonu, latu, lonv, latv):
     27182    """ Function to compute vertices of the cells using WRF 'XLONG_[U/V]', 'XLAT_[U/V]'
     27183      lonu: longitudes on the u-staggered matrix
     27184      latu: latitudes on the u-staggered matrix
     27185      lonv: longitudes on the v-staggered matrix
     27186      latv: latitudes on the v-staggered matrix
     27187    """
     27188    fname = 'compute_WRFbnds'
     27189
     27190    dx = lonv.shape[1]
     27191    dy = lonu.shape[0]
     27192    sdx = lonu.shape[1]
     27193    sdy = lonv.shape[0]
     27194
     27195    lonbnds = np.zeros((4, dy, dx), dtype=np.float)
     27196    latbnds = np.zeros((4, dy, dx), dtype=np.float)
     27197
     27198    for j in range(dy-1):
     27199        lonbnds[0,j,:] = 0.5*(lonu[j,0:sdx-1]+lonu[j+1,0:sdx-1])
     27200        lonbnds[1,j,:] = 0.5*(lonu[j,1:sdx]+lonu[j+1,1:sdx])
     27201        lonbnds[2,j,:] = 0.5*(lonu[j-1,1:sdx]+lonu[j,1:sdx])
     27202        lonbnds[3,j,:] = 0.5*(lonu[j-1,0:sdx-1]+lonu[j,0:sdx-1])
     27203
     27204    for i in range(dx-1):
     27205        latbnds[0,:,i] = 0.5*(latv[1:sdy,i-1]+latv[1:sdy,i])
     27206        latbnds[1,:,i] = 0.5*(latv[1:sdy,i]+latv[1:sdy,i+1])
     27207        latbnds[2,:,i] = 0.5*(latv[0:sdy-1,i]+latv[0:sdy-1,i+1])
     27208        latbnds[3,:,i] = 0.5*(latv[0:sdy-1,i-1]+latv[0:sdy-1,i])
     27209
     27210    return lonbnds, latbnds
     27211
    2718127212def area_weighted(values, ncfiles, variable):
    2718227213    """ Function to provide an area weighted statistics within two different values
     
    2720427235            [dimxn]: name of the dimension along the x-axis
    2720527236            [varxn]: name of the variable with the values of dimxn
    27206             [varxbndsn]: name of the variable with the bounds of the values of varxn
     27237            [varxbndsn]: name of the variable with the bounds of the values of varxn
     27238              ('WRFxbnds' to be computed from WRF output using XLONG_[U/V], XLAT_[U/V])
    2720727239            [dimyn]: name of the dimension along the y-axis
    2720827240            [varyn]: name of the variable with the values of dimyn
    2720927241            [varybndsn]: name of the variable with the bounds of the values of varyn
     27242              ('WRFybnds' to be computed from WRF output using XLONG_[U/V], XLAT_[U/V])
    2721027243            [slicedim]: series of values to define the slicing of the variable
    2721127244              * [integer]: which value of the dimension
     
    2730927342                quit(-1)
    2731027343
    27311             if not oncref.variables.has_key(refvarxbndsn):
     27344            if refvarxbndsn != 'WRFxbnds' and                                        \
     27345              not oncref.variables.has_key(refvarxbndsn):
    2731227346                print errormsg
    2731327347                print '  ' + fname + ": reference file '" + netcdfrefn + "' does " + \
     
    2733627370                quit(-1)
    2733727371
    27338             if not oncref.variables.has_key(refvarybndsn):
     27372            if refvarybndsn != 'WRFybnds' and                                        \
     27373              not oncref.variables.has_key(refvarybndsn):
    2733927374                print errormsg
    2734027375                print '  ' + fname + ": reference file '" + netcdfrefn + "' does " + \
     
    2734727382            slicedict = {refdimxn: slicedimx, refdimyn: slicedimy}
    2734827383            ovarx = oncref.variables[refvarxn]
    27349             ovarxbnds = oncref.variables[refvarxbndsn]
    2735027384            ovary = oncref.variables[refvaryn]
    27351             ovarybnds = oncref.variables[refvarybndsn]
    27352 
    2735327385            srefvarx, dims = SliceVarDict(ovarx,slicedict)
    27354             srefvarbndsx, dims = SliceVarDict(ovarxbnds,slicedict)
    2735527386            srefvary, dims = SliceVarDict(ovary,slicedict)
    27356             srefvarbndsy, dims = SliceVarDict(ovarybnds,slicedict)
    27357 
    2735827387            reflon, reflat =  gen.lonlat2D(ovarx[tuple(srefvarx)], ovary[tuple(srefvary)])
    27359             refvarbndsx = ovarxbnds[tuple(srefvarbndsx)]
    27360             refvarbndsy = ovarybnds[tuple(srefvarbndsy)]
     27388
     27389            if refvarxbndsn != 'WRFxbnds':
     27390                ovarxbnds = oncref.variables[refvarxbndsn]
     27391                ovarybnds = oncref.variables[refvarybndsn]
     27392                srefvarbndsx, dims = SliceVarDict(ovarxbnds,slicedict)
     27393                srefvarbndsy, dims = SliceVarDict(ovarybnds,slicedict)
     27394                refvarbndsx = ovarxbnds[tuple(srefvarbndsx)]
     27395                refvarbndsy = ovarybnds[tuple(srefvarbndsy)]
     27396            else:
     27397                oulon = oncref.variables['XLONG_U']
     27398                oulat = oncref.variables['XLAT_U']
     27399                ovlon = oncref.variables['XLONG_V']
     27400                ovlat = oncref.variables['XLAT_V']
     27401                refvarbndsx, refvarbndsy= compute_WRFbnds(oulon[0,:,:], oulat[0,:,:],\
     27402                  ovlon[0,:,:], ovlat[0,:,:])
    2736127403
    2736227404            refdx = reflon.shape[1]
     
    2741227454                quit(-1)
    2741327455
    27414             if not oncget.variables.has_key(getvarxbndsn):
     27456            if getvarxbndsn != 'WRFxbnds' and                                        \
     27457              not oncget.variables.has_key(getvarxbndsn):
    2741527458                print errormsg
    2741627459                print '  ' + fname + ": get file '" + netcdfgetn + "' does " +       \
     
    2743927482                quit(-1)
    2744027483
    27441             if not oncget.variables.has_key(getvarybndsn):
     27484            if getvarybndsn != 'WRFybnds' and                                        \
     27485              not oncget.variables.has_key(getvarybndsn):
    2744227486                print errormsg
    2744327487                print '  ' + fname + ": get file '" + netcdfgetn + "' does " +       \
     
    2745027494            getslicedict = {getdimxn: slicedimx, getdimyn: slicedimy}
    2745127495            ovarx = oncget.variables[getvarxn]
    27452             ovarxbnds = oncget.variables[getvarxbndsn]
    2745327496            ovary = oncget.variables[getvaryn]
    27454             ovarybnds = oncget.variables[getvarybndsn]
    27455 
    2745627497            sgetvarx, dims = SliceVarDict(ovarx,getslicedict)
    27457             sgetvarbndsx, dims = SliceVarDict(ovarxbnds,getslicedict)
    2745827498            sgetvary, dims = SliceVarDict(ovary,getslicedict)
    27459             sgetvarbndsy, dims = SliceVarDict(ovarybnds,getslicedict)
    27460 
    2746127499            getlon, getlat =  gen.lonlat2D(ovarx[tuple(sgetvarx)], ovary[tuple(sgetvary)])
    27462             getvarbndsx = ovarxbnds[tuple(sgetvarbndsx)]
    27463             getvarbndsy = ovarybnds[tuple(sgetvarbndsy)]
     27500
     27501            if getvarxbndsn != 'WRFxbnds':
     27502                ovarxbnds = oncget.variables[getvarxbndsn]
     27503                ovarybnds = oncget.variables[getvarybndsn]
     27504                sgetvarbndsx, dims = SliceVarDict(ovarxbnds,getslicedict)
     27505                sgetvarbndsy, dims = SliceVarDict(ovarybnds,getslicedict)
     27506                getvarbndsx = ovarxbnds[tuple(sgetvarbndsx)]
     27507                getvarbndsy = ovarybnds[tuple(sgetvarbndsy)]
     27508            else:
     27509                oulon = oncget.variables['XLONG_U']
     27510                oulat = oncget.variables['XLAT_U']
     27511                ovlon = oncget.variables['XLONG_V']
     27512                ovlat = oncget.variables['XLAT_V']
     27513                getvarbndsx, getvarbndsy= compute_WRFbnds(oulon[0,:,:], oulat[0,:,:],\
     27514                  ovlon[0,:,:], ovlat[0,:,:])
     27515
    2746427516            getdx = getlon.shape[1]
    2746527517            getdy = getlon.shape[0]
Note: See TracChangeset for help on using the changeset viewer.