Changeset 1368 in lmdz_wrf
- Timestamp:
- Dec 5, 2016, 6:10:42 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1366 r1368 159 159 warnmsg = 'WARNING -- warning -- WARNING -- warning' 160 160 161 ## Constants 162 # Radius of the Earth (m) 163 EarthR = 6378000. 164 161 165 class genericNCvariable(object): 162 166 """ Class to fake a netCDF varibale … … 14105 14109 if oncf.variables.has_key(varname): 14106 14110 print warnmsg 14107 print ' ' + fname + ": netCDF object already has variable '" + ovar.name +\14111 print ' ' + fname + ": netCDF object already has variable '" + varname + \ 14108 14112 "' !!" 14109 14113 print ' doing noting' … … 14144 14148 [xdimname]: name of the dimension for the x-axis 14145 14149 [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) 14147 14151 filen= name of the netCDF file 14148 14152 varn= name of the variable … … 14407 14411 ilatv = iolat[:] 14408 14412 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 14412 14458 TOTsumwgt = np.sum(ivarwgtv) 14413 14459 … … 14449 14495 newsumvals[id1,id2,id3] = np.sum(vals*ivarwgtv) 14450 14496 newvals[id1,id2,id3] = newsumvals[id1,id2,id3] / TOTsumwgt 14451 outweightvals = ivarwgtv 14497 outweightvals = ivarwgtv/TOTsumwgt 14452 14498 14453 14499 # Writting output file 14454 14500 ## 14501 print 'TOTAL sunm of weights:', np.sum(outweightvals) 14455 14502 onewnc = NetCDFFile(ofile, 'w') 14456 14503 … … 14481 14528 onewnc.sync() 14482 14529 14483 print 'newsumvals:',newsumvals,'newvals:',newvals,'TOTsumwgt:',TOTsumwgt14484 14485 14530 # Spatial weight 14486 14531 ## … … 14494 14539 onewnc.sync() 14495 14540 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 14496 14563 # Additional variables 14497 14564 ## 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() 14506 14574 14507 14575 # Global attributes … … 14654 14722 14655 14723 # Given constants 14656 EarthR = 6378000.14657 14724 RadDeg = 180./np.pi 14658 14725
Note: See TracChangeset
for help on using the changeset viewer.