disp('read_SCS_08')
clear
close all
fclose('all');

prtit = 0;

read_parameters_ICEALOT; %Reading parameter files flux conf from D:/data/lb/flux_eval_rhb_06
way_raw_data_flux = 'S:\ICEALOT_2008\KNORR\scs\Raw\';
way_images_flux = 'S:\ICEALOT_2008\KNORR\flux\Raw_Images\';
way_images_comp = 'S:\ICEALOT_2008\KNORR\Combined_instruments\comparisons\Raw_Images\';

% This is the cvs daily file and not the SCS from our system
md='0322'   %input('Input start mon and day to evaluate (0319).... ','s');

prtit = 0;

ict = 0;

e=[way_raw_data_flux, 'KN08' md '_00.csv'];

disp(['Reading SCS data from file  ',e]);
fid=fopen(e,'r');

%R/V Knorr Calliope CSV data file  (Time stamps are GMT)
%Date, Time_GMT, Depth12, Depth35, Air_Temp, WXT5_Ta, WXT5_Pa, dec_lat, dec_lon, EDO_knt, GPS_COG, GPS_SOG, Gyro, IMET_BPR, IMET_SWR, WXT5_Ri, WXT5_Rc, HRH, WXT5_Ua, WXT5_Dm, WXT5_Sm, SBE45C, SBE45S, SBE45T, Salinity, SSCND, Fluorometer, SSTMP, SSV, Wnd_Dir, TWD_5, Wnd_Spd, TWS_5, HdChkSum=52
%3/22/2008, 00:00:31,-99,-99,4.29,4.7,1011.1,41.18956,-72.402187, ,70,11.6,66.7,1011.39,-0.4,0,27.51,32.91,35.8,229,11.6,27.035,27.1761,5.5878,27.1761, ,151.5,4.4827,1458.6,291.1,278.9,13.38,16.5,52
    dmp=fgetl(fid);		    %read 2 header lines
    dmp=fgetl(fid);		    %read 2 header lines

    % for p = 1:4  % short test read
    while feof(fid)==0,
        ict = ict + 1;
        clear st1 stz;
            %st1=fscanf(fid,'%g,%g,%g,%g,%g',[5,inf]);
            dtmp = fgetl(fid);
            data1 = dtmp(21:length(dtmp));
            C = textscan(data1,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','Delimiter',',','CollectOutput',1);
            tmp = C{:};
            scs(ict,:) = tmp(1,:);
            if 1 
%3/22/2008, 00:00:31,
            tim = dtmp(1:20);
            YY = str2num(tim(:,6:9));
            MM = str2num(tim(:,1));
            DD = str2num(tim(:,3:4));
            hh = str2num(tim(:,12:13));
            mm = str2num(tim(:,15:16));
            ss = str2num(tim(:,18:19));;
            %calculate year day
            yd(ict,1) = datenum(YY,MM,DD,hh,mm,ss)-datenum(YY-1,12,31);
            year = num2str(YY);
            end
%                          1                           2                          3                          4                         5
    end;                        %end while feof
if 1  % daily files after 3/21
    Ti = scs(:,3); %IMET_HRH AIR_TEMP
    Tv = scs(:,4); %WXT5_Ta Vaisala Weather Transmitter
    Pv = scs(:,5); %WXT5_Pa
    Lat = scs(:,6);
    Lon = scs(:,7);
    cog = scs(:,9);
    sog = scs(:,10);
    head = scs (:,11);
    Pi = scs(:,12);  % IMET
    Esw = scs(:,13);  % Eppley
    Rvi = scs(:,14); %WXT rain intensity
    Rvc = scs(:,15); %WXT rain accumulation 
    RHi = scs(:,16); %IMET RH
    RHv = scs(:,17); % WXT RH
    WDv = scs(:,18); %WXT Dir Relative
    WSv = scs(:,19); %WXT Spd Relative
    SBsst = scs(:,22); % SeaBid SST
    TSGsst = scs(:,26); % TSG SST
    WDrmy = scs(:,28); % IMET RMY Dir True
    WDvT = scs(:,29);  %WXT Dir True
    WSrmy = scs(:,30); % IMET RMY SPD True
    WSvT = scs(:,31); %WXT Spd True
end
if 1  
    jad = yd;
    s = way_images_flux;

    figure(1);
    subplot(2,1,1),plot(jad,Ti,'r');
    title('Knorr SCS');
    hold
    subplot(2,1,1),plot(jad,Tv,'k'); 
    xlabel('Year Day (UTC)');
    %subplot(2,1,1),plot(jad,sst,'b');title('Knorr SCS');
    legend('IMET','WXT','Location','Best')
    ylabel('C');
    ylim([-5 15])
    
    subplot(2,1,2),plot(jad,RHi,'r');
    title('Knorr SCS');
    hold
    subplot(2,1,2),plot(jad,RHv,'k');
    xlabel('Year Day (UTC)');
    ylabel('%');
    ylim([0 100])
    legend('IMET','WXT','Location','Best')
    print_buffer = [s 'ICEALOT_scs_trh_' md '.png'];
    print('-dpng ', print_buffer);
    
    figure(2);
    subplot(2,1,1),plot(jad,WSv,'r');
    title('Knorr SCS Rel winds');
    hold
    subplot(2,1,1),plot(jad,sog,'k');title('Knorr SCS');    
    ylabel('m/s');
    ylim([0 40])
        legend('WXT rel','SOG','Location','Best')
    
    subplot(2,1,2),plot(jad,WDv,'r.');xlabel(['Year Day ', year]);
    hold
    subplot(2,1,2),plot(jad,cog,'k');
    subplot(2,1,2),plot(jad,head,'b');
    ylabel('Deg');
    ylim([0 360])
    legend('WXT rel','COG','Heading','Location','Best')
    print_buffer = [s 'ICEALOT_scs_SD_' md '.png'];
    print('-dpng ', print_buffer);

figure(3);
    subplot(2,1,1),plot(jad,WSrmy,'r.');
    hold
    subplot(2,1,1),plot(jad,WSvT,'k');title('Knorr SCS True winds');    
    ylabel('Spd (m/s)');
    legend('IMET','XMT','Location','Best')
    ylim([0 30])
    
    subplot(2,1,2),plot(jad,WDrmy,'k.');
    xlabel('Year Day (UTC)');
    hold
    subplot(2,1,2),plot(jad,WDvT,'r');title('Knorr SCS');
    ylabel('Dir (Deg)');
    legend('IMET','WXT','Location','Best')
    ylim([0 360])
    if prtit
    print_buffer = [s 'ICEALOT_scs_SDT_' md '.png'];
    print('-dpng ', print_buffer);
    end
    
    figure(4)
    plot(jad,Pi,'k');title('Knorr SCS');
    hold
    plot(jad,Pv,'r');xlabel(['Year Day ', year])
    ylabel('Pressure (mb)');
    ylim([980 1030])
    legend('IMET','WXT')
    if prtit 
    print_buffer = [s 'ICEALOT_scs_P_' md '.png'];
    print('-dpng ', print_buffer);
    end
        figure(5)
    subplot(2,1,1),plot(jad,Rvi,'k');
    title('Knorr SCS WXT rain');
    ylabel('Rain intensity');
    subplot(2,1,2),plot(jad,Rvc);
    ylabel('Rain accumulation');
    if prtit
    print_buffer = [s 'ICEALOT_WXT_rain_' md '.png'];
    print('-dpng ', print_buffer);
    end
end %skip any plots

%Save temperature profile to a file for calculating pwv
% after save date/time info in same array
[r,c] = size(scs);
scs(:,c+1) = jad;
        if 1
            f=[way_raw_data_flux 'ICEALOT_scs_' md '.txt'];
            flist=fopen(f,'w');
            save( f, 'scs', '-ascii','-tabs')
            fclose('all');
        end;
