Project

General

Profile

select grid using latlong and multiply it with a given constant value

Added by Fuad Yassin about 2 years ago

Hi all,

is it possible to select a grid using point latlong and multiply its variable with a constant value?

Thanks,
Fuad


Replies (3)

RE: select grid using latlong and multiply it with a given constant value - Added by Karin Meier-Fleischer about 2 years ago

Hi Fuad,

you can use the expr operator to select a grid cell and do the computation for the variable value.

Example:
1. create a file with 20x10 grid cells (topography data)
2. if lon 0 and lat -81 multiply the grid cell value by 1000
3. check the output with ncdump

cdo -f nc -topo,r20x10 tsmall.nc
cdo -expr,'topo = (clon(topo) == 0 && clat(topo) == -81) ? (topo*1000) : topo' tsmall.nc o.nc
ncdump -v lon,lat,topo o.nc
netcdf o {
dimensions:
    lon = 20 ;
    lat = 10 ;
variables:
    double lon(lon) ;
        lon:standard_name = "longitude" ;
        lon:long_name = "longitude" ;
        lon:units = "degrees_east" ;
        lon:axis = "X" ;
    double lat(lat) ;
        lat:standard_name = "latitude" ;
        lat:long_name = "latitude" ;
        lat:units = "degrees_north" ;
        lat:axis = "Y" ;
    float topo(lat, lon) ;
        topo:units = "m" ;
        topo:_FillValue = -9.e+33f ;
        topo:missing_value = -9.e+33f ;

// global attributes:
        :CDI = "Climate Data Interface version 2.0.5 (https://mpimet.mpg.de/cdi)" ;
        :Conventions = "CF-1.6" ;
        :history = "Tue Nov 08 09:41:00 2022: cdo -expr,topo = (clon(topo) == 0 && clat(topo) == -81) ? (topo*1000) : topo tsmall.nc o.nc\n",
            "Tue Nov 08 09:34:49 2022: cdo -f nc -topo,r20x10 tsmall.nc" ;
        :CDO = "Climate Data Operators version 2.0.5 (https://mpimet.mpg.de/cdo)" ;
data:

 lon = 0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198, 216, 234, 252, 
    270, 288, 306, 324, 342 ;

 lat = -81, -63, -45, -27, -9, 9, 27, 45, 63, 81 ;

 topo =
  2275000, 2878.333, 3467, 3772, 3979, 3764, 3340.333, 2865, 2236.667, -1, 
    -1, -1, 287.3333, 1146.333, 1890.333, 1866.333, -1, 45.33333, 0, 1589.333,
  -5411, -5044, -5072.667, -5040, -4002, -3841.667, -3757, -4237.333, 
    -3975.333, -2727.333, -3918, -2520.667, -3823, -5051.667, -5169, -4752, 
    -3442.333, -322.3333, -4876.333, -5231.333,
  -4081.667, -4310, -1085.667, -3820.667, -3564, -3175, -3903, -4624, -4815, 
    -4843.333, -1418.333, -4865, -5083.667, -4520, -3154, -4111, 1188.667, 
    -5780, -5441.667, -3745.333,
  -4992.667, 791, -1865.333, -5238.667, -2924.333, -4894.333, -5267.333, 404, 
    196.6667, -1233, -2405, -5035, -3772.667, -3764.333, -3160.333, -3582, 
    -5862.333, 393, -4554.333, -4102.333,
  -4892, 921.6667, 253, -4067, -3004.333, -5631, -3506.667, 369, -52, 
    -3525.333, -5131, -4313, -4714.667, -4347, -3236.667, -4137, 200, 365, 
    452, -3930,
  135, 327, 1418.667, -4751.333, -2234.667, -2890, -73.66666, -265.3333, 
    -4583.667, -3917, -5933, -4933, -5142, -4586, -3272.333, -3510.667, -1, 
    -4549, -4593.667, -2616.333,
  307, 370.3333, 362, 953, 221.3333, 2450.667, 1015, -1343.333, -5084, 
    -5892.667, -5348.667, -4192.667, -4792, -4408, 1700.333, -2051, -5247, 
    -5553.667, -5171.333, -3737.667,
  107, 113.3333, 43.33333, -37, 388, 1247.667, 1182, 154, -940.6667, -5780, 
    -5645.667, -5157.667, -4646, -2695.333, 1130, 409, 341, -83, -4166, 
    -4336.667,
  -1693.667, 135, 182, 120.6667, 103, 354, 291.6667, 198, 1186, 681.6667, 
    -68, 351, 1437.667, 1014, 359, -107, -1.333333, -1127.333, -2398, -104,
  -2418.667, -488, -307, -58, -550, -49, -3337, -3852, -1886, -2760.667, 
    -2203.333, -3490, -3674, -3409, -1185, -4, 442.6667, 652, 1466, 302.3333 ;
}

RE: select grid using latlong and multiply it with a given constant value - Added by Fuad Yassin about 2 years ago

Thank you, Karin!
It is beneficial guidance. I am glad I asked.
One thing I found out was my parameter name includes dot, underscore and alphanumeric.
When I tried it on my file as is with the parameter name I got "syntax error, unexpected VARIABLE!
Then I changed the parameter name by removing the dot and it worked.
To explain what I mean I recreated the error using your example
is there a way around this? so that I don't have to modify the parameter name

cdo -f nc -topo,r20x10 tsmall.nc
cdo chname,topo,to_v2.1_po tsmall.nc tsmall2.nc
cdo -expr,'to_v2.1_po = (clon(to_v2.1_po) 0 && clat(to_v2.1_po) -81) ? (to_v2.1_po*1000) : to_v2.1_po' tsmall2.nc o.nc

RE: select grid using latlong and multiply it with a given constant value - Added by Uwe Schulzweida about 2 years ago

Variable names containing a dot followed by digits are not considered by the parser in the CDO operator expr. We will correct this in the next CDO version 2.1.1. Thanks for the example!

    (1-3/3)