disp('rdlic_08')
close all;
clear
%Licro 7500
% S/N 000138

bd = 1;
ed = 0;
ddd=input('Input yearday to evaluate.... ');

read_parameters_rhb; %Reading parameter files flux conf from D:/data/lb/flux_eval_rhb_06

jd=num2str(ddd);

if ddd<100
    jd=['0' num2str(ddd)];
end;
if ddd<10
    jd=['00' num2str(ddd)];
end;

plotit=1;
prtit=0;

hr='00';
jhold=0;
jax=1;
jaxx=1;
backchk=0;
st1=[];
st2=[];
st3=[];
jaxd=[];
%if 0 % DEW 5/15/2007
for jam=0:23						%cycle thru 24 hourly stats files
    clear h2o co2 co ho lt p code jdn jad jade sonraw sonraw_dep st1
    if jam<10,
        hr=['0' num2str(jam)];
    else
        hr=num2str(jam);
    end;

    e=[way_raw_data, 'day', jd,'\lic008' jd hr '_raw.txt'];
    %03/22/2008 00:00 Licor 7500 Open Path Analizer (SN: 0772)
    %0000076 182	14.8846	82.196	5.67	100.9
    %0000185 182	14.8814	64.913	5.66	100.9
    %mmss    code     CO2    H2O     T        P
    %  1       2         3      4     5       6
    flist=fopen(e,'r');
    if flist>0,	   				%if the file exists,
        disp(['Reading LiCor file  ',e])
        dmp=fgets(flist);			%read 1st header line
        YY = str2num(dmp(7:10));
        MM = str2num(dmp(1:2));
        DD = str2num(dmp(4:5));
        hh = str2num(dmp(12:13));
        ict = 0;
        while feof(flist)==0,
            %for p = 1:1000
            dtmp=fgetl(flist);
            if length(dtmp) > 30 & length(dtmp) < 60
                data1 = dtmp(9:length(dtmp));
                C = textscan(data1,'%f%f%f%f%f','CollectOutput',1);
                tmp = C{:};
                if length(tmp) == 5
                ict = ict + 1;
                tim = dtmp(1:8);
                mm = str2num(tim(:,1:2));
                ss = str2num(tim(:,3:8))/1000;
                %calculate year day
                yd=datenum(YY,MM,DD,hh,0,0)-datenum(YY-1,12,31);
                jdn(ict,1) = yd + (((mm+(ss/60))/60))/24;
                st1(1,ict) = jdn(ict,1);
                    st1(2,ict) = tmp(1,1);  %  code
                    st1(3,ict) = tmp(1,2);  %  co2
                    st1(4,ict) = tmp(1,3);  % h2o
                    st1(5,ict) = tmp(1,4);  %  T
                    st1(6,ict) = tmp(1,5);  %  p
                end  % test for good data
            end % test for good data
        end  %WHILE LOOP
        ss = size(st1)
        if ss(1) == 0
            break
        end
        fclose(flist);
        %      sonfac=1200 or 600; %sonic runs at 10Hz, 36000 values/hour...
        sonfac=3600*fsonic/60;
        jade=  st1(1,:);  %  Julian day already in col 1   str2num(jd)+((st2(1,:)-tzero)/1000/60/60+tzilch)/24;
        % save only every second
        r = 0;
        for k = 1:100:length(st1);
            r = r + 1;
            lic(bd+r-1,1) = jade(1,k);
            lic(bd+r-1,2) = st1(2,k);
            lic(bd+r-1,3) = st1(3,k);
            lic(bd+r-1,4) = st1(4,k);
            lic(bd+r-1,5) = st1(5,k);
            lic(bd+r-1,6) = st1(6,k);
        end
        ed = bd + r - 1;
        bd = ed + 1;
    end;  %while loop
end;       %end jam
if 1
    jds = num2str(fix(jade(1,1)));
    f=['c:\data\icealot\data\licor\ICEALOT_licor_' jds '.txt'];
    flist=fopen(f,'w');
    save( f, 'lic', '-ascii','-tabs')
    fclose('all');
end;

if 1
    code = lic(:,2);
    co2 = lic(:,3);  %mmol/m3
    h2o = lic(:,4); %mmol/m3
    lt = lic(:,5);  %C
    p = lic(:,6); %kPa
    jad = lic(:,1); % year day
    jds = num2str(fix(jad(1)));
    s = 'c:\data\icealot\data\licor\Raw_Images\';

    figure;
    plot(jad,code,'r.');title('PSD LiCor');xlabel('Year Day (UTC)');
    ylabel('CODE');
    print_buffer = [s 'ICEALOT_lic_CODE_' jds '.png'];
    print('-dpng ', print_buffer);

    figure;
    subplot(2,1,1),plot(jad,co2,'k');title('PSD LiCor');xlabel('Year Day (UTC)');
    ylabel('CO2 mol/m3');
    mnco2 = min(co2) - 5;
    mxco2 = max(co2) + 5;
    ylim([mnco2 mxco2])
    ylim([15 35])
    subplot(2,1,2),plot(jad,h2o,'k');title('PSD LiCor');xlabel('Year Day (UTC)');
    ylabel('H2O mol/m3');
    mnh2o = min(h2o) - 5;
    mxh2o = max(h2o) + 5;
    ylim([mnh2o mxh2o])
    print_buffer = [s 'ICEALOT_lic_CO2_H20_' jds '.png'];
    print('-dpng ', print_buffer);

    figure
    subplot(2,1,1),plot(jad,lt,'k');title('PSD LiCor');xlabel('Year Day (UTC)')
    ylabel('Temperature C');
    ylim([-25,5])
    subplot(2,1,2),plot(jad,p*10,'k');;xlabel('Year Day (UTC)')
    mnp = min(p)*10 - 5;
    mxp = max(p)*10 + 5; ylabel('Pressure (mb)');
    print_buffer = [s 'ICEALOT_Lic__TP' jds  '.png'];
    print('-dpng ', print_buffer);
    ylim([mnp mxp]);
   
end %skip any plots
%sonraw = st1;   % load sonic array into temp array
%licfix  % run despiking routine
%lic_spc  % calculate sonic spectra was rdson2_08.m


