5

I have a NetCDF file for ocean temperature. It has 1 variable ('temp') and 4 dimensions (time, lon, lat, and depth). I would like to extract temperature only at maximum depth for each time, lon, and lat to obtain a bottom sea temperature raster brick. I am open to using R, or using Climate Data Operators in terminal.

Attributes of NetCDF file

nc_open('data.pre1980.nc')
File data.pre1980.nc (NC_FORMAT_CLASSIC):

     1 variables (excluding dimension variables):
        float temp[lon,lat,depth,time]   
            standard_name: sea_water_temperature
            long_name: TEMPERATURE
            units: Celsius_scale
            _FillValue: -9.98999971057742e+33
            missing_value: -9.98999971057742e+33
            pointwidth: 1

     4 dimensions:
        time  Size:324   *** is unlimited ***
            standard_name: time
            units: months since 1960-01-01
            calendar: 360_day
            axis: T
        lon  Size:440
            standard_name: longitude
            long_name: longitude
            units: degree_east
            axis: X
        lat  Size:179
            standard_name: latitude
            long_name: latitude
            units: degree_north
            axis: Y
        depth  Size:40
            units: meters
            axis: Z
            gridtype: 0

    4 global attributes:
        CDI: Climate Data Interface version 1.9.6 (http://mpimet.mpg.de/cdi)
        Conventions: CF-1.6
        history: Fri Aug 16 13:33:42 2019: cdo merge data.nc data.nc.1 data.nc.2 data.nc.3 data.nc.4 data.nc.5 data.pre1980.nc
        CDO: Climate Data Operators version 1.9.6 (http://mpimet.mpg.de/cdo)

Thanks in advance!

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
zoek
  • 141
  • 1
  • 7

2 Answers2

2

Try NCO's ncks with a negative hyperslab:

ncks -d depth,-1 in.nc out.nc

There is unlikely to be a more concise solution.

Charlie Zender
  • 5,929
  • 14
  • 19
  • Hello. How would I do that using pynco? I tried the following but did not work: `from nco import Nco from nco.custom import Limit, LimitSingle nco = Nco() opt = [ Limit("depth", -1) ] nco.ncks(input=filename.nc', output='filename_bottomT.nc', options=opt)` – ArnoLB Jul 20 '23 at 12:27
0

CDO does have the same functionality with a negative index to signify counting in reverse from the end of a file (in time, height etc...), so you can also do:

cdo sellevidx,-1 in.nc out.nc

But Charlie is correct, nco is more concise as it requires typing in one less character than with cdo unless of course you want to do this with a file that uses a longer name for the vertical coordinate than "depth" in which case cdo wins ;-)

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86