Project

General

Profile

Calculate area-weighted area-mean temperature of a region

Added by Joakim Kjellsson over 5 years ago

Hi

I've got temperature (t,z,y,x) from an ocean model and I'd like to calculate the mean vertical profile.
So I thought I'd use "fldmean".
The output does not include the grid cell areas, but I can use "setgridarea" for this, and it works fine for global means.
However, it does not work for regional means!

If I do

cdo -O -fldmean -selname,votemper -setgridarea,nemo_areas.nc temp.nc mean_temp.nc 

all goes well, and I get the area-weighted mean for each time and vertical level.
But if I do

cdo -O -fldmean -selindexbox,380,581,338,465 -setgridarea,nemo_areas.nc temp.nc mean_temp.nc

I get the warning

cdo fldmean (Warning): Grid cell bounds not available, using constant grid cell area weights for variable votemper!

I've debugged it, and it seems that when I use "sellonlatbox" or "selindexbox", the "cell_area" variable disappears from the file, so "fldmean" cannot find it.
Could you please advice on how to do area-weighted means over a region?
I'm enclosing "ncdump" of the original file and the area file (the original files are quite large).

Many thanks
Joakim

blogin3:~/models/foci1.9.4-openifs/experiments/FOCI1.9-JK002d/scripts $ ncdump -h temp.nc 
netcdf temp {
dimensions:
    x = 722 ;
    y = 511 ;
    deptht = 46 ;
    bnds = 2 ;
    time_counter = UNLIMITED ; // (12 currently)
variables:
    float nav_lon(y, x) ;
        nav_lon:standard_name = "longitude" ;
        nav_lon:long_name = "longitude" ;
        nav_lon:units = "degrees_east" ;
        nav_lon:_CoordinateAxisType = "Lon" ;
    float nav_lat(y, x) ;
        nav_lat:standard_name = "latitude" ;
        nav_lat:long_name = "latitude" ;
        nav_lat:units = "degrees_north" ;
        nav_lat:_CoordinateAxisType = "Lat" ;
    float deptht(deptht) ;
        deptht:long_name = "Vertical T levels" ;
        deptht:units = "m" ;
        deptht:positive = "down" ;
        deptht:axis = "Z" ;
        deptht:bounds = "deptht_bnds" ;
    float deptht_bnds(deptht, bnds) ;
    double time_counter(time_counter) ;
        time_counter:standard_name = "time" ;
        time_counter:long_name = "Time axis" ;
        time_counter:bounds = "time_counter_bnds" ;
        time_counter:units = "seconds since 1950-01-01 00:00:00" ;
        time_counter:calendar = "standard" ;
        time_counter:axis = "T" ;
    double time_counter_bnds(time_counter, bnds) ;
    float votemper(time_counter, deptht, y, x) ;
        votemper:standard_name = "sea_water_potential_temperature" ;
        votemper:long_name = "temperature" ;
        votemper:units = "degC" ;
        votemper:coordinates = "nav_lat nav_lon" ;
        votemper:_FillValue = 1.e+20f ;
        votemper:missing_value = 1.e+20f ;
        votemper:online_operation = "average" ;
        votemper:interval_operation = "1800 s" ;
        votemper:interval_write = "1 month" ;
        votemper:cell_methods = "time: mean (interval: 1800 s)" ;

// global attributes:
        :CDI = "Climate Data Interface version 1.8.2 (http://mpimet.mpg.de/cdi)" ;
        :history = "Wed Aug 29 14:23:55 2018: cdo -selname,votemper /gfs2/work/shkjocke/models/foci1.9.4-openifs/experiments/FOCI1.9-JK002d/outdata/nemo//FOCI1.9-JK002d_1m_19500101_19501231_grid_T.nc temp.nc\n",
            "Fri Aug 17 12:05:03 2018: ncks -7 -a -L 1 --cnk_dmn time,1 --cnk_dmn time_counter,1 --cnk_dmn z,1 --cnk_dmn depthu,1 --cnk_dmn depthv,1 --cnk_dmn depthw,1 --cnk_dmn deptht,1 --cnk_dmn x,100 --cnk_dmn y,100 FOCI1.9-JK002d_1m_19500101_19501231_grid_T.nc3 FOCI1.9-JK002d_1m_19500101_19501231_grid_T.nc" ;
        :Conventions = "CF-1.5" ;
        :name = "FOCI1.9-JK002d_1m_19500101_19501231" ;
        :description = "ocean T grid variables" ;
        :title = "ocean T grid variables" ;
        :production = "An IPSL model" ;
        :timeStamp = "2018-Aug-17 09:18:48 CEST" ;
        :NCO = "4.4.8" ;
        :CDO = "Climate Data Operators version 1.8.2 (http://mpimet.mpg.de/cdo)" ;

and for the area file

blogin3:~/models/foci1.9.4-openifs/experiments/FOCI1.9-JK002d/scripts $ ncdump -h nemo_areas.nc 
netcdf nemo_areas {
dimensions:
    x_opat = 722 ;
    y_opat = 511 ;
variables:
    double opat.srf(y_opat, x_opat) ;
        opat.srf:long_name = "Areas of opat" ;
        opat.srf:units = "m2" ;

// global attributes:
        :CDI = "Climate Data Interface version 1.8.2 (http://mpimet.mpg.de/cdi)" ;
        :Conventions = "CF-1.6" ;
        :history = "Wed Aug 29 13:51:20 2018: cdo -O selname,opat.srf, /gfs2/work/shkjocke/models/foci1.9.4-openifs/experiments/FOCI1.9-JK002d/outdata/oasis3mct//areas.nc nemo_areas.nc\n",
            "Mon Jul 16 16:06:48 2018: cdo -O merge OIFS_T159_areas_atmo_only.nc NEMO_ORCA05_areas.nc RNF_512x256_areas.nc OIFS_T159_NEMO_ORCA05_RNF_512x256_areas_atmo_only.nc\n",
            "Fri Jul 13 11:41:52 2018: ncks -v opat.srf,opac.srf areas_withrunoff_atmr.nc NEMO_ORCA05_areas.nc\n",
            "Sun Jul 08 01:37:19 2018: cdo merge areas_atmr.nc /gfs1/work/shkjocke/areas_withrunoff.nc /gfs1/work/shkjocke/areas_withrunoff_atmr.nc\n",
            "Sun Jul  8 01:35:47 2018: ncks -v atmr.srf areas.nc areas_atmr.nc" ;
        :CDO = "Climate Data Operators version 1.8.2 (http://mpimet.mpg.de/cdo)" ;
}

Replies (9)

RE: Calculate area-weighted area-mean temperature of a region - Added by Ralf Mueller over 5 years ago

hi!

would be good, if you could upload some data. did you check your calls with latest cdo-1.9.5?

cheers
ralf

RE: Calculate area-weighted area-mean temperature of a region - Added by Joakim Kjellsson over 5 years ago

Hi Ralf

Here are some example files.

And here's the output from my command line

blogin3:~/models/foci1.9.4-openifs/experiments/FOCI1.9-JK002d/scripts $ cdo -fldmean -sellonlatbox,-90,-10,45,70 -selname,votemper -setgridarea,nemo_areas.nc surf_temp.nc mean_temp.nc
cdo fldmean: Started child process "sellonlatbox,-90,-10,45,70 -selname,votemper -setgridarea,nemo_areas.nc surf_temp.nc (pipe1.1)".
cdo(2) sellonlatbox: Started child process "selname,votemper -setgridarea,nemo_areas.nc surf_temp.nc (pipe2.1)".
cdo(3) selname: Started child process "setgridarea,nemo_areas.nc surf_temp.nc (pipe3.1)".
cdo fldmean (Warning): Grid cell bounds not available, using constant grid cell area weights for variable votemper!
cdo(4) setgridarea: Processed 4796246 values from 2 variables over 13 timesteps ( 0.07s )
cdo(3) selname: Processed 4427304 values from 1 variable over 12 timesteps ( 0.07s )
cdo(2) sellonlatbox: Processed 4427304 values from 1 variable over 12 timesteps ( 0.07s )
cdo fldmean: Processed 227520 values from 1 variable over 12 timesteps ( 0.07s )

and CDO version

blogin3:~/models/foci1.9.4-openifs/experiments/FOCI1.9-JK002d/scripts $ cdo --version
Climate Data Operators version 1.8.2 (http://mpimet.mpg.de/cdo)
Compiled: by bzfbmsch on bdata2 (x86_64-unknown-linux-gnu) Jun 20 2017 16:51:04
Compiler: icc -std=gnu99 -O3 -Wl,-rpath,/sw/intel/compilers_and_libraries_2016.3.210/linux/compiler/lib/intel64_lin/ -fopenmp 
 version: icc (ICC) 16.0.3 20160415
Features: DATA PTHREADS OpenMP4 HDF5 NC4/HDF5/threadsafe SZ UDUNITS2 PROJ.4 FFTW3 SSE2
Libraries: HDF5/1.8.17 proj/4.92
Filetypes: srv ext ieg grb1 grb2 nc1 nc2 nc4 nc4c 
     CDI library version : 1.8.2 of Jun 20 2017 16:49:27
 CGRIBEX library version : 1.7.6 of Dec 20 2016 19:55:24
GRIB_API library version : 1.16.0
  NetCDF library version : 4.3.3.1 of Jul 25 2016 00:59:47 $
    HDF5 library version : 1.8.17 threadsafe
 SERVICE library version : 1.4.0 of Jun 20 2017 16:49:07
   EXTRA library version : 1.4.0 of Jun 20 2017 16:49:00
     IEG library version : 1.4.0 of Jun 20 2017 16:49:03
    FILE library version : 1.8.3 of Jun 20 2017 16:49:01

nemo_areas.nc (2.82 MB) nemo_areas.nc areas file
surf_temp.nc (20.7 MB) surf_temp.nc surface temperature

RE: Calculate area-weighted area-mean temperature of a region - Added by Ralf Mueller over 5 years ago

when I run

cdo -setgridarea,nemo_areas.nc surf_temp.nc surfTempWithArea.nc
the resulting file has a variable cell_area, but there is now coordinates attribute, which links the cell areas to the corresponding coordinates. hence the call of sellonlatbox cannot be successfully. I added them manually with
ncatted -a coordinates,cell_area,c,c,'nav_lat nav_lon' surfTempWithArea.nc 
You might give this one a try. I have other issues with my cdo-1.9.5 binary, it produces segfaults when calling
cdo -fldmean -sellonlatbox,-90,-10,45,70 surfTempWithArea.nc surfTempFldmean.nc 
I am not sure, what happens, yet.

hth
ralf

RE: Calculate area-weighted area-mean temperature of a region - Added by Uwe Schulzweida over 5 years ago

Processing the grid cell area with selindexbox/sellonlatbox is not supported. We will add it to the next CDO release.

RE: Calculate area-weighted area-mean temperature of a region - Added by Joakim Kjellsson over 5 years ago

Hi Ralf and Uwe

Thanks for the help! Even though it is not supported yet, I think I still managed to get it to work in the end, by using setlonlatbox on the data file and then on the areas file, and then using setgridarea.
In order to use "sellonlatbox" on the areas file, I had to use a few NCO commands to set longitudes and latitudes etc.
I'm enclosing the script I used as well as the output file if anyone is interested.

Many thanks again!
Joakim

RE: Calculate area-weighted area-mean temperature of a region - Added by Lauritz Lederer Lauritz Lederer about 4 years ago

Dear cdo users,

I´d like to calculate the mean temperature of an area aswell. So obviously it should be

cdo fldmean -selname,tas 1970.nc testfile.nc

But I get the warning:
„Warning: Grid cell bounds not available, using constant grid cell area weights!
Segmentation fault: 11.“

I tried to figure out the reason for the segmentation fault (e.g. a not thread-safe installation), but didn’t find a solution:

cdo -V
Climate Data Operators version 1.9.8 (https://mpimet.mpg.de/cdo)
System: x86_64-apple-darwin18.7.0
CXX Compiler: clang++ -std=gnu++11 -g -O2 -D_THREAD_SAFE -pthread
CXX version : Apple clang version 11.0.0 (clang-1100.0.33.17)
C Compiler: clang -g -O2 -D_THREAD_SAFE -pthread -D_THREAD_SAFE -D_THREAD_SAFE -pthread
C version : Apple clang version 11.0.0 (clang-1100.0.33.17)
F77 Compiler: gfortran -g -O2
F77 version : GNU Fortran (Homebrew GCC 9.2.0_3) 9.2.0
Features: 16GB 4threads C++11 Fortran DATA PTHREADS HDF5 NC4/HDF5 OPeNDAP SZ SSE4_2
Libraries: HDF5/1.10.6
Filetypes: srv ext ieg grb1 nc1 nc2 nc4 nc4c nc5
CDI library version : 1.9.8
cgribex library version : 1.9.4
NetCDF library version : 4.7.3 of Jan 20 2020 00:04:17 $
hdf5 library version : 1.10.6
exse library version : 1.4.1
FILE library version : 1.8.3

On the other hand I thought that the generic gridtype could cause the trouble (because it isn’t supported by cdo as mentioned in various other topics). So I tried the command „ncatted“ to change the attributes and also tried to regrid with cdo. But all my attempts failed.
Whats the problem here? What am I missing?
An example file is in attachment.

Thank you so much in advance,

Lauritz

1970.nc (7.74 MB) 1970.nc

RE: Calculate area-weighted area-mean temperature of a region - Added by Ralf Mueller about 4 years ago

hi!

you input misses the coordinates attribute for the data variable to make it CF-compliant:

cdo -setattribute,tas@coordinates='lon lat' ...

but still fldmean cannot work as expected because CDO cannot compute the cell area for curvilinear cell, I think.

And the missing_value attribute is wrong, so the whole masking does not work properly

cheers
ralf

RE: Calculate area-weighted area-mean temperature of a region - Added by Lauritz Lederer Lauritz Lederer about 4 years ago

Dear Ralf,

thank you for your answeser.
I see the point... what a pitty.
I'll try to find a solution with R instead.
Nevertheless thank you for CDO - it´s a pretty helpful tool.

Cheers,
Laurtz

RE: Calculate area-weighted area-mean temperature of a region - Added by Ralf Mueller about 4 years ago

you can repair your file with these two commands (ncatted comming from the NCO package)

  1. fix attributes and missing value:
    cdo -L  -setattribute,tas@coordinates='lon lat',tas@_FillValue=NaN 1970.nc misVal_coord_1970.nc
  2. rm the wrong attribute:
    ncatted -a missing_value,tas,d,, misVal_coord_1970.nc

hth
ralf

    (1-9/9)