%This program corrects the time of the Carriage data for NACHTT experiment

cd '/psd3data/bao/CU/Emily/CarriageData/work';
bdn=datenum([2011 2 22 15 0 0]);
edn=datenum([2011 3 8 21 34 34.796]);
cvec = ([ 0 0 0 0 0 0]);
tt=edn-bdn;
ddn=datenum([0 0 0 0 0 20]);
fin = fopen('timeold.txt','r');
fout = fopen('timenew.out','a');
while ~feof(fin)
    line=fgetl(fin);
    if( line(18) == 'G')
        ct = sscanf(line, '%d/%d/%d %d');
        cvec(1) = ct(3);
        cvec(2) = ct(1);
        cvec(3) = ct(2);
        cvec(4) = ct(4);
        fprintf(fout,'%s\n',line);
        fprintf(1,'%d/%d/%d %d\n',cvec(1:4));
        continue;
    end
    cvec(5) = str2double(line(1:2));
    cvec(6) = str2double(line(3:7))/1000.;
    cdn = datenum(cvec);
    frac=(cdn-bdn)/tt;
    ndn=cdn-frac*ddn;
    nvec=datevec(ndn);
    line(1:7)=sprintf('%02d%05d',fix(nvec(5)),round(nvec(6)*1000.));
    fprintf(fout,'%s\n', line);
    
end  %while ~feof(f300)
fclose(fin);
fclose(fout);
