close all 
clear all
clc

%aa1=['/Volumes/Time Machine Backups/others/Helium TIEGCM Data/n_time_w_upflux/'];



for smartindex=1:35
    smartindex

      dd=num2str(smartindex);
    if length(dd)==1, dd=['00' dd]; end
    if length(dd)==2, dd=['0' dd]; end
    
% ncid = netcdf.open(['data_wwind/tiegcm.s_gpi_helium2008_zeroflux_wwind_fixsolar_heelis_' dd '.nc'],'NC_NOWRITE');
ncid = netcdf.open(['tiegcm.s_gpi_2008dec316_heelis_hemajor_' dd '.nc'],'NC_NOWRITE');




%=========================================================================
%Read the netcdf file first


[num_dimmension,num_variables,num_globalattributes,unlimited_dimmension_id] = netcdf.inq(ncid);
%read the information of dimensions.
store_dimension{1,1} = 'dimension_name';
store_dimension{2,1} = 'dimension_length';
for i = 0:(num_dimmension - 1)
    [store_dimension{1,i+2}, store_dimension{2,i+2}] = netcdf.inqDim(ncid,i);

end

%read the global attributes name and global attributes value
store_global_attributes_name_value{1,1}='attributes_name';
store_global_attributes_name_value{2,1}='global_attributes_value';    
for i = 0:(num_globalattributes - 1)
    attributes_name = netcdf.inqAttName(ncid,netcdf.getConstant('NC_GLOBAL'),i);
    global_attributes_value = netcdf.getAtt(ncid,netcdf.getConstant('NC_GLOBAL'),attributes_name);
    store_global_attributes_name_value{1,i+2}=attributes_name;
    store_global_attributes_name_value{2,i+2}=global_attributes_value;
end


%read the information and data of variables
store_variable{1,1} = 'variable_name';
store_variable{2,1} = 'variable_data_type';
store_variable{3,1} = 'variable_dimensions_ID';
store_variable{4,1} = 'num_of_attributes';
store_variable{5,1} = 'All_data_of_variable';
for i = 0:(num_variables - 1)
    [store_variable{1,i+2},store_variable{2,i+2}, store_variable{3,i+2}, store_variable{4,i+2}] = netcdf.inqVar(ncid,i);
    store_variable{5,i+2} = netcdf.getVar(ncid,i);
    num_attributes = store_variable{4,i+2};
    for j = 1:num_attributes
        attribute_name = netcdf.inqAttName(ncid,i,j-1);
        store_variable{6 + (j-1)*3 + 1,i+2} = attribute_name;
        attribute_value = netcdf.getAtt(ncid,i,attribute_name);
        store_variable{6 + (j-1)*3 + 2,i+2} = attribute_value;
    end
end
netcdf.close(ncid);


if smartindex==1
%% for the first day file, the later day files structure are different from the first day
den=store_variable{5,87};   %total mass density
time=store_variable{5,2};
lon=store_variable{5,3};   %geograhpy longitude
lat=store_variable{5,4};
ilev=store_variable{5,6};   %interface level
lev=store_variable{5,5};   %midpoint level
year=store_variable{5,12};
day=store_variable{5,13};
zg=store_variable{5,91};     % geometric height in cm
z=store_variable{5,75};      %geopotential height in cm
he=store_variable{5,73}; 
tn=store_variable{5,60};   %total mass density
o1=store_variable{5,63};
o2=store_variable{5,69};
wn=store_variable{5,88};
nmf2=store_variable{5,89};
hmf2=store_variable{5,90};
mbar=store_variable{5,74};
%n2=store_variable{5,91};

else
 %% for the later day files   
den=store_variable{5,77};   %total mass density
time=store_variable{5,2};
lon=store_variable{5,3};   %geograhpy longitude
lat=store_variable{5,4};
ilev=store_variable{5,6};   %interface level
lev=store_variable{5,5};   %midpoint level
year=store_variable{5,12};
day=store_variable{5,13};
% before inplus the helium module
% zg=store_variable{5,79};     % geometric height in cm
% z=store_variable{5,78};      %geopotential height in cm

% after plusin the helium module
zg=store_variable{5,84};     % geometric height in cm
z=store_variable{5,83};      %geopotential height in cm
he=store_variable{5,78};      %helium mixing ratio
tn=store_variable{5,60};   %total mass density
o1=store_variable{5,63};
o2=store_variable{5,70};
wn=store_variable{5,79};
nmf2=store_variable{5,80};
hmf2=store_variable{5,81};
mbar=store_variable{5,82};
%n2=store_variable{5,83};

end


%% for the 




% save(['data_wwind/tiegcm.s_gpi_helium2008_zeroflux_wwind_fixsolar_heelis_' dd '.mat'],'den','time','lon','lat','ilev','year','day','zg','z','he')

save(['tiegcm.s_gpi_2008dec316_heelis_hemajor_' dd '.mat'],'den','time','lon','lat','ilev','year','day','zg','z','he',...
    'tn','o1','o2','wn','nmf2','hmf2','mbar')



end