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.