function [new]=reshapeHE(data,name,dim,wvl)
%
%
%For use in readHE.m
%
%
%
%called as
%   [new]=reshapeHE(data,name,dim)
%
%where
%   new: new data reshaped to appropriate size
%
%   data: old data, one long vector
%   name: name, as output from Droot file
%   dim: dimension vector from 2nd line of Droot file
%       dimensions:
%                       Zenith Angle
%                       Depths
%                       Wavelengths
%                       nconc??
%
%This function is basically a lookup table for the reshaping of data from
%the HE files. The name of the variable will be matched with how I want the
%data. EACH type of data will match this format (more on this later).
%
%
%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 1 x 1 x 1  
%   size(zeta)      = N x 1 x 1 x 1               
%   size(wave)      = 1 x 1 x 1 x 1         (remember, they will appended
%                                           side by side for each wvl)
%   size(omega)     = 1 x 1 x 10x 1
%   size(phi)       = 1 x 1 x 1 x 24
%
%Depth is N x 1 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
%
%These variables will be appended side by side as readHE.m reads in each
%wavelength. 
%
%
%Sam Wilson
% Version 1
% July 18, 2014


%name='acoef_3';


idu=strfind(name,'_');
if ~isempty(idu)
    name=name(1:idu-1);
end

%these two variables are input/HE
funOTH={'imisc';'fmisc'};

if sum(strcmp(name,funOTH))==1
    new=data;
    return
end

%these variables will only be a function of Z
funZ={'z';
    'zeta';
    'atten';
    'acoef';
    'bcoef';
    'bbcoef';
    'fMUd';
    'fMUtot';
    'fMUu';
    'Ed';
    'Eod';
    'Eou';
    'Eu';
    'R';
    'albedo'};

%function of Ang in ecolight
funAng_E={'RADrsa';
    'RADsky';
    'RADwla';
    'bndmu';
    'fmu';
    'omega'};

%function of Z and Angle
funZAng_E={'RADupz';
           'RADdnz'};



%if ECOLIGHT file, the dimension is 4
if length(dim)==4
    if strcmp(name,'wave') || strcmp(name,'waveb')
        new=wvl;
    else
        if sum(strcmp(name,funZ))==1
            %function of depth
            new=data;
            
        else if sum(strcmp(name,funAng_E))==1
                %function of angle
                new=permute(data,[3 2 1]);
            else
                if sum(strcmp(name,funZAng_E))==1
                    %function of angle and depth
                    
                    data2=reshape(data,dim(1),dim(2))';
                    new=permute(data2,[1 3 2]);
                else
                    error([name ' is not accounted for in this lookup function'])
                end
            end
        end
        
    end
    
    
    
end


funAng_H1={'fmu';
    'bndmu';
    'omega'};

funAng_H2={'RAD0Ma';
    'RAD0Pa';
    'RADMa';
    'RADPa';
    'radsky'};

funZAng_H={'RADMz';
    'RADPz'};

funZAng_Hsurf={'RAD0Pz'};

funAzi={'phi'; 'bndphi'};


if length(dim)==5
    
    if strcmp(name,'wave') || strcmp(name,'waveb')
        new=wvl;
    else
        
        if sum(strcmp(name,funZ))==1
            %function of depth
            new=data;
        else
            if sum(strcmp(name,funAng_H1))==1;
                %function of zenith
                new=permute(data,[3 2 1]);
            else
                if sum(strcmp(name,funAng_H2))==1;
                    %function of zenith and azimuth
                    data2=reshape(data,dim(1),dim(2));
                    new=permute(data2,[3 4 1 2]);
                else
                    if sum(strcmp(name,funZAng_H))==1;
                        %function of depth, zenith, and azimuth
                        data2=reshape(data,dim(1),dim(2),dim(3));
                        new=permute(data2,[3 4 2 1]);
                    else
                        if sum(strcmp(name,funZAng_Hsurf))==1;
                            %function of depth, zenith, and azimuth, with
                            %possible above surface measurement
                            if numel(data)==dim(1)*dim(2)*dim(3)
                                data2=reshape(data,dim(1),dim(2),dim(3));
                            else
                  ['You''re ' name ' has above surface measurements.']
                                data2=reshape(data,dim(1),dim(2),dim(3)+1);
                            end
                            new=permute(data2,[3 4 2 1]);
                        else
                            if sum(strcmp(name,funAzi))==1
                                new=permute(data,[3 4 2 1]);
                                
                            else
                                error([name ' is not accounted for in this lookup function'])
                            end
                        end
                    end
                end
            end
        end
        
        
    end
    
end
