- Timestamp:
- Jul 17, 2018, 9:22:53 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r1889 r1939 24 24 ## e.g. # nc_var.py -o splitfile_dim -S bottom_top:ZNU:QVAPOR_ZNU:.4f -v QVAPOR,XLONG,XLAT,Times -f ~/PY/wrfout_d01_1995-01-01_00:00:00 25 25 ## e.g. # nc_var.py -o itime -S WRFtime -f ../PY/wrfout_d01_1995-01-01_00:00:00 -v Times 26 ## e.g. # nc_var.py -o get_point -S 'XLONG:XLAT:Time|0' -f /home/lluis/PY/wrfout_d01_1995-01-01_00:00:00 -v -45.,-30. 27 ## e.g. # nc_var.py -o get_time -S '1422759600;seconds!since!1949-12-01!00:00:00' -f /home/lluis/PY/wrfout_d01_1995-01-01_00:00:00 -v WRFtime 26 28 27 29 ## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1 … … 107 109 # gaddattr: Add a global attribute to a netCDF. Removes previous attribute if it exist 108 110 # get_attribute: Function to get an attribute from a netCDF file 111 # get_point: Function to provide the closest grid point to a given lat,lon 112 # get_time: Function to provide the closest time-step to a given time 109 113 # get_namelist_vars: Function to get namelist-like values ([varname] = [value]) 110 114 # get_Variables: Function to get a list of variables from an existing file … … 196 200 'file_oper_alongdims', 'filter_2dim', \ 197 201 'flipdim', 'fvaradd', 'gaddattrk', 'gaddattr', 'get_attribute', \ 198 'get_ namelist_vars', 'get_Variables',\202 'get_point', 'get_time', 'get_namelist_vars', 'get_Variables', \ 199 203 'getvalues_lonlat', 'getvars_tofile', 'grattr', \ 200 204 'grmattr', 'idims', 'ifile', 'igattrs', 'increaseDimvar', 'isgattrs', \ … … 374 378 elif oper == 'get_namelist_vars': 375 379 ncvar.get_namelist_vars(opts.values, opts.ncfile) 380 elif oper == 'get_point': 381 ncvar.get_point(opts.values, opts.ncfile, opts.varname) 382 elif oper == 'get_time': 383 ncvar.get_time(opts.values, opts.ncfile, opts.varname) 376 384 elif oper == 'get_Variables': 377 385 ncvar.get_Variables(opts.values, opts.ncfile, opts.varname) -
trunk/tools/nc_var_tools.py
r1929 r1939 82 82 # getdim_listonc: Function to get a dimension object from a list of netCDF file object 83 83 # getvar_listonc: Function to get a variable object from a list of netCDF file object 84 # get_time: Function to provide the closest time-step to a given time 85 # get_point: Function to provide the closest grid point to a given lat,lon 84 86 # get_namelist_vars: Function to get namelist-like values ([varname] = [value]) 85 87 # get_str_nc: Function to get string values in a netCDF variable as a chains of 1char values … … 106 108 # maskvar: Function to mask a variable using a mask. It is assumed that mask[...,dimM,dimJ,dimK] and var[...,dimM,dimJ,dimK] share the last dimensions 107 109 # merge_files: Function to merge variables from two files 110 # mindist: Function to proivde the minimum distance toa pair of lon,lat 108 111 # ModelChar: Class object to determine major characterisitcs of a given model output 109 112 # model_characteristics: Functino to provide major characterisitcs of a given model output … … 24433 24436 #itime('WRFtime', '../PY/wrfout_d01_1995-01-01_00:00:00', 'Times') 24434 24437 24438 def mindist(lonv,latv,lonvalue,latvalue): 24439 """ Function to proivde the minimum distance toa pair of lon,lat 24440 lonv: matrix with longitudes 24441 latv: matrix with longitudes 24442 lonvalue: longitude to look for 24443 latvalue: latitude to look for 24444 """ 24445 fname = 'mindist' 24446 24447 if len(lonv.shape) == 1: 24448 distlon = np.abs(lonv - lonvalue) 24449 distlat = np.abs(latv - latvalue) 24450 24451 mindifflon = np.min(distlon) 24452 mindifflat = np.min(distlat) 24453 ix = gen.index_vec(distlon, mindistlon) 24454 iy = gen.index_vec(distlat, mindistlat) 24455 else: 24456 dist = np.sqrt((lonv - lonvalue)**2 + (latv - latvalue)**2) 24457 mindist = np.min(dist) 24458 [iy, ix] = gen.index_mat(dist, mindist) 24459 24460 return [ix, iy] 24461 24462 def get_point(values, ncfile, variable): 24463 """ Function to provide the closest grid point to a given lat,lon 24464 values=[lonvarn]:[latvarn]:[dimvals] 24465 [lonvarn]: name of the variable with the longitudes 24466 [latvarn]: name of the variable with the latitudes 24467 [dimvals]: ',' list of [dimname]|[value] telling at which dimension of the 24468 variable a given value is required: 24469 * [integer]: which value of the dimension 24470 * -1: all along the dimension 24471 * -9: last value of the dimension 24472 * [beg]@[end]@[inc] slice from [beg] to [end] every [inc] 24473 * NOTE, no dim name all the dimension size 24474 ncfile= file from which provide the information 24475 variable= [lonval],[latval] longitude and latitude values to look for 24476 """ 24477 fname = 'get_point' 24478 24479 if values == 'h': 24480 print fname + '_____________________________________________________________' 24481 print get_point.__doc__ 24482 quit() 24483 24484 expectargs = '[lonvarn]:[latvarn]:[dimvals]' 24485 gen.check_arguments(fname, values, expectargs, ':') 24486 24487 lonvarn = values.split(':')[0] 24488 latvarn = values.split(':')[1] 24489 dimvals = values.split(':')[2].replace('|',':') 24490 24491 [lonvalue, latvalue] = gen.str_list_k(variable, ',', 'R') 24492 24493 onc = NetCDFFile(ncfile, 'r') 24494 24495 if not onc.variables.has_key(lonvarn): 24496 print errormsg 24497 print ' ' + fname + ": file '" + ncfile + "' does not have longitude " + \ 24498 "varible '" + lonvarn + "' !!" 24499 print ' available ones:', onc.variables.keys() 24500 quit(-1) 24501 if not onc.variables.has_key(latvarn): 24502 print errormsg 24503 print ' ' + fname + ": file '" + ncfile + "' does not have latitude " + \ 24504 "varible '" + latvarn + "' !!" 24505 print ' available ones:', onc.variables.keys() 24506 quit(-1) 24507 24508 olon = onc.variables[lonvarn] 24509 olat = onc.variables[latvarn] 24510 24511 lonv, londims = slice_variable(olon, dimvals.replace(',','|')) 24512 latv, latdims = slice_variable(olat, dimvals.replace(',','|')) 24513 24514 if len(lonv.shape) > 2: 24515 print errormsg 24516 print ' ' + fname + ': wrong shape for longitudes. It must be <= 2D!!' 24517 print ' provided shape after slice:', lonv.shape, 'slice', dimvals 24518 quit(-1) 24519 if len(latv.shape) > 2: 24520 print errormsg 24521 print ' ' + fname + ': wrong shape for latitudes. It must be <= 2D!!' 24522 print ' provided shape after slice:', latv.shape, 'slice', dimvals 24523 quit(-1) 24524 24525 onc.close() 24526 # looking for minimum distance 24527 gridpoint = mindist(lonv,latv,lonvalue,latvalue) 24528 24529 print str(gridpoint[0])+','+str(gridpoint[1]) 24530 24531 return 24532 24533 #print get_point('XLONG:XLAT:Time|0', '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', '-45.,-30.') 24534 24535 def get_time(values, ncfile, variable): 24536 """ Function to provide the closest time-step to a given time 24537 values= [timevalue];[units] 24538 [timevalue]: CF value of time to look for 24539 [units]: CF units of time [Tunit] since [date] ('!' for spaces) 24540 ncfile= file from which provide the information 24541 variable= [timevarn] name of the variable with the CF-times ('WRFtime' for WRF 24542 Times variable) 24543 """ 24544 fname = 'get_time' 24545 24546 if values == 'h': 24547 print fname + '_____________________________________________________________' 24548 print get_time.__doc__ 24549 quit() 24550 24551 expectargs = '[timevalue];[units]' 24552 gen.check_arguments(fname, values, expectargs, ';') 24553 24554 timevalue = np.float(values.split(';')[0]) 24555 uTunits = values.split(';')[1].replace('!',' ') 24556 24557 onc = NetCDFFile(ncfile, 'r') 24558 24559 if not variable == 'WRFtime' and not onc.variables.has_key(variable): 24560 print errormsg 24561 print ' ' + fname + ": file '" + ncfile + "' does not have time " + \ 24562 "varible '" + variable + "' !!" 24563 print ' available ones:', onc.variables.keys() 24564 quit(-1) 24565 24566 if variable == 'WRFtime': 24567 otime = onc.variables['Times'] 24568 timev0, timeu = compute_WRFtime(otime[:]) 24569 else: 24570 otime = onc.variables[variable] 24571 timev0 = otime[:] 24572 timeu = otime.units 24573 24574 onc.close() 24575 24576 # Referring to the same units 24577 reftvals = gen.coincident_CFtimes(timev0, uTunits, timeu) 24578 24579 # looking for minimum distance 24580 dist = np.abs(reftvals - timevalue) 24581 mindist = np.min(dist) 24582 it = gen.index_vec(dist, mindist) 24583 24584 print it 24585 24586 return 24587 24588 #get_time('1422759600;seconds since 1949-12-01 00:00:00', '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'WRFtime') 24589 24435 24590 #quit()
Note: See TracChangeset
for help on using the changeset viewer.