add a new variable to a netcdf file that increases with time nco

邮差的信 提交于 2021-02-11 08:51:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!