trkacxfit.cc 10.9 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
static bool do_baselinesimplex = false;   // if true , perform baseline determination using Simplex minimisation  
95

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

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

138 139 140
      v_acxd.push_back(acxd);
      v_trk.push_back(tks);
    }
141

142 143 144 145 146 147 148 149 150 151 152 153
    string cxbofile = "cxb6_"+outfilename;
    string cxbckfile = "cxb6_"+checkfilename;
    CxBaselineFitter cxbfit(v_acxd, v_trk);
    //cxbfit.doSimplexMinimize();
    cxbfit.initFitParams();
    cxbfit.saveExpectedCx(cxbckfile);
    if (do_baselinesimplex) {
      cxbfit.doSimplexMinimize();
      cxbckfile="cxb6s_"+checkfilename;
      cxbfit.saveExpectedCx(cxbckfile);
      //      cxbfit.doCheck();
    }
154
    if (do_baselinefit) {
155
      cxbfit.dofit(cxbofile,fg_fixbaseline);
156
      cxbckfile = "cxb6f_"+checkfilename;
157
      cxbfit.saveExpectedCx(cxbckfile);
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 185 186 187 188 189 190 191 192
    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"
193
	 << "  options: -inp def_input_path -out OutFileName -ckf CheckFileName \n" 
194
	 << "           -docx6f -fixb -docx6s -ngb  -D dish_diameter -prt PrintLevel  \n"<<endl;
195 196 197 198
    if (!fglonghelp) {
      cout << " trkacxfit -h  to get option description " << endl;
      return 2;
    }
199 200 201 202 203 204
    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"
205 206
	 << "  -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"
207
	 << "  -docx6s : try simplex minimisation over 6 cross-corr (baseline and phases determination) - default NO  \n"
208
	 << "  -ngb : Use non gaussian beam profile (Bessel j1(angle) - default Gaussian beam)  \n"
209
	 << "  -D dish_diameter : define effective dish diameter (in meter D*eff , def=4.5) \n"
210
	 << "  -prt PrintLevel: specify print level \n" 
211 212 213 214
	 << "    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;
215 216 217 218 219 220 221 222
    return 1;
  }

  outfilename = "trkfit.txt";
  checkfilename = "chktrkfit.ppf";
  prtlevel=0;
  D_dish = 4.5;
  fggaussbeam=true;
223 224
  do_baselinefit=false;
  fg_fixbaseline=false;
225
  do_baselinesimplex=false;
226 227 228 229 230 231 232 233 234 235 236

  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(); 
    }
237
    else if (fbo=="-docx6f") {  // Perform  simultaneous fit over 6 cross-corr 
238
      do_baselinefit=true;   arg++;  narg--;  lastargs.clear(); 
239
    }
240 241 242
    else if (fbo=="-fixb") {  // Perform  the previous docx6f fit with fixed baselines (phases only)
      fg_fixbaseline=true;   arg++;  narg--;  lastargs.clear(); 
    }
243 244 245
    else if (fbo=="-docx6s") {  // Perform  simultaneous fit over 6 cross-corr 
      do_baselinesimplex=true;   arg++;  narg--;  lastargs.clear(); 
    }
246 247 248 249
    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(); 
    }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    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;
273
  cout << " Perform baseline fit ? " << (do_baselinefit?"Yes":"No")
274 275
       <<(fg_fixbaseline?"  With FIXED baselines":" (Phases & baselines)")
       <<" Simplex-Minimisation ? "<<(do_baselinesimplex?"Yes":"No")<<endl;
276
  cout << " --- TrackSetFiles: (NbFiles="<<lastargs.size()<<" default directory: "<<default_input_path<<")"<<endl;
277 278 279 280 281 282 283 284
  trksetfiles = lastargs;
  for (size_t i=0; i<trksetfiles.size(); i++) {
    cout << "["<<i<<"] : "<< trksetfiles[i]<<endl;
  }
  cout<<"---------------------------------------------------------------------------------"<<endl;
  return 0;
}