%BUILD INV data Struct
%formaT{1}:FILENAME
%    {2}: BGfile
%    {3}: Field Value
%    {4}: Polar Index
%    {5}: GPS (E)
%    {6}: GPS (N)
%    {7}: GPS (accuracy)
%    {8:10}:[x,y,z]
%   {11:12}: junk GPS
%   {13}DATE
%   {14}TIME
%   {15}AMPM
%   {16}TIMEZONE

clear all
GPO=0;
OF=1;
dataDir='/media/usbdisk2/wafb/'
gpoDat='gpoListOrdered.dat'
addpath('/data/22/kappler/TOOLS/');
ofDat='of_uxo_list_eg2.dat'
load trueTypes;


if GPO
    readWhat
    GPO={};
    fid = fopen([dataDir,gpoDat]);
    C = textscan(fid,'%s%s%f%f%f%f%f%f%f%f%f%f%s%s%s%s');  %u%f%f%s');
    fclose(fid);
    epsln=1e-6;
    C{4}(find(C{4}==0))=epsln;
    for file=1:length(C{1})
        clear INV QF
        fNum=zeroPadStr(file,4);
        INV=slurpInv([dataDir,'GPO/gpo_',fNum,'.inv']);
        QF=quadFitT(INV,34);
        INV.Qcf=QF.Qcf;
        INV.Res=QF.Res;
        INV.Pqf=QF.Pqf;
        INV.sym=QF.sym;
        INV.d23=QF.d23;
        INV.d12=QF.d12;
        INV.avgPolarindex=C{4}(file);
        INV.fname=['gpo_',fNum];
        INV.GT=GT{1}{file};
        GPO{file}.INV=INV;
        GPO{file}.fNameOrig=C{1}{file};
        GPO{file}.bgOrig=C{2}{file};
        GPO{file}.fValue=C{3}(file);
        GPO{file}.GPSE=C{5}(file);
        GPO{file}.GPSN=C{6}(file);
        GPO{file}.GPSACC=C{7}(file);
        GPO{file}.DATE=[ C{13}{file}, ' ',C{14}{file},' ',C{15}{file},' ',C{16}{file}]; ;
    end
    GPO{199}.INV.GT=trueTypes{8};
    save /scr/em1/kappler/UXO/WAFB/GPO GPO
end


if OF
    OF={};
    fid = fopen([dataDir,ofDat]);
    C = textscan(fid,'%s%s%f%d%f%f%f%f%f%f%f%f%s%s%s%s');  %u%f%f%s');
    fclose(fid);
    epsln=1e-6;
    C{4}(find(C{4}==0))=epsln;
    for file=1:length(C{1})
        clear INV QF
        fNum=zeroPadStr(file,4);
        INV=slurpInv([dataDir,'OF/of_',fNum,'.inv']);
        QF=quadFitT(INV,34);
        INV.Qcf=QF.Qcf;
        INV.Res=QF.Res;
        INV.Pqf=QF.Pqf;
        INV.sym=QF.sym;
        INV.d23=QF.d23;
        INV.fname=['OF_',fNum];
        OF{file}.INV=INV;
        OF{file}.fNameOrig=C{1}{file};
        OF{file}.bgOrig=C{2}{file};
        OF{file}.fValue=C{3}(file);
        OF{file}.polarIndex=C{4}(file);
        OF{file}.GPSE=C{5}(file);
        OF{file}.GPSN=C{6}(file);
        OF{file}.GPSACC=C{7}(file);
        OF{file}.DATE=[ C{13}{file}, ' ',C{14}{file},' ',C{15}{file},' ',C{16}{file}]; ;
    end
    save OF OF
end