MS2ASDM.h

Go to the documentation of this file.
00001 //# MS2ASDM.h 
00002 //#
00003 //#  ALMA - Atacama Large Millimeter Array
00004 //#  (c) European Southern Observatory, 2002
00005 //#  (c) Associated Universities Inc., 2002
00006 //#  Copyright by ESO (in the framework of the ALMA collaboration),
00007 //#  Copyright by AUI (in the framework of the ALMA collaboration),
00008 //#  All rights reserved.
00009 //#  
00010 //#  This library is free software; you can redistribute it and/or
00011 //#  modify it under the terms of the GNU Lesser General Public
00012 //#  License as published by the Free software Foundation; either
00013 //#  version 2.1 of the License, or (at your option) any later version.
00014 //#  
00015 //#  This library is distributed in the hope that it will be useful,
00016 //#  but WITHOUT ANY WARRANTY, without even the implied warranty of
00017 //#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 //#  Lesser General Public License for more details.
00019 //#  
00020 //#  You should have received a copy of the GNU Lesser General Public
00021 //#  License along with this library; if not, write to the Free Software
00022 //#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 //#  MA 02111-1307  USA
00024 //# $Id: $
00025 #include <ms/MeasurementSets/MeasurementSet.h>
00026 #include <ms/MeasurementSets/MSColumns.h>
00027 #include <casa/aips.h>
00028 #include <casa/Arrays/Array.h>
00029 #include <casa/Arrays/Vector.h>
00030 #include <map>
00031 #include <vector>
00032 #include <casa/OS/Directory.h>
00033 
00034 #include <alma/ASDM/ASDM.h>
00035 #include <alma/ASDM/Tag.h>
00036 #include <alma/ASDM/Complex.h>
00037 #include <alma/ASDM/Frequency.h>
00038 #include <alma/ASDM/Angle.h>
00039 #include <alma/ASDM/AngularRate.h>
00040 #include <alma/ASDM/Length.h>
00041 #include <alma/ASDM/Temperature.h>
00042 #include <alma/ASDM/ArrayTimeInterval.h>
00043 #include <alma/ASDM/EntityRef.h>
00044 #include <alma/Enumerations/CStokesParameter.h>
00045 #include <alma/Enumerations/CAntennaType.h>
00046 #include <alma/Enumerations/CBasebandName.h>
00047 #include <alma/Enumerations/CNetSideband.h>
00048 #include <alma/Enumerations/CFrequencyReferenceCode.h>
00049 #include <alma/Enumerations/CReceiverBand.h>
00050 #include <alma/Enumerations/CReceiverSideband.h>
00051 
00052 
00053 #ifndef MSVIS_MS2ASDM_H
00054 namespace casa { //# NAMESPACE CASA - BEGIN
00055 
00056 #define MSVIS_MS2ASDM_H
00057 
00058 // <summary>
00059 // MS2ASDM provides functionalities to create an ASDM (ALMA science data model)
00060 // from an existing MS
00061 // </summary>
00062 
00063 // <visibility=export>
00064 
00065 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00066 // </reviewed>
00067 
00068 // <prerequisite>
00069 //   <li> MeasurementSet
00070 // </prerequisite>
00071 //
00072 // <etymology>
00073 // </etymology>
00074 //
00075 // <synopsis>
00076 // </synopsis>
00077 
00078   class MS2ASDM : public ROMSColumns
00079 {
00080 
00081  public:
00082   
00083   // construct from an MS
00084   MS2ASDM(MeasurementSet& ms);
00085   
00086   ~MS2ASDM();
00087   
00088   const String& showversion();
00089 
00090   // set verbosity of the write methods
00091   void setVerbosity(const uInt verbosity = 2){ // 0 = only warnings, 1 = most, 2 = everything
00092     verbosity_p = verbosity; }
00093   
00094   void setBaseUid(const String& baseuid);
00095   
00096   const String& getBaseUid();
00097   
00098   const std::string& getCurrentUid();
00099   
00100   // return currentUid_p with all ":" and "/" characters replaced by "_"
00101   const std::string& getCurrentUidAsFileName();
00102 
00103   // set maximum duration of a subscan in seconds, 0. == no time limit
00104   void setSubScanDuration(const Double subscanDuration = 24.*3600.){
00105     subscanDuration_p = subscanDuration; }
00106 
00107   // get maximum duration of a subscan in seconds
00108   Double getSubScanDuration(){ return subscanDuration_p; }
00109 
00110   // set maximum duration of a Scheduling Block in seconds
00111   void setSBDuration(const Double sBDuration = 2700.){ // 45 minutes
00112     schedBlockDuration_p = sBDuration; }
00113 
00114   // get maximum duration of a Scheduling Block in seconds
00115   Double getSBDuration(){ return schedBlockDuration_p; }
00116 
00117   void setDataAPCorrected(const Bool isCorrected = True){
00118     dataIsAPCorrected_p = isCorrected; }
00119 
00120   Bool dataIsAPCorrected(){ return dataIsAPCorrected_p; }
00121 
00122   void setObservatoryName(const String& telName){
00123     telName_p = telName; }
00124 
00125   void getObservatoryName( String& telName ){
00126     telName = telName_p; }
00127 
00128   // convert CASA Stokes to ASDM Stokes
00129   StokesParameterMod::StokesParameter ASDMStokesParameter( Stokes::StokesTypes s);
00130 
00131   // convert CASA antenna type string to ASDM antenna type enum
00132   AntennaTypeMod::AntennaType ASDMAntennaType( const String& type ); 
00133 
00134   // convert time in seconds to an array time
00135   ArrayTime ASDMArrayTime( const Double seconds ){ 
00136     return ArrayTime((int64_t) (floor(seconds*ArrayTime::unitsInASecond))); }
00137 
00138   // convert array time to time in seconds
00139   Double MSTimeSecs( const ArrayTime atime ){ 
00140     return (Double) atime.get() / (Double)ArrayTime::unitsInASecond; }
00141 
00142   asdm::Interval ASDMInterval( const Double seconds ){ 
00143     return asdm::Interval((int64_t) (floor(seconds*ArrayTime::unitsInASecond))); }
00144 
00145   // convert MS style time interval to ASDM ArrayTimeInterval
00146   asdm::ArrayTimeInterval ASDMTimeInterval( const Quantity midpoint, const Quantity interval);
00147 
00148   // return start of MS main table timestamp (seconds)
00149   Double timestampStartSecs(const uInt mainTabRow){
00150     return timeQuant()(mainTabRow).getValue("s") - intervalQuant()(mainTabRow).getValue("s")/2.; }
00151 
00152   // return end of MS main table timestamp (seconds)
00153   Double timestampEndSecs(const uInt mainTabRow){
00154     return timeQuant()(mainTabRow).getValue("s") + intervalQuant()(mainTabRow).getValue("s")/2.; }
00155 
00156   // convert MDirection to a vector of Angles
00157   vector< asdm::Angle > ASDMAngleV(const MDirection mDir);
00158 
00159   // convert a base band converter number to an ASDM base band name
00160   BasebandNameMod::BasebandName ASDMBBName( const Int bbcNo );
00161 
00162   // convert a MS net sideband no. to an ASDM enum
00163   NetSidebandMod::NetSideband ASDMNetSideBand( const Int netSideband );
00164 
00165   // set a representative frequency, the receiver band and receiver sideband based on a frequency refFreq
00166   //   and the previously set observatory name telName_p, return the band id as an Int (1 to 10),
00167   //   -1 if refFreq is outside ALMA bands but observatory is ALMA, 0 if observatory not ALMA
00168   Int setRecBands( const asdm::Frequency refFreq,
00169                    Double& frequency,
00170                    ReceiverBandMod::ReceiverBand& frequencyBand,
00171                    ReceiverSidebandMod::ReceiverSideband& receiverSideband);
00172 
00173   FrequencyReferenceCodeMod::FrequencyReferenceCode ASDMFreqRefCode( const MFrequency::Types refFrame ); 
00174 
00175   Unit unitASDMFreq(){ return Unit(String(asdm::Frequency::unit())); }
00176 
00177   Unit unitASDMAngle(){ return Unit(String(asdm::Angle::unit())); }
00178 
00179   Unit unitASDMAngularRate(){ return Unit(String(asdm::AngularRate::unit())); }
00180 
00181   Unit unitASDMLength(){ return Unit(String(asdm::Length::unit())); }
00182 
00183   Unit unitASDMTemp(){ return Unit(String(asdm::Temperature::unit())); }
00184 
00185   asdm::Complex ASDMComplex( casa::Complex x ){ return asdm::Complex(x.real(), x.imag()); }
00186 
00187   // write the entire ASDM from scratch
00188   Bool writeASDM(const String& asdmfile="", 
00189                  const String& datacolumn="data", 
00190                  const String& archiveid="S0", 
00191                  const String& rangeid="X1", 
00192                  const Bool verbose=True,
00193                  const Double maxSubscanDuration = 24.*3600.,
00194                  const Double maxSchedBlockDuration = 2700.,
00195                  const Bool msDataIsAPCorrected=True
00196                  );
00197 
00198  private:
00199   // *** Private member functions ***
00200 
00201   Bool incrementUid(); // returns true if successful
00202 
00203   Bool setDirectory(const String& asdmfile);
00204 
00205 
00206   Bool writeStation();
00207 
00208   Bool writeAntenna();
00209 
00210   Bool writeSpectralWindow();
00211 
00212   Bool writeSource();
00213 
00214   Bool writePolarization();
00215 
00216   Bool writeCorrelatorMode(); // not called directly but optionally called by writeProcessor()
00217   Bool writeAlmaRadiometer(); // optionally called by writeProcessor()
00218   Bool writeHolography(); // optionally called by writeProcessor()
00219 
00220   Bool writeProcessor();
00221 
00222   Bool writeField();
00223 
00224   Bool writeReceiver();
00225 
00226   Bool writeFeed();
00227 
00228   Bool writeDataDescription();
00229 
00230   Bool writeSwitchCycle(); // not yet fully implemented
00231 
00232   Bool writeState();
00233 
00234   Bool writeSysCal();
00235 
00236   Bool writeConfigDescription();
00237 
00238   // Scheme
00239   // 1) We regard one MS Observation as a set of ASDM ExecBlocks modelled on 
00240   //    a single ASDM Scheduling Block
00241   // 2) ALMA ExecBlocks are at most 30 minutes long.
00242   //    If an MS Observation is more than 30 Minutes long, it is split up into 
00243   //    several ASDM ExecBlocks each referring to the same Scheduling Block.
00244   // 3) Each ASDM ExecBlock contains one or more ASDM Scans based on the MS scans 
00245   // 4) Each ASDM Scan contains one or more ASDM Subscans
00246   // 5) Each ASDM Subscan is at most subscanduration long. (external parameter)
00247   // 6) If an MS Scan is longer than subscanduration, it is split up into 
00248   //    several ASDM subscans.
00249 
00250   Bool writeSBSummaryAndExecBlockStubs(); // "stubs" because these tables will be completed later
00251                                           //  with information from the APDM
00252   Bool writeMainAndScanAndSubScan(const String& datacolumn);
00253 
00254   // write the Main binary data for one DataDescId/FieldId pair and one SubScan
00255   // (return number of integrations written and set the last three parameters in the list)
00256   Int writeMainBinSubScanForOneDDIdFIdPair(const Int theDDId, const Int theFieldId, 
00257                                            const String& datacolumn, 
00258                                            const uInt theScan, const uInt theSubScan,
00259                                            const uInt startRow, const uInt endRow,
00260                                            const asdm::Tag eBlockId,
00261                                            int& datasize, asdm::EntityRef& dataOid, 
00262                                            vector< asdm::Tag >& stateId);
00263 
00264   Bool writePointingModel(); // write dummy pointing models
00265 
00266   Bool writePointing();
00267     
00268   // *** Aux. methods ***
00269 
00270   // check if vector corrT already contains a stokes type equivalent to st
00271   Bool stokesTypePresent( const Vector< Int > corrT, const Stokes::StokesTypes st );
00272 
00273   // *** Member variables ***
00274 
00275   // Initialized* by ctors.  (Maintain order both here and in ctors.)
00276   MeasurementSet ms_p; // the measurement set from which the ASDM is filled
00277 
00278   asdm::ASDM* ASDM_p; // the new ASDM
00279 
00280   string asdmVersion_p; // the version of the new ASDM
00281 
00282   uInt verbosity_p; // verbosity of the write methods
00283 
00284   String baseUid_p;  // the part of the UID which is common to all elements of the ASDM,
00285                     // i.e. typically "uid://archiveid/rangeid/"
00286 
00287   uInt runningId_p; // counter for the tables written; starts at 1!
00288                     // used to construct the UIDs: uid = baseUid_p + (runningId_p converted to unpadded hex string)
00289 
00290   String currentUid_p; // the last used uid
00291 
00292   String telName_p; // the name of the observatory from first row of MS observation table
00293 
00294   Double subscanDuration_p; // maximum duration of a subscan in seconds
00295 
00296   Double schedBlockDuration_p; // maximum duration of a scheduling or exec block in seconds
00297 
00298   Bool dataIsAPCorrected_p; // true if the data in the selected MS data column is 
00299                             // AtmPhaseCorrectionMod::AP_CORRECTED, false if it is
00300                             // AtmPhaseCorrectionMod::AP_UNCORRECTED
00301 
00302   string asdmUID_p; // ASDM UID == container ID of all tables
00303 
00304   String asdmDir_p; // ASDM output directory name
00305 
00306   SimpleOrderedMap <String, asdm::Tag> asdmStationId_p;  
00307   SimpleOrderedMap <Int, asdm::Tag> asdmAntennaId_p;
00308   SimpleOrderedMap <Int, asdm::Tag> asdmSpectralWindowId_p;
00309   SimpleOrderedMap <Int, asdm::Tag> asdmPolarizationId_p;
00310   SimpleOrderedMap <Int, asdm::Tag> asdmProcessorId_p;
00311   SimpleOrderedMap <Int, asdm::Tag> asdmFieldId_p;
00312   SimpleOrderedMap <Int, asdm::Tag> asdmEphemerisId_p;
00313   SimpleOrderedMap <Int, asdm::Tag> asdmDataDescriptionId_p;
00314   SimpleOrderedMap <Int, asdm::Tag> asdmStateId_p;
00315   SimpleOrderedMap <uInt, asdm::Tag> asdmConfigDescriptionId_p; // maps from MS Main rows
00316   SimpleOrderedMap <Int, asdm::Tag> asdmSBSummaryId_p; // maps from MS Observation Id + 10000*SpwId
00317   SimpleOrderedMap <Double, asdm::Tag> asdmExecBlockId_p; // maps from MS Main timestamps 
00318   SimpleOrderedMap <Int, int> asdmFeedId_p; // ASDM feed id is not a Tag
00319   SimpleOrderedMap <Int, int> asdmSourceId_p; // neither is the source id
00320   SimpleOrderedMap <asdm::Tag, int> asdmPointingModelId_p; // maps ASDM Antenna Id to dummy pointing model
00321 
00322   vector< vector< Bool > > skipCorr_p; // skipCorr_p[j][PolId] indicates that correlation 
00323                                        // product j for POLARIZATION_ID PolId should not 
00324                                        // be written in the ASDM
00325 
00326 };
00327 
00328 
00329 } //# NAMESPACE CASA - END
00330 
00331 //#ifndef AIPS_NO_TEMPLATE_SRC
00332 //#include <alma/MS2ASDM/MS2ASDM.tcc>
00333 //#endif //# AIPS_NO_TEMPLATE_SRC
00334 
00335 #endif
00336 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines