function [beam_code calibration param geomag msis nepow proparam site time]=isr_sri_read_h5(filename)
%Date created: 20 November 2012 by Ashton Reimer
%Date modified: 22 November 2012 by Ashton Reimer
%Modification log: 22 November 2012 by Ashton Reimer - finished routine, more comments
%                  21 November 2012 by Ashton Reimer - finishing routine
%                  26 July 2017 by Ashton Reimer - Adding ability to detect FittedParams.Noise and read it
%
%As of 22 November 2012, this lengthy function will read in every field
%of an SRI ISR processed data file in the HDF5 format (.h5). To determine
%the contents of the HDF5 file, use the info=hdf5info(filename) command and
%then look through the resulting structure (info).



    %Check if the file has noise array in the fitted params
    fitted_info = h5info(filename,'/FittedParams');
    has_noise = false;
    for i=1:1:length(fieldnames(fitted_info.Datasets))
        if strcmp(fitted_info.Datasets(i).Name,'Noise')
            has_noise = true;
        end
    end


    % Start reading file contents
    %'/BeamCodes' found in info.GroupHierarchy.Datasets
    temp=hdf5read(filename,'/BeamCodes');     %Beam codes what is row 4?
    beam_code.beam_num=temp(1,:);                                                                                         
    beam_code.az=temp(2,:);
    beam_code.el=temp(3,:);
    beam_code.unknown=temp(4,:);

    %*************************************************************************************************

    %'/Calibration' found in info.GroupHierarchy.Groups(1)
    %'/Calibration/CalDataBeam' found in info.GroupHierarchy.Groups(1).Datasets(1)
    calibration.caldatabeam=hdf5read(filename,'/Calibration/CalDataBeam');              %beamcode, azimuth, elevation, ksys, scalar for each beam 5 x ?

    %'/Calibration/CalDataBeam' found in info.GroupHierarchy.Groups(1).Datasets(2)
    calibration.caldate=hdf5read(filename,'/Calibration/CalDate');                      %Extracts the date the calibration was performed
    calibration.caldate=calibration.caldate.Data;                                       %Date that the calibration was done.

    %'/Calibration/CalFileBeam' found in info.GroupHierarchy.Groups(1).Datasets(3)
    calibration.calfilebeam=hdf5read(filename,'/Calibration/CalFileBeam');              %Extracts the name of the file used to calibrate the data
    calibration.calfilebeam=calibration.calfilebeam.Data;                               %File used to calibrate the data.

    %'/Calibration/ChirpCorrection' found in info.GroupHierarchy.Groups(1).Datasets(4)
%     calibration.calchirpcorr=hdf5read(filename,'/Calibration/ChirpCorrection');         %Not in every file. In files that were corrected for a chirp problem.


    %*************************************************************************************************


    %'/FittedParams' found in info.GroupHierarchy.Groups(2)
    %'/FittedParams/FitInfo' found in info.GroupHierarchy.Groups(2).Groups
    %'/FittedParams/FitInfo/chi2' found in info.GroupHierarchy.Groups(2).Groups.Datasets(1)
    param.fitinfo.chi2=hdf5read(filename,'/FittedParams/FitInfo/chi2');                  %Something to do with chi^2 of the fit for each range x beam x time

    %'/FittedParams/FitInfo/dof' found in info.GroupHierarchy.Groups(2).Groups.Datasets(2)
    param.fitinfo.dof=hdf5read(filename,'/FittedParams/FitInfo/dof');                    %Something to do with least squares degree of fit for each range x beam x time

    %'/FittedParams/FitInfo/fitcode' found in info.GroupHierarchy.Groups(2).Groups.Datasets(3)
    param.fitinfo.fitcode=hdf5read(filename,'/FittedParams/FitInfo/fitcode');            %Integer stating status of a fit (1 is good, what about the others?)

    %'/FittedParams/FitInfo/nfev' found in info.GroupHierarchy.Groups(2).Groups.Datasets(4)
    param.fitinfo.nfev=hdf5read(filename,'/FittedParams/FitInfo/nfev');                  %Perhaps the iterations required to achieve a fit?

    %'/FittedParams/Altitude' found in info.GroupHierarchy.Groups(2).Datasets(1)
    param.altitude=hdf5read(filename,'/FittedParams/Altitude');                         %Altitude in meters, row=range, column=beam

    %'/FittedParams/Altitude' found in info.GroupHierarchy.Groups(2).Datasets(2)
    param.errors=hdf5read(filename,'/FittedParams/Errors');                             %errors but what? something x something x range x beam x time

    %'/FittedParams/Fits' found in info.GroupHierarchy.Groups(2).Datasets(3)
    param.fits=hdf5read(filename,'/FittedParams/Fits');                                 %fits? what? [fraction,temp,col freq,drift] x [ions in fit] x range x beam x time

    %'/FittedParams/IonMass' found in info.GroupHierarchy.Groups(2).Datasets(4)
    param.ion_mass=hdf5read(filename,'/FittedParams/IonMass');                          %Mass of Ions in fit

    %'/FittedParams/Ne' found in info.GroupHierarchy.Groups(2).Datasets(4)
    param.ne=hdf5read(filename,'/FittedParams/Ne');                                     %Electron density range x beam x time

    %'/FittedParams/Range' found in info.GroupHierarchy.Groups(2).Datasets(5)
    param.range=hdf5read(filename,'/FittedParams/Range');                               %Range along a beam, row=range cell, column=beam

    %'/FittedParams/dNe' found in info.GroupHierarchy.Groups(2).Datasets(6)
    param.dne=hdf5read(filename,'/FittedParams/dNe');                                   %Error in electron density, range x beam x time  

    if has_noise
        %'/FittedParams/Noise' found in info.GroupHierarchy.Groups(2).Datasets(7)
        param.noise=hdf5read(filename,'/FittedParams/Noise');                             %Fitted perturbation noise power, error, measured noise power, 3 x range x beam x time
    end

    %*************************************************************************************************


    %'/Geomag' found in info.GroupHierarchy.Groups(3)
    %'/Geomag/Altitude' found in info.GroupHierarchy.Groups(3).Datasets(1)
    geomag.alt_geom=hdf5read(filename,'/Geomag/Altitude');

    %'/Geomag/Babs' found in info.GroupHierarchy.Groups(3).Datasets(2)
    geomag.Babs=hdf5read(filename,'/Geomag/Babs');

    %'/Geomag/Bx' found in info.GroupHierarchy.Groups(3).Datasets(3)
    geomag.Bx=hdf5read(filename,'/Geomag/Bx');

    %'/Geomag/By' found in info.GroupHierarchy.Groups(3).Datasets(4)
    geomag.By=hdf5read(filename,'/Geomag/By');

    %'/Geomag/Bz' found in info.GroupHierarchy.Groups(3).Datasets(5)
    geomag.Bz=hdf5read(filename,'/Geomag/Bz');

    %'/Geomag/Declination' found in info.GroupHierarchy.Groups(3).Datasets(6)
    geomag.declination=hdf5read(filename,'/Geomag/Declination');

    %'/Geomag/Dip' found in info.GroupHierarchy.Groups(3).Datasets(7)
    geomag.dip=hdf5read(filename,'/Geomag/Dip');

    %'/Geomag/Latitute' found in info.GroupHierarchy.Groups(3).Datasets(8)
    geomag.lat=hdf5read(filename,'/Geomag/Latitude');

    %'/Geomag/Longitude' found in info.GroupHierarchy.Groups(3).Datasets(9)
    geomag.lon=hdf5read(filename,'/Geomag/Longitude');

    %'/Geomag/LshellRe' found in info.GroupHierarchy.Groups(3).Datasets(10)
    geomag.Lshell=hdf5read(filename,'/Geomag/LshellRe');

    %'/Geomag/MLTMidnightUT' found in info.GroupHierarchy.Groups(3).Datasets(11)
    geomag.mltmidnightUT=hdf5read(filename,'/Geomag/MLTMidnightUT');

    %'/Geomag/MagneticLatitude' found in info.GroupHierarchy.Groups(3).Datasets(12)
    geomag.mlat=hdf5read(filename,'/Geomag/MagneticLatitude');

    %'/Geomag/MagneticLongitude' found in info.GroupHierarchy.Groups(3).Datasets(13)
    geomag.mlon=hdf5read(filename,'/Geomag/MagneticLongitude');

    %'/Geomag/Range' found in info.GroupHierarchy.Groups(3).Datasets(14)
    geomag.range_geom=hdf5read(filename,'/Geomag/Range');

    %'/Geomag/ke' found in info.GroupHierarchy.Groups(3).Datasets(15)
    geomag.ke=hdf5read(filename,'/Geomag/ke');

    %'/Geomag/kn' found in info.GroupHierarchy.Groups(3).Datasets(16)
    geomag.kn=hdf5read(filename,'/Geomag/kn');


    %'/Geomag/kpar' found in info.GroupHierarchy.Groups(3).Datasets(17)
    geomag.kpar=hdf5read(filename,'/Geomag/kpar');

    %'/Geomag/kpe' found in info.GroupHierarchy.Groups(3).Datasets(18)
    geomag.kpe=hdf5read(filename,'/Geomag/kpe');

    %'/Geomag/kpn' found in info.GroupHierarchy.Groups(3).Datasets(19)
    geomag.kpn=hdf5read(filename,'/Geomag/kpn');

    %'/Geomag/kvec' found in info.GroupHierarchy.Groups(3).Datasets(20)
    geomag.kvec=hdf5read(filename,'/Geomag/kvec');

    %'/Geomag/kz' found in info.GroupHierarchy.Groups(3).Datasets(21)
    geomag.kz=hdf5read(filename,'/Geomag/kz');


    %*************************************************************************************************


    %'/MSIS' found in info.GroupHierarchy.Groups(4)
    %'/MSIS/AP' found in info.GroupHierarchy.Groups(4).Datasets(1)
    msis.ap=hdf5read(filename,'/MSIS/AP');                                              %AP index but what are the 7 rows about? x time

    %'/MSIS/LocalSolarTime' found in info.GroupHierarchy.Groups(4).Datasets(2)
    msis.sol_time=hdf5read(filename,'/MSIS/LocalSolarTime');                            %Solar time, range x beam x time


    %'/MSIS/SolarDec' found in info.GroupHierarchy.Groups(4).Datasets(3)
    msis.sol_dec=hdf5read(filename,'/MSIS/SolarDec');                                   %Solar declination, range x beam x time

    %'/MSIS/SolarZen' found in info.GroupHierarchy.Groups(4).Datasets(4)
    msis.sol_zenith=hdf5read(filename,'/MSIS/SolarZen');                                %Solar zenith, range x beam x time

    %'/MSIS/Texo' found in info.GroupHierarchy.Groups(4).Datasets(5)
    msis.Texo=hdf5read(filename,'/MSIS/Texo');                                          %Exospheric temperature, range x beam x time

    %'/MSIS/Tn' found in info.GroupHierarchy.Groups(4).Datasets(6)
    msis.Tn=hdf5read(filename,'/MSIS/Tn');                                              %Neutral temperature, range x beam x time

    %'/MSIS/f107' found in info.GroupHierarchy.Groups(4).Datasets(7)
    msis.f107=hdf5read(filename,'/MSIS/f107');                                          %F10.7 index vs. time

    %'/MSIS/f107a' found in info.GroupHierarchy.Groups(4).Datasets(8)
    msis.f107a=hdf5read(filename,'/MSIS/f107a');                                        %F10.7a? index vs. time

    %'/MSIS/nAr' found in info.GroupHierarchy.Groups(4).Datasets(9)
    msis.nAr=hdf5read(filename,'/MSIS/nAr');                                            %nAr (Argon number density per m^3 ) range x beam x time

    %'/MSIS/nH' found in info.GroupHierarchy.Groups(4).Datasets(10)
    msis.nH=hdf5read(filename,'/MSIS/nH');                                              %nH (Atomic Hydrogen number density per m^3 ) range x beam x time

    %'/MSIS/nHe' found in info.GroupHierarchy.Groups(4).Datasets(11)
    msis.nHe=hdf5read(filename,'/MSIS/nHe');                                            %nHe (Helium number density per m^3 ) range x beam x time

    %'/MSIS/nMass' found in info.GroupHierarchy.Groups(4).Datasets(12)
    msis.nMass=hdf5read(filename,'/MSIS/nMass');                                        %nMass (mass density per m^3 ) range x beam x time

    %'/MSIS/nN' found in info.GroupHierarchy.Groups(4).Datasets(13)
    msis.nN=hdf5read(filename,'/MSIS/nN');                                              %nN (Atomic Nitrogen number density per m^3 ) range x beam x time

    %'/MSIS/nN2' found in info.GroupHierarchy.Groups(4).Datasets(14)
    msis.nN2=hdf5read(filename,'/MSIS/nN2');                                            %nN2 (Molecular Nitrogen number density per m^3 ) range x beam x time

    %'/MSIS/nN2D' found in info.GroupHierarchy.Groups(4).Datasets(15)
    msis.nN2D=hdf5read(filename,'/MSIS/nN2D');                                          %nN2D (Molecular Nitrogen???? number density per m^3 ) range x beam x time

    %'/MSIS/nNO' found in info.GroupHierarchy.Groups(4).Datasets(16)
    msis.nNO=hdf5read(filename,'/MSIS/nNO');                                            %nNO (NO number density per m^3 ) range x beam x time

    %'/MSIS/nO' found in info.GroupHierarchy.Groups(4).Datasets(17)
    msis.nO=hdf5read(filename,'/MSIS/nO');                                              %nO (Atomic Oxygen number density per m^3 ) range x beam x time

    %'/MSIS/nO2' found in info.GroupHierarchy.Groups(4).Datasets(18)
    msis.nO2=hdf5read(filename,'/MSIS/nO2');                                            %nO2 (Molecular Oxygen number density per m^3 ) range x beam x time

    %'/MSIS/nOanom' found in info.GroupHierarchy.Groups(4).Datasets(19)
    msis.nOanom=hdf5read(filename,'/MSIS/nOanom');                                      %nOanom (Atomic Oxygen???? number density per m^3 ) range x beam x time

    %'/MSIS/qOp' found in info.GroupHierarchy.Groups(4).Datasets(20)
    msis.qOp=hdf5read(filename,'/MSIS/qOp');                                            %qOp ???? range x beam x time


    %*************************************************************************************************


    %'/NeFromPower' found in info.GroupHierarchy.Groups(5)
    %'/NeFromPower/Altitude' found in info.GroupHierarchy.Groups(5).Datasets(1)         ALL RANGES ARE THE SUB SAMPLED RANGES AND SO MAY BE 
    %                                                                                   MORE THAN THE PROCESSED FIT RANGES
    nepow.altitude_nfp=hdf5read(filename,'/NeFromPower/Altitude');                      %altitude range x beam

    %'/NeFromPower/Ne_Mod' found in info.GroupHierarchy.Groups(5).Datasets(2)
    nepow.Ne_Mod_nfp=hdf5read(filename,'/NeFromPower/Ne_Mod');                          %Ne_Mod range x beam x time

    %'/NeFromPower/Ne_NoTr' found in info.GroupHierarchy.Groups(5).Datasets(3)
    nepow.Ne_NoTr_nfp=hdf5read(filename,'/NeFromPower/Ne_NoTr');                        %Ne_NoTr??? range x beam x time

    %'/NeFromPower/Altitude' found in info.GroupHierarchy.Groups(5).Datasets(4)
    nepow.range_nfp=hdf5read(filename,'/NeFromPower/Range');                            %range range

    %'/NeFromPower/SNR' found in info.GroupHierarchy.Groups(5).Datasets(5)
    nepow.SNR_nfp=hdf5read(filename,'/NeFromPower/SNR');                                %SNR range x beam x time

    %'/NeFromPower/dNeFrac' found in info.GroupHierarchy.Groups(5).Datasets(6)
    nepow.dNeFrac_nfp=hdf5read(filename,'/NeFromPower/dNeFrac');                        %dNeFrac range x beam x time


    %*************************************************************************************************


    %'/ProcessingParams' found in info.GroupHierarchy.Groups(6)
    %'/ProcessingParams/AeuRx' found in info.GroupHierarchy.Groups(6).Datasets(1)
    proparam.AeuRx=hdf5read(filename,'/ProcessingParams/AeuRx');                        %AeuRx vs. time

    %'/ProcessingParams/AeuTotal' found in info.GroupHierarchy.Groups(6).Datasets(2)
    proparam.AeuTotal=hdf5read(filename,'/ProcessingParams/AeuTotal');                  %AeuTotal vs. time

    %'/ProcessingParams/AeuTx' found in info.GroupHierarchy.Groups(6).Datasets(3)
    proparam.AeuTx=hdf5read(filename,'/ProcessingParams/AeuTx');                        %AeuTx vs. time

    %'/ProcessingParams/BaudLength' found in info.GroupHierarchy.Groups(6).Datasets(4)
    proparam.BaudLength=hdf5read(filename,'/ProcessingParams/BaudLength');              %BaudLength in seconds

    %'/ProcessingParams/ProcessingTimeStamp' found in info.GroupHierarchy.Groups(6).Datasets(5)
    proparam.ProcessingTimeStamp=hdf5read(filename,'/ProcessingParams/ProcessingTimeStamp');               %ProcessingTimeStamp
    proparam.ProcessingTimeStamp=proparam.ProcessingTimeStamp.Data;

    %'/ProcessingParams/PulseLength' found in info.GroupHierarchy.Groups(6).Datasets(6)
    proparam.PulseLength=hdf5read(filename,'/ProcessingParams/PulseLength');            %PulseLength in seconds

    %'/ProcessingParams/RxFrequency' found in info.GroupHierarchy.Groups(6).Datasets(7)
    proparam.RxFrequency=hdf5read(filename,'/ProcessingParams/RxFrequency');            %RxFrequency in Hz

    %'/ProcessingParams/TxFrequency' found in info.GroupHierarchy.Groups(6).Datasets(8)
    proparam.TxFrequency=hdf5read(filename,'/ProcessingParams/TxFrequency');            %TxFrequency in Hz
    %'/ProcessingParams/TxPower' found in info.GroupHierarchy.Groups(6).Datasets(9)
    proparam.TxPower=hdf5read(filename,'/ProcessingParams/TxPower');                    %TxPower in Watts


    %*************************************************************************************************


    %'/Site' found in info.GroupHierarchy.Groups(7)
    %'/Site/Altitude' found in info.GroupHierarchy.Groups(7).Datasets(1)
    site.altitude=hdf5read(filename,'/Site/Altitude');                                  %site altitude in meters

    %'/Site/Code' found in info.GroupHierarchy.Groups(7).Datasets(2)
    site.code=hdf5read(filename,'/Site/Code');                                          %SRI site code number

    %'/Site/Latitude' found in info.GroupHierarchy.Groups(7).Datasets(3)
    site.latitude=hdf5read(filename,'/Site/Latitude');                                  %site latitude 

    %'/Site/Longitude' found in info.GroupHierarchy.Groups(7).Datasets(4)
    site.longitude=hdf5read(filename,'/Site/Longitude');                                %site longitude

    %'/Site/MagneticLatitude' found in info.GroupHierarchy.Groups(7).Datasets(5)
    site.mlat=hdf5read(filename,'/Site/MagneticLatitude');                              %site MagneticLatitude

    %'/Site/MagneticLocalTimeMidnight' found in info.GroupHierarchy.Groups(7).Datasets(6)
    site.mltmidnight=hdf5read(filename,'/Site/MagneticLocalTimeMidnight');              %site MagneticLocalTimeMidnight

    %'/Site/MagneticLongitude' found in info.GroupHierarchy.Groups(7).Datasets(7)
    site.mlon=hdf5read(filename,'/Site/MagneticLongitude');                             %site MagneticLongitude 

    %'/Site/Name' found in info.GroupHierarchy.Groups(7).Datasets(8)
    site.name=hdf5read(filename,'/Site/Name');                                          %site Name 
    site.name=site.name.Data;

    %*************************************************************************************************


    %'/Time' found in info.GroupHierarchy.Groups(8)
    %'/Time/Day' found in info.GroupHierarchy.Groups(8).Datasets(1)     ALL IN THE FORMAT [start time, stop time] x time gate
    time.day=hdf5read(filename,'/Time/Day');                                            %Day

    %'/Time/MagneticLocalTimeSite' found in info.GroupHierarchy.Groups(8).Datasets(2)
    time.mlt=hdf5read(filename,'/Time/MagneticLocalTimeSite');                          %MagneticLocalTimeSite

    %'/Time/Month' found in info.GroupHierarchy.Groups(8).Datasets(3)
    time.month=hdf5read(filename,'/Time/Month');                                        %Month

    %'/Time/UnixTime' found in info.GroupHierarchy.Groups(8).Datasets(4)
    time.unix_time=hdf5read(filename,'/Time/UnixTime');                                 %UnixTime (epoch time) in seconds
    time.unix_time=double(time.unix_time);
    
    %'/Time/Year' found in info.GroupHierarchy.Groups(8).Datasets(5)
    time.year=hdf5read(filename,'/Time/Year');                                          %Year

    %'/Time/doy' found in info.GroupHierarchy.Groups(8).Datasets(6)
    time.doy=hdf5read(filename,'/Time/doy');                                            %doy

    %'/Time/dtime' found in info.GroupHierarchy.Groups(8).Datasets(7)
    time.dtime=hdf5read(filename,'/Time/dtime');                                        %dtime


end