netcdf read, filling missing data by interpolation and write output nc file
2 views (last 30 days)
Show older comments
Hello
I was trying to read a nc file (file_in), fill missing data and create a new file (file_out) that filled the missing values using interpolation.
I write the code below. However, its doesn't give the desired output. I attached a sample input data if someone could help me in figuring out there the code went wrong or anyother way of ding it. Thanks
function fill_missing_data(file_in, file_out)
ncid_inp = netcdf.open(file_in,'NC_NOWRITE');
ncid_out = netcdf.create(file_out,'NC_CLOBBER');
info_inp = ncinfo(file_in);
[ndims,nvars,ngatts,unlimdimid] = netcdf.inq(ncid_inp);
for ii = 1:ndims
[dimname, ndim] = netcdf.inqDim(ncid_inp,ii-1);
%disp(['Inp: Dimension name:' dimname])
switch dimname
case 'lat'
ndim = lat;
case 'lon'
ndim = lon;
case 'time'
ndim = time;
% dimid(ii) = netcdf.defDim(ncid_out,dimname,netcdf.getConstant('NC_UNLIMITED'));
% otherwise
% for ii=1:length(info_inp.Dimensions)
% [dimname, dimlen] = netcdf.inqDim(ncid_inp,ii-1);
% end
% dimid(ii) = netcdf.defDim(ncid_out,dimname,dimlen);
case 'nc10'
ndim = nc10;
case 'nc11'
ndim = nc11;
case 'nc12'
ndim = nc12;
case 'nc13'
ndim = nc13;
case 'nc14'
ndim = nc14;
case 'nc15'
ndim = nc15;
end
dimid(ii) = netcdf.defDim(ncid_out,dimname,ndim);
end
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%
% Define variables
%
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for ivar = 1:nvars
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid_inp,ivar-1);
varid(ivar) = netcdf.defVar(ncid_out,varname,xtype,dimids);
varnames{ivar} = varname;
for iatt = 1:natts
attname = netcdf.inqAttName(ncid_inp,ivar-1,iatt-1);
attvalue = netcdf.getAtt(ncid_inp,ivar-1,attname);
netcdf.putAtt(ncid_out,ivar-1,attname,attvalue);
end
end
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%
% Copy variables
%
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for ivar = 1:nvars
data = netcdf.getVar(ncid_inp,ivar-1);
[varname,vartype,vardimids,varnatts] = netcdf.inqVar(ncid_inp,ivar-1);
switch varname
case 'EDGEE'
netcdf.putVar(ncid_out,ivar-1,data);
case 'EDGEW'
netcdf.putVar(ncid_out,ivar-1,data);
case 'EDGEN'
netcdf.putVar(ncid_out,ivar-1,data);
case 'EDGES'
netcdf.putVar(ncid_out,ivar-1,data);
case 'LONGXY'
netcdf.putVar(ncid_out,ivar-1,data);
case 'LATIXY'
netcdf.putVar(ncid_out,ivar-1,data);
case 'PSRF'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'time'
netcdf.putVar(ncid_out,ivar-1,data);
case 'TBOT'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'WIND'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'RH'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'FLDS'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'FSDS'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'PRECTmms'
data = fillmissing(data, 'linear') ;
netcdf.putVar(ncid_out,ivar-1,data);
case 'zbot'
netcdf.putVar(ncid_out,ivar-1,data);
end
% netcdf.putVar(ncid_out,ivar-1,data);
end
netcdf.close(ncid_inp);
netcdf.close(ncid_out);
0 Comments
Answers (0)
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!