Changeset 2247 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Nov 26, 2018, 3:10:55 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r2246 r2247 61 61 # DataSetSection_multivars: Function to get a section (values along multiple variables) of a given data-set 62 62 # DatesFiles: Function to find different time values on a series of WRF files in a folder 63 # dimrename: Renaming a dimension from a file 63 64 # dimrm: Removing a dimension from a file 64 65 # DimsLoop: Function to provide the shape of the dimensions which are not selected … … 1754 1755 1755 1756 return 1757 1758 1759 def dimrename(ncfile, newdimn, dimn): 1760 """ Renaming a dimension from a file 1761 ncfile = netCDF object file 1762 values= [newdimn], new name for the dimension 1763 dimn = dimension name to change 1764 """ 1765 import shutil as shu 1766 fname = 'dimrename' 1767 1768 if newdimn == 'h': 1769 print fname + '_____________________________________________________________' 1770 print dimrename.__doc__ 1771 quit() 1772 1773 ncf = NetCDFFile(ncfile,'a') 1774 if not ncf.dimensions.has_key(dimn): 1775 print errormsg 1776 print ' ' + fname + ": file does not have dimension name '" + dimn+ "' !!" 1777 dimns = ncf.dimensions 1778 dimns.sort() 1779 print ' available ones:', dimns 1780 ncf.close() 1781 quit(-1) 1782 1783 if ncf.dimensions.has_key(newdimn): 1784 print warnmsg 1785 print ' ' + fname + ": file already have dimension name '" + newdimn+ "' !!" 1786 print " looping all over that variables with dimension name '"+dimn+"' !!" 1787 for varn in ncf.variables.keys(): 1788 ovar = ncf.variables[varn] 1789 if gen.searchInlist(ovar.dimensions,dimn): 1790 vardims = list(ovar.dimensions) 1791 idim = vardims.index(dimn) 1792 vardims[idim] = newdimn 1793 1794 newvar = ncf.createVariable(varn+'tmp', ovar.dtype, tuple(vardims)) 1795 newvar[:] = ovar[:] 1796 for ncattr in ovar.ncattrs(): 1797 attrv = ovar.getncattr(ncattr) 1798 newvar.setncattr(ncattr, attrv) 1799 ncf.sync() 1800 ncf.renameVariable(varn, varn+'old') 1801 ncf.renameVariable(varn+'tmp', varn) 1802 ncf.sync() 1803 else: 1804 newdim = ncf.renameDimension(dimn, newdimn) 1805 ncf.sync() 1806 ncf.close() 1807 1808 return 1756 1809 1757 1810 def dimrm(ncfile, values, dimn):
Note: See TracChangeset
for help on using the changeset viewer.