1
votes

I have global 4D NetCDF files for ocean pH, o2, etc. Each file has 1 variable and 4 dimensions (time, longitude, latitude and depth). I am looking to extract data from the bottom-most depth for each cell that does not contain an NA. I have tried using NCO's ncks with a negative hyperslab:

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

However, this gives me data for only the deepest depth bin (i.e. -5700 m depth bin), outputting NaNs for all areas of the ocean that are shallower. Is there a way to extract data in a similar way, but specify that I want the deepest non-NaN value for each cell?

I am able to use R, CDO, or NCO. Thanks in advance for any help you can provide.

1

1 Answers

3
votes

If you are able to run a simple Python script you can do this using nctoolkit (https://nctoolkit.readthedocs.io/en/latest/installing.html), which uses CDO as a backend. For one file you would do the following:

import nctoolkit as nc
data = nc.open_data("infile.nc")
bottom = nc.open_data("infile.nc")
bottom.bottom_mask()
data.multiply(bottom)
data.vertical_sum()
data.to_nc("outfile.nc")