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
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
2019년 3월 24일
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_
Walter Roberson
2019년 3월 25일
Please experiment with the attached.
Amalu A
2019년 3월 30일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 NetCDF에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!