0

I have a NetCDF file, here is a truncated output of ncdump -h:

dimensions:
    lat = 720 ;
    lon = 1440 ;
    cft = 64 ;
    natpft = 14 ;
double PCT_CFT(cft, lat, lon) ;
    PCT_CFT:long_name = "percent cft" ;
    PCT_CFT:units = "unitless" ;
    PCT_CFT:_FillValue = -9999. ;
    PCT_CFT:coordinates = "LON LAT" ;
double PCT_NAT_PFT(natpft, lat, lon) ;
    PCT_NAT_PFT:long_name = "percent pft" ;
    PCT_NAT_PFT:units = "unitless" ;
    PCT_NAT_PFT:_FillValue = -9999. ;
    PCT_NAT_PFT:coordinates = "LON LAT" ;

What I need is to extract and sum values from the variable PCT_CFT for the layers 3, 4, 61 and 62 along the dimension cft and then sum up almost all the remaining layers (ie. 5-60, 63, 64) and add these two results to the variable PCT_NAT_PFT as layers 16 and 15 along the dimension natpft respectively.

I would like to achieve this using NCO (or CDO) if possible, I want to avoid using other tools like Python or R... I only know how to sum up the variable across the whole dimension but not across selected layers only - I could therefore probably work around this problem, but I'd like to know if there's a better and cleaner way to do so.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Janina
  • 441
  • 1
  • 4
  • 13

1 Answers1

1

I will assume your input file is in.nc
And your cft layers are one based !?

1) sum along cft layers,3-4,61-62

ncks --msa_usr_rdr -v PCT_CFT -d cft,2,3 -d cft,60,61  in.nc in_1.nc   
ncwa -a cft -y sum  in_1.nc sum_1.nc

2) sum along cft layers, 5-60,63-64

ncks --msa_usr_rdr -v PCT_CFT -d cft,4,59 -d cft,62,63 in.nc in_2.nc            
ncwa -a cft -y sum  in_2.nc sum_2.nc

3) add two new layers to PCT_NAT_PFT

ncks -v PCT_NAT_PFT --msa_usr_rdr -d natpft,0,13 -d natpft,0,1 in.nc in_3.nc

4) add sums from 1), 2) to PCT_NAT_PFT

ncap2 -v -A -s 'PCT_NAT_PFT(15,:,:)=PCT_CFT(:,:);'  sum_1.nc in_3.nc
ncap2 -v -A -s 'PCT_NAT_PFT(14,:,:)=PCT_CFT(:,:);'  sum_2.nc in_3.nc
Henry Fey
  • 99
  • 2
  • The dimension hyperslab arguments (-d ...) to ncks can use 1-based indices directly if you add the -F (or --fortran) argument http://nco.sf.net/nco.html#-F . Then you don't have to remember to subtract one from all the indices give to ncks or ncwa, However the -F option does not work with ncap2 so be careful there. – Charlie Zender Jan 16 '18 at 19:00
  • Thanks. The first two steps work fine. But I'm not sure about the indices of the dimensions of my file. There is obviously some problem when adding the two new layers. I viewed the result (`in_3.nc`) in Panoply and it correctly had 16 layers. However, the indices went from 0 to 13 and the last two were 0 and 1 (and so showing the original first two layers and not the sums). Also, would you mind explaining the `--msa_usr_rdr` option? I have never come across it. [Plus there is a typo in 4) `PC_NAT_PFT` but I cannot edit it due to it being just a very minor change.] – Janina Jan 17 '18 at 12:35