Changeset 869 in lmdz_wrf for trunk


Ignore:
Timestamp:
Jun 18, 2016, 2:31:27 PM (8 years ago)
Author:
lfita
Message:

Adding:

`idims': Give all the dimensions names of a file

Fixing:

  • using new dimension `time' in 'WRF_CFtime_creation'
  • using new dimensions `lon,lat' in 'WRF_CFlonlat_creation'
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r866 r869  
    5656# grattr: Function to read a global atribute
    5757# grmattr: Removing a global attribute
     58# idims: Give all the dimensions names of a file
    5859# igattrs: Give all the global attributes of a file
    5960# 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
     
    656657    return
    657658
    658 def chdimname(values, ncfile, varn):
     659def chdimname(values, ncfile):
    659660  """ Changing the name of the dimension
    660661  values = [olddimname]:[newdimname]
     
    662663    [newdimname]: new name of the dimension
    663664  ncfile = netCDF file name
    664   varn = variable name
    665665  """
    666 
    667666  fname = 'chdimname'
    668667
     
    674673  if not os.path.isfile(ncfile):
    675674    print errormsg
    676     print '    chdimname: File "' + ncfile + '" does not exist !!'
    677     print errormsg
     675    print '  ' + fname + ": File '" + ncfile + "' does not exist !!"
    678676    quit(-1)   
    679677
     
    685683  if not ncf.dimensions.has_key(olddimname):
    686684    print warnmsg
    687     print '    chdimname: File "' + ncfile + '" does not have dimension "' + olddimname + '" !!!!'
     685    print '  ' + fname + ": File '" + ncfile + "' does not have dimension '" +       \
     686      olddimname + "' !!"
    688687    ncf.close()
    689     quit()
     688    quit(-1)
    690689
    691690  if not olddimname == newdimname and not ncf.dimensions.has_key(newdimname):
     
    695694  else:
    696695      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..."
    699700      filevars = ncf.variables
    700701      for fvarn in filevars:
     
    702703              fvar = ncf.variables[fvarn]
    703704              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 + '" '
    706707                  varinf = variable_inf(fvar)
    707708
     
    710711# From http://stackoverflow.com/questions/9067043/python-replace-list-values-using-a-tuple
    711712                  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)
    713715                  varv = fvar[:]
    714716                  newvar[:] = varv
     
    15691571
    15701572  print '  # allvars= ' + allvars
     1573  ncf.close()
     1574
     1575  return
     1576
     1577def 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
    15711596  ncf.close()
    15721597
     
    92399264    tunits = tunitsval + ' since ' + refdateS
    92409265
     9266# Create dimension
     9267    if not wrfnc.dimensions.has_key('time'):
     9268        newdim = wrfnc.createDimension('time', None)
     9269
    92419270    if wrfnc.variables.has_key(varn):
    92429271        newvar = wrfnc.variables[varn]
    92439272    else:
    9244         newvar = wrfnc.createVariable('time','f4',('Time'))
     9273        newvar = wrfnc.createVariable('time','f8',('time'))
    92459274
    92469275    newattr = basicvardef(newvar, 'time','time',tunits)
    92479276    newvar[:] = cftimes
    92489277    newattr = set_attribute(newvar, 'calendar', 'standard')
     9278    newattr = set_attribute(newvar, 'axis', 'T')
     9279    newattr = set_attribute(newvar, '_CoordinateAxisType', 'Time')
    92499280
    92509281    wrfnc.sync()
     
    95089539        cflats = np.zeros((latv.shape[1],latv.shape[2]), dtype=np.float)
    95099540        cflats = latv[0,:,:]
     9541        dimx = lonv.shape[2]
     9542        dimy = lonv.shape[1]
    95109543    elif len(lonv.shape) == 2:
    95119544        cflons = np.zeros((lonv.shape), dtype=np.float)
    95129545        cflons = np.where(lonv < 0., lonv + 360., lonv)
    95139546        cflats = latv
     9547        dimx = lonv.shape[1]
     9548        dimy = lonv.shape[0]
    95149549    elif len(lonv.shape) == 1:
    95159550        cflons = np.zeros((lonv.shape), dtype=np.float)
    95169551        cflons = np.where(lonv < 0., lonv + 360., lonv)
    95179552        cflats = latv
     9553        dimx = lonv.shape[0]
     9554        dimy = latv.shape[0]
    95189555    else:
    95199556        print errormsg
     
    95289565#        for ix in range(lonv.shape[1]):
    95299566#            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)
    95309573
    95319574    if wrfnc.variables.has_key(cflonn):
     
    95369579        newvar = wrfnc.variables[cflonn]
    95379580    else:
    9538         newvar = wrfnc.createVariable(cflonn,'f4',('south_north','west_east'))
    9539     newattr = basicvardef(newvar, 'longitude','longitude','degrees East')
     9581        newvar = wrfnc.createVariable(cflonn,'f8',('lat','lon'))
     9582    newattr = basicvardef(newvar, 'longitude','Longitude','degrees_East')
    95409583    newvar[:] = cflons
     9584    newattr = set_attribute(newvar, 'axis', 'X')
     9585    newattr = set_attribute(newvar, '_CoordinateAxisType', 'Lon')
    95419586
    95429587    if wrfnc.variables.has_key(cflatn):
     
    95479592        newvar = wrfnc.variables[cflatn]
    95489593    else:
    9549         newvar = wrfnc.createVariable(cflatn,'f4',('south_north','west_east'))
    9550     newattr = basicvardef(newvar, 'latitude','latitude','degrees North')
     9594        newvar = wrfnc.createVariable(cflatn,'f8',('lat','lon'))
     9595    newattr = basicvardef(newvar, 'latitude','Latitude','degrees_North')
    95519596    newvar[:] = cflats
     9597    newattr = set_attribute(newvar, 'axis', 'Y')
     9598    newattr = set_attribute(newvar, '_CoordinateAxisType', 'Lat')
    95529599
    95539600    wrfnc.sync()
Note: See TracChangeset for help on using the changeset viewer.