Changeset 1744 in lmdz_wrf


Ignore:
Timestamp:
Dec 16, 2017, 11:29:53 PM (8 years ago)
Author:
lfita
Message:

Improving an getting there...

Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/ORforcing_reconstruct.py

    r1740 r1744  
    99# Importing generic tools file 'generic_tools.py'
    1010import generic_tools as gen
     11import nc_var_tools as ncvar
    1112import subprocess as sub
     13import module_ForSci as Sci
    1214
    1315filen = 'cruncep_halfdeg_1950.nc'
     
    2123
    2224# Range to retrieve
    23 Xmin=-90.
    24 Xmax=-7.
    25 Ymin=-67.
    26 Ymax=15.
    27 
    28 
     25Xmin=-90.25
     26#Xmin='all'
     27Xmax=-5.25
     28Ymin=-67.25
     29Ymax=15.25
     30
     31# Variables to reconstruct
     32#variable = 'all'
     33variable = 'LWdown'
     34
     35# Resolution
     36resX = 0.5
     37resY = 0.5
     38
     39# Minimum difference from matrix to localized point
     40maxdiff = 0.05
    2941
    3042# Projection of the matrix
     
    3446## MAIN
    3547    #######
     48main = 'ORforcing_reconstruct.py'
     49fname = 'ORforcing_reconstruct.py'
     50
    3651onc = NetCDFFile(filen, 'r')
    3752
     
    4055ofilen = 'reconstruct_matrix.nc'
    4156
    42 ovarns = onc.variables.keys()
     57ncvars = onc.variables.keys()
    4358
    4459varstocheck = [indvar, lonvar, latvar]
    45 for vn in vastocheck:
    46     if not gen.searchInlist(ovarns, vn):
     60for vn in varstocheck:
     61    if not gen.searchInlist(ncvars, vn):
    4762        print gen.errormsg
    4863        print '  ' + main + ": file '" + filen + "' does not have variable '" + vn + \
    4964          "' !!"
    50         print '    available ones:', ovarns
     65        print '    available ones:', ncvars
    5166        quit(-1)
    5267
     
    5570olat = onc.variables[latvar]
    5671
     72vecdimn = oind.dimensions[0]
     73Xdimn = olon.dimensions[1]
     74Ydimn = olon.dimensions[0]
     75
    5776indv = oind[:]
    5877lonv = olon[:]
     
    6281dimy = lonv.shape[0]
    6382
    64 veclon = np.zeros((len(indv), dtype=np.float)
    65 veclat = np.zeros((len(indv), dtype=np.float)
     83veclon = np.zeros((len(indv)), dtype=np.float)
     84veclat = np.zeros((len(indv)), dtype=np.float)
    6685
    6786# Constuct veclon, veclat
     
    102121
    103122if type(Xmin) == type('2') and Xmin == 'all':
    104     Xmin = np.min(xposv)
    105     Xmax = np.max(xposv)
    106     Ymin = np.min(yposv)
    107     Ymax = np.max(yposv)
     123    Xmin = np.min(lonv)
     124    Xmax = np.max(lonv)
     125    Ymin = np.min(latv)
     126    Ymax = np.max(latv)
    108127
    109128# Matrix values
    110129if matProj == 'latlon':
    111     dimx = (Xmax - Xmin+resX)/resX
    112     dimy = (Ymax - Ymin+resY)/resY
    113 
    114 print dir(Sci)
    115 matindt, matXt, matYt = Sci.module_scientific.reconstruct_matrix(vectorxpos=xposv,   \
    116   vectorypos=yposv, dvec=len(xposv), xmin=Xmin, xmax=Xmax, ymin=Ymin, ymax=Ymax,     \
     130    dimx = int((Xmax - Xmin+resX)/resX)
     131    dimy = int((Ymax - Ymin+resY)/resY)
     132
     133matindt, matXt, matYt = Sci.module_scientific.reconstruct_matrix(vectorxpos=veclon,  \
     134  vectorypos=veclat, dvec=veclon.shape[0], xmin=Xmin, xmax=Xmax, ymin=Ymin,ymax=Ymax,\
    117135  dmatx=dimx, dmaty=dimy, matproj=matProj, maxdiff=maxdiff)
    118136
     
    127145
    128146# Variable-dimension
    129 newvar = onewnc.createVariable(varXpos, 'f8', ('y', 'x'))
     147newvar = onewnc.createVariable('lon', 'f8', ('y', 'x'))
    130148newvar[:] = matXt.transpose()
    131 ncvar.basicvardef(newvar, varXpos, varXpos, xunits)   
    132 
    133 newvar = onewnc.createVariable(varYpos, 'f8', ('y', 'x'))
     149ncvar.basicvardef(newvar, 'lon', 'Longitude', 'degrees_east')   
     150
     151newvar = onewnc.createVariable('lat', 'f8', ('y', 'x'))
    134152newvar[:] = matYt.transpose()
    135 ncvar.basicvardef(newvar, varYpos, varYpos, yunits)
     153ncvar.basicvardef(newvar, 'lat', 'Latitude', 'degrees_north')   
    136154onewnc.sync()
    137155
     
    147165              dn != Ydimn:
    148166                if onc.dimensions[dn].isunlimited():
    149                     newdim = nc.createDimension(dn, None)
     167                    newdim = onewnc.createDimension(dn, None)
    150168                else:
    151                     newdim = nc.createDimension(dn, len(onc.dimensions[dn]))
     169                    newdim = onewnc.createDimension(dn, len(onc.dimensions[dn]))
    152170               
    153             if dn == vecdim:
     171            if dn == vecdimn:
    154172                vardims.append('y')
    155173                vardims.append('x')
     
    161179
    162180        if ovar.dtype == type(int(2)):
    163             newvar= onewnc.createVariable(vn, nctype(ovar.dtype), tuple(vardims),\
     181            newvar= onewnc.createVariable(vn,ncvar.nctype(ovar.dtype),tuple(vardims),\
    164182              fill_value=gen.fillValueI)
    165183            varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueI
    166         else:
    167             newvar= onewnc.createVariable(vn, nctype(ovar.dtype), tuple(vardims),\
     184        elif ovar.dtype == type(np.int32(2)):
     185            newvar= onewnc.createVariable(vn,ncvar.nctype(ovar.dtype),tuple(vardims),\
     186              fill_value=gen.fillValueI)
     187            varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueI
     188        elif ovar.dtype == type(np.int64(2)):
     189            newvar= onewnc.createVariable(vn,ncvar.nctype(ovar.dtype),tuple(vardims),\
     190              fill_value=gen.fillValueI)
     191            varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueI
     192        elif ovar.dtype == type(np.float(2.)):
     193            newvar= onewnc.createVariable(vn,ncvar.nctype(ovar.dtype),tuple(vardims),\
    168194              fill_value=gen.fillValueF)
    169195            varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueF
     196        elif ovar.dtype == type(np.float32(2.)):
     197            newvar= onewnc.createVariable(vn,ncvar.nctype(ovar.dtype),tuple(vardims),\
     198              fill_value=gen.fillValueF)
     199            varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueF
     200        else:
     201            print gen.errormsg
     202            print '  ' + fname + ': variable type:', ovar.dtype, ' not ready !!'
     203            quit(-1)
    170204
    171205        slices = gen.provide_slices(vardims, shapevar, ['x','y'])
    172206        # Filling variable. It would be faster if we can avoid this loop... I'm feeling lazy!
    173         iix = vardims.index('x')
    174         iiy = vardims.index('y')
    175         for slc in slices:
    176             ival = slc[iix]
    177             jval = slc[iiy]
    178             oldvarslice = []
    179             for dn in indn:
    180                 iid = vardims.index(dn)
    181                 if dn == vecdimn: oldvarslice.append(matind(jval,ival))
    182                 else: oldvarslice.append(slc[iid])
    183 
    184             newvar[tuple(slc)] = onc[tuple(oldvarslice)]
     207        if not gen.searchInlist(vardims,'x') and not gen.searchInlist(vardims,'y'):
     208            newvar[:] = ovar[:]
     209        else:
     210            iix = vardims.index('x')
     211            iiy = vardims.index('y')
     212            for slc in slices:
     213                ival = slc[iix]
     214                jval = slc[iiy]
     215                # Avoiding not found grid points
     216                if matind[jval,ival] != -1:
     217                    oldvarslice = []
     218                    for dn in indn:
     219                        if dn == vecdimn: oldvarslice.append(matind[jval,ival])
     220                        else:
     221                            iid = vardims.index(dn)
     222                            oldvarslice.append(slc[iid])
     223
     224                    print vn, 'Slice new:', slc, 'old:', oldvarslice, 'shape:', newvar.shape, 'oldshape:', ovar.shape
     225                    newvar[tuple(slc)] = ovar[tuple(oldvarslice)]
     226                else:
     227                    if type(newvar.dtype) == type(float(2.)) or                      \
     228                      type(newvar.dtype) == type(np.float(2.)) or                    \
     229                      type(newvar.dtype) == type(np.float32(2)) or                   \
     230                      type(newvar.dtype) == type(np.float64(2)):
     231                        newvar[tuple(slc)] = gen.fillValuerF
     232                    else:
     233                        newvar[tuple(slc)] = gen.fillValueI
    185234
    186235        # Attributes
    187236        for atn in ovar.ncattrs():
    188             if atn != 'fill_Value':
     237            if atn != 'fill_Value' and atn != 'units':
    189238                atv = ovar.getncattr(atn)
    190                 set_attribute(newvar, atn, atv)
     239                ncvar.set_attribute(newvar, atn, atv)
     240        ncvar.set_attribute(newvar, 'coordinates', 'lon lat')
    191241        onewnc.sync()
    192242   
     
    194244for atn in onc.ncattrs():
    195245    atv = ovar.getncattr(atn)
    196     set_attribute(onewnc, atn, atv)
     246    ncvar.set_attribute(onewnc, atn, atv)
    197247onewnc.sync()
    198 add_global_PyNCplot(onewnc, pyscript, fname, '0.1')
     248ncvar.add_global_PyNCplot(onewnc, pyscript, fname, '0.1')
    199249
    200250onc.close()
  • trunk/tools/nc_var_tools.py

    r1741 r1744  
    2078320783    yposv = oypos[:]
    2078420784
     20785    if len(xposv.shape) != 1:
     20786        print errormsg
     20787        print '  ' + fname + ": vector with X locations (from variable '" + varXpos +\
     20788          "') has a rank larger than 1!!"
     20789        print '    current shape:', xposv.shape
     20790        quit(-1)
     20791
     20792    if len(yposv.shape) != 1:
     20793        print errormsg
     20794        print '  ' + fname + ": vector with Y locations (from variable '" + varXpos +\
     20795          "') has a rank larger than 1!!"
     20796        print '    current shape:', yposv.shape
     20797        quit(-1)
     20798
    2078520799    gen.same_shape(xposv, yposv)
    2078620800    vecdim = oxpos.dimensions[0]
Note: See TracChangeset for help on using the changeset viewer.