casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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_v3/ASDM/ASDM.h>
00035 #include <alma_v3/ASDM/Tag.h>
00036 #include <alma_v3/ASDM/ComplexWrapper.h>
00037 #include <alma_v3/ASDM/Frequency.h>
00038 #include <alma_v3/ASDM/Angle.h>
00039 #include <alma_v3/ASDM/AngularRate.h>
00040 #include <alma_v3/ASDM/Length.h>
00041 #include <alma_v3/ASDM/Temperature.h>
00042 #include <alma_v3/ASDM/ArrayTimeInterval.h>
00043 #include <alma_v3/ASDM/EntityRef.h>
00044 #include <alma_v3/Enumerations/CStokesParameter.h>
00045 #include <alma_v3/Enumerations/CAntennaType.h>
00046 #include <alma_v3/Enumerations/CBasebandName.h>
00047 #include <alma_v3/Enumerations/CNetSideband.h>
00048 #include <alma_v3/Enumerations/CFrequencyReferenceCode.h>
00049 #include <alma_v3/Enumerations/CReceiverBand.h>
00050 #include <alma_v3/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 MDirection type to ASDM DirectionReferenceCode
00160   DirectionReferenceCodeMod::DirectionReferenceCode ASDMDirRefCode(const MDirection::Types type);
00161 
00162   // convert a base band converter number to an ASDM base band name
00163   BasebandNameMod::BasebandName ASDMBBName( const Int bbcNo );
00164 
00165   // convert a MS net sideband no. to an ASDM enum
00166   NetSidebandMod::NetSideband ASDMNetSideBand( const Int netSideband );
00167 
00168   // set a representative frequency, the receiver band and receiver sideband based on a frequency refFreq
00169   //   and the previously set observatory name telName_p, return the band id as an Int (1 to 10),
00170   //   -1 if refFreq is outside ALMA bands but observatory is ALMA, 0 if observatory not ALMA
00171   Int setRecBands( const asdm::Frequency refFreq,
00172                    Double& frequency,
00173                    ReceiverBandMod::ReceiverBand& frequencyBand,
00174                    ReceiverSidebandMod::ReceiverSideband& receiverSideband);
00175 
00176   FrequencyReferenceCodeMod::FrequencyReferenceCode ASDMFreqRefCode( const MFrequency::Types refFrame ); 
00177 
00178   Unit unitASDMFreq(){ return Unit(String(asdm::Frequency::unit())); }
00179 
00180   Unit unitASDMAngle(){ return Unit(String(asdm::Angle::unit())); }
00181 
00182   Unit unitASDMAngularRate(){ return Unit(String(asdm::AngularRate::unit())); }
00183 
00184   Unit unitASDMLength(){ return Unit(String(asdm::Length::unit())); }
00185 
00186   Unit unitASDMTemp(){ return Unit(String(asdm::Temperature::unit())); }
00187 
00188   asdm::Complex ASDMComplex( casa::Complex x ){ return asdm::Complex(x.real(), x.imag()); }
00189 
00190   // write the entire ASDM from scratch
00191   Bool writeASDM(const String& asdmfile="", 
00192                  const String& datacolumn="data", 
00193                  const String& archiveid="S0", 
00194                  const String& rangeid="X1", 
00195                  const Bool verbose=True,
00196                  const Double maxSubscanDuration = 24.*3600.,
00197                  const Double maxSchedBlockDuration = 2700.,
00198                  const Bool msDataIsAPCorrected=True
00199                  );
00200 
00201  private:
00202   // *** Private member functions ***
00203 
00204   Bool incrementUid(); // returns true if successful
00205 
00206   Bool setDirectory(const String& asdmfile);
00207 
00208 
00209   Bool writeStation();
00210 
00211   Bool writeAntenna();
00212 
00213   Bool writeSpectralWindow();
00214 
00215   Bool writeSource();
00216 
00217   Bool writePolarization();
00218 
00219   Bool writeCorrelatorMode(); // not called directly but optionally called by writeProcessor()
00220   Bool writeAlmaRadiometer(); // optionally called by writeProcessor()
00221   Bool writeHolography(); // optionally called by writeProcessor()
00222 
00223   Bool writeProcessor();
00224 
00225   Bool writeField();
00226 
00227   Bool writeReceiver();
00228 
00229   Bool writeFeed();
00230 
00231   Bool writeDataDescription();
00232 
00233   Bool writeSwitchCycle(); // not yet fully implemented
00234 
00235   Bool writeState();
00236 
00237   Bool writeSysCal();
00238 
00239   Bool writeConfigDescription();
00240 
00241   // Scheme
00242   // 1) We regard one MS Observation as a set of ASDM ExecBlocks modelled on 
00243   //    a single ASDM Scheduling Block
00244   // 2) ALMA ExecBlocks are at most 30 minutes long.
00245   //    If an MS Observation is more than 30 Minutes long, it is split up into 
00246   //    several ASDM ExecBlocks each referring to the same Scheduling Block.
00247   // 3) Each ASDM ExecBlock contains one or more ASDM Scans based on the MS scans 
00248   // 4) Each ASDM Scan contains one or more ASDM Subscans
00249   // 5) Each ASDM Subscan is at most subscanduration long. (external parameter)
00250   // 6) If an MS Scan is longer than subscanduration, it is split up into 
00251   //    several ASDM subscans.
00252 
00253   Bool writeSBSummaryAndExecBlockStubs(); // "stubs" because these tables will be completed later
00254                                           //  with information from the APDM
00255   Bool writeMainAndScanAndSubScan(const String& datacolumn);
00256 
00257   // write the Main binary data for one DataDescId/FieldId pair and one SubScan
00258   // (return number of integrations written and set the last three parameters in the list)
00259   Int writeMainBinSubScanForOneDDIdFIdPair(const Int theDDId, const Int theFieldId, 
00260                                            const String& datacolumn, 
00261                                            const uInt theScan, const uInt theSubScan,
00262                                            const uInt startRow, const uInt endRow,
00263                                            const asdm::Tag eBlockId,
00264                                            int& datasize, asdm::EntityRef& dataOid, 
00265                                            vector< asdm::Tag >& stateId);
00266 
00267   Bool writePointingModel(); // write dummy pointing models
00268 
00269   Bool writePointing();
00270     
00271   // *** Aux. methods ***
00272 
00273   // check if vector corrT already contains a stokes type equivalent to st
00274   Bool stokesTypePresent( const Vector< Int > corrT, const Stokes::StokesTypes st );
00275 
00276   // *** Member variables ***
00277 
00278   // Initialized* by ctors.  (Maintain order both here and in ctors.)
00279   MeasurementSet ms_p; // the measurement set from which the ASDM is filled
00280 
00281   asdm::ASDM* ASDM_p; // the new ASDM
00282 
00283   string asdmVersion_p; // the version of the new ASDM
00284 
00285   uInt verbosity_p; // verbosity of the write methods
00286 
00287   String baseUid_p;  // the part of the UID which is common to all elements of the ASDM,
00288                     // i.e. typically "uid://archiveid/rangeid/"
00289 
00290   uInt runningId_p; // counter for the tables written; starts at 1!
00291                     // used to construct the UIDs: uid = baseUid_p + (runningId_p converted to unpadded hex string)
00292 
00293   String currentUid_p; // the last used uid
00294 
00295   String telName_p; // the name of the observatory from first row of MS observation table
00296 
00297   Double subscanDuration_p; // maximum duration of a subscan in seconds
00298 
00299   Double schedBlockDuration_p; // maximum duration of a scheduling or exec block in seconds
00300 
00301   Bool dataIsAPCorrected_p; // true if the data in the selected MS data column is 
00302                             // AtmPhaseCorrectionMod::AP_CORRECTED, false if it is
00303                             // AtmPhaseCorrectionMod::AP_UNCORRECTED
00304 
00305   string asdmUID_p; // ASDM UID == container ID of all tables
00306 
00307   String asdmDir_p; // ASDM output directory name
00308 
00309   SimpleOrderedMap <String, asdm::Tag> asdmStationId_p;  
00310   SimpleOrderedMap <Int, asdm::Tag> asdmAntennaId_p;
00311   SimpleOrderedMap <Int, asdm::Tag> asdmSpectralWindowId_p;
00312   SimpleOrderedMap <Int, asdm::Tag> asdmPolarizationId_p;
00313   SimpleOrderedMap <Int, asdm::Tag> asdmProcessorId_p;
00314   SimpleOrderedMap <Int, asdm::Tag> asdmFieldId_p;
00315   SimpleOrderedMap <Int, asdm::Tag> asdmEphemerisId_p;
00316   SimpleOrderedMap <Int, asdm::Tag> asdmDataDescriptionId_p;
00317   SimpleOrderedMap <Int, asdm::Tag> asdmStateId_p;
00318   SimpleOrderedMap <uInt, asdm::Tag> asdmConfigDescriptionId_p; // maps from MS Main rows
00319   SimpleOrderedMap <Int, asdm::Tag> asdmSBSummaryId_p; // maps from MS Observation Id + 10000*SpwId
00320   SimpleOrderedMap <Double, asdm::Tag> asdmExecBlockId_p; // maps from MS Main timestamps 
00321   SimpleOrderedMap <Int, int> asdmFeedId_p; // ASDM feed id is not a Tag
00322   SimpleOrderedMap <Int, int> asdmSourceId_p; // neither is the source id
00323   SimpleOrderedMap <asdm::Tag, int> asdmPointingModelId_p; // maps ASDM Antenna Id to dummy pointing model
00324 
00325   vector< vector< Bool > > skipCorr_p; // skipCorr_p[j][PolId] indicates that correlation 
00326                                        // product j for POLARIZATION_ID PolId should not 
00327                                        // be written in the ASDM
00328 
00329 };
00330 
00331 
00332 } //# NAMESPACE CASA - END
00333 
00334 //#ifndef AIPS_NO_TEMPLATE_SRC
00335 //#include <alma_v3/MS2ASDM/MS2ASDM.tcc>
00336 //#endif //# AIPS_NO_TEMPLATE_SRC
00337 
00338 #endif
00339