%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example matlab routine to read in
% Greenland magnetometer data from
% ftp://ftp.space.dtu.dk/data/Ground_magnetometers/Adjusted/
%
% 19 August 2011; cst@space.dtu.dk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
year  = 2011;
month = 01;
day   = 01;

% Example station array
station_array = ['NAQ';
                 'SKT';
                 'STF';
                 'ATU';
                 'GDH';
                 'UMQ';
                 'UPN';
                 'KUV';
                 'THL'];             
             
% This is an example for GDH
for nstation = 5:5
  station = station_array(nstation,:);

  cdf_file = [station(1:3) '_' num2str(year, '%4.4d')...
		  num2str(month, '%2.2d') num2str(day, '%2.2d') '.cdf'];
    
  % Assign a file number to the cdf file
  cdfID = cdflib.open(cdf_file);
  % Store information about the CDF file
  info = cdfinfo(cdf_file);
  % Reading: read the variables
  alldata = cdfread(cdf_file, 'CombineRecords', 1);
  for i=1:size(info.Variables,1)
    data.(char(info.Variables(i))) = alldata{i};
  end
  % Close cdf file
  cdflib.close(cdfID);
end
    
%extract data structure
HEZ=data.HEZ;
time=data.time;
% reset data structure
data=0;

% See information on the cdf file 
disp('INFO CDF FILE:')
info
  
% See information on the variables
disp('INFO FILE TITLE (station code):')
info.GlobalAttributes
  
% See information on the variables
disp('INFO VARIABLES:')
info.Variables
  
% See information on the Attribute 'UNIT'
disp('INFO FIELDNAMES:')
info.VariableAttributes.FIELDNAME
  
% See information on the Attribute 'UNITS'
disp('INFO UNITS:')
info.VariableAttributes.UNITS
  
% See information on the Attribute 'FORMAT'
disp('INFO RECOMMENDED FORMAT:')
info.VariableAttributes.FORMAT
  
% See matlab information on the obtained variables
disp('INFO VARIABLE DIMENSIONS:')
whos time HEZ

