📣 An issue occured with the embedded container registry on October 25 2021, between 10:30 and 12:10 (UTC+2). Any persisting issues should be reported to CC-IN2P3 Support. 🐛

trk2dt.cc 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include "machdefs.h"
/* ---------------------------------------------------------- 
   Projet BAORadio/PAON4 - (C) LAL/IRFU  2008-2015 

   Programme de conversion fichiers ascii track (satellites, soleil)
   en DataTable, sauve en PPF (ou fits) Reza, Mai 2018 
   ---------------------------------------------------------- */

// include standard c/c++
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <string>

// include SOPHYA  

#include "pexceptions.h"
#include "timestamp.h"
#include "histats.h"
#include "datatable.h" 
#include "histinit.h" 
25
#include "fitsioserver.h" 
26 27 28 29 30 31 32

using namespace std;
using namespace SOPHYA;

int Usage(void)
{
  cout<<"--- trk2dt.cc :  Converting track file to DataTable \n"<<endl;
33 34 35 36 37 38
  cout<<"Usage: trk2dt track_file.txt outFile [StartDate yyyy/mm/dd]   \n";
  cout<<"    - outFile : PPF or FITS format based on the extension  (.ppf or .fits) \n";
  cout<<"    - StartDate : is used as offset (at 00h00) to compute the timesec value  \n";
  cout<<"      timesec = (DataTime-StarDate_00h00).ToSeconds \n";
  cout<<"      Default StartDate (if not specified) is extracted from the first DateTime in track file \n";

39 40 41 42 43 44 45 46 47 48 49
  cout<<endl;
  return 1;
}

//----------------------------------------------------
int main(int narg, const char* arg[])
{
  int rc=0;
  // --- Decoding parameters 
  if ((narg<3)||((narg>1)&&(strcmp(arg[1],"-h")==0)))  return Usage();

50

51 52 53 54
  string infile = arg[1];
  string outfile = arg[2];
  cout << " ==== trk2dt reading input track file "<<infile<<" to create DataTable"<<endl
       << " ... saved to output file"<<outfile<<endl;
55 56 57 58 59 60 61 62 63
  TimeStamp date0(2018, 1, 1, 0, 0, 0.);  // first date 
  bool fgdate0set=false;
  if (narg>3) {
    int yy=2018 , mm=1, dd=1;
    sscanf(arg[3],"%d/%d/%d",&yy,&mm,&dd);
    date0.SetDate(yy,mm,dd); 
    fgdate0set=true;
    cout << "StartDate (Offset for timesec column) set from command line argument ->"<<date0<<endl;
  }
64 65 66 67 68 69 70
  try {
    DataTable dt(128);

    dt.AddDateTimeColumn("datetime");
    dt.AddDoubleColumn("timesec");
    dt.AddDoubleColumn("azimuth");
    dt.AddDoubleColumn("elevation");
71
    dt.AddDoubleColumn("range");
72 73
    dt.AddDoubleColumn("latitude");
    dt.AddDoubleColumn("longitude");
74
    dt.AddDoubleColumn("altitude");
75 76 77 78 79 80 81

    dt.Info().Comment()="Satellite track DataTable from predictsatsgp4.cc";

    dt.Info()["datetime"] = "Date/Time";
    dt.Info()["timesec"] = "time of day, in seconds";
    dt.Info()["azimuth"] = "Azimuth angle , in degree";
    dt.Info()["elevation"] = "Elevation angle , in degree";
82
    dt.Info()["range"] = "Range (distance ?) in km";
83 84
    dt.Info()["latitude"] = "Latitude , in degree";
    dt.Info()["longitude"] = "Longitude , in degree";
85
    dt.Info()["altitude"] = "Altitude in km";
86 87 88 89 90

    int nlines = 0;
    int nok = 0;
    DataTableRow  row = dt.EmptyRow();

91
    ifstream is(infile.c_str());
92 93 94 95 96
    if (is.fail()){
	cout<<" Problem opening file !! "<<endl;
	throw IOExc(" trk2dt : bad file : "+infile+"  - theck it !!");
      }

97 98 99 100 101 102 103 104 105 106 107 108
    string sline;
    while (!is.eof()) {
      sline = "";
      getline(is, sline);   nlines++;
      if (!is.good() && !is.eof()) continue;
      if ((sline.length()<69) || (sline[0] == '#')) continue;
      int yy=2018 , mm=1, dd=1;
      sscanf(sline.c_str(), "%d-%d-%d",&yy,&mm,&dd);
      int hh=0 , mn=0;
      double ss=0.;
      sscanf(sline.c_str()+11, "%d:%d:%lg",&hh,&mn,&ss);
      TimeStamp datetm(yy, mm, dd, hh, mn, ss);
109 110 111 112 113
      if (!fgdate0set) {
	date0.SetDate(yy,mm,dd); 
	fgdate0set=true;
	cout << "StartDate (Offset for timesec column) set from first date in track file ->"<<date0<<endl;
      }
114
      
115 116
      double azim, elev, range, latitude, longitude, alt;
      sscanf(sline.c_str()+30, "%lg %lg %lg %lg %lg %lg", &azim, &elev, &range, &latitude, &longitude, &alt);
117 118 119 120 121 122

      double tmsec = TimeStamp::TimeDifferenceSeconds(datetm, date0);	
            	
      row[0] = datetm;
      row[1] = tmsec;
      row[2] = azim;
123 124 125 126 127
      row[3] = elev;
      row[4] = range;      
      row[5] = latitude;
      row[6] = longitude;
      row[7] = alt;
128 129 130 131 132 133 134

      dt.AddRow(row);   nok++;
    }

    dt.SetShowMinMaxFlag(true);
    cout << dt ;

135 136 137 138 139 140 141 142 143 144 145 146
    size_t ll = outfile.length();
    size_t pp = outfile.rfind('.');
    if ((pp<ll)&&(outfile.substr(pp)==".fits"))  {
      cout << "----- Saving track DataTable to FITS file "<<outfile<<endl;
      FitsInOutFile fos(outfile,FitsInOutFile::Fits_Create);
      fos << dt; 
    }
    else {
      cout << "----- Saving track DataTable to PPF file "<<outfile<<endl;
      POutPersist pof(outfile);
      pof << dt;
    }
147 148
  }
  catch (PException& exc) {
149
    cerr << " trk2dt.cc catched PException " << exc.Msg() << endl;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    rc = 77;
  }  
  catch (std::exception& sex) {
    cerr << "\n trk2dt.cc std::exception :" 
         << (string)typeid(sex).name() << "\n msg= " << sex.what() << endl;
      rc = 78;
  }
  catch (...) {
    cerr << " trk2dt.cc catched unknown (...) exception  " << endl; 
    rc = 79; 
  } 
  
  cout << ">>>> trk2dt.cc ------- END ----------- RC=" << rc << endl;
  return rc;
}