disp('read_mot_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 = 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;

    for jam=0:6:23   %0:6:23,					    %cycle thru 24 hourly stats files
        clear sonraw_dep mot jdn r p y ra pa ya
            ict = 0;

        if jam<10,
            hr=['0' num2str(jam)];
        else
            hr=num2str(jam);
        end;                            %end if jam

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

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

        if fid>0,					    %if the file exists,
            %03/19/2008 00:00 Motion Pak (SN: 7459)
            %0000108 -0.01775956,1.786885,-0.02185792,-0.06284152,0.03961748,-3.827868
            %mmss    x           y         z          x acc      y acc      z acc
            %  1       2         3         4           5          6           7
            dmp=fgetl(fid);		    %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(fid)==0,
                if backchk==0,
                    tzilch=jam;
                    backchk=1;
                end;						%end if
                clear st1 stz;
                if isfinite(fid),
                    dtmp = fgetl(fid);
                    data1 = dtmp(9:length(dtmp));
                    C = textscan(data1,'%f%f%f%f%f%f','Delimiter',',','CollectOutput',1);
                    tmp = C{:};
                    if length(tmp) == 6
                        ict = ict + 1;
                    mot(:,ict) = tmp(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;
                    end;  %end test for complete data line
                end;                    %end if st0
            end;                        %end while feof
            fclose(fid);
        end;                            %end if flist
        jad = jdn;
        r = mot(1,:);
        p = mot(2,:);
        y = mot(3,:);
        ra = mot(4,:);
        pa = mot(5,:);
        ya = mot(6,:);
        jds = num2str(fix(jad(1)));
        s = 'c:\data\icealot\data\motion\Raw_Images\'
        
          if 1
        jad = jdn;
        jds = num2str(fix(jad(1)));
        figure;
        plot(jad,r,'k',jad,p,'r',jad,y,'b');title('PSD Motion');xlabel(['Year Day', year]);
        ylabel('Deg')
        ylim([-1,2])  % pitch jumped to 2 after leaving Tromso
        legend('Roll','Pitch','Yaw')
        print_buffer = [s 'ICEALOT_Mot_' jds '.png'];
        print('-dpng ', print_buffer);

        figure;
        plot(jad,ra,'k',jad,pa,'r',jad,ya,'b');title('PSD Acceleration XYZ');xlabel(['Year Day', year])
        ;ylabel('Deg/sec)');
        ylim([-5,5])
        legend('Ra','Pa','Ya')
        print_buffer = [s 'ICEALOT_Acc_' jds '.png'];
        print('-dpng ', print_buffer);

    end %skip any plots
    np=jax-1;
    sonraw_dep(2,:) = r;
    sonraw_dep(3,:) = p;
    sonraw_dep(4,:) = y;
    mot_spc   % calculate spectra for roll pitch and yaw

    end;      %end for jam

end % day loop

