disp('mailbox_allcruise');
%process mailbox .raw files
clear all;
close all;

read_parameters_Stratus

tit = cruise;
lt = length(cruise);
tit(lt+1) = '-';
tit((lt+2):lt+5) = year;
fname = '*.raw';
%s1=['F:\AMMA_2007\RHB\radiometer\Mailbox\Raw\' fname];
%s='F:\AMMA_2007\RHB\radiometer\Mailbox\Raw\';
s1=[way_raw_data_mwr fname];
s=way_raw_data_mwr;
dr=dir(s1);
[n m]=size(dr);
mx=n;
j=1;
nj=1;
kx=1;					  % counts number of 10-min blocks of data
min=0:6;				  % 10-min parts of an hour
jdmx=min/6/24;			  % 10-min fraction of hour
%mx=2*24;

%count lines first
nlines=zeros(length(dr),1);
for i=1:mx
    flnm=dr(i).name;
    fid=fopen([s flnm],'r');
    while ~feof(fid);
        fgetl(fid);
        nlines(i)=nlines(i)+1;
    end
    fclose(fid);
end
gtlines=sum(nlines);

for i=1:mx  %loop through all the raw files
    pack
    tic;
    % preallocate datx for speed
    datx=NaN+zeros(floor(nlines(i)/3),23);
    k=1;
    %clear dath; % why do this?
    dath=NaN+zeros(floor(nlines(i)/3),3);
    flnm=dr(i).name
    fid=fopen([s flnm],'r');

    while ~feof(fid)
        %                   1   2   3   4   5  6    7  8
        stux1=fscanf(fid,'%02d%02d%02d%02d%02d%02d %d %f %f %f',[10,1]);
        stux2=fscanf(fid,'%g %g %g %g %g %g %g %g %g',[9,1]);
        stux3=fscanf(fid,'%g %g %g %g %g %g %g %g %g',[9,1]);
        if (isempty(stux1))
            break % from while loop reading lines of file, then go to next file
        else
            datx(j,1)=datenum(2000+stux1(1),stux1(2),stux1(3),stux1(4),stux1(5),stux1(6))-datenum(2000+stux1(1)-1,12,31);
            if (length(stux2)==9) && (length(stux3)==9)
                datx(j,2)=stux2(9);%vapor
                datx(j,3)=stux3(9);%liquid
                datx(j,4)=stux2(6);%tau 20
                datx(j,5)=stux2(8);%tb 20
                datx(j,6)=stux3(6);%tau 30
                datx(j,7)=stux3(8);%tb 30
                datx(j,8)=stux1(7);%mode
                datx(j,9)=stux1(8);%angle
                datx(j,10)=stux2(7);%cf20
                datx(j,11)=stux3(7);%cf30
                datx(j,12)=stux2(1);%sky20
                datx(j,13)=stux2(2);%nsky20
                datx(j,14)=stux2(3);%bb20
                datx(j,15)=stux2(4);%nbb20
                datx(j,16)=stux2(5);%tbb20
                datx(j,17)=stux3(1);%sky30
                datx(j,18)=stux3(2);%nsky30
                datx(j,19)=stux3(3);%bb30
                datx(j,20)=stux3(4);%nbb30
                datx(j,21)=stux3(5);%tbb30
                datx(j,22)=stux1(9);%pitch
                datx(j,23)=stux1(10);%roll
            else
                datx(j,1:23)=NaN;
            end;

            dath(k,1)=datx(j,1);
            dath(k,2)=datx(j,2);
            dath(k,3)=datx(j,3);

            j=j+1;
            k=k+1;
        end;%isempty
    end;%while feof
    fclose(fid);

    if k>1
        [a b]=size(dath);
        ii=find(dath(:,2)<-1 | dath(:,2)>8);
        [nz mz]=size(ii);
        dath(ii,2)=NaN;
        dath(ii,3)=NaN;
        jdd=dath(:,1);
        jd0=floor(jdd(1));  % first Year Day of data series
        jj=find(isfinite(dath(:,2)));

        if isempty(jj)
            dathr(nj,1)=datx(j-a,1);
            dathr(nj,2)=NaN;
            dathr(nj,3)=NaN;
            dathr(nj,4)=0;
            dathr(nj,5)=NaN;
            dathr(nj,6)=NaN;
        else
            kk=1;			 % if hourly data exists start 10-min calc.
            pp=jdd-floor(jdd); % fractional part of day

            while kk<7
                kj=find(pp>(pp(1)+jdmx(kk)) & pp<(pp(1)+jdmx(kk+1)));  % select 10-min section
                if isempty(kj)
                    datmn(kx,1)=dath(1,1)+jdmx(kk);
                    datmn(kx,2:6)=NaN;
                else
                    datmn(kx,1)=dath(kj(1),1);
                    kkj=find(~isnan(dath(kj,2)));%skip tip cals
                    if isempty(kkj)
                        datmn(kx,2:3)=NaN;
                    else
                        datmn(kx,2)=median(dath(kj(kkj),2));
                        datmn(kx,3)=median(dath(kj(kkj),3));
                    end;
                    y1=dath(kj,2);
                    y=sort(y1);
                    [ny my]=size(y);
                    j1=max(floor(ny*.16),1);j2=max(floor(ny*.84),1);
                    datmn(kx,4)=ny;
                    datmn(kx,5)=(y(j2)-y(j1))/2;
                    y2=dath(kj,3);
                    y=sort(y2);
                    datmn(kx,6)=(y(j2)-y(j1))/2;
                end; %end if isempty kj
                %jdmin(kx)=jd0+jdx(jj)+jdmx(kk); % integrate timestep
                kk=kk+1;
                kx=kx+1;
            end  %while kk
            dathr(nj,1)=dath(1,1);
            dathr(nj,2)=median(dath(jj,2));
            dathr(nj,3)=median(dath(jj,3));
            y1=dath(jj,2);
            y=sort(y1);
            [ny my]=size(y);
            j1=max(floor(ny*.16),1);j2=max(floor(ny*.84),1);
            dathr(nj,4)=ny;
            dathr(nj,5)=(y(j2)-y(j1))/2;
            y2=dath(jj,3);
            y=sort(y2);
            dathr(nj,6)=(y(j2)-y(j1))/2;
        end;
        nj=nj+1;
    end;
toc
end;%for i

pack
%NaN out the tip cals
ii=find(datx(:,2)<-1 | datx(:,2)>8);
datx(ii,2)=NaN;
datx(ii,3)=NaN;
%
daty=sortrows(datx,1);
dathry=sortrows(dathr,1);
datmny=sortrows(datmn,1);
prt_it=1;  %set to 0 to print to screen, 1 to print to a file (i.e., make ascii file)
if prt_it==1

    ss= way_raw_data_mwr;  %'S:\GOMECC_2007\RHB\radiometer\Mailbox\Raw\';

    %ss=s;
    e=[ss cruise '_' year '_micro_sb.txt'];
    flist1=fopen([ss cruise '_' year '_micro_s.txt'],'w');
    flist2=fopen([ss cruise '_' year '_micro_h.txt'],'w');
    flist3=fopen([ss cruise '_' year '_mn.txt'],'w');
else
    flist1=1;flist2=1;
end;

%fprintf(flist1,'%12.6f %10.5f %10.5f \r',daty');
fprintf(flist1,'%12.6f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f \r\n',daty');
%				jd	     vap   liq    tau20  tb30   tau30  tb30   mode   angle  cf20   cf30   sky20  nsky20 bb20   nbb20  tbb20  sky30  nsky30 bb30   nbb30  tbb30 pitch    roll
%                1        2      3      4      5      6      7      8      9     10     11     12     13     14     15     16     17     18     19    20      21     22      23
fprintf(flist2,'%12.6f %10.5f %10.5f %5d %10.5f %10.5f \r\n',dathry');
%						jd	     vap  liq   #obs  sigv   sigl

fprintf(flist3,'%12.6f %10.5f %10.5f %5d %10.5f %10.5f \r\n',datmny');
%						jd	     vap  liq   #obs  sigv   sigl


fclose('all');


%[jdav,y,yvar,nn]=timave_rawmicro(daty(:,1),10,daty);%average to 10-min
%y(:,1)=y(:,1)-5/24/60;%change mean time to beginning of record

%ee=[ss 'micro_10m1.txt'];
%save(ee,'y','-ascii','-tabs');


%save e datx -ascii;
ld = length(dathry);
ejd = max(dathry(:,1));
bjd = dathry(1,1);
% Plot last day
%figure;plot(dathry(:,1),dathry(:,2),dathry(:,1),dathry(:,5));axis([ejd-1 ejd 0 8]);
%xlabel('DOY (2007)');ylabel('Integrated atmospheric water vapor (cm)');
%figure;plot(dathry(:,1),dathry(:,3)*1e4,dathry(:,1),dathry(:,6)*1e4);axis([ejd-1 ejd -10 400]);
%xlabel('DOY (2007)');ylabel('Integrated cloud liquid content (g/m^2)');
%Plot multiple days
%figure;plot(dathry(:,1),dathry(:,2),dathry(:,1),dathry(:,5));axis([bjd ejd 0 8]);

figure;
plot(dathry(:,1),dathry(:,2),'k');axis([bjd ejd 0 9]);
xlabel('Year Day');ylabel('Integrated atmospheric water vapor (cm)');
title(tit)
print_buffer = [way_images_mwr cruise '_' year '_MWR_all_vap.jpg'];
print('-djpeg90 ', print_buffer);
%figure;plot(dathry(:,1),dathry(:,3)*1e4,dathry(:,1),dathry(:,6)*1e4);axis([bjd ejd -10 400]);

figure;
plot(dathry(:,1),dathry(:,3)*1e4,'k');
axis([bjd ejd -30 800]);
xlabel('Year Day');ylabel('Integrated cloud liquid content (g/m^2)');
title(tit)
print_buffer = [way_images_mwr cruise '_' year '_MWR_all_liq.jpg'];
print('-djpeg90 ', print_buffer);


zilch=0;
if zilch
    %******************************   LWP zero bias calculations
    %datmny(:,3) is LWP
    %datmny(:,6) is stand dev of LWP in interval


    hg=0:23;
    hg=.0004+.04167*hg;
    dx=[];for i=1:24;ii=find(abs(datmny(:,1)-floor(datmny(:,1))-hg(i))<.002);dx=[dx ii'];end;%find indices with tip cals
    dy=datmny;
    dy(dx,:)=[];%time series without tip cals
    dz=sortrows(dx',1);%put in time order
    kk=[];
    liq0=zeros(length(datmny(:,1)),1);%set up radiometer bias correction
    for i=1:length(dx)-1
        kk=find(datmny(:,1)>datmny(dz(i),1) & datmny(:,1)<datmny(dz(i+1),1));%find 10-min values in 1 hr interval

        if ~isempty(kk)
            jj=find(1e4*datmny(kk,6)<25);%check for low std of LWP retrieval
            if ~isempty(jj);liq0([kk(1)-1 kk'],1)=min(datmny(kk(jj),3));end;%pick min value for zero bias
        end;
    end;

    ik=find( ~isnan(liq0) & ~isnan(datmny(:,3)));%valid microwave values
    ij=find((datmny(:,6))*1e4>15 & ~isnan(liq0) & ~isnan(datmny(:,3)));%presence of clouds
    disp('Mean LWP when cloud present and mean cloud fraction')
    [mean((datmny(ij,3)-liq0(ij))*1e4) length(ij)/length(ik)];

    figure;plot(datmny(:,1),datmny(:,3)*1e4,datmny(:,1),(datmny(:,3)-liq0)*1e4,'-x',dy(:,1),dy(:,6)*1e4);axis([276 293 -100 500]);
    xlabel('Year Day');ylabel('LWP (g/m^2)');
end;


% No ballonns during GOMECC
if 0
% Need to run GOMECC07_read_mw21_composite and then pwv_sonde_07 to calculate sonde PWV
load S:/GOMECC_2007/RHB/balloon/raw/GOMECC_PWV_sonde


figure
plot(jd,pw,'r*')
hold
%plot(jd,pw,'r*')
plot(dathry(:,1),dathry(:,2),'k');axis([bjd ejd 0 9]);
xlabel('Year Day (UTC)');
ylabel('PWV (cm)');
title(tit)
legend('Sonde','MWR')
print_buffer = [way_images_mwr cruise '_' year '_PWV_sonde_MWR.jpg'];
print('-djpeg90 ', print_buffer);

%save S:/GOMECC_2007/RHB/radiometer/mailbox/raw/MBpwall %% to be used with balloon processing programs
end
