Changeset 1867 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Mar 26, 2018, 8:09:39 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1866 r1867 77 77 # get_1str_nc: Function to get 1 string value in a netCDF variable as a chain of 1char values 78 78 # get_attribute: Function to get an attribute from a netCDF file 79 # getvar_listonc: Function to get a variable object from a list of netCDF file object 79 80 # get_namelist_vars: Function to get namelist-like values ([varname] = [value]) 80 81 # get_str_nc: Function to get string values in a netCDF variable as a chains of 1char values … … 22866 22867 #quit() 22867 22868 22869 def getvar_listonc(varn, listoc): 22870 """ Function to get a variable object from a list of netCDF file object 22871 varn= name of the variable to be found 22872 listoc= list of NetCDF file objects 22873 """ 22874 fname = 'getvar_listonc' 22875 22876 allfs = {} 22877 fns = [] 22878 for ioc in range(len(listoc)): 22879 oc = listoc[ioc] 22880 allfs[oc.filepath] = [oc.dimensions, oc.variables.keys()] 22881 fns.append(oc.filepath) 22882 22883 # Looping for all files 22884 found = False 22885 for ioc in range(len(listoc)): 22886 oc = listoc[ioc] 22887 oncvars = allfs[ioc] 22888 oncvars.sort() 22889 if oc.variables.has_key(varn): 22890 ov = oc.variables[varn] 22891 found = True 22892 break 22893 22894 if not found: 22895 print errormsg 22896 print ' ' + fname + "': any netcdf file '" + allfs.keys() + "' contain " + \ 22897 "variable '" + varn + "' !!" 22898 print ' available ones _______' 22899 ioc = 0 22900 for fn in fns: 22901 print fn, ':', allfs[fn][1] 22902 listoc[ioc].close() 22903 ioc = ioc + 1 22904 quit(-1) 22905 22906 return ov 22907 22868 22908 def projection2D_information(PROJn, AXinf, projinf, dx, dy, oncs): 22869 22909 """ Function to get the basic information of the 2D-projection: new dimensions, variables, ... … … 22872 22912 AXinf: dictionary with the informatino of the axes 'X', 'Y', 'Z', 'T' 22873 22913 projinf: dictionary with all the information of the projection 22874 oncs= netCDF objects from which retrieve the information22914 oncs= list of netCDF objects from which retrieve the information 22875 22915 Returns: 22876 22916 newdim: dictionary new dimensions to be added {dimn1: dimlength1, ..., dimnN: dimlengthN} … … 22981 23021 newvar['rlon'] = ['grid_longitude', 'longitude in rotated pole grid', \ 22982 23022 'degrees'] 22983 found = Flase 22984 fileNs = '' 22985 for ionc in range(len(oncs)): 22986 onc = oncs[ionc] 22987 fileNs = fileNs + onc.filepath + ', ' 22988 if gen.searchInlist(onc.variables.keys(),projinf['fileXrefvals'][0]): 22989 oXvar = onc.variables[projinf['fileXvals'][0]] 22990 break 22991 if not found: 22992 print errormsg 22993 print ' ' + fname + ": files: " + fileNs + " do not have variable " + \ 22994 "with X ref values '" + projinf['fileXrefvals'][0] + "' !!" 22995 print ' available ones:', onc.variables.keys() 22996 quit(-1) 23023 23024 ovar = getvar_listonc(projinf['fileXvals'][0], oncs) 23025 oXvar = ovar[0] 22997 23026 if len(oXvar.shape) == 3: 22998 23027 newvarv['rlon'] = oXvar[0,0,:] … … 23014 23043 newdim['rlat'] = dy 23015 23044 newvar['rlat'] = ['grid_latitude', 'latitude in rotated pole grid', 'degrees'] 23016 if not gen.searchInlist(onc.variables.keys(),projinf['fileYrefvals'][0]): 23017 print errormsg 23018 print ' ' + fname + ": file does not have variable with Y ref values '"+\ 23019 projinf['fileYrefvals'][0] + "' !!" 23020 print ' available ones:', onc.variables.keys() 23021 quit(-1) 23022 oYvar = onc.variables[projinf['fileYvals'][0]] 23045 ovar = getvar_listonc(projinf['fileYvals'][0], oncs) 23046 oYvar = ovar[0] 23023 23047 if len(oYvar.shape) == 3: 23024 23048 newvarv['rlat'] = oYvar[0,0,:] … … 23411 23435 Varns = gen.str_list(variable, ',') 23412 23436 23437 # Joining all netCDF files 23438 allonc = exonc + [onc] 23439 allfileNs = {} 23440 for ionc in range(len(allonc)): 23441 onc = allonc[ionc] 23442 allfileNs[onc.filepath] = [onc.dimensions.keys(), onc.variables.keys()] 23443 23413 23444 # Creation of new file name 23414 23445 # This will done following CORDEX rules (getting information from the ASCII … … 23430 23461 axes = ['X', 'Y', 'Z', 'T'] 23431 23462 23432 oncvars = onc.variables.keys()23433 oncvars.sort()23434 23435 23463 # Axes information: dimensions and variables dependency 23436 23464 # CF-dimensions of a given axis … … 23440 23468 # CD-fimensions of the CF-variables for each axis 23441 23469 CFaxisvardimvals = {} 23470 23442 23471 for axn in axes: 23443 23472 axisv = axisinf[axn] … … 23447 23476 # Axis values from file 23448 23477 if vardimn != 'WRFtime': 23449 if not onc.variables.has_key(vardimn): 23450 print errormsg 23451 print ' '+fname+"': netcdf file '"+ ncfile + "' does not contain "+ \ 23452 "variable '" + vardimn + "' !!" 23453 print ' available ones:', oncvars 23454 onc.close() 23455 quit(-1) 23456 oaxisv = onc.variables[vardimn] 23478 oaxisv = getvar_listonc(vardimn, allloncs) 23457 23479 else: 23458 23480 print infmsg 23459 23481 print ' ' + fname + ": creation of variable 'time' from WRF 'Times' !!" 23460 odimvar = onc.variables['Times']23482 odimvar = getvar_listonc('Times', allloncs) 23461 23483 timewrfv = odimvar[:] 23462 23484 refT = gen.datetimeStr_conversion(gattr['basetime'], 'Y-m-dTH:M:SZ', \ … … 23577 23599 newDim, newVar, newVARv, nonCFproj = projection2D_information(gattr['grid'], \ 23578 23600 CFdimvals, pattr, filedimvals['X'].shape[1], filedimvals['X'].shape[0], \ 23579 exonc + [onc])23601 alloncs) 23580 23602 23581 23603 if len(newDim.keys()) > 0:
Note: See TracChangeset
for help on using the changeset viewer.