Multiplying an Netcdf by a constant

Help
moracamilo
2012-08-29
2013-10-17
  • moracamilo
    moracamilo
    2012-08-29

    Hi,

    I wonder if there is a way to multiply an entire netcdf file by a constant value?.

    Thanks,

    Camilo

     
  • henry Butowsky
    henry Butowsky
    2012-08-29

    Hi Camilo,
    you could hack it by adding the attribute 'scale_factor' with the value you want to multiply the vars by; and then unpack the file using ncpdq. The following multiplies all vars by 2

    ncatted -a scale_factor,,c,d,2.0 infile.nc
    ncpdq -U infile.nc outfile.nc
    

    The problem with the above code is that all vars are converted to type double
    The other method is to use ncap2 and manually add each var:

    ncap2 -v -s 'three=three*2.0f; four=four*2.0d;' infile.nc outfile.nc

    … Henry

     

     
  • augusto
    augusto
    2012-08-29

    Hi Camilo,

    I've used 'ncflint' to do what you want, just multiply the 2nd file by 0.0:
    ncflint -C -O -v my_var -w const_fact,0.0 file1.nc file1.nc outf.nc
    file1 will be multiplied by 'const_fact'.
    Make sure that you change 'missing_value' to "Filing value' in file1 using 'ncrename'

    Hope it helps,

    Augusto

     
  • Mubashar
    Mubashar
    2013-03-05

    Hi,

    I wanted to multiply all variables by a constant number 0.8:

    I do the following:
    ncflint -w 0.8,0.0 extsw.nc extsw.nc tst.nc

    It seems that time axis is also modified. So regain original time I applied:
    ncks -A -v time  extsw.nc tst.nc

    Are the above steps are fine?

    ncdump -c of my original file looks as follows:

    netcdf extsw_out {
    dimensions:
    lon = 1 ;
    lat = 90 ;
    pfull = 40 ;
    time = UNLIMITED ; // (1 currently)
    variables:
    float lon(lon) ;
    lon:standard_name = "longitude" ;
    lon:long_name = "longitude" ;
    lon:units = "degrees_east" ;
    lon:axis = "X" ;
    float lat(lat) ;
    lat:standard_name = "latitude" ;
    lat:long_name = "latitude" ;
    lat:units = "degrees_north" ;
    lat:axis = "Y" ;
    float pfull(pfull) ;
    pfull:standard_name = "air_pressure" ;
    pfull:long_name = "pressure" ;
    pfull:units = "hPa" ;
    pfull:positive = "down" ;
    pfull:axis = "Z" ;
    double time(time) ;
    time:standard_name = "time" ;
    time:units = "days since 1800-01-01 00:00:00" ;
    time:calendar = "julian" ;
    double extsw_b01(time, pfull, lat, lon) ;
    extsw_b01:long_name = "test data" ;
    extsw_b01:units = "1/m" ;
    double extsw_b02(time, pfull, lat, lon) ;
    extsw_b02:long_name = "test data" ;
    extsw_b02:units = "1/m" ;
    double extsw_b03(time, pfull, lat, lon) ;
    extsw_b03:long_name = "test data" ;
    extsw_b03:units = "1/m" ;
    double extsw_b04(time, pfull, lat, lon) ;
    extsw_b04:long_name = "test data" ;
    extsw_b04:units = "1/m" ;
    double extsw_b05(time, pfull, lat, lon) ;
    extsw_b05:long_name = "test data" ;
    extsw_b05:units = "1/m" ;
    double extsw_b06(time, pfull, lat, lon) ;
    extsw_b06:long_name = "test data" ;
    extsw_b06:units = "1/m" ;
    double extsw_b07(time, pfull, lat, lon) ;
    extsw_b07:long_name = "test data" ;
    extsw_b07:units = "1/m" ;
    double extsw_b08(time, pfull, lat, lon) ;
    extsw_b08:long_name = "test data" ;
    extsw_b08:units = "1/m" ;
    double extsw_b09(time, pfull, lat, lon) ;
    extsw_b09:long_name = "test data" ;
    extsw_b09:units = "1/m" ;
    double extsw_b10(time, pfull, lat, lon) ;
    extsw_b10:long_name = "test data" ;
    extsw_b10:units = "1/m" ;
    double extsw_b11(time, pfull, lat, lon) ;
    extsw_b11:long_name = "test data" ;
    extsw_b11:units = "1/m" ;
    double extsw_b12(time, pfull, lat, lon) ;
    extsw_b12:long_name = "test data" ;
    extsw_b12:units = "1/m" ;
    double extsw_b13(time, pfull, lat, lon) ;
    extsw_b13:long_name = "test data" ;
    extsw_b13:units = "1/m" ;
    double extsw_b14(time, pfull, lat, lon) ;
    extsw_b14:long_name = "test data" ;
    extsw_b14:units = "1/m" ;
    double extsw_b15(time, pfull, lat, lon) ;
    extsw_b15:long_name = "test data" ;
    extsw_b15:units = "1/m" ;
    double extsw_b16(time, pfull, lat, lon) ;
    extsw_b16:long_name = "test data" ;
    extsw_b16:units = "1/m" ;
    double extsw_b17(time, pfull, lat, lon) ;
    extsw_b17:long_name = "test data" ;
    extsw_b17:units = "1/m" ;
    double extsw_b18(time, pfull, lat, lon) ;
    extsw_b18:long_name = "test data" ;
    extsw_b18:units = "1/m" ;

    // global attributes:
    :CDI = "Climate Data Interface version 1.5.5 (http://code.zmaw.de/projects/cdi)" ;
    :Conventions = "CF-1.4" ;
    :history = "Sat Mar  2 21:03:16 2013: ncks -A -v time testtime.nc extsw_out.nc\n",
    "Sat Mar 02 21:03:15 2013: cdo -b F64 -timmean extsw_1990-1999.nc extsw_out.nc\n",
    "Sun Feb 10 19:45:43 2013: ncks -d time,1680,1799 ../extsw.nc extsw_1990-1999.nc" ;
    :NCO = "4.0.5" ;
    :CDO = "Climate Data Operators version 1.5.5 (http://code.zmaw.de/projects/cdo)" ;
    }

     
  • Charlie Zender
    Charlie Zender
    2013-03-06

    Yes, the commands above are fine. Good job figuring that out!
    NCO should probably add a switch that prevents arithmetic on coordinate variables.
    I have added this as TODO nco1091.
    But the current behavior is acceptable, even necessary, because it allows files to be time-interpolated.
    cz

     
  • Charlie Zender
    Charlie Zender
    2013-03-08

    I have committed a patch that allows the user to ensure that ncflint does not change record coordinates.
    It will be in NCO 4.2.6. From the forthcoming ANNOUNCE:

    A. ncflint accepts a new switch, -fix_rec_crd, that prevents record
       coordinates from being multiplied/interpolated. By default, ncflint
       multplies/interpolates record coordinates (like time) but not other
       coordinates (like, typically, latitude). This is because ncflint
       is most often used to time-interpolate files (so you want the time
       to change, but not the spatial coordinates). However, ncflint is
       also used quite often to "scale" files by multiplying the variables
       by a constant. Now one can easily multiply all variables (except
       coordinates) by a constant, and if -fix_rec_crd is specified, the
       record coordinate will not change.