0

I have two netCDF files. One file elevation.nc contains just the 'elevation' of an area. Other file climate.nc has ('lat', 'lon','prcp', 'temp'). I have used the following:

cdo merge  elevation.nc climate.nc merged.nc

The merge.nc file only has on single prcp and temp from the date that the elevation had been recorded.

How to get time varying prcp and temp in merged.nc similar to climate.nc but also with the static variable elevation?

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Sayantan4796
  • 169
  • 1
  • 10
  • did you try reversing the order? cdo merge climate.nc elevation.nc merged.nc – ClimateUnboxed Feb 16 '23 at 12:50
  • 2
    That should work @AdrianTompkins. `merge` can be tricky in CDO. The user guide says the files should have "the same number of timesteps". However, this is not quite true when one of the files has one or none. In the code you have given it will add the first time step from climate to elevation. But if you reverse the order it will add elevation to all of the time steps of climate. Hope that clarifies – Robert Wilson Feb 16 '23 at 15:48
  • This is exactly what the issue was @RobertWilson. Thank You. – Sayantan4796 Feb 20 '23 at 08:06

3 Answers3

1

You only need to reverse the order of the input files to ensure that the multi-step file is the first input file as cdo takes the dimensions from that. So this would work:

cdo merge climate.nc elevation.nc merged.nc

If you do it in the "wrong" order (i.e. the single timestep input file first) cdo explicitly tells you in a warning that it is chopping off all the remaining steps of the time-dependent file in order to match the first input file:

cdo    merge (Warning): Input stream 1 has 1 timestep. Stream 2 has more timesteps, skipped!

Reversing the input order to have the longest input file first, everything works fine and no warning is given.

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

the main issue was found to be that the elevation.nc has a single time step attribute. It is essential to remove this attribute before the merge. hence, the steps shall involve (using ncks from nco):

ncwa -a time elevation.nc test1.nc
ncks -O -x -v time test1.nc test2.nc #removing the time attribute
ncks -A -v hgt test2.nc  climate.nc #appending the hgt or elevation.nc with climate.nc
Sayantan4796
  • 169
  • 1
  • 10
  • 1
    Sure, but I just tested my cdo solution and it works fine, you just need to ensure the time-dependent file is first and the single step file is second, so you could have simply reversed the order of the input files in the cdo command. Much simpler. – ClimateUnboxed Mar 01 '23 at 08:01
1

This would be more concise, though you'll have to test it to be sure it works as expected:

ncwa -O -x -v time -a time elevation.nc test1.nc
ncks -A -v hgt test1.nc climate.nc
Charlie Zender
  • 5,929
  • 14
  • 19