trkacxfit.cc 9.51 KB
Newer Older
1
/*---------------------------- 
2 3 4 5 6 7 8 9
   PAON4 analysis : 
   Determining antenna pointing and dish size fitting parameters ... 
   (source track corresponds to a satellite track (or planet or sun ...)
   Does also fit of phases for the 6 cross-correlations
   Uses gacfit.h and gcxfit.h acbeam.h cxbeam.h  
   R. Ansari  , February 2019 

   Evolution of trkacfit.cc , intended to replace it 
10 11 12 13 14 15

   Assuming that the file trk_1.d contains :
@zenang 10 
@trk  dt_A21  1358,1410  1278.5  trk_A21_43057
@trk  dt_A21  1512,1555  1278.5  trk_A21_43055    

16
   Example command to run : 
17
csh> ../AnaPaon4/Objs/trkacxfit -D 4.5 -out A21_fac.txt  trk_1.d  
18

19 20 21 22 23 24 25 26 27 28 29 30

  visi-track-set datacard format 
----------------------------------
# input path (directory for files) listed here (optional)
@inpath  path 
## Zenith angle or declination shift 
# Zenith angle_in_degree (positive toward north)  initial value for fit 
@zenang ZenithAngle 
### Input data and track files - multiple @trk cards 
# filenames with .ppf extension , Tstart,Tend in minutes 
@trk AutoCrossVisiDataTableFile  Tstart,Tend  Freq  TrackFile 
 ----------------------------------------------------------------------------*/
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include <cmath>
#include <complex>

#include "array.h"
#include "randr48.h"
#include "skymap.h"
#include "samba.h"
#include "fftwserver.h"
#include "strutilxx.h"

#include "ctimer.h"
#include "tarrinit.h"
#include "skymapinit.h"
#include "fitsioserver.h"
#include "slininterp.h"

#include "p4autils.h"

#include "acbeam.h"
#include "gacfit.h"
#include "gcxfit.h"
#include "gcxfitbaseline.h"
#include "trkfit.h"

using namespace std; 
using namespace SOPHYA; 

//---------------------------------------------------------------------------
//---  Grouping here beam & computation parameters -------------
//--- Global parameters 

// static double latitude=45.;   // latitude in degree 
// static double longitude=0.;   // longitude in degree 


static double D_dish = 4.5;       // effective dish size = effD*D 
static bool fggaussbeam=true;

// --- if true, perform Beam normalisation 
// static bool fgdobeamnormalise=false;

static string outfilename;            // output filename for auto-correlation fit parameters  
static string checkfilename;       // output filename for PAON4 and computed auto-cor using fitted parameters  

static bool fguseAac4Cx=true;       //if true, use fitted Amplitudes on autocor for initial Cross-cor fit parameter value

// static bool fgoutfits = false;   // true -> write output in fits format 

static int prtlevel=0;  // print level 

88
static string default_input_path;
89 90
static vector<string> trksetfiles;   // datacard files defining the track/data sets 

91 92
static bool do_baselinefit = false;   // if true , perform baseline fit 

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
//---   End of list of global parameters 



//-------- Declaration of functions in this file - code after the main, below 
int decode_args(int narg, char* arg[]);


//--------------------------------------------------------------
//---------  Main program -------------
//--------------------------------------------------------------
int main (int narg, char* arg[])
{
  cout << " ------ trkacxfit.cc : Fitting auto-correlations using track (satellites...) files  ------- " << endl;
  int rc = 0;
  try {
    SophyaInit();
    int rcda=decode_args(narg, arg);
    if (rcda)  return rcda;

113
    TrkFit_SetPrintLevel(prtlevel);
114 115 116
    vector<AcxDataSet> v_acxd;
    vector<TrackSet> v_trk;
    for(size_t i=0; i<trksetfiles.size(); i++) {
117 118 119
      TrkInputDataSet tids(trksetfiles[i], default_input_path);
      cout<<" --- Processing data-track set # "<<i+1<<" /max="<<trksetfiles.size()<<endl;
      cout<<tids;
120 121 122
      AcxDataSet  acxd(tids);
      TrackSet  tks(tids);
      ACxSetFitter acxfit(acxd, tks);
123 124 125
      char fnbuff[32];
      sprintf(fnbuff,"ac_%d_",(int)(i+1));
      string acofile = fnbuff+outfilename;
126
      acxfit.doACfit(acofile);  
127
      string acckfile = fnbuff+checkfilename;
128
      acxfit.saveExpectedAC(acckfile);
129 130 131
      sprintf(fnbuff,"cx_%d_",(int)(i+1));
      string cxofile = fnbuff+outfilename;
      string cxckfile = fnbuff+checkfilename;
132 133 134
      acxfit.doCxfit(cxofile, fguseAac4Cx); 
      acxfit.saveExpectedCx(cxckfile);

135 136 137
      v_acxd.push_back(acxd);
      v_trk.push_back(tks);
    }
138 139 140 141 142

    if (do_baselinefit) {
      CxBaselineFitter cxbfit(v_acxd, v_trk);
      string cxbofile = "cxb6_"+outfilename;
      string cxbckfile = "cxb6_"+checkfilename;
143 144
      cxbfit.dofit(cxbofile);
      cxbfit.saveExpectedCx(cxbckfile);
145
    }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    Timer tm("trkacxfit");
  }  // End of try bloc 
  catch (PThrowable & exc) {
    cerr << " trkacxfit.cc: Catched Exception (PThrowable)" << (string)typeid(exc).name() 
         << " - Msg= " << exc.Msg() << endl;
    rc = 99;
  }
  catch (std::exception & e) {
    cerr << " trkacxfit.cc: Catched std::exception "  
         << " - what()= " << e.what() << endl;
    rc = 98;
  }
  catch (...) {
    ;    cerr << " trkacxfit.cc: some other exception (...) was caught ! " << endl;
    rc = 97;
  }
  cout << " ------------ END OF trkacxfit.cc  (Rc= " << rc << ") ------------ " << endl;
  return rc;
}


//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------


/* --Fonction-- */
int decode_args(int narg, char* arg[])
//! Decode program arguments 
{
  bool fglonghelp=false;
  if ((narg>1)&&(strcmp(arg[1],"-h")==0))  fglonghelp=true;
  if ((narg<2)||fglonghelp) {
    cout << "  trkacxfit : fit array geometry and   \n"
	 << "  Usage: trkacxfit [-options]  track_Set_1 [track_Set_2]  [track_Set_3 ...] \n"
180
	 << "  options: -inp def_input_path -out OutFileName -ckf CheckFileName \n" 
181
	 << "           -doblf -ngb  -D dish_diameter -prt PrintLevel  \n"<<endl;
182 183 184 185
    if (!fglonghelp) {
      cout << " trkacxfit -h  to get option description " << endl;
      return 2;
    }
186 187 188 189 190 191
    cout << "  -inp def_input_path : default directory path for input files (track_Set_J files) \n"
      	 << "    This path can be overwritten in each track_Set_J datacard file  \n"
      	 << "  -out OutFileName : Output file (text) to save fitted parameters vectors (def trkfit.txt) \n"
      	 << "  -ckf CheckFileName : Output file (PPF) to save expected signals (def chktrkfit.ppf)  \n"
	 << "    Output file names are constructed from OutFileName and CheckFileName prepending \n" 
	 << "  ac_JJ_ or cx_JJ_  where JJ=1...n is the track_set number \n"
192 193
	 << "  -doblf : Perform baseline determination (fit) - default NO  \n"
	 << "  -ngb : Use non gaussian beam profile (Bessel j1(angle) - default Gaussian beam)  \n"
194
	 << "  -D dish_diameter : define effective dish diameter (in meter D*eff , def=4.5) \n"
195
	 << "  -prt PrintLevel: specify print level \n" 
196 197 198 199
	 << "    track_Set_J (J=1..n) are the auto/cross correlation and track data sets  \n" 
	 << "    in datacard format [ @trk (multiple) @zenang cards @inpath] \n" 
	 << "    @trk AutoCrossVisDataTableFile Tstart,Tend Freq TrackFileName \n" 
	 << "    Filenames without .ppf extension, Tstart,Tend in minutes Freq in MHz for @trk cards \n" <<endl;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    return 1;
  }

  outfilename = "trkfit.txt";
  checkfilename = "chktrkfit.ppf";
  prtlevel=0;
  D_dish = 4.5;
  fggaussbeam=true;

  vector<string> lastargs;
  while (narg>1) {
    string fbo = arg[1];
    if (fbo=="-D") {  // dish size   (effective dish diameter) 
      if (narg<2) { cout << "trkacxfit/decode_args missing/bad argument, -h for help " << endl;  return -1; }
      D_dish=atof(arg[2]);   arg+=2; narg-=2;   lastargs.clear(); 
    }
    else if (fbo=="-ngb") {  // Use Non gaussian beam 
      fggaussbeam=false;   arg++;  narg--;  lastargs.clear(); 
    }
219
    else if (fbo=="-doblf") {  // Use Non gaussian beam 
220
      do_baselinefit=true;   arg++;  narg--;  lastargs.clear(); 
221
    }
222 223 224 225
    else if (fbo=="-inp") {  // output file name 
      if (narg<2) { cout << "trkacxfit/decode_args missing/bad argument, -h for help " << endl;  return -1; }
      default_input_path=arg[2];   arg+=2; narg-=2;  lastargs.clear(); 
    }
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    else if (fbo=="-out") {  // output file name 
      if (narg<2) { cout << "trkacxfit/decode_args missing/bad argument, -h for help " << endl;  return -1; }
      outfilename=arg[2];   arg+=2; narg-=2;  lastargs.clear(); 
    }
    else if (fbo=="-ckf") {  // output file name for checking (data & fitted 
      if (narg<2) { cout << "trkacxfit/decode_args missing/bad argument, -h for help " << endl;  return -1; }
      checkfilename=arg[2];   arg+=2; narg-=2;  lastargs.clear(); 
    }
    else if (fbo=="-prt") {  // print level 
      if (narg<2) { cout << "trkacxfit/decode_args missing/bad argument, -h for help " << endl;  return -1; }
      prtlevel=atoi(arg[2]);   arg+=2; narg-=2;  lastargs.clear(); 
    }
    else { arg++; narg--; lastargs.push_back(fbo); }
  }

  if (lastargs.size() < 1) {
    cout << "  trkacxfit/ERROR : No input track/data specified (trkacxfit -h for usage)  \n";
    return 9;
  }

  cout << " ------------------- trkacxfit/run parameters:"<<endl; 
  cout << " Beam: D_dish (initial guess)="<<D_dish<<" GaussianBeam ? "<<(fggaussbeam?"Yes":"No")<<endl;
  cout << " OutFileName= "<<outfilename<<" CheckFileName= "<<checkfilename<<endl;
249
  cout << " Perform baseline fit ? " << (do_baselinefit?"Yes":"No")<<endl;
250
  cout << " --- TrackSetFiles: (NbFiles="<<lastargs.size()<<" default directory: "<<default_input_path<<")"<<endl;
251 252 253 254 255 256 257 258
  trksetfiles = lastargs;
  for (size_t i=0; i<trksetfiles.size(); i++) {
    cout << "["<<i<<"] : "<< trksetfiles[i]<<endl;
  }
  cout<<"---------------------------------------------------------------------------------"<<endl;
  return 0;
}