问题
I want to add a new variable to an existing netcdf file which simply increases linearly with the time variable of file, that is to say, on the first time slice the variable has the number 1 everywhere, on the second timeslice it is set to 2, etc.
I know how to open the file in python or fortran and define and add the variable and write out the modified file, but I was hoping that there might be a quick and easy way to do this from the command line using nco. I also wondered if I could do it by dumping the file to CDL format with ncdump, hacking it and turning it back to netcdf with ncgen, but that seemed more longwinded and problematic than the python approach.
回答1:
For 1-D array use this
ncap2 -s 'var=array(1,1,$time)' in.nc out.nc
For multi-dimensional arrays just add that to the zero-array
ncap2 -s 'var=array(0,0,/$time,$lat,$lon/)+array(1,1,$time)' in.nc out.nc
The numeric type of the array is determined by the type of the first argument to the array function. See the manual on array()
for details.
来源:https://stackoverflow.com/questions/59315910/add-a-new-variable-to-a-netcdf-file-that-increases-with-time-nco