How to write a 3d variable to a netcdf

I want to extract a variable from an existing NetCDF file, Shift its dimensions and write the shifted variable to a new netcdf which is a copy of existing file by rewriting existing variable. So far I am able to extract a variable from an existing NetCDF file and Shift its dimensions but getting error when trying to write it to a new netcdf file
clc
clear all
data=ncread('MISR_AM1_AS_AEROSOL_P125_O075081_F13_0023.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff')
Shift= shiftdim(data,1)
copyfile(which('MISR_AM1_AS_AEROSOL_P125_O075081_F13_0023.nc'),'myfile.nc');
ncdisp('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff');
ncwrite('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff',Shift);
ncdisp('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff');
where
MISR_AM1_AS_AEROSOL_P125_O075081_F13_0023.nc
is the existing nc file
/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff
Is the subfolder and existing variable
and the error which Iam getting is
Error using netcdflib
The NetCDF library encountered an error during execution of 'putVaraDouble' function - 'Start+count exceeds dimension
bound (NC_EEDGE)'.
Error in netcdf.putVar (line 84)
netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/write (line 831)
netcdf.putVar(gid, varid,start, count, varData);
Error in ncwrite (line 75)
ncObj.write(varName, varData, start, stride);
Error in netcdfread (line 9)
ncwrite('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff',Shift);
Any suggetion or correction will be very much helpful,
Thanks alot in advance.

7 件のコメント

Walter Roberson
Walter Roberson 2019 年 3 月 23 日
When you copyfile, the output file still contains all of the variables in the input file. You need to extract all of the variables and attributes and write them to the new file along with changed variable -- unless you use an external utility to delete a variable (which will end up copying etc. itself, but at least you could be sure that it would do so correctly.)
Amalu A
Amalu A 2019 年 3 月 24 日
can I just create a new nc file with only one variable named '/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff' and write the shifted data to that variable?
clc
clear all
data=ncread('MISR_AM1_AS_AEROSOL_P125_O075081_F13_0023.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff')
Shift= shiftdim(data,1);
nccreate('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff1');
ncwrite('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff1',Shift);
ncdisp('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff1');
I used the above code to do so , But getting the error
Error using netcdflib
The number of input elements does not match the variable size.
Error in netcdf.putVar (line 84)
netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/write (line 803)
netcdf.putVar(gid, varid, varData);
Error in ncwrite (line 75)
ncObj.write(varName, varData, start, stride);
Error in netcdfread (line 11)
ncwrite('myfile.nc','/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff1',Shift);
I want to change dimension of the variable
Shift
each time because I have a number of files which have different dimensions for the variable '/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff' in a folder, Iterate through it and do the same for each of the file that is for each file in the folder I want to read, shift dimension, write to a new nc file created, So how can I specify the variable size if the newly created .nc file in advance because it is changing for each input file in the folder ?
Any suggetion will ignite a spark in my head, Thanks
Walter Roberson
Walter Roberson 2019 年 3 月 24 日
I would have thought that what you are doing here would work -- unless, that is, that myfile.nc already exists, in which case you would be overwriting the same variable but with different size. Perhaps you should dynamically create the file name
ncvarname = '/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff');
projectdir = pwd;
dinfo = dir( fullfile(projectdir, 'MISR*AEROSOL_P125*.nc'));
filenames = fullfile(projectdir, {dinfo.name});
nfiles = length(filenames);
for K = 1 : nfiles
filename = filenames{K};
[folder, basename, ext] = fileparts(filename);
newfilename = fullfile(folder, ['SASC_', basename, ext]);
data = ncread(filename, ncvarname);
Shift = shiftdim(data, 1);
ncreate(newfilename, ncvarname);
ncwrite(newfilename, ncvarname, Shift);
end
This would use the same file name as input, but prefixed with SASC_
Amalu A
Amalu A 2019 年 3 月 24 日
編集済み: Amalu A 2019 年 3 月 24 日
Thanks for the reply.
I have applied your code to write that variable but I got the output file like this which is an one dimensional metrix. I have made some altrations to the code some thing like
ncvarname = '/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff'
projectdir = 'D:\inputpath\misrtry\misrcoeff';
dinfo = dir( fullfile(projectdir, 'MISR*AEROSOL_P125*.nc'));
filenames = fullfile(projectdir, {dinfo.name});
nfiles = length(filenames);
for K = 1 : nfiles
filename = filenames{K};
[folder, basename, ext] = fileparts(filename);
newfilename = fullfile(folder, ['SASC_', basename, ext]);
data = ncread(filename, ncvarname);
Shift = shiftdim(data, 1);
nccreate(newfilename, ncvarname,'Dimensions',{'x',size(Shift,1),'y',size(Shift,2),'z',size(Shift,3)},...
'Format','netcdf4');
ncwrite(newfilename, ncvarname, Shift(504,4032,3));
end
And it is saving a file with the desired dimensions, But the problem is all the values of the variable 'Spectral_AOD_Scaling_Coeff' are same somethig like 9.969209968386869e+36.
I suspect something I am doing incorrectly when writing the variable to new nc file. And here is how the output file looks like.
How can I write the values which is exacltly on the 'Shift' variable to the 'Spectral_AOD_Scaling_Coeff' variable in new nc file ?. I checked the individual array of Shift variable with squeeze function and it seems ok, But when writing to output file all values become same.
Thanks
Amalu A
Amalu A 2019 年 3 月 25 日
編集済み: Amalu A 2019 年 3 月 25 日
How can I take input of 'data' variable which I want to shift from one folder and the files I want to write my 'Shift' variable from another folder through iteration, Because Iam writing my variable Shift to a nc file which is already deleted the variable '/4.4_KM_PRODUCTS/Spectral_AOD_Scaling_Coeff' and stored in another folder. I dont want to write the data to a 'newvariable' want to write to a nc file which is already in another folder Which have the same name of 'data'. When I do both things from same folder I cant Shift the data.
Thanks
Walter Roberson
Walter Roberson 2019 年 3 月 25 日
Please experiment with the attached.
Amalu A
Amalu A 2019 年 3 月 30 日
Thank you so much,

サインインしてコメントする。

回答 (0 件)

製品

リリース

R2016a

タグ

質問済み:

2019 年 3 月 23 日

コメント済み:

2019 年 3 月 30 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by