Processing APMP_Cal data from CRL Japan

Off[General::spell1];

dataPath = "g:\\APMP_Cal\\";

Definitions

Read data file, ReadCCTF["filename"]

\(\(ReadCCTF[name_] := 
        Module[{filename, infile, str, flag = False, data = {}, 
            format = Table[Number, {i, 18}]}, 
filename = 
            StringJoin[dataPath, name]; infile = OpenRead[filename]; 
          format[[2]] = \(format[[18]] = 
              Word\); 
While[
            True, 
If[\((str = Read[infile, String])\) == 
                EndOfFile, 
              Break[]]; 

instr = 
              StringToStream[str]; 
AppendTo[data, 
              Flatten[ReadList[instr, format]]]; 
Close[
              instr];
]; 
Close[
            infile]; 
Print["> Read ", 
            Length[data], " tracks from ", 
            filename]; 
Return[
            data];
];\)\)

Select data columns

SV[list_] := 
        list[[1]];
TrackMJD[list_] := 
        list[[3]];
TrackTime[list_] := 
        list[[4]];
TrackLength[list_] := 
        list[[5]];
Elevation[list_] := 
        list[[6]];
Azimuth[list_] := 
        list[[7]];
RefSV[list_] := 
        list[[8]]/
          10;
SlopeSV[list_] := 
        list[[9]];
RefGPS[list_] := 
        list[[10]]/
          10;
SlopeGPS[list_] := 
        list[[11]];

RefSV2[list_] := list[[26]]/10;
RefGPS2[list_] := list[[28]]/10;
TrackLength2[list_] := list[[23]];

Catenate two data sets together, AddCCTF[data1,data2]

AddCCTF[data1_, data2_] := 
        FlattenAt[Append[data1, data2], -1];

Sort data (into time order then SV order), SortCCTF[data]

OrderCCTF[a_, b_] := 
        If[a[[3]] == 
            b[[3]], 
          If[a[[4]] == 
              b[[4]], 
            First[a] < First[b], 
            a[[4]] < 
              b[[4]]], 
          a[[3]] < 
            b[[3]]];

SortCCTF[data_] := Sort[data, OrderCCTF[#1, #2] &];

Merge two CCTF data sets on matching tracks, MergeCCTF[data1,data2]

\(\(MergeCCTF[data1_, data2_] := 
        Module[{d1, d2, i, j, data = {}}, 
d1 = 
            SortCCTF[data1]; 
d2 = 
            SortCCTF[data2]; 
j = 1; 
For[
            i = 1, i ≤ 
              Length[d1], \(i++\), 
While[\((j <= 
                    Length[d2])\)\  && \ 
\((\((d2[[j, 3]] < 
                        d1[[i, 
                          3]])\)\  || \ 
\((\((d2[[j, 
                              3]] == 
                            d1[[i, 
                              3]])\)\  && \ 
\((\((d2[[j, 4]] < 
                                d1[[i, 
                                  4]])\)\  || \ 
\((\((d2[[j, 
                                      4]] == 
                                    d1[[i, 
                                      4]])\)\  && \ 
\((d2[[j, 1]] < 
                                    d1[[i, 
                                      1]])\))\))\))\))\), 
\(j++\)]; 
If[\((j ≤ 
                    Length[d2])\)\  && 
\((d2[[j, 3]] == 
                    d1[[i, 
                      3]])\) && \((d2[[
                      j, 4]] == 
                    d1[[i, 
                      4]])\)\  && \ \((d2[[j, 1]] == 
                    d1[[i, 
                      1]])\), 
AppendTo[
                data, Flatten[{d1[[i]], 
                    d2[[
                      j]]}]]];
]; 
Print["> First ", Length[d1], " tracks, second ", 
            Length[d2], " tracks, matching ", 
            Length[data], " tracks"]; 
Return[
            data];
];\)\)

Miscellaneous

TimeValue[list_] := 
        Module[{val, h, m, s}, 
val = 
            TrackTime[list]; 
h = 
            Quotient[val, 10000]; 
m = 
            Quotient[Mod[val, 10000], 100]; 
s = 
            Mod[val, 100]; 
Return[
            3600 h  +  60 m  +  
              s];
];

MakeXY[list1_, list2_] := 
        Module[{i, l1, l2}, 
l1 = 
            list1; 
l2 = list2; 
Return[
            Table[{l1[[i]], l2[[i]]}, {i, 
                Length[l1]}]];
];

\(\(FilterTrackLength[list_, threshold_] := 
        Module[{i, j = 0, outdata = {}}, 
For[i = 1, 
            i ≤ 
              Length[list], \(i++\), 
If[\((list[\([i, 
                        3]\)] >= \ 
                    threshold)\)\  && \ \((list[\([i, 
                        4]\)]\  ≥ \ 
                    threshold)\), 
	\(j++\); 
              AppendTo[
                outdata, {list[\([i, 1]\)], 
                  list[\([i, 2]\)]}]]]; 
Print[
            j, \ " common tracks out of ", 
            Length[list], " were of length greater than or equal to ", 
            threshold, " seconds."]; 
Return[
            outdata];
];\)\)

DateValue[list_] := 
        TrackMJD[list] + TimeValue[list]/86400;

dataHost = ReadCCTF["host.crl.dat"];
(*Host  reported values:*)
RepHostIntDly = 
        49.7;
     RepHostRefDly = 515.9;
   RepHostAntDly  = 250;
RepHostDly = 
          RepHostIntDly  +  RepHostAntDly  -  RepHostRefDly;

(*Host Receiver internal settings:*)
RxHostIntDly = 
        49.7;
     RxHostRefDly = 515.9;
   RxHostAntDly  = 
          250;
RxHostDly = 
        RxHostIntDly  +  RxHostAntDly  -  
          RxHostRefDly;
HostCorrection = RepHostDly  -  RxHostDly;

dataTrav = ReadCCTF["trav.crl.dat"];
(*Host  reported values:*)
RepTravIntDly = 
        68;
     RepTravRefDly = 
          527.42  +  207.56;
   RepTravAntDly  = 234.5;
RepTravDly = 
          RepTravIntDly  +  RepTravAntDly  -  RepTravRefDly;

(*Travelling receiver internal settings:*)
RxTravIntDly = 
        68;
     RxTravRefDly = 527.4;
   RxTravAntDly  =  
          235;
RxTravDly = 
        RxTravIntDly  +  RxTravAntDly  -  
          RxTravRefDly;
TravCorrection = RepTravDly  -  RxTravDly;

<< Graphics`Graphics`

ListPlot[
        MakeXY[Map[DateValue, dataTrav], 
          Map[RefGPS, dataTrav]]];

ListPlot[
        MakeXY[Map[DateValue, dataHost], 
          Map[RefGPS, dataHost]]];

dMerge = MergeCCTF[dataHost, dataTrav];

diffdataGPS = 
        Map[{DateValue[#1], 
              RefSV[#1]  -  RefSV2[#1]  -  HostCorrection  +  
                TravCorrection, TrackLength[#1], TrackLength2[#1]} &, 
          dMerge];

diffdataGPS = FilterTrackLength[diffdataGPS, 780];

ListPlot[diffdataGPS, PlotRange -> All];

<< Statistics`LinearRegression`

regress  =  
        Regress[diffdataGPS,  {1,  x},  
          x];
rtable = 
        ANOVATable  /.  regress;
ptable = 
        ParameterTable  /.  
          regress;
MJDFirst = First[
            dMerge][[3]];
MJDLast = Last[
            dMerge][[3]];
MJDMiddle = 
        MJDFirst +  (MJDFirst - MJDLast)/
            2;
intercept = 
        ptable[[1, 1, 1]];
SEintercept = 
        ptable[[1, 1, 2]];
slope = 
        ptable[[1, 2, 1]];
SEslope = 
        ptable[[1, 2, 2]];
rms = 
        Sqrt[rtable[[1, 2, 3]]];
MeanOffset = 
        intercept + slope*MJDMiddle;
Print["\!\(\*
StyleBox[\"Summary\",\n\"Output\"]\)"];
Print[
      Length[dMerge], " common-view tracks were analysed between MJD ",  
      MJDFirst, " and MJD ", MJDLast]; 
    Print["The mean offset (Host Rx - Travelling Rx) between the two \
receivers was ", MeanOffset, " ns, with an RMS deviation of ", 
      rms, "  ns."];
Print["The slope of \
the line of best fit was ", 
        slope*1000, " ps/day, with a standard error of ", 
        SEslope*1000, " ps/day."];