Changeset 1952 in lmdz_wrf
- Timestamp:
- Jul 23, 2018, 2:55:23 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/create_OBSnetcdf.py
r1951 r1952 168 168 return ncvar 169 169 170 def set_attributek(ncv, attrname, attrval, attrkind): 171 """ Sets a value of an attribute of a netCDF variable with a kind. Removes previous attribute value if exists 172 ncvar = object netcdf variable 173 attrname = name of the attribute 174 attrvalue = value of the attribute 175 atrtrkind = kind of attribute: 'S', string ('!' as spaces); 'U', unicode ('!' as spaces); 'I', integer; 176 'Inp32', numpy integer 32; 'R', ot 'F' real; ' npfloat', np.float; 'D', np.float64 177 """ 178 fname = 'set_attributek' 179 validk = {'S': 'string', 'U': 'unicode', 'I': 'integer', \ 180 'Inp32': 'integer long (np.int32)', 'F': 'float', 'R': 'float', \ 181 'npfloat': 'np.float', 'D': 'float long (np.float64)'} 182 183 if type(attrkind) == type('s'): 184 if attrkind == 'S': 185 attrvalue = str(attrval.replace('!', ' ')) 186 elif attrkind == 'U': 187 attrvalue = unicode(attrval.replace('!',' ')) 188 elif attrkind == 'I': 189 attrvalue = np.int(attrval) 190 elif attrkind == 'R' or attrkind == 'F' : 191 attrvalue = float(attrval) 192 elif attrkind == 'npfloat': 193 attrvalue = np.float(attrval) 194 elif attrkind == 'D': 195 attrvalue = np.float64(attrval) 196 else: 197 print errormsg 198 print ' ' + fname + ": '" + attrkind + "' kind of attribute is not ready!" 199 print ' valid values: _______' 200 for key in validk.keys(): 201 print ' ', key,':', validk[key] 202 quit(-1) 203 else: 204 if attrkind == type(str('a')): 205 attrvalue = str(attrval.replace('!', ' ')) 206 elif attrkind == type(unicode('a')): 207 attrvalue = unicode(attrval.replace('!',' ')) 208 elif attrkind == type(np.int(1)): 209 attrvalue = np.int(attrval) 210 elif attrkind == np.dtype('i'): 211 attrvalue = np.int32(attrval) 212 elif attrkind == type(float(1.)): 213 attrvalue = float(attrval) 214 elif attrkind == type(np.float(1.)): 215 attrvalue = np.float(attrval) 216 elif attrkind == np.dtype('float32'): 217 attrvalue = np.array(attrval, dtype='f') 218 elif attrkind == type(np.float32(1.)): 219 attrvalue = np.float32(attrval) 220 elif attrkind == type(np.float64(1.)): 221 attrvalue = np.float64(attrval) 222 elif attrkind == type(np.array([1.,2.])): 223 attrvalue = attrval 224 else: 225 print errormsg 226 print ' ' + fname + ": '" + attrkind + "' kind of attribute is not ready!" 227 print ' valid values: _______' 228 for key in validk.keys(): 229 print ' ', key,':', validk[key] 230 quit(-1) 231 232 attvar = ncv.ncattrs() 233 if searchInlist(attvar, attrname): 234 attr = ncv.delncattr(attrname) 235 236 if attrname == 'original_subroutines_author': 237 attrvalue = 'Cindy Bruyere' 238 attr = ncv.setncattr(attrname, attrvalue) 239 240 return attr 241 242 170 243 def basicvardef(varobj, vstname, vlname, vunits): 171 244 """ Function to give the basic attributes to a variable … … 1130 1203 return 1131 1204 1205 def add_global_PyNCplot(ObjFile, pyscript, funcname, version): 1206 """ Function to add global attributes from 'PyNCplot' to a given netCDF 1207 ObjFile= netCDF file object to which add the global attributes 1208 funcname= name of the function from which file comes from 1209 version= version of the function 1210 """ 1211 fname = 'add_global_PyNCplot' 1212 1213 # Global values 1214 ObjFile.setncattr('author', 'L. Fita') 1215 newattr = set_attributek(ObjFile, 'institution', unicode('Centro de ' + \ 1216 'Investigaciones del Mar y la Atm' + unichr(243) + 'sfera (CIMA)'), 'U') 1217 newattr = set_attributek(ObjFile, 'institution2', unicode('Instituto Franco-' + \ 1218 'Argentino sobre Estudios de Clima y sus Impactos (CNRS, UMI-3351-IFAECI'), 'U') 1219 newattr = set_attributek(ObjFile, 'center', unicode('Consejo Nacional de ' + \ 1220 'Investigaciones Cient' + unichr(237) + 'ficas y T' + unichr(233) + \ 1221 'cnicas (CONICET)'), 'U') 1222 ObjFile.setncattr('university', 'Universidad de Buenos Aires (UBA)') 1223 ObjFile.setncattr('city', 'Buenos Aires') 1224 ObjFile.setncattr('country', 'Argentina') 1225 ObjFile.setncattr('tool', 'PyNCplot') 1226 ObjFile.setncattr('url', 'http://www.xn--llusfb-5va.cat/python/PyNCplot') 1227 ObjFile.setncattr('script', pyscript) 1228 if funcname is not None: 1229 ObjFile.setncattr('function', funcname) 1230 ObjFile.setncattr('version', version) 1231 1232 ObjFile.sync() 1233 1234 return 1235 1132 1236 ####### ###### ##### #### ### ## # 1133 1237 … … 1414 1518 set_attribute(objfile, descn, description[descn]) 1415 1519 1416 set_attribute(objfile,'author_nc','Lluis Fita') 1417 set_attribute(objfile,'institution_nc','Centro de Investigaciones del Mar y la ' + \ 1418 'Atmosfera (CIMA), CONICET-UBA, CNRS UMI-IFAECI, C.A. Buenos Aires') 1419 set_attribute(objfile,'country_nc','Argentina') 1420 set_attribute(objfile,'script_nc','create_OBSnetcdf.py') 1421 set_attribute(objfile,'version_script',version) 1422 set_attribute(objfile,'information', \ 1423 'http://www.lluÃsfb.cat/python/PyNCplot/') 1520 add_global_PyNCplot(objfile, main, 'main', version) 1424 1521 1425 1522 objfile.sync()
Note: See TracChangeset
for help on using the changeset viewer.