Changeset 736 in lmdz_wrf
- Timestamp:
- May 2, 2016, 4:12:16 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r735 r736 9284 9284 [lonNE],[latNE]: longitude and latitude of the NE vertex of the box 9285 9285 ncfile= netCDF file 9286 varn= name of the variable9286 varn= ',' list of names of the variables ('all', for all variables) 9287 9287 """ 9288 9288 fname = 'sellonlatbox' … … 9310 9310 latv = latobj[:] 9311 9311 9312 if not searchInlist(objfile.variables, varn): 9313 print errormsg 9314 print ' ' + fname + ': variable name "' + varn + '" is not in file "' + \ 9315 ncfile + '" !!!!!' 9316 quit(-1) 9317 9318 varobj = objfile.variables[varn] 9319 9320 Ndimslon = len(lonobj.shape) 9321 Ndimslat = len(latobj.shape) 9322 Ndimsvar = len(varobj.shape) 9312 if varn == 'all': 9313 varns = objfile.variables 9314 elif varn.find(',') != -1: 9315 varns = varn.split(',') 9316 else: 9317 varns = [varn] 9318 9319 for vn in varns: 9320 if not searchInlist(objfile.variables, vn): 9321 print errormsg 9322 print ' ' + fname + ': variable name "' + vn + '" is not in file "' + \ 9323 ncfile + '" !!!!!' 9324 quit(-1) 9325 9326 varobj = objfile.variables[vn] 9327 9328 Ndimslon = len(lonobj.shape) 9329 Ndimslat = len(latobj.shape) 9330 Ndimsvar = len(varobj.shape) 9323 9331 9324 9332 # Looking for coincidence of dimensions 9325 samedim = [] 9326 for idv in range(Ndimsvar): 9327 for idl in range(Ndimslon): 9328 if varobj.dimensions[idv] == lonobj.dimensions[idl]: 9329 if not searchInlist(samedim,varobj.dimensions[idv]): 9330 samedim.append(varobj.dimensions[idv]) 9333 samedim = [] 9334 for idv in range(Ndimsvar): 9335 for idl in range(Ndimslon): 9336 if varobj.dimensions[idv] == lonobj.dimensions[idl]: 9337 if not searchInlist(samedim,varobj.dimensions[idv]): 9338 samedim.append(varobj.dimensions[idv]) 9339 break 9340 for idl in range(Ndimslat): 9341 if varobj.dimensions[idv] == latobj.dimensions[idl]: 9342 if not searchInlist(samedim,varobj.dimensions[idv]): 9343 samedim.append(varobj.dimensions[idv]) 9344 break 9345 9346 Ndimshare = len(samedim) 9347 print 'variable and lon/lat matrices share ', Ndimshare,' dimensions: ',samedim 9348 9349 samedimslonlat = True 9350 for idL in range(Ndimslon): 9351 for idl in range(Ndimslat): 9352 if lonobj.dimensions[idl] != latobj.dimensions[idl]: 9353 samedimslonlat = False 9331 9354 break 9332 for idl in range(Ndimslat): 9333 if varobj.dimensions[idv] == latobj.dimensions[idl]: 9334 if not searchInlist(samedim,varobj.dimensions[idv]): 9335 samedim.append(varobj.dimensions[idv]) 9336 break 9337 9338 Ndimshare = len(samedim) 9339 print 'variable and lon/lat matrices share ', Ndimshare,' dimensions: ',samedim 9340 9341 samedimslonlat = True 9342 for idL in range(Ndimslon): 9343 for idl in range(Ndimslat): 9344 if lonobj.dimensions[idl] != latobj.dimensions[idl]: 9345 samedimslonlat = False 9346 break 9347 9348 if not samedimslonlat: break 9355 9356 if not samedimslonlat: break 9349 9357 9350 9358 # Creation of the lon/lat matrices to search 9351 if Ndimshare == 1: 9352 lonmat = lonobj[:] 9353 latmat = latobj[:] 9354 elif Ndimshare == 2: 9355 if samedimslonlat: 9359 if Ndimshare == 1: 9356 9360 lonmat = lonobj[:] 9357 9361 latmat = latobj[:] 9362 elif Ndimshare == 2: 9363 if samedimslonlat: 9364 lonmat = lonobj[:] 9365 latmat = latobj[:] 9366 else: 9367 if Ndimslon != 1 and Ndimslat != 1: 9368 print errormsg 9369 print ' ' + fname + ': I do not know how to keep going!' 9370 print ' lon ', Ndimslon,' and lat ',Ndimslat, \ 9371 ' matrices do not share the same dimensions, and do not ' + \ 9372 'have shape 1' 9373 quit(-1) 9374 else: 9375 lonmat = np.zeros((latobj.shape[0], lonobj.shape[0]), dtype=np.float) 9376 latmat = np.zeros((latobj.shape[0], lonobj.shape[0]), dtype=np.float) 9377 9378 for i in range(lonobj.shape[0]): 9379 latmat[:,i] = latobj[:] 9380 for j in range(latobj.shape[0]): 9381 lonmat[j,:] = lonobj[:] 9382 elif Ndimshare == 3: 9383 if samedimslonlat: 9384 lonmat = lonobj[0,:,:] 9385 latmat = latobj[0,:,:] 9358 9386 else: 9359 if Ndimslon != 1 and Ndimslat != 1: 9360 print errormsg 9361 print ' ' + fname + ': I do not know how to keep going!' 9362 print ' lon ', Ndimslon,' and lat ',Ndimslat, \ 9363 ' matrices do not share the same dimensions, and do not have shape 1' 9364 quit(-1) 9387 print errormsg 9388 print ' ' + fname + ': dimension sharing of lon/lat not ready!' 9389 quit(-1) 9390 9391 # Searching for inside points 9392 iind = 0 9393 if Ndimshare == 1: 9394 inside = {} 9395 for iL in range(lonobj.shape[0]): 9396 if lonobj[iL] >= lonSW and lonobj[iL] <= lonNE and latobj[iL] >= latSW\ 9397 and latobj[iL] <= latNE: 9398 inside[iind] = iL 9399 iind = iind + 1 9400 elif Ndimshare == 2: 9401 newidx = [] 9402 newidy = [] 9403 if (len(lonobj.shape) == 3): 9404 inside = np.zeros((lonobj.shape[1], lonobj.shape[2]), dtype=bool) 9365 9405 else: 9366 lonmat = np.zeros((latobj.shape[0], lonobj.shape[0]), dtype=np.float) 9367 latmat = np.zeros((latobj.shape[0], lonobj.shape[0]), dtype=np.float) 9368 9369 for i in range(lonobj.shape[0]): 9370 latmat[:,i] = latobj[:] 9371 for j in range(latobj.shape[0]): 9372 lonmat[j,:] = lonobj[:] 9373 elif Ndimshare == 3: 9374 if samedimslonlat: 9375 lonmat = lonobj[0,:,:] 9376 latmat = latobj[0,:,:] 9377 else: 9378 print errormsg 9379 print ' ' + fname + ': dimension sharing of lon/lat not ready!' 9380 quit(-1) 9381 9382 # Searching for inside points 9383 iind = 0 9384 if Ndimshare == 1: 9385 inside = {} 9386 for iL in range(lonobj.shape[0]): 9387 if lonobj[iL] >= lonSW and lonobj[iL] <= lonNE and latobj[iL] >= latSW \ 9388 and latobj[iL] <= latNE: 9389 inside[iind] = iL 9390 iind = iind + 1 9391 elif Ndimshare == 2: 9392 newidx = [] 9393 newidy = [] 9394 if (len(lonobj.shape) == 3): 9406 inside = np.zeros((lonobj.shape), dtype=bool) 9407 9408 for iL in range(lonobj.shape[1]): 9409 for il in range(lonobj.shape[0]): 9410 if lonobj[il,iL] >= lonSW and lonobj[il,iL] <= lonNE and \ 9411 latobj[il,iL] >= latSW and latobj[il,iL] <= latNE: 9412 if not searchInlist(newidx, iL): newidx.append(iL) 9413 if not searchInlist(newidy, il): newidy.append(il) 9414 inside[il, iL] = True 9415 iind = iind + 1 9416 elif Ndimshare == 3: 9417 newidx = [] 9418 newidy = [] 9395 9419 inside = np.zeros((lonobj.shape[1], lonobj.shape[2]), dtype=bool) 9396 else: 9397 inside = np.zeros((lonobj.shape), dtype=bool) 9398 9399 for iL in range(lonobj.shape[1]): 9400 for il in range(lonobj.shape[0]): 9401 if lonobj[il,iL] >= lonSW and lonobj[il,iL] <= lonNE and \ 9402 latobj[il,iL] >= latSW and latobj[il,iL] <= latNE: 9403 if not searchInlist(newidx, iL): newidx.append(iL) 9404 if not searchInlist(newidy, il): newidy.append(il) 9405 inside[il, iL] = True 9406 iind = iind + 1 9407 elif Ndimshare == 3: 9408 newidx = [] 9409 newidy = [] 9410 inside = np.zeros((lonobj.shape[1], lonobj.shape[2]), dtype=bool) 9411 9412 for iL in range(lonobj.shape[2]): 9413 for il in range(lonobj.shape[1]): 9414 if lonobj[0,il,iL] >= lonSW and lonobj[0,il,iL] <= lonNE and \ 9415 latobj[0,il,iL] >= latSW and latobj[0,il,iL] <= latNE: 9416 if not searchInlist(newidx, iL): newidx.append(iL) 9417 if not searchInlist(newidy, il): newidy.append(il) 9418 inside[il, iL] = True 9419 iind = iind + 1 9420 9421 Ninpts = len(inside) 9422 newdx = len(newidx) 9423 newdy = len(newidy) 9424 9425 print ' ' + fname +': ',Ninpts,' pts found in the box: ',lonSW,',',latSW,' x ', \ 9426 lonNE,',',latNE 9420 9421 for iL in range(lonobj.shape[2]): 9422 for il in range(lonobj.shape[1]): 9423 if lonobj[0,il,iL] >= lonSW and lonobj[0,il,iL] <= lonNE and \ 9424 latobj[0,il,iL] >= latSW and latobj[0,il,iL] <= latNE: 9425 if not searchInlist(newidx, iL): newidx.append(iL) 9426 if not searchInlist(newidy, il): newidy.append(il) 9427 inside[il, iL] = True 9428 iind = iind + 1 9429 9430 Ninpts = len(inside) 9431 newdx = len(newidx) 9432 newdy = len(newidy) 9433 9434 print ' ' + fname + ': ', Ninpts, 'pts found in the box:', lonSW, ',', \ 9435 latSW, 'x', lonNE, ',', latNE 9427 9436 9428 9437 # Creation of cropped matrices 9429 inlon = np.zeros((Ninpts), dtype=np.float)9430 inlat = np.zeros((Ninpts), dtype=np.float)9431 invar = np.zeros((varobj.shape[0],Ninpts), dtype=np.float)9438 inlon = np.zeros((Ninpts), dtype=np.float) 9439 inlat = np.zeros((Ninpts), dtype=np.float) 9440 invar = np.zeros((varobj.shape[0],Ninpts), dtype=np.float) 9432 9441 9433 9442 # Creation of the netCDF file 9434 9443 ## 9435 objofile = NetCDFFile(ofile, 'w') 9436 9437 if Ndimshare == 1: 9444 if vn in varns[0]: 9445 objofile = NetCDFFile(ofile, 'w') 9446 9447 if Ndimshare == 1: 9438 9448 9439 9449 # Dimensions 9440 newdim = objofile.createDimension('boxpt', Ninpts)9441 newdim = objofile.createDimension('time', None)9450 newdim = objofile.createDimension('boxpt', Ninpts) 9451 newdim = objofile.createDimension('time', None) 9442 9452 9443 9453 # var dimensions 9444 newvar = objofile.createVariable('lon', 'f8', ('boxpt')) 9445 newattr = basicvardef(newvar, 'lon', 'longitude', 'degrees west_east') 9446 newvar[:] = lonobj[inside[iin]] 9447 9448 newvar = objofile.createVariable('lat', 'f8', ('boxpt')) 9449 newattr = basicvardef(newvar, 'lat', 'latitude', 'degrees north_south') 9450 newvar[:] = latobj[inside[iin]] 9451 9452 newvar = objofile.createVariable('time', 'f8', ('time')) 9453 if objfile.variables.has_key('time'): 9454 otime = objfile.variables['time'] 9455 timevals = otime[:] 9456 if searchInlist(otime.ncattrs(),'units'): 9457 tunits = otime.getncattr['units'] 9454 newvar = objofile.createVariable('lon', 'f8', ('boxpt')) 9455 newattr = basicvardef(newvar, 'lon', 'longitude', 'degrees west_east') 9456 newvar[:] = lonobj[inside[iin]] 9457 9458 newvar = objofile.createVariable('lat', 'f8', ('boxpt')) 9459 newattr = basicvardef(newvar, 'lat', 'latitude', 'degrees north_south') 9460 newvar[:] = latobj[inside[iin]] 9461 9462 newvar = objofile.createVariable('time', 'f8', ('time')) 9463 if objfile.variables.has_key('time'): 9464 otime = objfile.variables['time'] 9465 timevals = otime[:] 9466 if searchInlist(otime.ncattrs(),'units'): 9467 tunits = otime.getncattr['units'] 9468 else: 9469 tunits = 'unkown' 9470 else: 9471 timevals = np.arange(varobj.shape[0]) 9472 tunits = 'unkown' 9473 9474 newattr = basicvardef(newvar, 'time', 'time', tunits) 9475 9476 newvar[:] = timevals 9477 9478 # variable 9479 newvar = objofile.createVariable(varn, 'f4', ('time', 'boxpt')) 9480 newvar[:] = varobj[invar] 9458 9481 else: 9459 tunits = 'unkown'9460 else:9461 timevals = np.arange(varobj.shape[0])9462 tunits = 'unkown'9463 9464 newattr = basicvardef(newvar, 'time', 'time', tunits)9465 9466 newvar[:] = timevals9467 9468 # variable9469 newvar = objofile.createVariable(varn, 'f4', ('time', 'boxpt'))9470 newvar[:] = varobj[invar]9471 else:9472 9482 9473 9483 # Dimensions 9474 newdim = objofile.createDimension('x', newdx)9475 newdim = objofile.createDimension('y', newdy)9476 newdim = objofile.createDimension('time', None)9484 newdim = objofile.createDimension('x', newdx) 9485 newdim = objofile.createDimension('y', newdy) 9486 newdim = objofile.createDimension('time', None) 9477 9487 9478 9488 # var dimensions 9479 newvar = objofile.createVariable('lon', 'f8', ('y', 'x'))9480 if Ndimshare == 2:9481 Vals = lonobj[:]9482 else:9483 Vals = lonobj[0,:,:]9484 9485 for it in range(varobj.shape[0]):9486 newvar[:] = Vals[inside].reshape(newdy,newdx)9487 9488 newattr = basicvardef(newvar, 'lon', 'longitude', 'degrees west_east')9489 9490 newvar = objofile.createVariable('lat', 'f8', ('y', 'x'))9491 newattr = basicvardef(newvar, 'lat', 'latitude', 'degrees north_south')9492 if Ndimshare == 2:9493 Vals = latobj[:]9494 else:9495 Vals = latobj[0,:,:]9496 9497 9498 for it in range(varobj.shape[0]):9499 newvar[:] = Vals[inside].reshape(newdy,newdx)9500 9501 newvar = objofile.createVariable('time', 'f8', ('time'))9502 if objfile.variables.has_key('time'):9503 otime = objfile.variables['time']9504 timevals = otime[:]9505 if searchInlist(otime.ncattrs(),'units'):9506 tunits = otime.getncattr['units']9507 else:9508 tunits = 'unkown'9509 else:9510 timevals = np.arange(varobj.shape[0])9511 tunits = 'unkown'9512 9513 newattr = basicvardef(newvar, 'time', 'time', tunits)9514 9515 newvar[:] = timevals9489 newvar = objofile.createVariable('lon', 'f8', ('y', 'x')) 9490 if Ndimshare == 2: 9491 Vals = lonobj[:] 9492 else: 9493 Vals = lonobj[0,:,:] 9494 9495 for it in range(varobj.shape[0]): 9496 newvar[:] = Vals[inside].reshape(newdy,newdx) 9497 9498 newattr = basicvardef(newvar, 'lon', 'longitude', 'degrees west_east') 9499 9500 newvar = objofile.createVariable('lat', 'f8', ('y', 'x')) 9501 newattr = basicvardef(newvar, 'lat', 'latitude', 'degrees north_south') 9502 if Ndimshare == 2: 9503 Vals = latobj[:] 9504 else: 9505 Vals = latobj[0,:,:] 9506 9507 9508 for it in range(varobj.shape[0]): 9509 newvar[:] = Vals[inside].reshape(newdy,newdx) 9510 9511 newvar = objofile.createVariable('time', 'f8', ('time')) 9512 if objfile.variables.has_key('time'): 9513 otime = objfile.variables['time'] 9514 timevals = otime[:] 9515 if searchInlist(otime.ncattrs(),'units'): 9516 tunits = otime.getncattr['units'] 9517 else: 9518 tunits = 'unkown' 9519 else: 9520 timevals = np.arange(varobj.shape[0]) 9521 tunits = 'unkown' 9522 9523 newattr = basicvardef(newvar, 'time', 'time', tunits) 9524 9525 newvar[:] = timevals 9516 9526 9517 9527 # variable … … 9521 9531 newvar[it,:,:] = valsvar[inside] 9522 9532 9523 if searchInlist(varobj.ncattrs(),'standard_name'):9524 vsname = varobj.getncattr('standard_name')9525 else:9526 vsname = varn9527 if searchInlist(varobj.ncattrs(),'long_name'):9528 vlname = varobj.getncattr('long_name')9529 else:9530 vlname = varn9531 if searchInlist(varobj.ncattrs(),'units'):9532 vunits = varobj.getncattr('units')9533 else:9534 vunits = 'unkown'9535 9536 newattr = basicvardef(newvar, vsname, vlname, vunits)9533 if searchInlist(varobj.ncattrs(),'standard_name'): 9534 vsname = varobj.getncattr('standard_name') 9535 else: 9536 vsname = varn 9537 if searchInlist(varobj.ncattrs(),'long_name'): 9538 vlname = varobj.getncattr('long_name') 9539 else: 9540 vlname = varn 9541 if searchInlist(varobj.ncattrs(),'units'): 9542 vunits = varobj.getncattr('units') 9543 else: 9544 vunits = 'unkown' 9545 9546 newattr = basicvardef(newvar, vsname, vlname, vunits) 9537 9547 9538 9548 # global attributes … … 20132 20142 # fin.module_forinterpolate.coarseinterpolate(projlon, projlat, lonvs, \ 20133 20143 # latvs, percen, mindiff, ivar, dimx, dimy, ninpts) 20144 Ninpts=499999 20134 20145 for ir in range(ptsf[0],Ninpts,fracs): 20135 20146 iri = ir … … 20150 20161 lonvss, latvss, np.float64(mindiff), ovars) 20151 20162 newnc.sync() 20163 print newvar 20164 print 'newvar:',type(newvar), newvar.shape, np.min(newvar), np.max(newvar) 20165 print 'newvarin:',type(newvarin), newvarin.shape, np.min(newvarin), np.max(newvarin) 20166 newnc.close() 20167 for i in range(iri,ire,1): 20168 print lonvss[i], latvss[i], ovars[i] 20169 20170 quit() 20152 20171 20153 20172 elif kind == 'lonlat':
Note: See TracChangeset
for help on using the changeset viewer.