Index: trunk/tools/nc_var.py
===================================================================
--- trunk/tools/nc_var.py	(revision 417)
+++ trunk/tools/nc_var.py	(revision 418)
@@ -1,12 +1,13 @@
 #!/usr/bin/python
-## g.e. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1
-## g.e. ccrc468-17 # ./nc_var.py -v prac -f xyz/CCRC_NARCliM_Sydney_DAM_1990-1999_prac.nc -o mname -S pluja
-## g.e. ccrc468-17 # ./nc_var.py -v lluis -f CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o addref -S 'prac:standard_name@lluis_variable:long_name@test variable lluis:units@m s-1:0.'
-## g.e. ccrc468-17 # ./nc_var.py -v lluis66 -f ~/UNSW-CCRC-WRF/tools/python/CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o addattr -S 'comment|Lluis Fita-123456'
-## g.e. ccrc468-17 # ./nc_var.py -v lluis66 -f ~/UNSW-CCRC-WRF/tools/python/CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o rmvattr -S 'comment'
-## g.e. acuna # ./nc_var.py -f /d4/lflmd/etudes/WRF_LMDZ/WaquaL/WRF/control/wrfout/wrfout_d01_1979-12-01_00:00:00 -o checkallvars -S 'DateStrLen,Time,soil_layers_stag,bottom_top_stag,bottom_top,west_east_stag,west_east,south_north,south_north_stag:-3,1,2,2,2,-2,-2,-2,-2'
-## g.e. foudre # nc_var.py -f ~/etudes/WRF_LMDZ/tests/wrf_input/AR40.0/wrfout_d01_1979-01-01_00\:00\:00 -o checkallvars -S 'bottom_top_plus1,num_orchidee_soil_levels,lmdz_ksoil_types,DIM0009,DateStrLen,Time,soil_layers_stag,bottom_top_stag,bottom_top,west_east_stag,west_east,south_north,south_north_stag:2,0,0,1,-3,1,2,2,2,-2,-2,-2,-2'
-## g.e. # nc_var.py -o field_stats -f ~/etudes/domains/Polynesie/geo_em.d03.nc -S full -v HGT_M
-## g.e. # nc_var.py -o filter_2dim -S '80,y,x,lon,lat' -f 'tahiti_5m_ll.grd' -v 'z'
+## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1
+## e.g. ccrc468-17 # ./nc_var.py -v prac -f xyz/CCRC_NARCliM_Sydney_DAM_1990-1999_prac.nc -o mname -S pluja
+## e.g. ccrc468-17 # ./nc_var.py -v lluis -f CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o addref -S 'prac:standard_name@lluis_variable:long_name@test variable lluis:units@m s-1:0.'
+## e.g. ccrc468-17 # ./nc_var.py -v lluis66 -f ~/UNSW-CCRC-WRF/tools/python/CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o addattr -S 'comment|Lluis Fita-123456'
+## e.g. ccrc468-17 # ./nc_var.py -v lluis66 -f ~/UNSW-CCRC-WRF/tools/python/CCRC_NARCliM_Sydney_MOM_1990-1999_prac.nc -o rmvattr -S 'comment'
+## e.g. acuna # ./nc_var.py -f /d4/lflmd/etudes/WRF_LMDZ/WaquaL/WRF/control/wrfout/wrfout_d01_1979-12-01_00:00:00 -o checkallvars -S 'DateStrLen,Time,soil_layers_stag,bottom_top_stag,bottom_top,west_east_stag,west_east,south_north,south_north_stag:-3,1,2,2,2,-2,-2,-2,-2'
+## e.g. foudre # nc_var.py -f ~/etudes/WRF_LMDZ/tests/wrf_input/AR40.0/wrfout_d01_1979-01-01_00\:00\:00 -o checkallvars -S 'bottom_top_plus1,num_orchidee_soil_levels,lmdz_ksoil_types,DIM0009,DateStrLen,Time,soil_layers_stag,bottom_top_stag,bottom_top,west_east_stag,west_east,south_north,south_north_stag:2,0,0,1,-3,1,2,2,2,-2,-2,-2,-2'
+## e.g. # nc_var.py -o field_stats -f ~/etudes/domains/Polynesie/geo_em.d03.nc -S full -v HGT_M
+## e.g. # nc_var.py -o filter_2dim -S '80,y,x,lon,lat' -f 'tahiti_5m_ll.grd' -v 'z'
+## e.g. # nc_var.py -o selvar -f /home/lluis/PY/met_em.d01.1979-01-01_00:00:00.nc -S 'west_east@XLONG_M,south_north@XLAT_M,num_metgrid_levels@int,Time@Times' -v TT,UU,VV,SKINTEMP
 
 from optparse import OptionParser
@@ -30,5 +31,5 @@
   'ncreplace', 'ncstepdiff', 'netcdf_concatenation', 'netcdf_fold_concatenation',    \
   'remapnn',                                                                         \
-  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'setvar_asciivalues',               \
+  'seasmean', 'sellonlatbox', 'sellonlatlevbox', 'selvar', 'setvar_asciivalues',     \
   'sorttimesmat', 'spacemean', 'statcompare_files', 'submns', 'subyrs', 'TimeInf',   \
   'timemean', 'valmod', 'valmod_dim','varaddattrk', 'varaddattr', 'varaddref',       \
@@ -197,4 +198,6 @@
 elif oper == 'sellonlatlevbox':
     ncvar.sellonlatlevbox(opts.values, opts.ncfile, opts.varname)
+elif oper == 'selvar':
+    ncvar.selvar(opts.values, opts.ncfile, opts.varname)
 elif oper == 'setvar_asciivalues':
     ncvar.setvar_asciivalues(opts.values, opts.ncfile, opts.varname)
Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 417)
+++ trunk/tools/nc_var_tools.py	(revision 418)
@@ -14519,4 +14519,174 @@
 
 #changevartype('i', 'test.nc', 'var')
+
+def add_dims(oc,nc,dnames):
+    """ Function to add dimensions from a given netCDF object to another one
+      oc: source netcdfile object
+      nc: netcdfile object to add dimensions
+      dnames: list of name of dimensions to add
+    """
+    fname = 'add_dims'
+
+    for dn in dnames:
+        print '  ' + fname + ': adding dim:',dn,' ...'
+        if oc.dimensions[dn].isunlimited():
+            newdim = nc.createDimension(dn, None)
+        else:
+            newdim = nc.createDimension(dn, len(oc.dimensions[dn]))
+
+    nc.sync()
+
+    return
+
+def add_vars(oc,nc,vnames):
+    """ Function to add variables from a given netCDF object to another one
+      oc: source netcdfile object
+      nc: netcdfile object to add dimensions
+      vnames: list of name of variables to add
+    """
+    fname = 'add_vars'
+
+    for vn in vnames:
+        print '  ' + fname + ': adding var:',vn,' ...'
+
+        varo = oc.variables[vn]
+        vartype = varo.dtype
+        vardims = varo.dimensions
+        varattrs = varo.ncattrs()
+
+        for vdn in vardims:
+            if not searchInlist(nc.dimensions,vdn):
+                print warnmsg
+                print '  ' + fname + ": adding dimension '" + vdn +                  \
+                  "' from variable '" + vdn + "' which is not in file !!"
+                add_dims(oc,nc,[vdn])
+
+        if searchInlist(varattrs,'_FillValue'):
+            newvar = nc.createVariable(vn, vartype, vardims,                         \
+              fill_value=varo.getncattr('_FillValue'))
+        else:
+            newvar = nc.createVariable(vn, vartype, vardims)
+
+        for attrn in varattrs:
+            attrv = varo.getncattr(attrn)
+            newattr = set_attribute(newvar, attrn, attrv)
+
+        newvar[:] = varo[:]
+
+    nc.sync()
+
+    return
+
+def add_globattrs(oc,nc,attrns):
+    """ Function to add global attributes from a given netCDF object to another one
+      oc: source netcdfile object
+      nc: netcdfile object to add dimensions
+      vnames: list of name of dimensions to add ('all', for all attributes)
+    """
+    fname = 'add_globattrs'
+
+    allfattrs = oc.ncattrs()
+
+    if attrns == 'all':
+        fattrns = allfattrs
+    else:
+        fattrns = attrns
+
+    for an in fattrns:
+        if not searchInlist(allfattrs,an):
+            print errormsg
+            print '  ' + fname + ": file has not global attribute '" + an + "' !!"
+            quit(-1)
+
+        print '  ' + fname + ': adding attr:',an,' ...'
+        
+        av = oc.getncattr(an)
+        newattr = set_attribute(nc, an, av)
+
+    nc.sync()
+
+    return
+
+def selvar(values, ncfile, varn):
+    """ Function to select a series of variables from a netcdf file. Variables with 
+      the values of the associated dimensions will be also retrieved
+      values= ',' list of couples [dimname]@[vardimname]
+        [dimname]: name of the dimension
+        [vardimname]: name of the variable with values for the dimension ('int', for 
+          counter of integers with the dimension length)
+      ncfile= netCDF file to use
+      varnames= ',' list of variables to retrieve
+    """
+    fname = 'selvar'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print selvar.__doc__
+        quit()
+
+    varnames = varn.split(',')
+    dimvals = values.split(',')
+
+    ofile = fname + '_new.nc'
+
+    onc = NetCDFFile(ncfile, 'r')
+
+    for varn in varnames:
+        if not searchInlist(onc.variables, varn):
+            print errormsg
+            print '  ' + fname + ": file '"  + ncfile + "' has not variable '" +     \
+              varn + "' !!"
+            quit(-1)
+
+    dimsv = {}
+    vardims = []
+    for dimv in dimvals:
+        dimn = dimv.split('@')[0]
+        if not searchInlist(onc.dimensions, dimn):
+            print errormsg
+            print '  ' + fname + ": file '"  + ncfile + "' has not dimension '" +    \
+              dimn + "' !!"
+            quit(-1)
+        varn = dimv.split('@')[1]
+        if not varn == 'int' and not searchInlist(onc.variables, varn):
+            print errormsg
+            print '  ' + fname + ": file '"  + ncfile + "' has not variable '" +     \
+              varn + "' !!"
+            quit(-1)
+        dimsv[dimn] = varn
+        if varn != 'int': vardims.append(varn)
+
+    newnc = NetCDFFile(ofile, 'w')
+
+# Creation of dimensions
+    add_dims(onc,newnc,dimsv.keys())
+
+# Getting variables from dimensions
+    add_vars(onc,newnc,vardims)
+
+# Getting variables
+    for varn in varnames:
+        varobj = onc.variables[varn]
+        vardims = varobj.dimensions
+        for dimn in vardims:
+            if not searchInlist(vardims,dimn):
+                print errormsg
+                print '  ' + fname + ": no assigned variable to dimension '" +       \
+                  dimn + "' !!"
+                quit(-1)
+
+    add_vars(onc,newnc,varnames)
+
+# Getting global attributes
+    add_globattrs(onc,newnc,'all')
+
+    onc.close()
+    newnc.close()
+
+    print "Successfull written of file '" + ofile + "' !!"
+
+    return
+
+selvar('x@lon,y@lat,time@time,shan@int', '/home/lluis/PY/test.nc', 'var')
 #quit()
 
