Changeset 1400 in lmdz_wrf for trunk


Ignore:
Timestamp:
Jan 9, 2017, 2:40:29 PM (8 years ago)
Author:
lfita
Message:

Adding checking of existence of dimension before to add it in `add_dims'
Adding `curve_section': Function to provide a section of a file following a given curve

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1398 r1400  
    4444# compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
    4545# compute_tevolboxtraj_radialsec: Function to compute tevolboxtraj_radialsec: temporal evolution at a given point along a number of radii at a given frequency following a trajectory
     46# curve_section: Function to provide a section of a file following a given curve
    4647# DataSetSection: Function to get a section (values along a dimension) of a given data-set
    4748# DataSetSection_multidims: Function to get a section (values along multiple dimensions) of a given data-set
     
    258259
    259260    for dn in dnames:
    260         print '  ' + fname + ': adding dim:',dn,' ...'
    261         if oc.dimensions[dn].isunlimited():
    262             newdim = nc.createDimension(dn, None)
    263         else:
    264             newdim = nc.createDimension(dn, len(oc.dimensions[dn]))
     261        if not gen.searchInlist(nc.dimensions, dn):
     262            print '  ' + fname + ': adding dim:',dn,' ...'
     263            if oc.dimensions[dn].isunlimited():
     264                newdim = nc.createDimension(dn, None)
     265            else:
     266                newdim = nc.createDimension(dn, len(oc.dimensions[dn]))
    265267
    266268    nc.sync()
     
    1843818440          [iBL],[jBL] to `right-up' grid point [iUR],[jUR] and [Ncurve] number of line-points with
    1843918441          [xdimn],[ydimn] name of the x and y dimensions
    18440         'lonlatline',[londimn]|[lonvarname],[latdimn]|[latvarname],[iSW],[jSW],[iNE],[jNE],[Ncurve]: line from starting
    18441           `SW' lonlat point [lonSW],[latSW] to `NE' lonlat point [lonNE],[latNE] and [Ncurve] line-points
     18442        'lonlatline',[londimn]|[lonvarname],[latdimn]|[latvarname],[lonSW],[latSW],[lonNE],[latNE],[Ncurve]: line from
     18443          starting `SW' lonlat point [lonSW],[latSW] to `NE' lonlat point [lonNE],[latNE] and [Ncurve] line-points
    1844218444          with '[lon/latdimn]|[lon/latvarn]' name of the dimension and variable-dimension of longitude
    1844318445          and latitude
    18444         'gridfilecurve',[xdimn],[ydimn],[curvefile]: line following grid-values in the file
     18446        'gridfilecurve',[xdimn],[ydimn],[curvefile]: curve following grid-values in the file
    1844518447          [curvefile](x y; two columns space separated, '#' commment) with [xdimn],[ydimn] name of the x and y dimensions
    18446         'lonlatfilecurve',[londimn]|[lonvarname],[latdimn]|[latvarname],[curvefile]: line following
     18448        'lonlatfilecurve',[londimn]|[lonvarname],[latdimn]|[latvarname],[curvefile]: curve following
    1844718449          lonlat-values in the file [curvefile](lon lat; two columns space separated, '#' commment) with
    1844818450          '[lon/latdimn]|[lon/latvarn]' name of the dimension and variable-dimension of longitude
     
    1852418526        curve = np.zeros((Ncurve,2), dtype=np.float)
    1852518527        for icv in range(Ncurve):
    18526             curve[icv,0] = lonSW + Dycurve*icv
    18527             curve[icv,1] = latSW + Dxcurve*icv
     18528            curve[icv,0] = latSW + Dycurve*icv
     18529            curve[icv,1] = lonSW + Dxcurve*icv
    1852818530
    1852918531    elif values.split(',')[0] == 'gridfilecurve':
     
    1862718629
    1862818630    # Getting curve-values
    18629     loccurve, curvewgts, Nwgts, curveloc3x3 = gen.curvelocalize_2D(curve,xvals,yvals)
     18631    loccurve, curvewgts, Nwgts, curveloc3x3, exctloccurve = gen.curvelocalize_2D(    \
     18632      curve,xvals,yvals)
    1863018633
    1863118634    # Creation of file
     
    1864718650    basicvardef(newvar,'ycurve', 'y-' +Lcurvey+ ' localization of the curve', cyunit)
    1864818651
    18649     # variables
     18652    # variable curvetype
     18653    curvetype = values.split(',')[0]
     18654    newvar = newnc.createVariable(curvetype, 'c')
     18655    if curvetype == 'gridline':
     18656        desc = "line from starting `bottom-left` grid point [iBL],[jBL] to " +       \
     18657          "`right-up` grid point [iUR],[jUR] and [Ncurve] number of line-points " +  \
     18658          "with [xdimn],[ydimn] name of the x and y dimensions"
     18659        set_attributek(newvar,'description',desc,'S')
     18660        set_attributek(newvar,'xdimn',xdimn,'S')
     18661        set_attributek(newvar,'ydimn',ydimn,'S')
     18662        set_attributek(newvar,'iBL',iBL,'I')
     18663        set_attributek(newvar,'jBL',jBL,'I')
     18664        set_attributek(newvar,'iUR',iUR,'I')
     18665        set_attributek(newvar,'jUR',jUR,'I')
     18666        set_attributek(newvar,'Ncurve',Ncurve,'I')
     18667    elif curvetype == 'lonlatline':
     18668        desc = "line from starting `SW` lonlat point [lonSW],[latSW] to `NE` " +     \
     18669          "lonlat point [lonNE],[latNE] and [Ncurve] line-points with " +            \
     18670          "`[lon/latdimn]|[lon/latvarn]` name of the dimension and " +               \
     18671          "variable-dimension of longitude and latitude"
     18672        set_attributek(newvar,'description',desc,'S')
     18673        set_attributek(newvar,'londimn',xdimn,'S')
     18674        set_attributek(newvar,'lonvarname',xvardimn,'S')
     18675        set_attributek(newvar,'latdimn',ydimn,'S')
     18676        set_attributek(newvar,'latvarname',yvardimn,'S')
     18677        set_attributek(newvar,'lonSW',lonSW,'R')
     18678        set_attributek(newvar,'latSW',latSW,'R')
     18679        set_attributek(newvar,'lonNE',lonNE,'R')
     18680        set_attributek(newvar,'latNE',latNE,'R')
     18681        set_attributek(newvar,'Ncurve',Ncurve,'I')
     18682    elif curvetype == 'gridfilecurve':
     18683        desc = "curve following grid-values in the file [curvefile](x y; two " +     \
     18684          "columns space separated, `#` commment) with [xdimn],[ydimn] name of " +   \
     18685          "the x and y dimensions"
     18686        set_attributek(newvar,'description',desc,'S')
     18687        set_attributek(newvar,'xdimn',xdimn,'S')
     18688        set_attributek(newvar,'ydimn',ydimn,'S')
     18689        set_attributek(newvar,'curvefile',curvefile,'S')
     18690    elif curvetype == 'lonlatfilecurve':
     18691        desc = "curve following lonlat-values in the file [curvefile](lon lat; " +   \
     18692          "two columns space separated, `#` commment) with `[lon/latdimn]|" +        \
     18693          "[lon/latvarn]` name of the dimension and variable-dimension of " +        \
     18694          "longitude and latitude"
     18695        set_attributek(newvar,'description',desc,'S')
     18696        set_attributek(newvar,'londimn',xdimn,'S')
     18697        set_attributek(newvar,'lonvarname',xvardimn,'S')
     18698        set_attributek(newvar,'latdimn',ydimn,'S')
     18699        set_attributek(newvar,'latvarname',yvardimn,'S')
     18700        set_attributek(newvar,'curvefile',curvefile,'S')
     18701
     18702    # Localization curve within file
     18703    newvar = newnc.createVariable('icurve', 'f8', ('curve'))
     18704    newvar[:] = exctloccurve[:,1]
     18705    basicvardef(newvar,'icurve', 'x-axis localization of the curve', '1')
     18706
     18707    newvar = newnc.createVariable('jcurve', 'f8', ('curve'))
     18708    newvar[:] = exctloccurve[:,0]
     18709    basicvardef(newvar,'jcurve', 'y-axis localization of the curve', '1')
     18710
     18711    # variable weight
    1865018712    newvar = newnc.createVariable('weights', 'f', ('box','box','curve'))
    1865118713    for icv in range(Ncurve):
     
    1865318715    basicvardef(newvar,'weights', 'weights on the 3x3 around box following curve','1')
    1865418716
    18655     # variables
     18717    # variable Nweigth
    1865618718    newvar = newnc.createVariable('Nweights', 'f', ('curve'))
    1865718719    newvar[:] = Nwgts[:]
     
    1865918721      'following curve', '-')
    1866018722
    18661     # variables
     18723    # variable coordweights
    1866218724    newvar = newnc.createVariable('coordweights', 'f', ('pt','boxbox','curve'))
    1866318725    for icv in range(Ncurve):
     
    1866618728      '3x3 around box following curve', '-')
    1866718729
     18730    # File variables to get the section
    1866818731    for vn in varns:
    1866918732        if not onc.variables.has_key(vn):
     
    1867618739
    1867718740        ovar = onc.variables[vn]
    18678         dimvar = list(ovar.dimensions)
    18679         if gen.searchInlist(dimvar, xdimn): dimvar.remove(xdimn)
    18680         if gen.searchInlist(dimvar, ydimn): dimvar.remove(ydimn)
    18681 
    18682         varcurve = interp_curve(ovar, xdimn, ydimn, loccurve, curvewgts, Nwgts,      \
    18683           curveloc3x3)
    18684 
    18685         # Writing values of variable along the curve
    18686         add_dims(onc,newnc,dimvar)
    18687 
    18688         newvar = newnc.createVariable(vn+'curve', 'f4', tuple(dimvar+['curve']))
    18689         newvar[:] = varcurve[:]
    18690         add_varattrs(onc,newnc,[vn],[vn+'curve'])
    18691         set_attribute(newvar,'along','xcurve,ycurve')
     18741        if len(ovar.shape) == 0:
     18742            print warnmsg
     18743            print '  ' +fname+ ": scalar variable '" + vn + "' section impossible !!"
     18744            newvar = newnc.createVariable(vn, ovar.dtype)
     18745            newvar[:] = ovar[:]
     18746            add_varattrs(onc,newnc,[vn],[vn])
     18747        else:
     18748            print '    ' + fname + ": adding section of '" + vn + "' ..."
     18749            dimvar = list(ovar.dimensions)
     18750            if gen.searchInlist(dimvar, xdimn): dimvar.remove(xdimn)
     18751            if gen.searchInlist(dimvar, ydimn): dimvar.remove(ydimn)
     18752
     18753            varcurve = interp_curve(ovar, xdimn, ydimn, loccurve, curvewgts, Nwgts, \
     18754              curveloc3x3)
     18755
     18756            # Writing values of variable along the curve
     18757            add_dims(onc,newnc,dimvar)
     18758
     18759            newvar = newnc.createVariable(vn+'curve', 'f4', tuple(dimvar+['curve']))
     18760            newvar[:] = varcurve[:]
     18761            add_varattrs(onc,newnc,[vn],[vn+'curve'])
     18762            set_attribute(newvar,'along','xcurve,ycurve')
     18763            set_attribute(newvar,'curve',curvetype)
     18764            newnc.sync()
    1869218765 
    1869318766    # Global attributes
     
    1870818781    return
    1870918782
    18710 values = 'gridline,x,y,8.,8.,16.,16.,32'
    18711 curve_section(values,'/home/lluis/PY/test.nc','var')
    18712 
    1871318783#quit()
Note: See TracChangeset for help on using the changeset viewer.