disp('read_me2_08')
clear
close all
fclose('all');
% modified on 30-mar-01 by D.E.Lane for Kai 2001 cruise
% modified 10 nov 03 by cfairall for the weller 2003 cruise
% modified 09 jul 04 by hare
% modified April 06 by lbariteau for the AMMA 2006 cruise (changement of
% the variables in the Mean Campbell datalogger)

dddb=input('Input start yearday to evaluate.... ');
ddde=dddb;   %input('Input end yearday to evaluate.... ');
prtit= 0;   %input('Print means to a file?.... y=1 n=0 ');

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


for ddd = dddb:ddde  % loop throuh all the days
    jd=num2str(ddd);
    if ddd<100
        jd=['0' num2str(ddd)];
    end;

    if ddd<10
        jd=['00' num2str(ddd)];
    end;
    clear jad;
    hr='00';							%input('gmt hr','s');
    g=0.0098;						    %adiabatic lapse rate
    eps_w=0.97;						    %emissivity of water
    sig_sb=5.67e-8;				        %Stefan Boltzmann constant
    blk=1;							    %10-min index
    jax=1;
    backchk=0;
    clear st2
    total_num_fields=72;
    ict = 0;

    for jam=0:23,					    %cycle thru 24 hourly stats files
        if jam<10,
            hr=['0' num2str(jam)];
        else
            hr=num2str(jam);
        end;                            %end if jam

        e=[way_raw_data_flux, 'day', jd,'\me2008' jd hr '_raw.txt'];
        %e=[way_raw_data, 'me2008' jd hr '_raw.txt'];

        disp(['Reading means from file  ',e]);
        flist=fopen(e,'r');

        if flist>0,					    %if the file exists,
            %03/19/2008 00:00 Mean 2 Datalogger (SN: 7908)
            %0059032 12.85,5.051,0.72,0.721,-0.147,-0.013,3.006,2.858,-37.14,-1.507,NAN,-273.2,-7999,-7999,0.003,0,0,0,0.002,0,0,0,0.477,0,NAN,0
            %mmss    VDCb    Tb   Cr   Dr    PIRv   PSPv   CT    DT    PIR    PSP  SST4C SST4V  SST2V SST2C
            %         1       2   3     4     5      6     7      8    9      10    11     12    13   14
            dmp=fgets(flist);		    %read header line
            YY = str2num(dmp(7:10));
            MM = str2num(dmp(1:2));
            DD = str2num(dmp(4:5));
            hh = str2num(dmp(12:13));
            % for p = 1:2  % short test read
            while feof(flist)==0,
                if backchk==0,
                    tzilch=jam;
                    backchk=1;
                end;						%end if
                clear st1 stz;
                if isfinite(flist),
                    dtmp = fgetl(flist);
                    data1 = dtmp(9: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','Delimiter',',','CollectOutput',1);
                    tmp = C{:};
                    %test for a bad line and skip
                    if tmp(1,1) < 10
                        data1 % print out bad line
                    end
                    if tmp(1,1) > 10
                        ict = ict + 1;
                        stx(ict,:) = tmp(1,:);
                        tim = dtmp(1:8);
                        mm = str2num(tim(:,1:2));
                        ss = str2num(tim(:,3:8))/1000;
                        if length(mm) == 1 & length(ss) == 1
                            %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;
                        else
                            ict = ict -1;
                        end
                    end
                end;                    %end if st0
            end;                        %end while feof
            fclose(flist);
        end;                            %end if flist
    end;      %end for jam

    %01-Mar-2000 15:45:17

    st2 = stx;
    if 1
        Tsea4 = st2(:,12);
        %press=st2(34,:);
        psp=st2(:,10);
    end



    % % DEW Oct 14, 2006
    % PIR calculation
    sig_sb=5.67e-8;				        %Stefan Boltzmann constant
    Tc=st2(:,7);
    Td=st2(:,8);
    therm=st2(:,9);
    pir=therm+sig_sb*(Tc+273.16).^4-4*sig_sb*((Td+273.16).^4-(Tc+273.16).^4);

    jad = jdn;
    if 1
        tjd = num2str(fix(jad(1)));
        if length(tjd) == 2
            jds(2:3) = tjd;
            jds(1) = '0';  %make jd 3 characters
        end
        if length(tjd) == 3
            jds = tjd;
        end
        s = way_images_flux;
        figure(1);
        plot(jad,Tc,jad,Td,jad,Tsea4);
        legend('Case','Dome','SST4')

        title('PSD Temperatures')
        ylim([-25 10]);
        xlabel(['DOY', year]);ylabel('Temp (C)');
        if prtit
        print_buffer = [s 'ICEALOT_M2_CDSST_' jds '.png'];
        print('-dpng ', print_buffer);
        end
        
        figure;plot(jad,psp,jad,pir);title('PSD');xlabel(['DOY', year]);ylabel('Solar FLux (W/m^2)');
        legend('PSP','PIR')
        ylim([-5 1000]);
        if prtit
        print_buffer = [s 'ICEALOT_M2_PSPPIR_' jds '.png'];
        print('-dpng ', print_buffer);
        end
        
        
        figure;plot(jad,Tsea4);title('PSD SST4');xlabel(['DOY', year]);ylabel('Temperature (degC)');
        ylim([-5 10]);
        if prtit
        print_buffer = [s 'ICEALOT_M2_SST4_' jds '.png'];
        print('-dpng ', print_buffer);
        end
        
        %save d:\data\PSDrad jad psp1 psp2 pir1 pir2  % save data for comparison with SCS using rad_stat_06.m

    end %skip any plots
    np=jax-1;

end % day loop
[r,c] = size(st2);
st2(:,c+1) = jad;
if 0
    f=[way_raw_data_flux 'ICEALOT_m2_' jds '.txt'];
    flist=fopen(f,'w');
    save( f, 'st2', '-ascii','-tabs')
    fclose('all');
end;