Changeset 2195 in lmdz_wrf for trunk


Ignore:
Timestamp:
Oct 18, 2018, 6:12:36 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `compute_slice2Dstats': Function to compute stats of variables of a file following slices along 2 variables
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var.py

    r2168 r2195  
    6262## e.g. # nc_var.py -o temporal_stats -S 'Time:WRFtime:day@1@min,LTday@-3@1@min:bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2
    6363## e.g. # nc_var.py -o retrieve_stations -f wrfout_d01_1995-01-01_00:00:00 -S 'tmin_percentiles.nc:stname:None:stlon:stlat:None:nearest:west_east:XLONG:south_north:XLAT:HGT:Time:WRFtime' -v T2,QVAPOR
     64## e.g. # nc_var.py -o compute_slice2Dstats -S 'XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2
    6465
    6566from optparse import OptionParser
     
    8990# compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps
    9091# compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps
     92# compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables
    9193# compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
    9294# computevar_model: Function to provide the way to compute a CF-variable providing its name
     
    203205  'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum',                \
    204206  'compute_opersvarsfiles',                                                          \
    205   'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj',          \
     207  'compute_opervaralltime', 'compute_opervartimes', 'compute_slice2Dstats',          \
     208  'compute_tevolboxtraj',                                                            \
    206209  'computevar_model', 'curve_section', 'DatesFiles',                                 \
    207210  'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
     
    342345elif oper == 'compute_opervartimes':
    343346    ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname)
     347elif oper == 'compute_slice2Dstats':
     348    ncvar.compute_slice2Dstats(opts.values, opts.ncfile, opts.varname)
    344349elif oper == 'compute_tevolboxtraj':
    345350    ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname)
  • trunk/tools/nc_var_tools.py

    r2194 r2195  
    4646# compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps
    4747# compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps
     48# compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables
    4849# computevar_model: Function to provide the way to compute a CF-variable providing its name
    4950# compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
     
    2613226133
    2613326134def compute_slice2Dstats(values, ncfile, variable):
    26134     """ Function to compute stats of variables of a file following slices along 2 variables
    26135       values=dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,maxvar2,slcevar2
    26136         dimn[1/2]: dimension from which produce the slices of variable 1/2 ('all'
    26137           for non-specific dimension to cut along)
     26135    """ Function to compute stats of variables of a file following slices along 2
     26136        variables
     26137      values=varn1,minvar1,maxvar1,slcevar1,varn2,minvar2,maxvar2,slcevar2
    2613826138        varn[1/2]: name of the variable 1/2
    2613926139        minvar[1/2]: minimum value to start the slices for variable 1/2
    2614026140        maxvar[1/2]: maximum value to end the slices for variable 1/2
    2614126141        slcevar[1/2]: length of slices in variable 1/2
     26142        dimvars: ':' separated list of [dimn]|[vardimn] dimension name [dimn] and
     26143          its associated dimension-variable to get its values to provide values for
     26144          the final file
    2614226145      ncfile= netCDF file to use
    2614326146      variable: ',' list of variables ('all' for all variables)
     
    2615026153        quit()
    2615126154
    26152     expectargs = 'dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,' +       \
    26153      'maxvar2,slcevar2'
     26155    expectargs = 'varn1,minvar1,maxvar1,slcevar1,varn2,minvar2,maxvar2,slcevar2,' +  \
     26156      'dimvars'
    2615426157    gen.check_arguments(fname, values, expectargs, ',')
    2615526158
    26156     dimn1 = values.split(',')[0]
    26157     varn1 = values.split(',')[1]
    26158     minvar1 = np.float(values.split(',')[2])
    26159     maxvar1 = np.float(values.split(',')[3])
    26160     slicevar1 = np.float(values.split(',')[4])
    26161     dimn2 = values.split(',')[5]
    26162     varn2 = values.split(',')[6]
    26163     minvar2 = np.float(values.split(',')[7])
    26164     maxvar2 = np.float(values.split(',')[8])
    26165     slicevar2 = np.float(values.split(',')[9])
     26159    varn1 = values.split(',')[0]
     26160    minvar1 = np.float(values.split(',')[1])
     26161    maxvar1 = np.float(values.split(',')[2])
     26162    slicevar1 = np.float(values.split(',')[3])
     26163    varn2 = values.split(',')[4]
     26164    minvar2 = np.float(values.split(',')[5])
     26165    maxvar2 = np.float(values.split(',')[6])
     26166    slicevar2 = np.float(values.split(',')[7])
     26167    dimvars = gen.stringS_dictvar(values.split(',')[8], Dc=':', DVs='|')
    2616626168   
    2616726169    onc = NetCDFFile(ncfile, 'r')
    26168 
    26169     if dimn1 != 'all' and not gen.searchInlist(onc.dimensions, dimn1):
    26170         print errormsg
    26171         print '  ' + fname + ": file '" + ncfile + "' does not have dimension 1: '" +\
    26172           dimn1 + "' !!"
    26173         dimns = list(onc.dimensions)
    26174         dimns.sort()
    26175         print '    available ones:', dimns
    26176         quit(-1)
    26177 
    26178     if dimn2 != 'all' and not gen.searchInlist(onc.dimensions, dimn2):
    26179         print errormsg
    26180         print '  ' + fname + ": file '" + ncfile + "' does not have dimension 2: '" +\
    26181           dimn2 + "' !!"
    26182         dimns = list(onc.dimensions)
    26183         dimns.sort()
    26184         print '    available ones:', dimns
    26185         quit(-1)
    2618626170
    2618726171    if not onc.variables.has_key(varn1):
     
    2620326187        quit(-1)
    2620426188
     26189    for dn in dimvars.keys():
     26190        if not gen.searchInlist(onc.dimensions,dn):
     26191            print errormsg
     26192            print '  ' +fname+ ": file '" + ncfile + "' does not have dimension: '"+ \
     26193              dn + "' !!"
     26194            dimns = list(onc.dimensions)
     26195            dimns.sort()
     26196            print '    available ones:', dimns
     26197            quit(-1)
     26198
     26199
    2620526200    ovar1 = onc.variables[varn1]
    2620626201    ovar2 = onc.variables[varn2]
     26202    v1u = ovar1.units
     26203    v2u = ovar2.units
    2620726204
    2620826205    dvar1 = (maxvar1 - minvar1+slicevar1)/slicevar1
     
    2622326220    sliceshape1 = [Nslices1] + list(ovar1.shape)
    2622426221    slcvar1 = np.zeros(tuple(sliceshape1), dtype=bool)
     26222    slcvalsc1 = np.zeros((Nslices1), dtype=np.float)
     26223    slcvals1 = np.zeros((Nslices1,2), dtype=np.float)
    2622526224    var1v = ovar1[:]
    2622626225    for islc in range(Nslices1-1):
    2622726226        slcvar1[islc,] = ma.masked_inside(var1v, slices1[islc], slices1[islc+1]).mask
     26227        slcvalsc1[islc] = (slices1[islc+1]+slices1[islc+1])/2.
     26228        slcvals1[islc,0] = slices1[islc]
     26229        slcvals1[islc,1] = slices1[islc+1]
    2622826230
    2622926231    # Slices var2
    2623026232    sliceshape2 = [Nslices2] + list(ovar2.shape)
    2623126233    slcvar2 = np.zeros(tuple(sliceshape2), dtype=bool)
     26234    slcvalsc2 = np.zeros((Nslices2), dtype=np.float)
     26235    slcvals2 = np.zeros((Nslices2,2), dtype=np.float)
    2623226236    var2v = ovar2[:]
    2623326237    for islc in range(Nslices2-1):
    2623426238        slcvar2[islc,] = ma.masked_inside(var2v, slices2[islc], slices2[islc+1]).mask
     26239        slcvalsc2[islc] = (slices2[islc+1]+slices2[islc+1])/2.
     26240        slcvals2[islc,0] = slices2[islc]
     26241        slcvals2[islc,1] = slices2[islc+1]
    2623526242
    2623626243    if variable == 'all':
     
    2623926246        varns = gen.str_list(variable, ',')
    2624026247
     26248    onewnc = NetCDFFile(fname + '.nc', 'w')
     26249    # dimensions
     26250    newdim = onewnc.createDimension('slice_'+varn1, Nslices1)
     26251    newdim = onewnc.createDimension('slice_'+varn2, Nslices2)
     26252    newdim = onewnc.createDimension('slice_bnds', 2)
     26253
     26254    # variable dimensions
     26255    newvar = onewnc.createVariable('slice_'+varn1, 'f', ('slice_'+varn1))
     26256    newvar[:] = slcvalsc1[:]
     26257    basicvardef(newvar, 'slice_'+varn1, 'slices for variable ' + varn1, v1u)
     26258
     26259    newvar = onewnc.createVariable('slice_'+varn1+'_bnds', 'f', ('slice_'+varn1,     \
     26260      'slice_bnds'))
     26261    newvar[:] = slcvals1[:]
     26262    basicvardef(newvar, 'slice_'+varn1+'_bnds', 'boundaries of slices for variable '+\
     26263      varn1, v1u)
     26264
     26265    newvar = onewnc.createVariable('slice_'+varn2, 'f', ('slice_'+varn2))
     26266    newvar[:] = slcvalsc2[:]
     26267    basicvardef(newvar, 'slice_'+varn2, 'slices for variable ' + varn2, v2u)
     26268
     26269    newvar = onewnc.createVariable('slice_'+varn2+'_bnds', 'f', ('slice_'+varn2,     \
     26270      'slice_bnds'))
     26271    newvar[:] = slcvals2[:]
     26272    basicvardef(newvar, 'slice_'+varn2+'_bnds', 'boundaries of slices for variable '+\
     26273      varn2, v2u)
     26274
    2624126275    for varn in varns:
     26276        print '    ' + varn + ' ...'
    2624226277        ovar = onc.variables[varn]
    2624326278        varv = ovar[:]
    2624426279        vdimns = list(ovar.dimensions)
    2624526280        vshape = list(ovar.shape)
     26281        vrank = len(vdimns)
     26282        vu = ovar.units
     26283
     26284        for dn in vdimns:
     26285            if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn])
    2624626286
    2624726287        # mask1 in var slice
     
    2627226312
    2627326313        newvarshape = [Nslices2, Nslices1] + vshape
    26274         newvar2masked = np.zeros(tuple(newvarshape), dtype=ovar.dtype)
     26314        newvar2masked = np.ones(tuple(newvarshape), dtype=ovar.dtype)*gen.fillValueF
    2627526315
    2627626316        for islc2 in range(Nslices2):
    2627726317            newmask2 = mask2varslice[islc2]
    2627826318            for islc1 in range(Nslices1):
     26319                slcnewvar = []
     26320                slcnewvar.append(islc2)
     26321                slcnewvar.append(islc1)
     26322                for iid in vshape:
     26323                    slcnewvar.append(slice(0,iid))
    2627926324                newmask1 = mask1varslice[islc1]
    26280                 newmask = newmask2+newmask1
    26281 
    26282                 print '   Lluis shapes: newvar2masked',  newvar2masked[islc2,islc1,].shape, 'varv:', varv[newmask].shape
    26283                 newvar2masked[islc2,islc1,] = ma.masked_array(varv, mask=~newmask)
    26284 
    26285         onewnc = NetCDFFile(fname + '.nc', 'w')
    26286         # dimensions
    26287         newdim = onewnc.createDimension('lon', varv.shape[2])
    26288         newdim = onewnc.createDimension('lat', varv.shape[1])
    26289         newdim = onewnc.createDimension('time', None)
    26290         newdim = onewnc.createDimension('slice_'+varn1, Nslices1)
    26291         newdim = onewnc.createDimension('slice_'+varn2, Nslices2)
    26292 
     26325                newmask = newmask2*newmask1
     26326                mavals = ma.array(varv, mask=~newmask)
     26327                newvar2masked[tuple(slcnewvar)] = mavals.filled(gen.fillValueF)
     26328                 
    2629326329        # variables
    26294         newvar = onewnc.createVariable(varn, 'f', ('slice_'+varn2, 'slice_'+varn1, 'time', 'lat', 'lon'))
     26330        newvard = ['slice_'+varn2,'slice_'+varn1] + vdimns
     26331        newvar = onewnc.createVariable(varn+'sliced', 'f', tuple(newvard),           \
     26332          fill_value=gen.fillValueF)
    2629526333        newvar[:] = newvar2masked[:]
     26334        basicvardef(newvar, varn+'sliced', varn + 'sliced by '+varn1+' & '+varn2, vu)
     26335        add_varattrs(onc, onewnc, [varn], [varn+'sliced'])
     26336
     26337        manewvar = ma.masked_equal(newvar2masked, gen.fillValueF)
     26338
     26339        newvard = ['slice_'+varn2,'slice_'+varn1]
     26340        newvarn = onewnc.createVariable(varn+'minsliced', 'f', tuple(newvard),        \
     26341          fill_value=gen.fillValueF)
     26342        basicvardef(newvarn, varn+'minsliced', 'minimum value of '+varn+'sliced by '+ \
     26343          varn1+' & '+varn2, vu)
     26344        add_varattrs(onc, onewnc, [varn], [varn+'minsliced'])
     26345
     26346        newvarx = onewnc.createVariable(varn+'maxsliced', 'f', tuple(newvard),        \
     26347          fill_value=gen.fillValueF)
     26348        basicvardef(newvarx, varn+'maxsliced', 'maximum value of '+varn+'sliced by '+ \
     26349          varn1+' & '+varn2, vu)
     26350        add_varattrs(onc, onewnc, [varn], [varn+'maxsliced'])
     26351
     26352        newvarm = onewnc.createVariable(varn+'meansliced', 'f', tuple(newvard),       \
     26353          fill_value=gen.fillValueF)
     26354        basicvardef(newvarm, varn+'meansliced', 'mean value of '+varn+'sliced by ' +  \
     26355          varn1+' & '+varn2, vu)
     26356        add_varattrs(onc, onewnc, [varn], [varn+'meansliced'])
     26357
     26358        newvars = onewnc.createVariable(varn+'stdsliced', 'f', tuple(newvard),        \
     26359          fill_value=gen.fillValueF)
     26360        basicvardef(newvars, varn+'stdsliced', 'standard deviation of '+varn+         \
     26361          'sliced by '+varn1+' & '+varn2, vu)
     26362        add_varattrs(onc, onewnc, [varn], [varn+'stdsliced'])
     26363
     26364        newvarn[:] = np.min(manewvar, axis=tuple(np.arange(vrank) + 2))
     26365        newvarx[:] = np.max(manewvar, axis=tuple(np.arange(vrank) + 2))
     26366        newvarm[:] = np.mean(manewvar, axis=tuple(np.arange(vrank) + 2))
     26367        newvars[:] = np.std(manewvar, axis=tuple(np.arange(vrank) + 2))
    2629626368
    2629726369        onewnc.sync()
    26298         onewnc.close()
    26299 
    26300         quit()
     26370        # Adding dimension-variables
     26371        for dn in vdimns:
     26372            if not dimvars.has_key(dn):
     26373                print errormsg
     26374                print '  ' + fname + ": no dimension-variable provided for " +       \
     26375                  "dimension '" + dn + "' !!"
     26376                print '    provided ones _______'
     26377                gen.printing_dictionary(dimvars)
     26378                quit(-1)
     26379            if not onewnc.variables.has_key(dimvars[dn]):
     26380                add_vars(onc, onewnc, [dimvars[dn]])
     26381                onewnc.sync()
     26382
     26383    # Add global attributes
     26384    add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0')
     26385    add_globattrs(onc,onewnc,'all')
     26386    onewnc.sync()
     26387    onc.close()
     26388    onewnc.close()
     26389    print fname + ": successfull written of file '" + fname + ".nc' !!"
    2630126390
    2630226391    return
    2630326392
    26304 values='south_north,XLAT,-63.,19.,2.,all,HGT,250.,7000.,500.'
    26305 
    26306 compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2')
     26393#values='XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT'
     26394#compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,Q2')
    2630726395
    2630826396#quit()
Note: See TracChangeset for help on using the changeset viewer.