Trying to write a variable varying with 3 dimensions to MatLab
7 次查看(过去 30 天)
显示 更早的评论
I am trying to write A_sulphod (a variable for sulphate optical depth) that varies with respect to time, latitude, and longitude. My routine works for variables that only vary with respect to one dimension (time), but I am having trouble extending the routine to handle variables which vary over multiple dimensions. Here is my failed attempt - any suggestions?
%-------------------------------------------------------------------
% Define variables
%-------------------------------------------------------------------
sulph = load('new_sulphate.txt')
latit = load('new_latitude.txt')
longit = load('new_longitude.txt')
tim = load('new_year.txt')
t=tim(1,:);
S=sulph(1,:);
L1=latit(1,:);
L2=longit(1,:);
time=t;
nt=length(time);
nlat = length(L1);
nlong = length(L2);
%-------------------------------------------------------------------
% Write A_sulphod netCDF file
%-------------------------------------------------------------------
ncnc= netcdf.create('A_sulphod.nc','NC_WRITE');
%define dimensions
tID=netcdf.defDim(ncnc,'time',nt);
vtID=netcdf.defVar(ncnc,'time','double',tID);
netcdf.putAtt(ncnc,vtID,'units','years');
netcdf.putAtt(ncnc,vtID,'axis','T');
latID=netcdf.defDim(ncnc,'latitude', nlat)
vlatID=netcdf.defVar(ncnc,'latitude','float',latID);
netcdf.putAtt(ncnc,vtID,'units','degrees');
netcdf.putAtt(ncnc,vtID,'axis','lat');
longID=netcdf.defDim(ncnc,'longitude', nlong)
vlongID=netcdf.defVar(ncnc,'longitude','float',longID);
netcdf.putAtt(ncnc,vtID,'units','degrees');
netcdf.putAtt(ncnc,vtID,'axis','long');
varname = 'A_sulphod';
long_name = 'anthropogenic sulphate aerosol optical depth';
unit = '1'; %for example of course
vNID=netcdf.defVar(ncnc,varname,'float',[tID latID longID]); % we need to define axis of the field
netcdf.putAtt(ncnc,vNID,'long_name',long_name); % Give it the long_name
netcdf.putAtt(ncnc,vNID,'units',unit); % The unit
%ncatt('FillValue_','float',-9999.99,nc{varname}); % Missing var fill value
%C(isnan(C)) = -9999.99;
% end define mode
netcdf.endDef(ncnc)
% input data
netcdf.putVar(ncnc,vNID,S);
netcdf.putVar(ncnc,vtID,time);
netcdf.putVar(ncnc,vlatID,L1);
netcdf.putVar(ncnc,vlongID,L2);
%close
netcdf.close(ncnc)
This results in the following errors:
Error using netcdflib The number of input elements does not match the variable size.
Error in netcdf.putVar (line 87) netcdflib(funcstr,ncid,varid,varargin{:});
Error in Make_sulphate (line 57) netcdf.putVar(ncnc,vNID,S);
I have also tried replacing:
netcdf.putVar(ncnc,vNID,S);
with:
netcdf.putVar(ncnc,vNID,S,time,L1,L2);
but that opens another can of error worms
0 个评论
回答(1 个)
Ashish Uthama
2013-1-4
vNID will have a dimension of nt by nlat by nlon. But you are trying to write the variable S which is sulph(1,:) (just a column vector?). The two dimensions, the variable you created in the file and the data your are trying to write do not match.
Consider updating your example with some dummy data (RAND/ONES etc), that will help us run the full example and try showing you the correct approach.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!