Error in Merge Multiple NetCDF files in a folder to one file using Lavnish Gupta NetCDF add on in Matlab

1 view (last 30 days)
netcat ('D:\Try\OMIHCHOd\','\key_science_data\column_amount','merged')
Kindly check this error, where is the issue ?
All Details about variables of the files are given in comment below:
function netcat(direc,varname,outname,xdim,ydim,zdim,att)
if nargin<7
att=1;
end
if nargin<4
zdim='time';
xdim='lat';
ydim='lon';
end
if direc(length(direc))~='\'
direc=strcat(direc,'\'); %append '\' to direc if does not exists
end
fpat=fullfile(direc, '*.nc');
files = dir(fpat);
bfile = files(1).name;
filename1 = fullfile(direc, bfile); %read first file location
% ncid = netcdf.open(filename,'NOWRITE');
% fmt = netcdf.inqFormat(ncid); %check the format of first file
% netcdf.close(ncid)
longitude=ncread(filename1,ydim);
latitude=ncread(filename1,xdim); %read x and y dimensions from first file
% time=1:length(files);
% time=time';
%
%
% varnew=zeros(length(longitude),length(latitude),length(time));
%
% for s = 1 : length(files)
% bfile = files(s).name;
% filename = fullfile(direc, bfile);
%
% varnew(:,:,s)=ncread(filename,varname);
%
% end
varnew=[]; kold=0;
for s = 1 : length(files)
bfile = files(s).name;
filename = fullfile(direc, bfile);
aux=ncread(filename,varname);
k=length(squeeze(aux(1,1,:)));
varnew(:,:,kold+1:kold+k)=aux;
kold=k+kold;
end
time=1:kold; time=time';
%create the merged netcdf file in netcdf4_classic format
ncid1 = netcdf.open(filename1,'NC_NOWRITE');
varid1 = netcdf.inqVarID(ncid1,varname);
varidx = netcdf.inqVarID(ncid1,xdim);
varidy = netcdf.inqVarID(ncid1,ydim);
ncid = netcdf.create(strcat(direc,outname,'.nc'),'NC_WRITE');
dimidlat = netcdf.defDim(ncid,xdim,length(latitude));
dimidlon = netcdf.defDim(ncid,ydim,length(longitude));
dimidtime = netcdf.defDim(ncid,zdim,length(time));
latitude_ID=netcdf.defVar(ncid,xdim,'double',[dimidlat]);
longitude_ID=netcdf.defVar(ncid,ydim,'double',[dimidlon]);
time_ID=netcdf.defVar(ncid,zdim,'double',[dimidtime]);
var_ID = netcdf.defVar(ncid,varname,'double',[dimidlon dimidlat dimidtime]);
if att==1 % copy the attributes
info=ncinfo(filename1,varname);
len=length(info.Attributes);
for i=0:len-1
attname = netcdf.inqAttName(ncid1,varid1,i);
if ~strcmp(attname,'_FillValue') && ~strcmp(attname,'scale_factor') && ~strcmp(attname,'add_offset')
netcdf.copyAtt(ncid1,varid1,attname,ncid,var_ID);
end
end
info=ncinfo(filename1,xdim);
len=length(info.Attributes);
for i=0:len-1
attname = netcdf.inqAttName(ncid1,varidx,i);
if ~strcmp(attname,'_FillValue') && ~strcmp(attname,'scale_factor') && ~strcmp(attname,'add_offset')
netcdf.copyAtt(ncid1,varidx,attname,ncid,latitude_ID);
end
end
info=ncinfo(filename1,ydim);
len=length(info.Attributes);
for i=0:len-1
attname = netcdf.inqAttName(ncid1,varidy,i);
if ~strcmp(attname,'_FillValue') && ~strcmp(attname,'scale_factor') && ~strcmp(attname,'add_offset')
netcdf.copyAtt(ncid1,varidy,attname,ncid,longitude_ID);
end
end
end
netcdf.endDef(ncid);
netcdf.putVar(ncid,latitude_ID,latitude);
netcdf.putVar(ncid,longitude_ID,longitude);
netcdf.putVar(ncid,time_ID,time);
netcdf.putVar(ncid,var_ID,varnew);
netcdf.close(ncid);
if att==1
%copy the global attributes
check=ncinfo(filename1); check=check.Attributes;
lth=length(check);
for i=1:lth
ncwriteatt(strcat(direc,outname,'.nc'),'/',check(i).Name,check(i).Value);
end
end
end
function netcat(direc,varname,outname,xdim,ydim,zdim,att)
if nargin<7
att=1;
end
if nargin<4
zdim='time';
xdim='lat';
ydim='lon';
end
if direc(length(direc))~='\'
direc=strcat(direc,'\'); %append '\' to direc if does not exists
end
fpat=fullfile(direc, '*.nc');
files = dir(fpat);
bfile = files(1).name;
filename1 = fullfile(direc, bfile); %read first file location
% ncid = netcdf.open(filename,'NOWRITE');
% fmt = netcdf.inqFormat(ncid); %check the format of first file
% netcdf.close(ncid)
longitude=ncread(filename1,ydim);
latitude=ncread(filename1,xdim); %read x and y dimensions from first file
% time=1:length(files);
% time=time';
%
%
% varnew=zeros(length(longitude),length(latitude),length(time));
%
% for s = 1 : length(files)
% bfile = files(s).name;
% filename = fullfile(direc, bfile);
%
% varnew(:,:,s)=ncread(filename,varname);
%
% end
varnew=[]; kold=0;
for s = 1 : length(files)
bfile = files(s).name;
filename = fullfile(direc, bfile);
aux=ncread(filename,varname);
k=length(squeeze(aux(1,1,:)));
varnew(:,:,kold+1:kold+k)=aux;
kold=k+kold;
end
time=1:kold; time=time';
%create the merged netcdf file in netcdf4_classic format
ncid1 = netcdf.open(filename1,'NC_NOWRITE');
varid1 = netcdf.inqVarID(ncid1,varname);
varidx = netcdf.inqVarID(ncid1,xdim);
varidy = netcdf.inqVarID(ncid1,ydim);
ncid = netcdf.create(strcat(direc,outname,'.nc'),'NC_WRITE');
dimidlat = netcdf.defDim(ncid,xdim,length(latitude));
dimidlon = netcdf.defDim(ncid,ydim,length(longitude));
dimidtime = netcdf.defDim(ncid,zdim,length(time));
latitude_ID=netcdf.defVar(ncid,xdim,'double',[dimidlat]);
longitude_ID=netcdf.defVar(ncid,ydim,'double',[dimidlon]);
time_ID=netcdf.defVar(ncid,zdim,'double',[dimidtime]);
var_ID = netcdf.defVar(ncid,varname,'double',[dimidlon dimidlat dimidtime]);
if att==1 % copy the attributes
info=ncinfo(filename1,varname);
len=length(info.Attributes);
for i=0:len-1
attname = netcdf.inqAttName(ncid1,varid1,i);
if ~strcmp(attname,'_FillValue') && ~strcmp(attname,'scale_factor') && ~strcmp(attname,'add_offset')
netcdf.copyAtt(ncid1,varid1,attname,ncid,var_ID);
end
end
info=ncinfo(filename1,xdim);
len=length(info.Attributes);
for i=0:len-1
attname = netcdf.inqAttName(ncid1,varidx,i);
if ~strcmp(attname,'_FillValue') && ~strcmp(attname,'scale_factor') && ~strcmp(attname,'add_offset')
netcdf.copyAtt(ncid1,varidx,attname,ncid,latitude_ID);
end
end
info=ncinfo(filename1,ydim);
len=length(info.Attributes);
for i=0:len-1
attname = netcdf.inqAttName(ncid1,varidy,i);
if ~strcmp(attname,'_FillValue') && ~strcmp(attname,'scale_factor') && ~strcmp(attname,'add_offset')
netcdf.copyAtt(ncid1,varidy,attname,ncid,longitude_ID);
end
end
end
netcdf.endDef(ncid);
netcdf.putVar(ncid,latitude_ID,latitude);
netcdf.putVar(ncid,longitude_ID,longitude);
netcdf.putVar(ncid,time_ID,time);
netcdf.putVar(ncid,var_ID,varnew);
netcdf.close(ncid);
if att==1
%copy the global attributes
check=ncinfo(filename1); check=check.Attributes;
lth=length(check);
for i=1:lth
ncwriteatt(strcat(direc,outname,'.nc'),'/',check(i).Name,check(i).Value);
end
end
end
I am getting this error :
Index exceeds the number of array elements (0).
Error in netcat (line 67)
bfile = files(1).name;
Source:
D:\Try\OMHCHOd\OMI0401.nc
Format:
netcdf4
Global Attributes:
IdentifierProductDOI = '10.5067/AURA/OMI/DATA3010'
ProductGenerationAlgorithm = 'OMHCHOd'
RangeBeginningDate = '2004-10-01'
ContactPersonName = 'Kelly Chance'
GranuleID = 'OMI-Aura_L3-OMHCHOd_2004m1001_v003-2019m0725t193741.nc'
RangeBeginningTime = '00:00:00Z'
RangeEndingTime = '00:00:00Z'
ContactPersonEmail = 'kchange@cfa.harvard.edu'
DataSetQuality = 'OMHCHOd dataset has been computed after excluding level 2 observations with SZA > 70 and cloud fraction > 0.3 and including only the highest quality retrievals as characterized by the level 2 product quality flags. Further details can be found in the OMHCHOd.README file'
ProcessingLevel = 'Level 3'
VersionID = '003'
IdentifierProductionDOIAuthority = 'https://www.doi.org'
references = 'Sun et al., 2018 (https://doi.org/10.5194/amt-11-6679-2018),Gonzalez Abad et al., 2015 (https://doi.org/10.5194/amt-8-19-2015)'
SouthernmostLatitude = -90
ContactPersonRole = 'Lead algorithm scientist'
ObservationArea = 'Global'
institution = 'OMI SIPS'
ProductionDateTime = '2019m0725t193741Z'
NorthernmostLatitude = 90
MaximumCloudFraction = 0.3
ProductGenerationAlgorithmVersion = '0.0.5'
LongitudeResolution = 0.1
title = 'OMI/Aura Formaldehyde (HCHO) Total Column Daily L3 Weighted Mean Global 0.1deg Lat/Lon Grid'
MaximumSZA = 70
Format = 'NetCDF-4'
EasternmostLongitude = 180
InputOriginalFile = 'OMI-Aura_L2-OMHCHO_2004m1001t0003-o01132_v003-2014m0617t164312.he5,OMI-Aura_L2-OMHCHO_2004m1001t0141-o01133_v003-2014m0617t164042.he5,OMI-Aura_L2-OMHCHO_2004m1001t0320-o01134_v003-2014m0617t164015.he5,OMI-Aura_L2-OMHCHO_2004m1001t0459-o01135_v003-2014m0617t164142.he5,OMI-Aura_L2-OMHCHO_2004m1001t0638-o01136_v003-2014m0617t164247.he5,OMI-Aura_L2-OMHCHO_2004m1001t0817-o01137_v003-2014m0617t180159.he5,OMI-Aura_L2-OMHCHO_2004m1001t0956-o01138_v003-2014m0617t164343.he5,OMI-Aura_L2-OMHCHO_2004m1001t1135-o01139_v003-2014m0617t164013.he5,OMI-Aura_L2-OMHCHO_2004m1001t1314-o01140_v003-2014m0617t164040.he5,OMI-Aura_L2-OMHCHO_2004m1001t1452-o01141_v003-2014m0617t164524.he5,OMI-Aura_L2-OMHCHO_2004m1001t1631-o01142_v003-2014m0617t164412.he5,OMI-Aura_L2-OMHCHO_2004m1001t1810-o01143_v003-2014m0617t164511.he5,OMI-Aura_L2-OMHCHO_2004m1001t1949-o01144_v003-2014m0617t164624.he5,OMI-Aura_L2-OMHCHO_2004m1001t2128-o01145_v003-2014m0617t164251.he5,OMI-Aura_L2-OMHCHO_2004m1001t2307-o01146_v003-2014m0617t164236.he5'
Conventions = 'CF-1.7'
source = 'OMI'
DayOfYear = '275'
LatitudeResolution = 0.1
LongName = 'OMI/Aura Formaldehyde (HCHO) Total Column Daily L3 Weighted Mean Global 0.1deg Lat/Lon Grid'
RangeEndingDate = '2004-10-02'
ShortName = 'OMHCHOd'
WesternmostLongitude = -180
history = '2019m0725t193741Z; Original file created by OMI SIPS'
Dimensions:
longitude = 3600
latitude = 1800
Variables:
longitude
Size: 3600x1
Dimensions: longitude
Datatype: single
Attributes:
_FillValue = -1.000000015047466e+30
comment = 'longitude at grid box center'
long_name = 'longitude'
units = 'degrees_east'
valid_min = -180
valid_max = 180
_Storage = 'contiguous'
latitude
Size: 1800x1
Dimensions: latitude
Datatype: single
Attributes:
_FillValue = -1.000000015047466e+30
comment = 'latitude at grid box center'
long_name = 'latitude'
units = 'degrees_north'
valid_min = -90
valid_max = 90
_Storage = 'contiguous'
Groups:
/key_science_data/
Variables:
column_amount
Size: 3600x1800
Dimensions: /longitude,/latitude
Datatype: single
Attributes:
_FillValue = -1.000000015047466e+30
comment = 'mean formaldehyde column amount; units of molecules/cm^2'
long_name = 'HCHO column amount'
units = 'molecules/cm^2'
coordinates = 'longitude latitude'
_Storage = 'contiguous'
column_uncertainty
Size: 3600x1800
Dimensions: /longitude,/latitude
Datatype: single
Attributes:
_FillValue = -1.000000015047466e+30
comment = 'mean column amount uncertainty; units of molecules/cm^2'
long_name = 'HCHO column uncertainty'
units = 'molecules/cm^2'
coordinates = 'longitude latitude'
_Storage = 'contiguous'
/qa_statistics/
Variables:
num_samples
Size: 3600x1800
Dimensions: /longitude,/latitude
Datatype: single
Attributes:
_FillValue = -1
comment = 'number of samples in the calculation considering the summed spatial sensitivity of all satellite pixels in each level 3 grid box'
long_name = 'number of samples'
units = '1'
valid_min = 0
valid_max = 1000
coordinates = 'longitude latitude'
_Storage = 'contiguous'
data_quality_flag
Size: 3600x1800
Dimensions: /longitude,/latitude
Datatype: int8
Attributes:
_FillValue = 2
comment = 'main data quality flag. 0 (good, number of samples > 0.1)1 (good, number of samples < 0.1) 2 (bad / not computed)'
long_name = 'main data quality flag'
falg_values = [0 1 2]
flag_meanings = 'good_number_of_samples_greater_than_0.1 good_number_of_samples_less_than_0.1 bad_or_not_computed'
valid_min = 0
valid_max = 2
coordinates = 'longitude latitude'
_Storage = 'contiguous'
/support_data/
Variables:
albedo
Size: 3600x1800
Dimensions: /longitude,/latitude
Datatype: single
Attributes:
_FillValue = -1.000000015047466e+30
comment = 'surface albedo'
long_name = 'surface albedo'
units = '1'
valid_min = 0
valid_max = 1
coordinates = 'longitude latitude'
_Storage = 'contiguous'

Answers (0)

Community Treasure Hunt

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

Start Hunting!