0

I have a 2D .nc file with dimensions time and depth that I want to convert to a 4D .nc file. Latitude and longitude are saved as variable names in the 2D file. They are not in a particular order and there is large missing areas as well. The .nc file also contains temperature recordings for each time and depth.

The file header is as follows:

dimensions: 
  time = UNLIMITED ; // (309 currently) 
  level = 2000 ; 

variables: 
  float latitude(time) ; 
        latitude:units = "degree_north" ; 
  float longitude(time) ; 
        longitude:units = "degree_east" ; 
  float temperature(time, level) ; 
        temperature:standard_name = "sea_water_temperature" ; 
        temperature:long_name = "Water Temperature" ; 
        temperature:units = "Celsius" ; 
        temperature:_FillValue = -9999.f ; 
        temperature:missing_value = -9999.f ; 

Is there an easy way using cdo or nco to bin the temperature recordings into a pre-defined latitude x longitude grid so that the resulting .nc file has four dimensions? (time,depth,latitude,longitude)

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • I think this question needs some clarificiation... so the temperature is *only* a function of depth and time? And you want to copy the values so that they are repeated for every grid cell? What does the header of the file look like if you do ncdump -h file.nc ? – ClimateUnboxed May 13 '22 at 16:27
  • @AdrianTompkins The header is: 'dimensions: time = UNLIMITED ; // (309 currently) level = 2000 ; variables: float latitude(time) ; latitude:units = "degree_north" ; float longitude(time) ; longitude:units = "degree_east" ; float temperature(time, level) ; temperature:standard_name = "sea_water_temperature" ; temperature:long_name = "Water Temperature" ; temperature:units = "Celsius" ; temperature:_FillValue = -9999.f ; temperature:missing_value = -9999.f ; ' – jonathanc May 13 '22 at 17:39
  • ah, so basically the file is for a single location.. – ClimateUnboxed May 13 '22 at 18:16

2 Answers2

2

Adrian's answer looks correct to me except you do not need/want the dollarsigns in front of the dimension names, to so try

ncap2 -s 'Temp_new[time,depth,latitude,longitude]=temperature' in.nc out.nc

Documentation is here.

Charlie Zender
  • 5,929
  • 14
  • 19
  • Oh yes. Of course, thanks for the correction. – ClimateUnboxed May 14 '22 at 18:49
  • Sorry for the late response. I've tried the above method but I'm getting the errror 'ncap2: ERROR ncap_cst_mk(): Unrecognized dimension "latitude" in LHS subscripts'. Also, the latitude and longitude are not constant in time, instead each time step represents a unique spatial location – jonathanc May 25 '22 at 16:55
  • I re-read the OP and what you would like to do cannot be done without a much more involved custom script. Sorry ncap2 won't be much help with that. – Charlie Zender May 26 '22 at 18:30
  • 1
    No worries, I was mostly checking out possible alternate methods. I'm able to create a 4D netcdf output by using xarray and binning the results but it's quite time and memory intensive. – jonathanc May 27 '22 at 15:48
1

I think this posting is perhaps related to your question.

Maybe try

ncap2 -s 'Temp_new[time,depth,latitude,longitude]=temperature' in.nc out.nc

I'm not great at ncap2, perhaps Charlie will correct this post if this is not exactly correct. (now edited to correct the error pointed out by Charlie)

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86