Changeset 1949 in lmdz_wrf
- Timestamp:
- Jul 22, 2018, 12:40:21 AM (7 years ago)
- Location:
- trunk/tools
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/create_OBSnetcdf.py
r1948 r1949 1025 1025 return newdvals 1026 1026 1027 def WMOcodevar(unitsn, onc, Lstrcode=1024): 1028 """ Function to add a variabe providing description of a units based on WMO code 1029 unitsn= name of the units (all WMO codes derived units must be labelled as 1030 'codeWMO_[num]' associated to a file valled wmo_[num].code) 1031 onc= netCDF object file to add the description 1032 Lstrcode= length of description of codes 1033 1034 wmo_[num].code must have the structure: ('#' for comments) 1035 reference:web page, document reference with the code 1036 short_description:main description of the code 1037 long_description:long description of the code 1038 codeTYPE:type of the code (andy of 'D', 'F', 'I', 'S') 1039 @: Values line giving the start of the values 1040 [val1]=[meaning of first value] 1041 (...) 1042 [valN]=[meaning of last value] 1043 """ 1044 fname = 'WMOcodevar' 1045 1046 # From http://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python 1047 folder = os.path.dirname(os.path.realpath(__file__)) 1048 1049 code = unitsn.split('_')[1] 1050 1051 infile = folder + '/wmo_' + code +'.code' 1052 1053 if not os.path.isfile(infile): 1054 print warnmsg 1055 print ' ' + fname + ": WMO code file '" + infile + "' does not exist !!" 1056 return 1057 # main expected values 1058 descvals = ['reference', 'short_description', 'long_description', 'codeTYPE', \ 1059 '@'] 1060 1061 availcodetype = ['D', 'F', 'I', 'S'] 1062 1063 codevalues = {} 1064 ocode = open(infile, 'r') 1065 inivals = False 1066 codvals = [] 1067 codmeanings = [] 1068 for line in ocode: 1069 if len(line) > 1 and line[0:1] != '#': 1070 linev = line.replace('\n','').replace('\t',' ').replace('\r','') 1071 print inivals, '<>', linev 1072 if not inivals: 1073 Sv = linev.split(':')[0] 1074 Vv = linev.split(':')[1] 1075 if searchInlist(descvals, Sv): 1076 if Sv != '@': codevalues[Sv] = Vv 1077 else: inivals = True 1078 else: 1079 Svv = linev.split('=')[0] 1080 Vvv = linev.split('=')[1] 1081 print 'Lluis Sv:', Svv, 'Vv:', Vvv 1082 codvals.append(Svv) 1083 codmeanings.append(Vvv) 1084 1085 # Creating variable 1086 if not searchInlist(onc.dimensions, 'Lstringcode'): 1087 print ' ' + fname + ": Adding string length dimension 'Lstringcode' for " + \ 1088 " code descriptions" 1089 newdim = onc.createDimension('Lstringcode', Lstrcode) 1090 print 'Lluis Svv:', Svv 1091 Ncodes = len(codvals) 1092 codedimn = 'wmo_code_' + str(code) 1093 if not searchInlist(onc.dimensions, codedimn): 1094 print ' ' + fname + ": Adding '" + codedimn + "' dimension for code " + \ 1095 "description" 1096 newdim = onc.createDimension(codedimn, Ncodes) 1097 onc.sync() 1098 1099 # Variable with the value of the code 1100 if not onc.variables.has_key(codedimn): 1101 if codevalues['codeTYPE'] == 'D': 1102 newvar = onc.createVariable(codedimn, 'f8', (codedimn)) 1103 for iv in range(Ncodes): newvar[iv] = np.float64(codvals[iv]) 1104 elif codevalues['codeTYPE'] == 'F': 1105 newvar = onc.createVariable(codedimn, 'f', (codedimn)) 1106 for iv in range(Ncodes): newvar[iv] = np.float(codvals[iv]) 1107 elif codevalues['codeTYPE'] == 'I': 1108 newvar = onc.createVariable(codedimn, 'i', (codedimn)) 1109 for iv in range(Ncodes): newvar[iv] = integer(codvals[iv]) 1110 elif codevalues['codeTYPE'] == 'S': 1111 newvar = onc.createVariable(codedimn, 'c', (codedimn, 'Lstringcode')) 1112 writing_str_nc(newvar, codvals, Lstrcode) 1113 else: 1114 print errormsg 1115 print ' ' + fname + ": codeTYPE '" + codevalues['codeTYPE'] + "' not" + \ 1116 " ready !!" 1117 print ' available ones:', availcodetype 1118 quit(-1) 1119 1120 for descv in descvals: 1121 if descv != '@' and descv != 'codeTYPE': 1122 newvar.setncattr(descv, codevalues[descv]) 1123 # Variable with the meaning of the code 1124 if not onc.variables.has_key(codedimn+'_meaning'): 1125 newvar = onc.createVariable(codedimn+'_meaning','c',(codedimn,'Lstringcode')) 1126 print 'Lluis len Vc:', len(Vvv), 'Ncodes:', Ncodes 1127 writing_str_nc(newvar, codmeanings, Lstrcode) 1128 newvar.setncattr('description', 'meaning of WMO code ' + str(code)) 1129 1130 onc.sync() 1131 1132 return 1133 1027 1134 ####### ###### ##### #### ### ## # 1028 1135 … … 1225 1332 basicvardef(newvar, varn, description['varLN'][ivar], \ 1226 1333 description['varU'][ivar]) 1227 writing_str_nc(newvar, datavalues[varn], StringLength) 1334 vals = datavalues[varn] 1335 for iv in range(Ntvalues): 1336 if vals[iv] is None: vals[iv] = fillValueS 1337 writing_str_nc(newvar, vals, StringLength) 1338 1339 # Getting new variables to describe certain units as codeWMO_[num] from an 1340 # external file 1341 if description['varU'][ivar][0:7] == 'codeWMO': 1342 WMOcodevar(description['varU'][ivar], objfile) 1228 1343 1229 1344 # Extra variable descriptions/attributes … … 1282 1397 basicvardef(newvar, timeCFname, 'time', tunits ) 1283 1398 else: 1284 if searchInlist(objfile.dimensions, 'time'): 1285 print warnmsg 1286 print ' ' + main + ": dimension 'time' already exist !!" 1287 print " renaming it as 'CFtime'" 1288 tdimn = 'CFtime' 1289 newdim = objfile.renameDimension('time','CFtime') 1290 else: 1291 tdmin = 'time' 1292 newdim = objfile.createDimension(tdimn,None) 1399 if not searchInlist(objfile.dimensions, 'time'): 1400 newdim = objfile.createDimension('time',None) 1293 1401 timeCFname = 'time' 1294 newvar = objfile.createVariable( timeCFname, 'f8', ( tdimn))1402 newvar = objfile.createVariable( timeCFname, 'f8', ('time')) 1295 1403 newvar[:] = np.zeros(timevals.shape[0]) 1296 1404 basicvardef(newvar, timeCFname, 'time', tunits ) … … 1300 1408 newvar[:] = timevals[0] 1301 1409 else: 1302 print 'Lluis shapes: newvar:', newvar.shape, 'timevals:', timevals.shape1303 1410 newvar[:] = timevals 1304 1411
Note: See TracChangeset
for help on using the changeset viewer.