casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ASDM2MSFiller.h
Go to the documentation of this file.
00001 // ASDM2MSFiller.h: implementation of a MeasurementSet's filler
00002 // for Francois Viallefond & Frederic Badia ALMA Simulator
00003 //
00004 //  Copyright (C) 2001
00005 //  OBSERVATOIRE DE PARIS - DEMIRM
00006 //  Avenue Denfert Rochereau - 75014 - PARIS
00007 //
00008 //  This program is free software; you can redistribute it and/or modify
00009 //  it under the terms of the GNU General Public License as published by
00010 //  the Free Software Foundation; either version 2 of the License, or
00011 //  (at your option) any later version.
00012 //
00013 //  This program is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //  GNU General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU General Public License
00019 //  along with this program; if not, write to the Free Software
00020 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 //
00022 //
00023 //
00025 #if !defined(ALMA_ASDM2MSFILLER_H)
00026 #define ALMA_ASDM2MSFILLER_H
00027 //# Includes
00028 
00029 #include <casa/aips.h>
00030 #include <casa/Utilities/Assert.h>
00031 #include <tables/Tables.h>
00032 #include <ms/MeasurementSets/MeasurementSet.h>
00033 #include <ms/MeasurementSets/MSAntennaColumns.h>
00034 #include <ms/MeasurementSets/MSDataDescColumns.h>
00035 #include <ms/MeasurementSets/MSFeedColumns.h>
00036 #include <ms/MeasurementSets/MSFieldColumns.h>
00037 #include <ms/MeasurementSets/MSFlagCmdColumns.h>
00038 #include <ms/MeasurementSets/MSHistoryColumns.h>
00039 #include <ms/MeasurementSets/MSMainColumns.h>
00040 
00041 #include <ms/MeasurementSets/MSObsColumns.h>
00042 #include <ms/MeasurementSets/MSPointingColumns.h>
00043 #include <ms/MeasurementSets/MSPolColumns.h>
00044 #include <ms/MeasurementSets/MSProcessorColumns.h>
00045 #include <ms/MeasurementSets/MSSourceColumns.h>
00046 #include <ms/MeasurementSets/MSStateColumns.h>
00047 #include <ms/MeasurementSets/MSSpWindowColumns.h>
00048 #include <ms/MeasurementSets/MSSysCalColumns.h>
00049 #include <ms/MeasurementSets/MSWeatherColumns.h>
00050 
00051 #include <tables/Tables/StandardStMan.h>
00052 #include <tables/Tables/TiledShapeStMan.h>
00053 #include <tables/Tables/SetupNewTab.h>
00054 #include <tables/Tables/TableDesc.h>
00055 #include <tables/Tables/TableRecord.h>
00056 #include <casa/Arrays/Vector.h>
00057 #include <casa/Arrays/Cube.h>
00058 #include <casa/Arrays/Matrix.h>
00059 #include <casa/Arrays/ArrayMath.h>
00060 #include <casa/Arrays/ArrayUtil.h>
00061 #include <casa/Arrays/ArrayLogical.h>
00062 #include <casa/Containers/Block.h>
00063 #include <casa/Containers/OrderedMap.h>
00064 #include <measures/Measures/MPosition.h>
00065 #include <measures/Measures/MBaseline.h>
00066 #include <measures/Measures/Muvw.h>
00067 #include <measures/Measures/MeasTable.h>
00068 #include <measures/Measures/Stokes.h>
00069 #include <measures/Measures/MeasConvert.h>
00070 #include <measures/TableMeasures/TableMeasValueDesc.h>
00071 #include <measures/TableMeasures/TableMeasOffsetDesc.h>
00072 #include <measures/TableMeasures/TableMeasRefDesc.h>
00073 #include <measures/TableMeasures/TableMeasDesc.h>
00074 #include <measures/Measures/MeasConvert.h>
00075 #include "measures/Measures/Stokes.h"
00076 #include <casa/BasicSL/Constants.h>
00077 #include <casa/OS/File.h>
00078 #include <casa/OS/Path.h>
00079 #include <complex>
00080 
00081 #include <vector>
00082 
00083 
00084 using namespace casa;
00085 using namespace std;
00086 
00087 //# Forward Declarations
00088 
00089 class TimeRange;
00090 class MPosition;
00091 class MeasFrame;
00092 class MeasurementSet;
00093 class MSMainColumns;
00094 
00095 //
00096 // A structure to define a range of rows in the Pointing table where the attribute overTheTop is defined and with which value.
00097 //
00098 struct s_overTheTop {
00099   unsigned int  start;   // The index of the first row of the range. 
00100   unsigned int  len;     // The number of consecutive rows in the range.
00101   bool value;   // The value of overTheTop in that range.
00102 };
00103 
00104 //
00105 // Class timeMgr is a utility to help for the management
00106 // of time in tables with TIME and INTERVAL columns
00107 class timeMgr {
00108  private:
00109   int index;
00110   double startTime;
00111 
00112  public:
00113   timeMgr();
00114   timeMgr(int i, double t);
00115   void   setIndex(int i);
00116   void   setStartTime(double t);
00117   int    getIndex();
00118   double getStartTime();
00119 };
00120 
00121 
00122 // Class ddMgr is a utility to help for the management
00123 // of DataDescription, SpectralWindow and Polarization ids.
00124 // Here we provide enough space to store 100 values for 
00125 // each quantity; this is very likeky far beyond the actual
00126 // needs.
00127 class ddMgr {
00128  private:
00129   int     numCorr[100];
00130   int     numChan[100];
00131   struct  {
00132     int polId;
00133     int swId;
00134   } dd[100];
00135   
00136  public:
00137 
00138   ddMgr();
00139 
00140   int setNumCorr(int i, int numChan);
00141   int setNumChan(int i, int numCorr);
00142 
00143   int getNumCorr(int i);
00144   int getNumChan(int i);
00145 
00146   int setDD(int i, int polId, int swId); 
00147 
00148   int getPolId(int i);
00149   int getSwId(int i);
00150 };
00151 
00152 
00153  
00154 // Class ASDM2MSFiller
00155 class ASDM2MSFiller
00156 {
00157  private:
00158   double         itsCreationTime;
00159   const string   itsName;
00160   int            itsNumAntenna;
00161   int            itsNumChan;
00162   int            itsNumCorr;
00163   casa::MeasurementSet *itsMS;
00164   casa::MSMainColumns  *itsMSCol;
00165   /*
00166     Block<timeMgr> itsFeedTimeMgr;
00167     Block<timeMgr> itsPointingTimeMgr;
00168     Block<timeMgr> itsSyscalTimeMgr;
00169     Block<timeMgr> itsWeatherTimeMgr;
00170     Block<timeMgr> itsObservationTimeMgr;
00171   */
00172     
00173   String     itsMSPath;
00174   timeMgr* itsFeedTimeMgr;
00175   timeMgr* itsFieldTimeMgr;
00176   timeMgr* itsObservationTimeMgr;
00177   timeMgr* itsPointingTimeMgr;
00178   //OrderedMap<int, timeMgr> itsSourceTimeMgr;
00179   timeMgr* itsSourceTimeMgr;
00180   timeMgr* itsSyscalTimeMgr;
00181   timeMgr* itsWeatherTimeMgr;
00182     
00183   Bool     itsWithRadioMeters;     /* Are we building an ALMA MS ?*/
00184   Bool     itsFirstScan;
00185   uInt     itsMSMainRow;
00186   /*TiledDataStManAccessor itsImWgtAcc;*/
00187   Block<IPosition> itsDataShapes;
00188 
00189   int itsScanNumber;
00190   int itsNCat;
00191     
00192   ddMgr    itsDDMgr;
00193 
00194          
00195   int createMS(const string& msName, 
00196                bool complexData, 
00197                bool withCompression, 
00198                const string& telName, 
00199                int maxNumCorr,
00200                int maxNumChan,
00201                bool withCorrectedData=false);
00202 
00203   const char** getPolCombinations(int numCorr);
00204     
00205   static map<string, MDirection::Types> string2MDirection;
00206   static map<string, MDirection::Types> string2MDirectionInit();
00207    
00208  public:  
00209   ASDM2MSFiller (const string&  name_,
00210                  double         creation_time_,
00211                  bool           withRadioMeters,
00212                  bool           complexData,
00213                  bool           withCompression,
00214                  const string&  telName, 
00215                  int            intintmaxNumCorr,
00216                  int            maxNumChan,
00217                  bool           withCorrectedData=false);
00218   
00219   // Destructor
00220   ~ASDM2MSFiller();
00221 
00222   const casa::MeasurementSet* ms();
00223 
00224   int addAntenna(const string&   name_,
00225                  const string&   station_,
00226                  double          lx_,
00227                  double          ly_,
00228                  double          lz_,
00229                  double          offset_x_,
00230                  double          offset_y_,
00231                  double          offset_z_,
00232                  float           dish_diam_);
00233 
00234   void addData (bool                      complexData,
00235                 vector<double>            &time_,
00236                 vector<int>               &antennaId1_,
00237                 vector<int>               &antennaId2_,
00238                 vector<int>               &feedId1_,
00239                 vector<int>               &feedId2_,
00240                 vector<int>               &dataDescId_,
00241                 int                       processorId_,
00242                 vector<int>               &fieldId_,
00243                 vector<double>            &interval_,
00244                 vector<double>            &exposure_,
00245                 vector<double>            &timeCentroid_,
00246                 int                       scanNumber_,
00247                 int                       arrayId_,
00248                 int                       observationId_,
00249                 vector<int>               &stateId_,
00250                 vector<double>            &uvw_,
00251                 vector<vector<unsigned int> >      &dataShape_,
00252                 vector<float *>           &uncorrectedData_,
00253                 vector<float *>           &correctedData_,
00254                 vector<unsigned int>      &flag_);
00255 
00256   void addData (bool                      complexData,
00257                 vector<double>            &time_,
00258                 vector<int>               &antennaId1_,
00259                 vector<int>               &antennaId2_,
00260                 vector<int>               &feedId1_,
00261                 vector<int>               &feedId2_,
00262                 vector<int>               &dataDescId_,
00263                 int                       processorId_,
00264                 vector<int>               &fieldId_,
00265                 vector<double>            &interval_,
00266                 vector<double>            &exposure_,
00267                 vector<double>            &timeCentroid_,
00268                 int                       scanNumber_,
00269                 int                       arrayId_,
00270                 int                       observationId_,
00271                 vector<int>               &stateId_,
00272                 vector<double>            &uvw_,
00273                 vector<vector<unsigned int> >      &dataShape_,
00274                 vector<float *>           &data_,
00275                 vector<unsigned int>      &flag_);
00276   
00277 
00278                
00279   int  addDataDescription(int spectral_window_id_,
00280                           int polarizarion_id_);
00281 
00282   int  addUniqueDataDescription(int spectral_window_id_,
00283                                 int polarizarion_id_);
00284 
00285   int  exists(char *path);
00286   String msPath();
00287 
00288 
00289   void addFeed(int      antenna_id_,
00290                int      feed_id_,
00291                int      spectral_window_id_,
00292                double   time_,
00293                double   interval_,
00294                int      num_receptors_,
00295                int      beam_id_,
00296                vector<double> &   beam_offset_,
00297                vector<std::string> & pol_type_,
00298                vector<std::complex<float> > & polarization_response_,
00299                vector<double>&   position_,  // Must be a 3 elements vector !!!
00300                vector<double>&   receptor_angle_);
00301   
00302   void addField( const string&           name_,
00303                  const string&           code_,
00304                  double            time_,
00305                  vector<double>&   delay_dir_,
00306                  vector<double>&   phase_dir_,
00307                  vector<double>&   reference_dir_,
00308                  const string&     direction_code_,
00309                  int               source_id_);
00310 
00311   void addFlagCmd(double        time_,
00312                   double        interval_,
00313                   const string& type_,
00314                   const string& reason_,
00315                   int           level_,
00316                   int           severity_,
00317                   int           applied_,
00318                   string&       command_);
00319 
00320   void addHistory( double               time_,
00321                    int                  observation_id_,
00322                    const string&        message_,
00323                    const string&        priority_,
00324                    const string&        origin_,
00325                    int                  object_id_,
00326                    const string&        application_,
00327                    const string&        cli_command_,
00328                    const string&        app_parms_ );
00329 
00330   void addObservation(const string&             telescopeName_,
00331                       double                    startTime_,
00332                       double                    endTime_,
00333                       const string&             observer_,
00334                       const vector<string>&     log_,
00335                       const string&             schedule_type_,
00336                       const vector<string>&     schedule_,
00337                       const string&             project_,
00338                       double                    release_date_);
00339 
00340   void addPointingSlice(unsigned int                  n_row_,
00341                         vector<int>&                  antenna_id_,
00342                         vector<double>&               time_,
00343                         vector<double>&               interval_,
00344                         vector<double>&               direction_,
00345                         vector<double>&               target_,
00346                         vector<double>&               pointing_offset_,
00347                         vector<double>&               encoder_,
00348                         vector<bool>&                 tracking_,
00349                         bool                          overTheTopExists4All_,
00350                         vector<bool>&                 v_overTheTop_,
00351                         vector<s_overTheTop>&         v_s_overTheTop_);
00352 
00353   int  addPolarization(int num_corr_,
00354                        vector<int>& corr_type_,
00355                        vector<int>& corr_product_);
00356 
00357   int addUniquePolarization(int num_corr_,
00358                             //                      const vector<Stokes::StokesTypes>& corr_type_,
00359                             const vector<int>& corr_type_,
00360                             const vector<int>& corr_product_);
00361 
00362   void addProcessor(string& type_,
00363                     string& sub_type_,
00364                     int  type_id_,
00365                     int  mode_id_);
00366 
00367   void addSource(int             source_id_,
00368                  double          time_,
00369                  double          interval_,
00370                  int             spectral_window_id_,
00371                  int             num_lines_,
00372                  string&         name_,
00373                  int             calibration_group_,
00374                  string&         code_,
00375                  vector<double>& direction_,
00376                  string&         direction_code_,
00377                  vector<double>& position_,
00378                  vector<double>& proper_motion_,
00379                  vector<string>& transition_,
00380                  vector<double>& rest_frequency_,
00381                  vector<double>& sysvel_);
00382                  
00383   int  addSpectralWindow(int                    num_chan_,
00384                          const string&          name_,
00385                          double                 ref_frequency_,
00386                          const vector<double>&  chan_freq_,
00387                          const vector<double>&  chan_width_,
00388                          int                    meas_freq_ref_,
00389                          const vector<double>&  effective_bw_,
00390                          const vector<double>&  resolution_,
00391                          double                 total_bandwidth_,
00392                          int                    net_sideband_,
00393                          int                    bbc_no_,
00394                          int                    if_conv_chain_,
00395                          int                    freq_group_,
00396                          const string&          freq_group_name_,
00397                          int                    num_assoc_,
00398                          const vector<int>&     assoc_sp_id_,
00399                          const vector<string>&  assoc_nature_);
00400 
00401   int  addUniqueState(bool sig_,
00402                       bool ref_,
00403                       double cal_,
00404                       double load_,
00405                       unsigned int sub_scan_,
00406                       string& obs_mode_,
00407                       bool flag_row_);
00408   
00409   
00410   void addState(bool    sig_,
00411                 bool    ref_,
00412                 double  cal_,
00413                 double  load_,
00414                 int     sub_scan_,
00415                 string& obs_mode_);
00416   
00417   void addSysCal(int    antenna_id,
00418                  int    feed_id,
00419                  int    spectral_window_id,
00420                  double time_,
00421                  double interval_,
00422                  int    numReceptor_,
00423                  int    numChan_,
00424                  pair<bool, vector<float> >& tcal_spectrum_pair,
00425                  pair<bool, bool>&           tcal_flag_pair,
00426                  pair<bool, vector<float> >& trx_spectrum_pair,
00427                  pair<bool, bool>&           trx_flag_pair,
00428                  pair<bool, vector<float> >& tsky_spectrum_pair,
00429                  pair<bool, bool>&           tsky_flag_pair,
00430                  pair<bool, vector<float> >& tsys_spectrum_pair,
00431                  pair<bool, bool>&           tsys_flag_pair,
00432                  pair<bool, vector<float> >& tant_spectrum_pair,
00433                  pair<bool, bool>&           tant_flag_pair,
00434                  pair<bool, vector<float> >& tant_tsys_spectrum_pair,
00435                  pair<bool, bool>&           tant_tsys_flag_pair);
00436 
00437  void addWeather(int                            antenna_id_,
00438                   double                        time_,
00439                   double                        interval_,
00440                   const pair<bool, float>&      pressure_opt_,
00441                   const pair<bool, float>&      relHumidity_opt_,
00442                   const pair<bool, float>&      temperature_opt_,
00443                   const pair<bool, float>&      windDirection_opt_,
00444                   const pair<bool, float>&      windSpeed_opt_,
00445                   const pair<bool, float>&      dewPoint_opt_,
00446                   int                           wx_station_id_,
00447                   vector<double>&               wx_station_position_);
00448 
00460   void addCalDevice(int                         antennaId,
00461                     int                         feedId,
00462                     int                         spectralWindowId,
00463                     double                      time,
00464                     double                      interval,
00465                     unsigned int                numCalLoad,
00466                     vector<string>              calloadNames,
00467                     unsigned int                numReceptor,
00468                     vector<vector<float> >&     calEff,
00469                     vector<vector<float> >&     noiseCal,
00470                     vector<double >&            temperatureLoad);
00471 
00494   void addSysPower(int                  antennaId,
00495                    int                  feedId,
00496                    int                  spectralWindowId,
00497                    double               time,
00498                    double               interval,
00499                    unsigned int         numReceptor,
00500                    vector<float>&       switchedPowerDifference,
00501                    vector<float>&       switchedPowerSum,
00502                    vector<float>&       requantizerGain); 
00503 
00504   void addSysPowerSlice(unsigned int    nRow,
00505                         vector<int>&    antennaId,
00506                         vector<int>&    spectralWindowId,
00507                         vector<int>&    feedId,
00508                         vector<double>& time,
00509                         vector<double>& interval,
00510                         unsigned int    numReceptor,
00511                         vector<float>&  switchedPowerDifference,
00512                         vector<float>&  switchedPowerSum,
00513                         vector<float>&  requantizerGain);
00514 
00515   void end(double time_);
00516 };
00517 #endif
00518