disp('read_me1_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=input('Print means to a file?.... y=1 n=0 ');
prtit = 1;

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,'\me1008' jd hr '_raw.txt'];
        %e=[way_raw_data, 'me1008' 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 1 Datalogger (SN: 7911)
            %0055870 13.57,4.493,0.454,0.574,2.966,4.667,0.028,3.69,2.964,59.31,0,4.998,-7999,-7999,0.003,0,0,0.001,0.003,0.001,0,0,0.024,0.053,0,0,0,0
            %mmss    VDCb    Tb   SSv  Tv     RHv   ORGCv ORGSv SST  T     RH   ORG Flow
            %         1       2    3    4      5     6     7     8   9     10    11   12
            
            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(d1,'%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{:};
                    tim = dtmp(1:8);
                    mm = str2num(tim(:,1:2));
                    ss = str2num(tim(:,3:8))/1000;
                    %test for a bad line and skip
                    if length(tmp) == 0 || length(ss) > 1
                        data1 % print out bad line
                    end
                    if length(tmp) > 0 & length(ss) == 1
                        ict = ict + 1;
                        stx(ict,:) = tmp(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;
                        %stx=fscanf(flist,'%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g',[77,1]);
                        %                          1                           2                          3                          4                         5
                        [nr1,nl1]=size(stx);
                        dmp4=fgets(flist);  %read time from blank line
                    end;                    %end if st0
                end
            end;                        %end while feof
            fclose(flist);
        end;                            %end if flist
    end;      %end for jam
    st2 = stx;
    if 1
        aspir_on=st2(:,12);            %aspirator operational (+5V) or backflow (0V)
        org=st2(:,11);
        Tvais=st2(:,9);
        ii=find(Tvais<-100);Tvais(ii)=-999;    %ii=logical(Tvais<-100);Tvais(ii)=-999;
        Rhvais=st2(:,10);
        Tsea=st2(:,8);   %correct for lead resistance 20C 2 wire
        jad = jdn;
        %Compute cummulative precip
        jj = find(~isnan(org));
        cumorg = cumsum(org(jj)); %cummulative precip
        jdm = jad(jj);
        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 = jd;
            end
            s = way_raw_data_flux;
            figure;plot(jad,Tvais,'k');title('PSD Tair)');
            xlabel('Year Day (UTC)');
            ylabel('Temperature (C)');
            ylim([-25,10])
            if prtit
                print_buffer = [s 'ICEALOT_M1_SSTT_' jds '.png'];
                print('-dpng ', print_buffer);
            end
            
            figure;plot(jad,Rhvais);title('PSD RH');xlabel(['DOY', year]);ylabel('Relative Humidity (%)');
            ylim([0 100])
            if prtit
                print_buffer = [s 'ICEALOT_M1_RH_' jds '.png'];
                print('-dpng ', print_buffer);
            end
            
            figure;plot(jad,org);title('PSD org rain rate');xlabel(['DOY', year]);ylabel('Rain Rate (mm/hr)');
            if prtit
                print_buffer = [s 'ICEALOT_M1_RainRate_' jds '.png'];
                print('-dpng ', print_buffer);
            end
            
            figure;
            plot(jdm,cumorg/10);
            title('PSD org rain total');
            xlabel('Year Day (UTC)');
            ylabel('Rain(cm)');
            if prtit
                print_buffer = [s 'ICEALOT_M1_raintotal_' jds '.png'];
                print('-dpng ', print_buffer);
            end
            
            figure;plot(jad,aspir_on);title('PSD aspirator');xlabel(['DOY', year]);ylabel('Backflow Indicator (Volt). If 0V, there is backflow');%axis([max(jad)-1 max(jad) -0.2 5.2])
            ylim([0 5.5])
            if prtit
                print_buffer = [s 'ICEALOT_M1_asp_' jds '.png'];
                print('-dpng ', print_buffer);
            end
            
            if 0
                figure;plot(jad,org_carrier);title('PSD org carrier');xlabel(['DOY', year]);ylabel('Rain Gauge Function (V)');
                ylim([0,6])
                if prtit
                    print_buffer = [s 'ICEALOT_M1_osic_' jds '.png'];
                    print('-dpng ', print_buffer);
                end
                
            end
        end %skip any plots
        np=jax-1;
        
    end  % skip
end % day loop

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