ATSData.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: ATSData.cpp 3140 2012-06-18 15:03:02Z susancummins $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00020 //  
00021 //  Copyright 2004, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 //============================================================================
00026 //
00027 //This software developed by Applied Research Laboratories at the University of
00028 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00029 //Department of Defense. The U.S. Government retains all rights to use,
00030 //duplicate, distribute, disclose, or release this software. 
00031 //
00032 //Pursuant to DoD Directive 523024 
00033 //
00034 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00035 //                           release, distribution is unlimited.
00036 //
00037 //=============================================================================
00038 
00039 #include "StringUtils.hpp"
00040 #include "BinUtils.hpp"
00041 
00042 #include "ATSData.hpp"
00043 #include "ATSStream.hpp"
00044 
00045 using namespace std;
00046 
00047 using gpstk::BinUtils::computeCRC;
00048 using gpstk::StringUtils::asString;
00049 using gpstk::StringUtils::d2x;
00050 using gpstk::StringUtils::int2x;
00051 
00052 
00053 namespace gpstk
00054 {
00055    // Set to zero for no debugging output
00056    // set to 1 to output text messages about decode/format/range errors
00057    // set to 2 to add a hex dump of those messages
00058    // set to 3+ to add the tossed bytes whether or not they are bad
00059    int ATSData::debugLevel = 0;
00060 
00061    // set true to print a hex dump of every message to cout
00062    bool ATSData::hexDump = false;
00063    const uint8_t ATSData::MaxNumChan = 48;
00064    const uint8_t ATSData::MaxNumSubChan = 12;
00065 
00066    //---------------------------------------------------------------------------
00067    void ATSData::reallyPutRecord(FFStream& ffs) const
00068       throw(std::exception, gpstk::StringUtils::StringException, 
00069             gpstk::FFStreamError)
00070    {
00071       if (typeid(*this) == typeid(ATSData))
00072       {
00073          gpstk::FFStreamError e("Directly writing an ATSData object to an"
00074                                 " FFStream is not supported.");
00075          GPSTK_THROW(e);
00076       }
00077 
00078       ATSStream& stream = dynamic_cast<ATSStream&>(ffs);
00079 
00080       string str;
00081       stream << str;
00082 
00083       if (hexDump)
00084       {
00085          cout << endl;
00086          StringUtils::hexDumpData(cout, str);
00087       }
00088    } // ATSData::reallyPutRecord()
00089 
00090    template <class T>
00091    T decodeVar( std::string& str, std::string::size_type pos = std::string::npos)
00092    {
00093       T t;
00094       char *cp = reinterpret_cast<char*>( &t );
00095       
00096       if (pos == std::string::npos)
00097       {
00098          str.copy( cp, sizeof(T) );
00099          t = gpstk::BinUtils::intelToHost( t );
00100          str.erase( 0, sizeof(T) );
00101       }
00102       else
00103       {
00104          str.copy( cp, sizeof(T) , pos);
00105          t = gpstk::BinUtils::intelToHost( t );
00106       }
00107       return t;
00108    }
00109 
00110    //---------------------------------------------------------------------------
00111    void ATSData::reallyGetRecord(FFStream& ffs)
00112       throw(std::exception, gpstk::StringUtils::StringException, 
00113             gpstk::FFStreamError, gpstk::EndOfFile)
00114    {
00115       // Note that this will generate a bad_cast exception if it doesn't work.
00116       ATSStream& stream=dynamic_cast<ATSStream&>(ffs);
00117 
00118       char tmp;
00119       stream.getData(static_cast<char*>(&tmp), 1);
00120       if (!stream)
00121       {
00122          FFStreamError err("Error reading stream.");
00123          GPSTK_THROW(err);
00124       }
00125       numChan = tmp;
00126 
00127       if (numChan > MaxNumChan)
00128       {
00129          FFStreamError err("Channel count error: " +
00130                            StringUtils::asString((int)numChan)
00131                            + " > " + StringUtils::asString((int)MaxNumChan));
00132          GPSTK_THROW(err);
00133       }
00134 
00135       stream.getData(&tmp, 1);
00136       if (!stream)
00137       {
00138          FFStreamError err("Error reading stream.");
00139          GPSTK_THROW(err);
00140       }
00141       numSubChan = tmp;
00142 
00143       if (numSubChan > MaxNumSubChan)
00144       {
00145          FFStreamError err("Sub channel count error: " +
00146                            StringUtils::asString(numChan)
00147                            + " > " + StringUtils::asString(MaxNumSubChan));
00148          GPSTK_THROW(err);
00149       }
00150 
00151       if (debugLevel>2)
00152          cout << "numChan:"<< (int)numChan
00153               << ", numSubChan:" << (int)numSubChan << endl;
00154       size_t recSize = numChan * (9 + numSubChan * 65);
00155 
00156       char *buff = new char [recSize];
00157       stream.getData(buff, recSize);
00158       if (!stream)
00159       {
00160          FFStreamError err("Error reading stream.");
00161          GPSTK_THROW(err);
00162       }
00163 
00164       string str(buff, recSize);
00165       delete buff;
00166       stream.rawData = string(1,numChan) + string(1,numSubChan) + str;
00167 
00168       if (channels.size() != numChan)
00169          channels.resize(numChan);
00170 
00171       for (int i=0; i<numChan; i++)
00172       {
00173          ChannelBlock& cb = channels[i];
00174          int prn = decodeVar<uint8_t>(str);
00175          cb.svid = SatID(prn, SatID::systemGPS);
00176          cb.absTime = decodeVar<double>(str);
00177          if (cb.subChannels.size() != numSubChan)
00178             cb.subChannels.resize(numSubChan);
00179          for (int j=0; j<numSubChan; j++)
00180          {
00181             SubChannelBlock& scb = cb.subChannels[j];;
00182             scb.pseudorange = decodeVar<double>(str) + stream.rangeBias[i];
00183             scb.phase = decodeVar<double>(str);
00184             scb.rangeRate = decodeVar<double>(str);
00185             scb.cn0 = decodeVar<double>(str);
00186             scb.flags = decodeVar<uint8_t>(str);
00187             for (int k=0; k<4; k++)
00188                scb.navMSB[k] = decodeVar<uint32_t>(str);
00189             for (int k=0; k<4; k++)
00190                scb.navLSB[k] = decodeVar<uint32_t>(str);;
00191          }
00192       }
00193 
00194       if (debugLevel && stream.rdstate())
00195          ATSData::dump(cout);
00196 
00197       if (hexDump)
00198       {
00199          cout << "Record Number:" << stream.recordNumber << endl;
00200          StringUtils::hexDumpData(cout, stream.rawData);
00201       }
00202    } // ATSData::reallyGetRecord()
00203 
00204    //---------------------------------------------------------------------------
00205    void ATSData::dump(ostream& out, int detail) const
00206       throw()
00207    {
00208       ostringstream oss;
00209       if (detail)
00210          oss << getName() << " :"
00211              << " numChan:" << (int)numChan
00212              << " numSubChan:" << (int)numSubChan
00213              << endl;
00214       for (int i = 0; i < channels.size(); i++)
00215       {
00216          const ChannelBlock& cb = channels[i];
00217          if (detail)
00218             oss << getName() << " : prn:" << cb.svid
00219                 << " absTime:" << setprecision(15) << cb.absTime << endl;
00220          for (int j=0; j<numSubChan; j++)
00221          {
00222             if (detail)
00223             {
00224                oss << getName() << setprecision(12)
00225                    << " : range:" << cb.subChannels[j].pseudorange
00226                    << " phase:" << cb.subChannels[j].phase
00227                    << " rangeRate:" << cb.subChannels[j].rangeRate
00228                    << setprecision(4)
00229                    << " cn0:" << cb.subChannels[j].cn0
00230                    << " flags:" << hex << (int)cb.subChannels[j].flags
00231                    << endl
00232                    << "   ";
00233                for (int k=0; k<4; k++)
00234                {
00235                   if (k>0)
00236                      oss << ", ";
00237                   oss << cb.subChannels[j].navMSB[k]
00238                       << " " << cb.subChannels[j].navLSB[k];
00239                   oss << dec << endl;
00240                }
00241             }
00242             else
00243             {
00244                short week = static_cast<short>(cb.absTime / FULLWEEK);
00245                double sow = cb.absTime - week * FULLWEEK;
00246                oss << left << setw(4) << week << right << setprecision(9)
00247                    << " " << setw(7) << sow
00248                    << "    " << setw(5) << cb.svid.id
00249                    << "  " << setprecision(14) << setw(18)
00250                    << cb.subChannels[j].pseudorange
00251                    << " " << setprecision(9) << setw(14) 
00252                    << cb.subChannels[j].rangeRate
00253                    << "  " << setprecision(14) << setw(18)
00254                    << cb.subChannels[j].phase
00255                    << setprecision(3)
00256                    << "  " << setw(4) << cb.subChannels[j].cn0
00257                    << "  " << hex << (int)cb.subChannels[j].flags << dec
00258                    << endl;
00259             }
00260          }
00261       }
00262       out << oss.str() << endl;
00263    }  // ATSData::dump()
00264    
00265 } // namespace gpstk

Generated on Sat May 18 03:31:01 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1