Changeset 1959 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Jul 23, 2018, 11:28:50 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1948 r1959 103 103 # ivars: Give all the variable names of a file 104 104 # ivattrs: Give all the attributes of a variable and its type 105 # join_singlestation_obsfiles: Function to join files from 'single-station' `create_OBSnetcdf.py' script to create a single file 105 106 # load_ncvariable_lastdims: Function to load the last [Nlastdims] dimensions of a variable [varn] from a netCDF object at the other dimensions at [prevdims] 106 107 # lonlatProj: Function to compute longitudes and latitudes for a given projection following subroutine calc_resolution_in from ORCHIDEE/src_global/module_InterpWeight … … 24588 24589 #get_time('1422759600;seconds since 1949-12-01 00:00:00', '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'WRFtime') 24589 24590 24591 def join_singlestation_obsfiles(values, variable): 24592 """ Function to join files from 'single-station' `create_OBSnetcdf.py' script to 24593 create a single file 24594 values=[namefoldobs]:[headerF] 24595 [namefoldobs]: name of the foler holding all single-stations netCDF files 24596 [headerF]: header of the names of the files with the observations 24597 variable= ',' separated list of variables names ('all' for all variables) 24598 """ 24599 fname = 'join_singlestation_obsfiles' 24600 24601 if values == 'h': 24602 print fname + '_____________________________________________________________' 24603 print join_singlestation_obsfiles.__doc__ 24604 quit() 24605 24606 expectargs = '[foldobs]:[headerF]' 24607 gen.check_arguments(fname, values, expectargs, ':') 24608 24609 namefoldobs = values.split(':')[0] 24610 headerF = values.split(':')[1] 24611 24612 fobs = gen.files_folder(namefoldobs, headerF) 24613 24614 # Getting number stations 24615 ifile = 0 24616 namevals = [] 24617 lonvals = [] 24618 latvals = [] 24619 heightvals = [] 24620 for fobsn in fobs: 24621 filen = namefoldobs + '/' + fobsn 24622 if not os.path.isfile(filen): 24623 print errormsg 24624 print ' ' + fname + ": file '" + filen + "' does not exist !!" 24625 print " files found in folder '" + namefoldobs + "':", fobs 24626 quit(-1) 24627 24628 onc = NetCDFFile(filen, 'r') 24629 if gen.searchInlist(onc.dimensions,'time'): dimt = onc.dimensions['time'] 24630 elif gen.searchInlist(onc.dimensions,'CFtime'): dimt=onc.dimensions['CFtime'] 24631 else: 24632 print errormsg 24633 print ' ' + fname + ": file '" + filen + "' does not have any 'time'" + \ 24634 "dimensisn !!" 24635 print " available ones:", onc.dimensions 24636 quit(-1) 24637 24638 if ifile == 0: 24639 if variable == 'all': varns = onc.variables.keys() 24640 else: varns = gen.str_list(variable, ',') 24641 stn = '' 24642 for stS in onc.variables['station'][0,:]: 24643 if stS == onc.variables['station'][0,0]: stn = stS 24644 elif stS != '--': stn = stn + stS 24645 else: break 24646 24647 print 'Lluis :', stn 24648 namevals.append(stn) 24649 lonvals.append(onc.variables['lon'][0]) 24650 latvals.append(onc.variables['lat'][0]) 24651 heightvals.append(onc.variables['height'][0]) 24652 print ' ' + fname + ": found values for station '" + namevals[ifile] + \ 24653 "' at:", lonvals[ifile], ',', latvals[ifile], ' ...' 24654 24655 onc.close() 24656 ifile = ifile + 1 24657 24658 Nfile = ifile 24659 24660 # Creation of joined file 24661 ofile = 'joined_singlestations.nc' 24662 24663 onewnc = NetCDFFile(ofile, 'w') 24664 # dimensions 24665 newdim = onewnc.createDimension('Nstations', Nfile) 24666 newdim = onewnc.createDimension('StrLength', 200) 24667 newdim = onewnc.createDimension('time', None) 24668 24669 # Creation of dimension-variables 24670 newvar = onewnc.createVariable('stsname', 'c', ('Nstations', 'StrLength')) 24671 basicvardef(newvar, 'station_name', 'Name of stations', '-') 24672 newvals = writing_str_nc(newvar, namevals, 200) 24673 24674 newvar = onewnc.createVariable('stslon', 'f8', ('Nstations')) 24675 basicvardef(newvar, 'lon', 'Longitude of stations', 'degrees_east') 24676 newvar[:] = lonvals[:] 24677 24678 newvar = onewnc.createVariable('stslat', 'f8', ('Nstations')) 24679 basicvardef(newvar, 'lat', 'Latitude of stations', 'degrees_north') 24680 newvar[:] = latvals[:] 24681 24682 newvar = onewnc.createVariable('stsheight', 'f', ('Nstations')) 24683 basicvardef(newvar, 'height', 'height of stations', 'm') 24684 newvar[:] = heightvals[:] 24685 24686 ifile = 0 24687 for fobsn in fobs: 24688 filen = namefoldobs + '/' + fobsn 24689 if not os.path.isfile(filen): 24690 print errormsg 24691 print ' ' + fname + ": file '" + filen + "' does not exist !!" 24692 print " files found in folder '" + namefoldobs + "':", fobs 24693 quit(-1) 24694 24695 onc = NetCDFFile(filen, 'r') 24696 if ifile == 0: 24697 for varn in varns: 24698 if not onc.variables.has_key(varn): 24699 print errormsg 24700 print ' ' + fname + ": file '" + filen + "' does nor have " + \ 24701 "variable '" + varn + "' !!" 24702 varnsorted = onc.variables.has_key(varn) 24703 varnsorted.sort() 24704 print ' available ones:', varnsorted 24705 quit(-1) 24706 ovar = onc.variables[varn] 24707 attrns = ovar.ncattrs() 24708 vartype = ovar.dtype 24709 vardims = ovar.dimensions 24710 newdims = list(vardims) + ['Nstations'] 24711 for vard in vardims: 24712 if not gen.searchInlist(onewnc.dimensions, vard): 24713 print ' ' + fname + ": adding dimension '" + vard + "' !!" 24714 dobj = onc.dimensions[vard] 24715 if dobj.isunlimited(): 24716 newdim = onewnc.createDimension(vard, None) 24717 else: 24718 lend = len(dobj) 24719 newdim = onewnc.createDimension(vard, lend) 24720 if gen.searchInlist(attrns, '_FillValue'): 24721 varfill = ovar._FillValue 24722 newvar = onewnc.createVariable(varn, vartype, tuple(newdims), \ 24723 fill_value = varfill) 24724 else: 24725 newvar = onewnc.createVariable(varn, vartype, tuple(newdims)) 24726 24727 varv = ovar[:] 24728 if len(newvar.shape) == 4: newvar[:,:,:,ifile] = varv[:] 24729 elif len(newvar.shape) == 3: newvar[:,:,ifile] = varv[:] 24730 elif len(newvar.shape) == 2: newvar[:,ifile] = varv[:] 24731 for attrn in attrns: 24732 if attrn != '_FillValue': 24733 attrv = ovar.getncattr(attrn) 24734 newattr = newvar.setncattr(attrn, attrv) 24735 # Global attributes 24736 for attrn in onc.ncattrs(): 24737 attrv = onc.getncattr(attrn) 24738 onewnc.setncattr(attrn, attrv) 24739 24740 else: 24741 for varn in varns: 24742 if not onc.variables.has_key(varn): 24743 print errormsg 24744 print ' ' + fname + ": file '" + filen + "' does nor have " + \ 24745 "variable '" + varn + "' !!" 24746 varnsorted = onc.variables.has_key(varn) 24747 varnsorted.sort() 24748 print ' available ones:', varnsorted 24749 quit(-1) 24750 ovar = onc.variables[varn] 24751 newvar = onewnc.variables[varn] 24752 varv = ovar[:] 24753 if len(newvar.shape) == 4: newvar[:,:,:,ifile] = varv[:] 24754 elif len(newvar.shape) == 3: newvar[:,:,ifile] = varv[:] 24755 elif len(newvar.shape) == 2: newvar[:,ifile] = varv[:] 24756 24757 onewnc.sync() 24758 onc.close() 24759 ifile = ifile + 1 24760 24761 # Global attributes 24762 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 24763 onewnc.sync() 24764 onewnc.close() 24765 print fname + ": succesfull creation of file '" + ofile + "' !!" 24766 24767 return 24768 24590 24769 #quit()
Note: See TracChangeset
for help on using the changeset viewer.