Search code examples
rrasternetcdfncocdo-climate

Extracting only bottom temperature from 4d NetCDF file


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!


Solution

  • 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.