Changeset 574 in lmdz_wrf for trunk/tools
- Timestamp:
- Jul 6, 2015, 5:32:42 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r570 r574 16402 16402 return newstr 16403 16403 16404 #quit() 16404 def getvalues_lonlat(values, ncfile): 16405 """ Function to retrieve the values from the closest grid point to a set of longitude, latitude values 16406 values=[longitude]:[latitude]:[dnames]:[vdnames] 16407 [longitude]: value for the longitude 16408 [latitude]: value for the latitude 16409 [dnames]: [xdimname],[ydimname] names of the dimensions on the 'x' and 'y' axis 16410 [dvnames]: [xvdimname],[yvdimname] names of the variables with the values for 16411 the dimensions on the 'x' and 'y' axis 16412 ncfile= netcdf file to use 16413 """ 16414 import subprocess as sub 16415 fname = 'getvalues_lonlat' 16416 16417 ofile = fname + '.nc' 16418 16419 if values == 'h': 16420 print fname + '_____________________________________________________________' 16421 print getvalues_lonlat.__doc__ 16422 quit() 16423 16424 arguments = '[longitude]:[latitude]:[dnames]:[vdnames]' 16425 check_arguments(fname, len(values.split(':')), arguments, ':', \ 16426 len(arguments.split(','))) 16427 16428 longitude = np.float(values.split(':')[0]) 16429 latitude = np.float(values.split(':')[1]) 16430 dnames = values.split(':')[2].split(',') 16431 dvnames = values.split(':')[3].split(',') 16432 16433 onc = NetCDFFile(ncfile, 'r') 16434 16435 for dn in dnames: 16436 if not searchInlist(onc.dimensions, dn): 16437 print errormsg 16438 print ' ' + fname + ": file '" + ncfile + "' does not have dimension '"+\ 16439 dn + "' !!" 16440 quit(-1) 16441 16442 for vdn in dvnames: 16443 if not searchInlist(onc.variables, vdn): 16444 print errormsg 16445 print ' ' + fname + ": file '" + ncfile + "' does not have variable '"+ \ 16446 vdn + "' !!" 16447 quit(-1) 16448 16449 # Getting grid point localization 16450 ## 16451 lon2d, lat2d = lonlat2D(onc.variables[dvnames[0]], onc.variables[dvnames[1]]) 16452 16453 dimx = lon2d.shape[1] 16454 dimy = lon2d.shape[0] 16455 16456 mappairs = np.ones((2), dtype=int) 16457 dist=np.sqrt((longitude - lon2d)**2. + (latitude - lat2d)**2.) 16458 mindist = np.min(dist) 16459 minydist, minxdist = index_mat(dist,mindist) 16460 mappairs[:] = [minydist, minxdist] 16461 16462 print ' ' + fname + ': nearest point: ',mappairs, 'distance:', mindist 16463 16464 onc.close() 16465 16466 vals = dnames[1] + ',' + str(mappairs[1]) + ',' + str(mappairs[1]) + ',1@' 16467 vals = vals + dnames[0] + ',' + str(mappairs[0]) + ',' + str(mappairs[0]) + ',1' 16468 16469 DataSetSection_multidims(vals, ncfile) 16470 of = ncfile.split('.')[0] + '_' + dnames[1] + '_B' + str(mappairs[1]) + '-E' + \ 16471 str(mappairs[1]) + '-I1_' + dnames[0] + '_B' + str(mappairs[0]) + '-E' + \ 16472 str(mappairs[0]) + '-I1.nc' 16473 16474 sub.call(['mv',of,ofile]) 16475 16476 onc = NetCDFFile(ofile, 'a') 16477 16478 newdim = onc.createDimension('lLd',1) 16479 newvar = onc.createVariable('lon_point','f',('lLd')) 16480 newattr = basicvardef(newvar,'lon_point','longitude of selection of the values', \ 16481 'degrees_East') 16482 newvar[:] = longitude 16483 16484 newvar = onc.createVariable('lat_point','f',('lLd')) 16485 newattr = basicvardef(newvar,'lat_point','latitude of selection of the values', \ 16486 'degrees_North') 16487 newvar[:] = latitude 16488 16489 newvar = onc.createVariable('flon_point','f',('lLd')) 16490 newattr = basicvardef(newvar,'flon_point','closest longitude of selection of ' + \ 16491 'the values', 'degrees_East') 16492 newvar[:] = lon2d[tuple(mappairs)] 16493 16494 newvar = onc.createVariable('flat_point','f',('lLd')) 16495 newattr = basicvardef(newvar,'flat_point','closest latitude of selection of ' + \ 16496 'the values', 'degrees_North') 16497 newvar[:] = lat2d[tuple(mappairs)] 16498 16499 newvar = onc.createVariable('fdist_point','f',('lLd')) 16500 newattr = basicvardef(newvar,'fdist_point','distance to the point of selection '+\ 16501 'of the values', 'degrees') 16502 newvar[:] = mindist 16503 16504 onc.sync() 16505 onc.close() 16506 16507 print fname + ": successful writing of file '" + ofile + "' !!" 16508 16509 return 16510 16511 vals='20.5:36.0:west_east,south_north:XLONG_M,XLAT_M' 16512 ncf='/home/lluis/etudes/domains/medic051215_2km/geo_em.d01.nc' 16513 16514 getvalues_lonlat(vals, ncf) 16515 quit() 16405 16516 16406 16517 """operation to make:
Note: See TracChangeset
for help on using the changeset viewer.