editing a netcdf variable

浪子不回头ぞ 提交于 2019-12-13 00:48:19

问题


I have a 3 dimensional variable my_var within a netcdf (original.nc) file (150x100x20000). I want to convert all of the elements within this variable from Celsius to Kelvin and then rewrite the variable's new values in a copy (test.nc) of the original file.

%make a copy of the file I’m editing (so I can write the new values to it later
copyfile('original.nc','test.nc');
%gets the attributes of the file
fileattrib('test.nc','+w');
%get id and opens the file for editing
ncid=netcdf.open('test.nc','WRITE');
%gets variable id and specifies the original variable for editing
varid=netcdf.inqVarID(ncid,'my_var');
my_var=ncread('original.nc','my_var');
%converts the data from celcius to kelvin
new_myvar=convtemp(my_var,'C','K'); 
netcdf.putVar(ncid,varid,new_myvar);
netcdf.close(ncid);

I can't get the new_myvar variable to write over the original variable properly. The conversion to Kelvin is occurring correctly as far as I can tell: so the new_myvar variable is correct. I think it has to do with the indexing..but I'm not sure. I tried:

[dimname0,dimlength0]=netcdf.inqDim(ncid,0);
[dimname1,dimlength1]=netcdf.inqDim(ncid,0);
[dimname2,dimlength2]=netcdf.inqDim(ncid,0);
netcdf.putVar(ncid,testid,new_myvar,[0,0,0],[dimlength0-1,dimlength1-1,dimlength2-1]);

thinking that this would specify how many dimensions (and how many elements (the count)) needed to be filled. But it's not rewriting the variable correctly. Can anyone see what I'm doing wrong?

来源:https://stackoverflow.com/questions/23362906/editing-a-netcdf-variable

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