
% oasisHR2plot.m  plots HR spectra from a file decoded data file
%
% Daniel Cosgrove
% 20 Sep 01

%-----------------------------------------------------------------------------
% Plottype options: 0 2D scrolling plots
%                   1 3D surface contour plot
%-----------------------------------------------------------------------------
plottype = 1;

%-----------------------------------------------------------------------------
% platform options: 0 EPAC
%                   1 Monterey (M1)
%-----------------------------------------------------------------------------
platform = 0;

close all;

currentdir = cd;

if platform == 0
    cd('//Tower/oasisnt/oasis/ep2/');
else
    cd('G:/m1/');
end

[infile inpath] = uigetfile('*.*','Select decoded data file',0,0);
cd(currentdir);

if findstr(inpath,'ep')
    mooring = str2num(inpath(findstr(inpath,'ep')+2));
else
    mooring = 3;
end

if findstr(inpath,'hr')
    ChNum = str2num(inpath(findstr(inpath,'hr')+2));
else
    fprintf('Error finding channel number.\n');
    return
end

switch mooring
case 1
    switch ChNum
    case 1
        calfile = 'SR020202.CSV';   
    case 2
        calfile = 'HR020231.CSV'; 
    case 3
        calfile = 'HR020231.CSV'; 
    end
case 2
    switch ChNum
    case 1
        calfile = 'SR020203.CSV';   
    case 2
        calfile = 'HR020232.CSV';
    case 3
        calfile = 'HR020232.CSV'; 
    end 
case 3
    switch ChNum
    case 1
        calfile = 'SR020203.CSV';   
    case 2
        calfile = 'HR020232.CSV';
    case 3
        calfile = 'HR020232.CSV';
    case 4
        calfile = 'HR020232.CSV';
    end 
end

if infile == 0
    S = [];
    return                            % Return if CANCEL is selected
end
filename = [inpath infile];

fid = fopen(filename,'r');

i = 1;
index = 1;
start =1;

%-----------------------------------------------------------------------------
%Read data in from decoded OASIS data file
%-----------------------------------------------------------------------------
while ~feof(fid)
    datastr = fgetl(fid);
    
    if isempty(datastr)
        while isempty(datastr)
            datastr = fgetl(fid);
        end
    end
    
    while findstr(datastr, '#')
        datastr = fgetl(fid);
    end
    
    if platform == 1 & (ChNum == 1 | ChNum == 2)
        datastr = fgetl(fid);
    end
    
    time = sscanf(datastr,' %f');
    HR(i).tStamp = time(1);
    
    start = findstr(datastr, '0ff0');
    strend = findstr(datastr(start:end),'0d0a'); 
    
    if ~isempty(start)
        % Range of '0ff0' tag. This a 2 byte tag (i.e. 4 hex characters)
        % 'a' and 'b' indicate start and end position for the current 
        % data record. These will be incremented depending on data type
        if strend > start
            
            a = start + 4;
            b = a + 7;
            HR(i).RawTime = hex2dec(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).Temp = hex2float(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).Voltage = hex2float(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).Depth = hex2float(datastr(a:b));
            
            a = b + 1;
            b = a + 3;
            HR(i).Process = hex2dec(datastr(a:b));
            
            a = b + 1;
            b = a + 3;
            HR(i).N = hex2dec(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).Scale = hex2float(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).Do = hex2float(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).Dt = hex2float(datastr(a:b));
            
            a = b + 1;
            b = a + 7;
            HR(i).IntTime = hex2dec(datastr(a:b));
            
            a = b + 1;
            b = a + 3;
            HR(i).FirstPix = hex2dec(datastr(a:b));
            
            a = b + 1;
            b = a + 3;
            HR(i).PixInc = hex2dec(datastr(a:b));
            
            a = b + 1;
            b = a + 3;
            HR(i).PixCount = hex2dec(datastr(a:b));
            
            fprintf('Reading Channel %d, %s...',ChNum,datestr(jul2date(HR(i).tStamp(1),2002),0));
           
            j = 1;
            while b < strend - 8
                a = b + 1;
                b = a + 7;
                HR(i).pixels(j) = hex2float(datastr(a:b));
                j = j + 1;
            end
            
            if j > 1
                fprintf('done\n');
            end
            
            i = i + 1;
            
            clear datastr;
            clear start;
            clear strend;
%             if i > 5
%                 break
%             end
        end
    end
end
fclose(fid);

HR = HRcalibrate(HR,calfile);
HRplot(HR,plottype);

%-----------------------------------------------------------------------------
%Output argos wavelengths to file
%-----------------------------------------------------------------------------
if platform == 0
    filename = 'data.out';
    fprintf(['Outputing ARGOS wavelengths to ' filename '...']);
    fout = fopen(filename,'w');
    fprintf(fout,'Date        Time     412      443       490      510      590\n');
    for k = 1:i-1
        if ~isempty(HR(k).eu)
            fprintf(fout,'%s %f %f %f %f %f\n',...
                datestr(jul2date(HR(k).tStamp(1),2002),0),...
                HR(k).eu(73), HR(k).eu(95), HR(k).eu(130),...
                HR(k).eu(145), HR(k).eu(180));
            %  fprintf(fout,'%s %f %f %f %f %f\n',...
            %             datestr(jul2date(HR(k).tStamp(1),2002),0),...
            %             HR(k).eu(70), HR(k).eu(71), HR(k).eu(72),...
            %             HR(k).eu(73), HR(k).eu(74));
        end
    end
    fclose(fout);
    fprintf('done\n');
end

clear all;