- Timestamp:
- Jun 18, 2016, 2:31:27 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r866 r869 56 56 # grattr: Function to read a global atribute 57 57 # grmattr: Removing a global attribute 58 # idims: Give all the dimensions names of a file 58 59 # igattrs: Give all the global attributes of a file 59 60 # increaseDimvar: Function to increase with 1 dimension an existing variable within a netcdf file. Values of the variable will be repeated along the new dimension … … 656 657 return 657 658 658 def chdimname(values, ncfile , varn):659 def chdimname(values, ncfile): 659 660 """ Changing the name of the dimension 660 661 values = [olddimname]:[newdimname] … … 662 663 [newdimname]: new name of the dimension 663 664 ncfile = netCDF file name 664 varn = variable name665 665 """ 666 667 666 fname = 'chdimname' 668 667 … … 674 673 if not os.path.isfile(ncfile): 675 674 print errormsg 676 print ' chdimname: File "' + ncfile + '" does not exist !!' 677 print errormsg 675 print ' ' + fname + ": File '" + ncfile + "' does not exist !!" 678 676 quit(-1) 679 677 … … 685 683 if not ncf.dimensions.has_key(olddimname): 686 684 print warnmsg 687 print ' chdimname: File "' + ncfile + '" does not have dimension "' + olddimname + '" !!!!' 685 print ' ' + fname + ": File '" + ncfile + "' does not have dimension '" + \ 686 olddimname + "' !!" 688 687 ncf.close() 689 quit( )688 quit(-1) 690 689 691 690 if not olddimname == newdimname and not ncf.dimensions.has_key(newdimname): … … 695 694 else: 696 695 print warnmsg 697 print ' chdimname: File "' + ncfile + '" already has dimension name "' + newdimname + '" ' 698 print ' chdimname: modifying all the variables which use the old dimension' 696 print ' ' + fname + ": File '" + ncfile + "' already has dimension name '" + \ 697 newdimname + '" ' 698 print ' ' + fname + ": modifying all variables which use the old '" + \ 699 olddimname + "' dimension..." 699 700 filevars = ncf.variables 700 701 for fvarn in filevars: … … 702 703 fvar = ncf.variables[fvarn] 703 704 fvardims = fvar.dimensions 704 if searchInlist(fvardims, olddimname):705 print ' variable "' + fvarn + '" uses dimension "' + olddimname + '" '705 if gen.searchInlist(fvardims, olddimname): 706 # print ' variable "' + fvarn + '" uses dimension "' + olddimname + '" ' 706 707 varinf = variable_inf(fvar) 707 708 … … 710 711 # From http://stackoverflow.com/questions/9067043/python-replace-list-values-using-a-tuple 711 712 newdims = tuple([ change.get(x,x) for x in fvardims ]) 712 newvar = ncf.createVariable(fvarn + 'tmpdname', varinf.dtype, newdims, fill_value=varinf.FillValue) 713 newvar = ncf.createVariable(fvarn + 'tmpdname', varinf.dtype, \ 714 newdims, fill_value=varinf.FillValue) 713 715 varv = fvar[:] 714 716 newvar[:] = varv … … 1569 1571 1570 1572 print ' # allvars= ' + allvars 1573 ncf.close() 1574 1575 return 1576 1577 def idims(ncfile): 1578 """Give all the dimensions names of a file 1579 ncfile= netCDFfile name from which all variables will be given 1580 """ 1581 fname = 'idims' 1582 1583 if not os.path.isfile(ncfile): 1584 print errormsg 1585 print ' ' + fname + ': File "' + ncfile + '" does not exist !!' 1586 quit(-1) 1587 1588 ncf = NetCDFFile(ncfile,'r') 1589 ncdims = ncf.dimensions 1590 alldims='' 1591 for dim in ncdims: 1592 print vdim 1593 alldims=alldims + ':' + dim 1594 1595 print ' # alldims= ' + alldims 1571 1596 ncf.close() 1572 1597 … … 9239 9264 tunits = tunitsval + ' since ' + refdateS 9240 9265 9266 # Create dimension 9267 if not wrfnc.dimensions.has_key('time'): 9268 newdim = wrfnc.createDimension('time', None) 9269 9241 9270 if wrfnc.variables.has_key(varn): 9242 9271 newvar = wrfnc.variables[varn] 9243 9272 else: 9244 newvar = wrfnc.createVariable('time','f 4',('Time'))9273 newvar = wrfnc.createVariable('time','f8',('time')) 9245 9274 9246 9275 newattr = basicvardef(newvar, 'time','time',tunits) 9247 9276 newvar[:] = cftimes 9248 9277 newattr = set_attribute(newvar, 'calendar', 'standard') 9278 newattr = set_attribute(newvar, 'axis', 'T') 9279 newattr = set_attribute(newvar, '_CoordinateAxisType', 'Time') 9249 9280 9250 9281 wrfnc.sync() … … 9508 9539 cflats = np.zeros((latv.shape[1],latv.shape[2]), dtype=np.float) 9509 9540 cflats = latv[0,:,:] 9541 dimx = lonv.shape[2] 9542 dimy = lonv.shape[1] 9510 9543 elif len(lonv.shape) == 2: 9511 9544 cflons = np.zeros((lonv.shape), dtype=np.float) 9512 9545 cflons = np.where(lonv < 0., lonv + 360., lonv) 9513 9546 cflats = latv 9547 dimx = lonv.shape[1] 9548 dimy = lonv.shape[0] 9514 9549 elif len(lonv.shape) == 1: 9515 9550 cflons = np.zeros((lonv.shape), dtype=np.float) 9516 9551 cflons = np.where(lonv < 0., lonv + 360., lonv) 9517 9552 cflats = latv 9553 dimx = lonv.shape[0] 9554 dimy = latv.shape[0] 9518 9555 else: 9519 9556 print errormsg … … 9528 9565 # for ix in range(lonv.shape[1]): 9529 9566 # print ix, lonv[10,ix], cflons[10,ix] 9567 9568 # Creation of CF-dimensions 9569 if not wrfnc.dimensions.has_key('lon'): 9570 newdim = wrfnc.createDimension('lon', dimx) 9571 if not wrfnc.dimensions.has_key('lat'): 9572 newdim = wrfnc.createDimension('lat', dimy) 9530 9573 9531 9574 if wrfnc.variables.has_key(cflonn): … … 9536 9579 newvar = wrfnc.variables[cflonn] 9537 9580 else: 9538 newvar = wrfnc.createVariable(cflonn,'f 4',('south_north','west_east'))9539 newattr = basicvardef(newvar, 'longitude',' longitude','degreesEast')9581 newvar = wrfnc.createVariable(cflonn,'f8',('lat','lon')) 9582 newattr = basicvardef(newvar, 'longitude','Longitude','degrees_East') 9540 9583 newvar[:] = cflons 9584 newattr = set_attribute(newvar, 'axis', 'X') 9585 newattr = set_attribute(newvar, '_CoordinateAxisType', 'Lon') 9541 9586 9542 9587 if wrfnc.variables.has_key(cflatn): … … 9547 9592 newvar = wrfnc.variables[cflatn] 9548 9593 else: 9549 newvar = wrfnc.createVariable(cflatn,'f 4',('south_north','west_east'))9550 newattr = basicvardef(newvar, 'latitude',' latitude','degreesNorth')9594 newvar = wrfnc.createVariable(cflatn,'f8',('lat','lon')) 9595 newattr = basicvardef(newvar, 'latitude','Latitude','degrees_North') 9551 9596 newvar[:] = cflats 9597 newattr = set_attribute(newvar, 'axis', 'Y') 9598 newattr = set_attribute(newvar, '_CoordinateAxisType', 'Lat') 9552 9599 9553 9600 wrfnc.sync()
Note: See TracChangeset
for help on using the changeset viewer.