%
%Read Hydrolight or Ecolight files
%
%Change your fi variable to match your file. You will also need to change
%the file location.
%
%
%
%%New Data Format:
%
%   Rows: depths
%   Columns: Wavelengths
%   Stacks: Zenith angle
%   4th dim: Azimuthal angle
%
%A single variable will only be in the size (and orientation) in which it
%changes. Examples
%   size(z)         = N x M x 1 x 1  
%   size(zeta)      = N x M x 1 x 1               
%   size(wave)      = 1 x M x 1 x 1
%   size(omega)     = 1 x 1 x 10x 1
%   size(phi)       = 1 x 1 x 1 x 24
%
%Depth is N x M so that it works if you use either optical or geometrical
%depth format. If in geometrical depth format, the z will be repeated for
%each wavelength. Similarly, optical depth format will repeat zeta for each
%wavelength
%
%
%
%written by Sam
%with help from Eli
%
%July 18, 2013
%
%
%July 23
%   --applied large exponent error to all lines
%           (originally just lines with numel>
%
%

% clear all;
% close all;

% clearvars *_full

% fi='Dfiles/0723_MM01/DMM01_003e.txt';
fid=fopen(fi);

%first line
line=fgetl(fid);

if strcmp(line(1:10),'  ECOLIGHT')
    disp('THIS IS AN ECOLIGHT FILE.')
else
    if strcmp(line(1:10),'HYDROLIGHT')
    disp('THIS IS A HYDROLIGHT FILE.')
    else
        error('THIS IS NEITHER AN ECOLIGHT or HYDROLIGHT FILE. YOU DID SOMETHING WRONG')
    end
end



%for each wavelength
%(because HYDROLIGHT does all the info for one wavelength
%then starts over with another wavelength)

%j is chosen to end REALLY LARGE, so that it will never be reached
%an end of file will break this loop
name='misc';
varNum=0;
wvls=[];
for j=1:10^3
    %dimensions are in the next line
    line=fgetl(fid);
    ide=strfind(line,'=');
    dim=str2num(line(ide+1:end));
    %dim=[nz nwave nmu nphi];
    
    %extract the wavlength from the next line
    line=fgetl(fid);
    ids=strfind(line,' ');
    wv=str2num(line(ids(end):end)); %the wvl is the last value on that line
    wvls=[wvls wv];
    %skip fmisc and imisc
    for is=1:100
        line=fgetl(fid);
        if strcmp(line(1:3),'fmu')
            name='fmu';
            names={'fmu'};
            break
        end
    end
    
    data=[];
    %start reading the data for this wavlength
    
    for i=1:10^8
        line=fgetl(fid);
        %if end of file, break for loop
        %OR if new wavelength
        if logical(sum(line==-1)) ...
                || strcmp(line(1:4),'HYDR') ...
                || strcmp(line(1:4),'  EC'); 
            eval([name '=reshapeHE(data,name,dim,wv);'])
            %%%%%%%%%load radsky
            
            break; 
        end
        
        %data is indented 2 lines, variable names are no indented
        %if this is a variable name, begin new variable
        if line(1)~=' '
            
            %(if it's not the first variable, we want the data)
            if i>1
%                 eval([name '=data;'])
                eval([name '=reshapeHE(data,name,dim,wv);'])
                names=[ names {name}];
                data=[];%re-initialize data
            end
            
            %define varaible name
            ids=strfind(line,' ');
            if isempty(ids)
                nameNew=line(1:end);
            else
                nameNew=line(1:ids-1);
            end
            
            %Sometime the same variable name is used for different
            %components. For example (with new name)
            %acoef total                (acoef)
            %acoef component 1          (acoef_1)
            %acoef component 2          (acoef_2)
            %etc...
            
            if strcmp(nameNew,name) || strcmp([nameNew '_'],name(1:end-1))
                %if the last name is the same as the next
%                 pause
%                 nameNew(1:end-1)
%                 name
                varNum=varNum+1;
                nameNew=[nameNew '_' num2str(varNum)];
            else
                varNum=0;
            end
            
            %set the new name
            name=nameNew;
            
            
        else %if it's not a new variable
            
            %read in line
            dat=textscan(line,'%f');
            dat2=dat{:};%convert to vector;
            
            
            %huge exponents sometimes drop the E, which messes me up
            %this little if tries to take care of that
            
            dat3=dat2(1);
            for ii=2:numel(dat2)
                if dat2(ii)<-99
                    dat3(end)=0;
                else
                    dat3=[dat3; dat2(ii)];
                end
            end
%                 pause
           
            data=[data; dat3];%compound data;
        end        
        
        
        
    end
    
    if exist('fmu_full')==0
        for in=1:size(names,2)
            na=char(names(in));
            eval([na '_full=' na ';'])
        
        end
    else
        for in=1:size(names,2)
            na=char(names(in));
            eval([na '_full=[' na '_full ' na '];'])
        
        end
        
    end
    
    if line==-1; break; end
    
    %reshape
    
    
    
end
fclose(fid);

%remove variables used to read the file
% clearvars dat* fi* name ids i line  
% clearvars -except *full wvls