trkacxfit.cc 10.2 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
// ---- fit simultane sur les 6 cross-correlations 
92
static bool do_baselinefit = false;   // if true , perform baseline fit 
93
static bool fg_fixbaseline = false;   // if true , do phases fit with fixed baselines 
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
//---   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;

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

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

    if (do_baselinefit) {
      CxBaselineFitter cxbfit(v_acxd, v_trk);
      string cxbofile = "cxb6_"+outfilename;
      string cxbckfile = "cxb6_"+checkfilename;
145 146 147
      //cxbfit.doSimplexMinimize();
      cxbfit.initFitParams();
	//      cxbfit.doCheck();
148
      cxbfit.dofit(cxbofile,fg_fixbaseline);
149
      cxbfit.saveExpectedCx(cxbckfile);
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 180 181 182 183 184
    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"
185
	 << "  options: -inp def_input_path -out OutFileName -ckf CheckFileName \n" 
186
	 << "           -docx6f -fixb -ngb  -D dish_diameter -prt PrintLevel  \n"<<endl;
187 188 189 190
    if (!fglonghelp) {
      cout << " trkacxfit -h  to get option description " << endl;
      return 2;
    }
191 192 193 194 195 196
    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"
197 198
	 << "  -docx6f : Perform simultaneous fit over 6 cross-corr (baseline and phases determination) - default NO  \n"
	 << "  -fixb : perform the previous fit (docx6f) with fixed baselines - default NO  \n"
199
	 << "  -ngb : Use non gaussian beam profile (Bessel j1(angle) - default Gaussian beam)  \n"
200
	 << "  -D dish_diameter : define effective dish diameter (in meter D*eff , def=4.5) \n"
201
	 << "  -prt PrintLevel: specify print level \n" 
202 203 204 205
	 << "    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;
206 207 208 209 210 211 212 213
    return 1;
  }

  outfilename = "trkfit.txt";
  checkfilename = "chktrkfit.ppf";
  prtlevel=0;
  D_dish = 4.5;
  fggaussbeam=true;
214 215
  do_baselinefit=false;
  fg_fixbaseline=false;
216 217 218 219 220 221 222 223 224 225 226

  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(); 
    }
227
    else if (fbo=="-docx6f") {  // Perform  simultaneous fit over 6 cross-corr 
228
      do_baselinefit=true;   arg++;  narg--;  lastargs.clear(); 
229
    }
230 231 232
    else if (fbo=="-fixb") {  // Perform  the previous docx6f fit with fixed baselines (phases only)
      fg_fixbaseline=true;   arg++;  narg--;  lastargs.clear(); 
    }
233 234 235 236
    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(); 
    }
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    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;
260 261
  cout << " Perform baseline fit ? " << (do_baselinefit?"Yes":"No")
       <<(fg_fixbaseline?"  With FIXED baselines":" (Phases & baselines)")<<endl;
262
  cout << " --- TrackSetFiles: (NbFiles="<<lastargs.size()<<" default directory: "<<default_input_path<<")"<<endl;
263 264 265 266 267 268 269 270
  trksetfiles = lastargs;
  for (size_t i=0; i<trksetfiles.size(); i++) {
    cout << "["<<i<<"] : "<< trksetfiles[i]<<endl;
  }
  cout<<"---------------------------------------------------------------------------------"<<endl;
  return 0;
}