Changeset 1368 in lmdz_wrf


Ignore:
Timestamp:
Dec 5, 2016, 6:10:42 PM (9 years ago)
Author:
lfita
Message:

Improving `SpatialWeightedMean?'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1366 r1368  
    159159warnmsg = 'WARNING -- warning -- WARNING -- warning'
    160160
     161## Constants
     162# Radius of the Earth (m)
     163EarthR = 6378000.
     164
    161165class genericNCvariable(object):
    162166    """ Class to fake a netCDF varibale
     
    1410514109    if oncf.variables.has_key(varname):
    1410614110        print warnmsg
    14107         print '  ' + fname + ": netCDF object already has variable '" + ovar.name +  \
     14111        print '  ' + fname + ": netCDF object already has variable '" + varname +   \
    1410814112          "' !!"
    1410914113        print '    doing noting'
     
    1414414148        [xdimname]: name of the dimension for the x-axis
    1414514149        [ydimname]: name of the dimension for the y-axis
    14146         [addvars]: ':', separetd list of name of variables to add to the output file
     14150        [addvars]: ':', separetd list of name of variables to add to the output file ('None' for any)
    1414714151      filen= name of the netCDF file
    1414814152      varn= name of the variable
     
    1440714411        ilatv = iolat[:]
    1440814412
    14409         lonv, latv = lonlat2D(ilonv, ilatv)
    14410 
    14411         ivarwgtv = np.abs(np.cos(latv*np.pi/180.))
     14413        lonv, latv = gen.lonlat2D(ilonv, ilatv)
     14414
     14415        # Direct and wrong
     14416        #ivarwgtv = np.abs(np.cos(latv*np.pi/180.))
     14417
     14418        # Using shoelace formula
     14419        dx = lonv.shape[1]
     14420        dy = lonv.shape[0]
     14421
     14422        dlon = np.zeros((lonv.shape), dtype=np.float)
     14423        dlat = np.zeros((latv.shape), dtype=np.float)
     14424        ivarwgtv = np.zeros((latv.shape), dtype=np.float)
     14425
     14426        dlon[:,0:dx-1] = lonv[:,1:dx] - lonv[:,0:dx-1]
     14427        dlat[0:dy-1,:] = latv[1:dy,:] - latv[0:dy-1,:]
     14428        dlon[:,dx-1] = lonv[:,dx-1] - lonv[:,dx-2]
     14429        dlat[dy-1,:] = latv[dy-1,:] - latv[dy-2,:]
     14430
     14431        coslat = np.abs(np.cos(latv*np.pi/180.))
     14432        dsx = EarthR*coslat*dlon
     14433        dsy = EarthR*dlat
     14434        for j in range(dy-1):
     14435            for i in range(dx-1):
     14436#                xvals = [dsx[j,i], dsx[j+1,i], dsx[j+1,i+1], dsx[j,i+1]]
     14437#                yvals = [dsy[j,i], dsy[j+1,i], dsy[j+1,i+1], dsy[j,i+1]]
     14438                xvals = [0., 0., dsx[j+1,i+1], dsx[j,i+1]]
     14439                yvals = [0., dsy[j+1,i], dsy[j+1,i+1], 0.]
     14440                ivarwgtv[j,i] = gen.PolyArea(xvals, yvals)
     14441        j=dy-1
     14442        for i in range(dx-1):
     14443            xvals = [0., 0., dsx[j-1,i+1], dsx[j,i-1]]
     14444            yvals = [0., dsy[j-1,i], dsy[j-1,i+1], 0.]
     14445            ivarwgtv[j,i] = gen.PolyArea(xvals, yvals)
     14446        i=dx-1
     14447        for j in range(dy-1):
     14448            xvals = [0., 0., dsx[j+1,i-1], dsx[j,i-1]]
     14449            yvals = [0., dsy[j+1,i], dsy[j+1,i-1], 0.]
     14450            ivarwgtv[j,i] = gen.PolyArea(xvals, yvals)
     14451
     14452        i=dx-1
     14453        j=dy-1
     14454        xvals = [0., 0., dsx[j-1,i-1], dsx[j,i-1]]
     14455        yvals = [0., dsy[j-1,i], dsy[j-1,i-1], 0.]
     14456        ivarwgtv[j,i] = gen.PolyArea(xvals, yvals)
     14457
    1441214458        TOTsumwgt = np.sum(ivarwgtv)
    1441314459
     
    1444914495                        newsumvals[id1,id2,id3] = np.sum(vals*ivarwgtv)
    1445014496                        newvals[id1,id2,id3] = newsumvals[id1,id2,id3] / TOTsumwgt
    14451         outweightvals = ivarwgtv
     14497        outweightvals = ivarwgtv/TOTsumwgt
    1445214498
    1445314499# Writting output file
    1445414500##
     14501    print 'TOTAL sunm of weights:', np.sum(outweightvals)
    1445514502    onewnc = NetCDFFile(ofile, 'w')
    1445614503
     
    1448114528    onewnc.sync()
    1448214529
    14483     print 'newsumvals:',newsumvals,'newvals:',newvals,'TOTsumwgt:',TOTsumwgt
    14484 
    1448514530# Spatial weight
    1448614531##
     
    1449414539    onewnc.sync()
    1449514540
     14541    newvar = onewnc.createVariable('dlon', 'f4', tuple([ydimname, xdimname]), \
     14542      fill_value=gen.fillValueF)
     14543    basicvardef(newvar, 'dlon', 'dlon', 'degrees_East')
     14544    newvar[:] = dlon[:]
     14545    newvar = onewnc.createVariable('dlat', 'f4', tuple([ydimname, xdimname]), \
     14546      fill_value=gen.fillValueF)
     14547    basicvardef(newvar, 'dlat', 'dlat', 'degrees_North')
     14548    newvar[:] = dlat[:]
     14549    newvar = onewnc.createVariable('coslat', 'f4', tuple([ydimname, xdimname]), \
     14550      fill_value=gen.fillValueF)
     14551    basicvardef(newvar, 'coslat', 'latitude cosinus', '-')
     14552    newvar[:] = coslat[:]
     14553    newvar = onewnc.createVariable('dsx', 'f4', tuple([ydimname, xdimname]), \
     14554      fill_value=gen.fillValueF)
     14555    basicvardef(newvar, 'dsx', 'x distance', 'm')
     14556    newvar[:] = dsx[:]
     14557    newvar = onewnc.createVariable('dsy', 'f4', tuple([ydimname, xdimname]), \
     14558      fill_value=gen.fillValueF)
     14559    basicvardef(newvar, 'dsy', 'y distance', 'm')
     14560    newvar[:] = dsy[:]
     14561    onewnc.sync()
     14562
    1449614563# Additional variables
    1449714564##
    14498     for vn in addvars.split(':'):
    14499         if not onc.variables.has_key(vn):
    14500             print errormsg
    14501             print '  ' + fname + ": file '" + filen + "' does not have variable '" + \
    14502               vn + "' !!"
    14503             quit(-1)
    14504         ovar_onc(onc, onc.variables[vn], onewnc)
    14505     onewnc.sync()
     14565    if addvars != 'None':
     14566        for vn in addvars.split(':'):
     14567            if not onc.variables.has_key(vn):
     14568                print errormsg
     14569                print '  ' + fname + ": file '" + filen + "' does not have variable '" + \
     14570                  vn + "' !!"
     14571                quit(-1)
     14572            ovar_onc(onc, onc.variables[vn], onewnc)
     14573        onewnc.sync()
    1450614574
    1450714575# Global attributes
     
    1465414722
    1465514723    # Given constants
    14656     EarthR = 6378000.
    1465714724    RadDeg = 180./np.pi
    1465814725
Note: See TracChangeset for help on using the changeset viewer.