Changeset 1740 in lmdz_wrf
- Timestamp:
- Dec 16, 2017, 8:01:31 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/ORforcing_reconstruct.py
r1739 r1740 26 26 Ymax=15. 27 27 28 29 30 # Projection of the matrix 31 matProj = 'latlon' 32 28 33 ####### ####### 29 34 ## MAIN 30 35 ####### 31 36 onc = NetCDFFile(filen, 'r') 37 38 availProj = ['latlon'] 39 32 40 ofilen = 'reconstruct_matrix.nc' 33 41 … … 61 69 iy = int(indv[iid]/dimx) 62 70 ix = indv[iid] - iy*dimx 63 print iid, 'Lluis: indv:', indv[iid], 'iy:', iy, 'ix:', ix 71 veclon[iid] = lonv[iy,ix] 72 veclat[iid] = latv[iy,ix] 73 74 if not gen.searchInlist(availProj, matProj): 75 print errormsg 76 print ' ' + fname + ": projection '" + matProj + "' not available !!" 77 print ' available ones:', availProj 64 78 quit(-1) 79 80 ncdims = onc.dimensions 81 82 if variable == 'all': 83 varns = ncvars 84 else: 85 varns = variable.split(',') 86 for vn in varns: 87 if not gen.searchInlist(ncvars, vn): 88 print errormsg 89 print ' ' + fname + ": file '" + ncfile + "' does not have " + \ 90 " variable '" + vn + "' !!" 91 print ' available ones:', ncvars 92 quit(-1) 93 94 if gen.searchInlist(olon.ncattrs(), 'units'): 95 xunits = olon.getncattr('units') 96 else: 97 xunits = '-' 98 if gen.searchInlist(olat.ncattrs(), 'units'): 99 yunits = olat.getncattr('units') 100 else: 101 yunits = '-' 102 103 if 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) 108 109 # Matrix values 110 if 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, \ 117 dmatx=dimx, dmaty=dimy, matproj=matProj, maxdiff=maxdiff) 118 119 matind = matindt.transpose() 120 121 # Creation of file 122 onewnc = NetCDFFile(ofilen, 'w') 123 124 # Dimensions 125 newdim = onewnc.createDimension('x', dimx) 126 newdim = onewnc.createDimension('y', dimy) 127 128 # Variable-dimension 129 newvar = onewnc.createVariable(varXpos, 'f8', ('y', 'x')) 130 newvar[:] = matXt.transpose() 131 ncvar.basicvardef(newvar, varXpos, varXpos, xunits) 132 133 newvar = onewnc.createVariable(varYpos, 'f8', ('y', 'x')) 134 newvar[:] = matYt.transpose() 135 ncvar.basicvardef(newvar, varYpos, varYpos, yunits) 136 onewnc.sync() 137 138 # Getting variables 139 for vn in varns: 140 if not onewnc.variables.has_key(vn): 141 ovar = onc.variables[vn] 142 indn = ovar.dimensions 143 vardims = [] 144 shapevar = [] 145 for dn in indn: 146 if not gen.searchInlist(onewnc.dimensions, dn) and dn != Xdimn and \ 147 dn != Ydimn: 148 if onc.dimensions[dn].isunlimited(): 149 newdim = nc.createDimension(dn, None) 150 else: 151 newdim = nc.createDimension(dn, len(onc.dimensions[dn])) 152 153 if dn == vecdim: 154 vardims.append('y') 155 vardims.append('x') 156 shapevar.append(dimy) 157 shapevar.append(dimx) 158 else: 159 vardims.append(dn) 160 shapevar.append(len(onc.dimensions[dn])) 161 162 if ovar.dtype == type(int(2)): 163 newvar= onewnc.createVariable(vn, nctype(ovar.dtype), tuple(vardims),\ 164 fill_value=gen.fillValueI) 165 varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueI 166 else: 167 newvar= onewnc.createVariable(vn, nctype(ovar.dtype), tuple(vardims),\ 168 fill_value=gen.fillValueF) 169 varvals = np.ones(tuple(shapevar), dtype=ovar.dtype)*gen.fillValueF 170 171 slices = gen.provide_slices(vardims, shapevar, ['x','y']) 172 # 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)] 185 186 # Attributes 187 for atn in ovar.ncattrs(): 188 if atn != 'fill_Value': 189 atv = ovar.getncattr(atn) 190 set_attribute(newvar, atn, atv) 191 onewnc.sync() 192 193 # Global attributes 194 for atn in onc.ncattrs(): 195 atv = ovar.getncattr(atn) 196 set_attribute(onewnc, atn, atv) 197 onewnc.sync() 198 add_global_PyNCplot(onewnc, pyscript, fname, '0.1') 199 200 onc.close() 201 onewnc.sync() 202 onewnc.close() 203 204 print fname + ": Successful writing of file 'reconstruct_matrix.nc' !!"
Note: See TracChangeset
for help on using the changeset viewer.