casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
GBTACSTable.h
Go to the documentation of this file.
00001 //# GBTACSTable.h: GBT backend table for the ACS.
00002 //# Copyright (C) 2001,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id$
00028 
00029 #ifndef NRAO_GBTACSTABLE_H
00030 #define NRAO_GBTACSTABLE_H
00031 
00032 #include <nrao/FITS/GBTFITSBase.h>
00033 #include <nrao/FITS/GBTBackendTable.h>
00034 
00035 #include <casa/Arrays/Cube.h>
00036 #include <casa/Arrays/IPosition.h>
00037 #include <casa/Arrays/Matrix.h>
00038 #include <casa/Arrays/Vector.h>
00039 #include <casa/Containers/RecordField.h>
00040 #include <casa/BasicSL/Complex.h>
00041 #include <scimath/Mathematics/FFTServer.h>
00042 #include <scimath/Mathematics/VanVleck.h>
00043 
00044 #include <casa/namespace.h>
00045 // forward declarations
00046 namespace casa { //# NAMESPACE CASA - BEGIN
00047 class String;
00048 } //# NAMESPACE CASA - END
00049 
00050 
00051 // <summary>
00052 // A GBTBackendTable for the ACS
00053 // </summary>
00054 //
00055 // <use visibility=export>
00056 //
00057 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00058 // </reviewed>
00059 //
00060 // <prerequisite>
00061 // </prerequisite>
00062 //
00063 // <etymology>
00064 // </etymology>
00065 //
00066 // <synopsis>
00067 // GBTACSTable is a GBTBackendTable with some additional
00068 // methods specific to the ACS.  
00069 // </synopsis>
00070 //
00071 // <example>
00072 // </example>
00073 //
00074 // <motivation>
00075 // </motivation>
00076 
00077 class GBTACSTable : public GBTBackendTable
00078 {
00079 public:
00080     // define the vanVleck correction to used.
00081     enum VanVleckCorr {
00082         // No vanVleck correction will be done.
00083         NoVanVleck,
00084         // The correction from Schwab as implemented in the VanVleck class.
00085         Schwab,
00086         // The old correction from the Arecibo code.
00087         Old,
00088         // the default correction
00089         DefaultVanVleck = Schwab
00090     };
00091     // define the smoothing to apply after the data have been corrected
00092     enum Smoothing {
00093         // No smoothing
00094         NoSmoothing,
00095         // hanning smoothing
00096         Hanning,
00097         // hamming smoothing
00098         Hamming,
00099         // the default smoothing
00100         DefaultSmoothing = Hanning
00101     };
00102 
00103     // Construct it from a file name.  Use the indicated vanVleck correction
00104     // and smoothing in the data method.  The vvsize is the table size
00105     // used when the Schwab correction (the default) is requested.  If
00106     // useDCBias is True, an attemp is made to use the DCBias (the average
00107     // of the autocorrrelations over the last 5% of the lags) in the vanVleck
00108     // correction if the Schwab correction is specified.  If usebias is False
00109     // then dcbias is used as the bias.  If minbiasfactor is >= 0 then the
00110     // nearest integer >= minbiasfactor is found that, when multiplied by
00111     // 0.5/65536, raises the mean of the last 5% of the lags to just > 0.0.
00112     // This is an attempt to correct for a truncation error introduced when
00113     // the lags are converted to 32-bit values.
00114     GBTACSTable(const String &fileName, VanVleckCorr vanVleckCorr=DefaultVanVleck,
00115                 Smoothing smoothing = DefaultSmoothing, Int vvsize=65,
00116                 Bool useDCBias = False, Double dcbias=0.0, Int minbiasfactor=-1,
00117                 Bool fixlags = False, String fixlagslog="",
00118                 Double sigmaFactor = 6.0, Int spikeStart = 200);
00119     ~GBTACSTable();
00120 
00121     // Attach this GBTACSTable to a new file name.
00122     // when resync is True, if fileName is the same as the
00123     // currently opened file, if there is one, then this just
00124     // closes and reopens the FITS file setting things right
00125     // back to the next row in the table, if there is one
00126     virtual Bool reopen(const String &fileName, Bool resync);
00127 
00128     // the default reopen from FITSTabular uses resync=True
00129     virtual Bool reopen(const String &fileName) 
00130     {return reopen(fileName, True);}
00131 
00132     // get the raw data from the specific sampler
00133     virtual Array<Float> rawdata(uInt whichSampler);
00134 
00135     // get the data from the specific sampler,
00136     // applying the vanVleck correction and smoothing specified
00137     virtual Array<Float> data(uInt whichSampler);
00138 
00139     // is the data bad for the specific sampler
00140     virtual Array<Bool> badData(uInt whichSampler);
00141 
00142     // the zero channel for the specific sampler
00143     virtual Array<Float> zeroChannel(uInt whichSampler);
00144                              
00145     // the channel spacing as deduced from the CDELT keyword and number of lags
00146     virtual Double spacing() {return itsSpacing;}
00147 
00148     // separate isValid needed here to watch for problems in init
00149     virtual Bool isValid() const {return isValid_p;}
00150 
00151     // Do the switching signals in ACT_STATE follow the
00152     // documentation?  Prior to FITSVER 1.2 they had the
00153     // opposite sense.
00154     virtual Bool switchOK() const
00155     { return (deviceVersion() > 1 || baseVersion() > 1); }
00156 
00157     // return the number of sampler levels being used at the moment
00158     virtual Int nlevels() const { return itsCachedLevel;}
00159 
00160     // The value of the INTEGRAT field in the current row for
00161     // the given sampler and state
00162     virtual Float integrat(uInt whichSampler, uInt whichState) const;
00163 
00164     // The DMJD value from the current row
00165     virtual Double dmjd() {return *itsDMJD;}
00166 
00167     // The value of the TIME-MID field in the current row
00168     virtual Double timeMid() {return *itsTimeMid;}
00169 
00170     // A string giving the bank here
00171     virtual String bank() {return itsBank;}
00172 
00173 private:
00174     // remember what the cached data cube currently has
00175     VanVleckCorr itsVanVleckCorr;
00176     Smoothing itsSmoothing;
00177     Int itsVVsize, itsMinbiasfactor, itsSpikeStart;
00178     Bool itsUseDCBias, itsNormalTintAxes, itsNew, itsFixlags;
00179     Double itsDCBias, itsTruncError, itsSigmaFactor;
00180 
00181     // used in reporting 
00182     String itsTimestamp;
00183     Int itsScan;
00184 
00185     // the last row that was corrected and FFTed and is now
00186     // in itsCachedData.
00187     Int itsCachedDataRow, itsCachedLagRow;
00188 
00189     // cache the spacing when (re)opening the file
00190     Double itsSpacing;
00191 
00192     // itsCachedData is what is returned by data().
00193     Array<Float> itsCachedData;
00194     // itsCachedDataMatrix references the same values, but with a 2D shape
00195     // to make it trivial to iterate through each spectra.
00196     Matrix<Float> *itsCachedDataMatrix;
00197     // itsCachedZeroChannel is the vector of corrected zero channels
00198     // associated with itsCachedData - one per sampler
00199     Array<Float> itsCachedZeroChannel;
00200     Vector<Float> *itsCachedZeroVector;
00201     // itsCachedBadData is the vector of booleans indicating which 
00202     // lag sequences are bad - one per sampler
00203     Array<Bool> itsCachedBadData;
00204     Vector<Bool> *itsCachedBadDataVector;
00205     Vector<Bool> itsCachedBadZeroLags, itsCachedDiscontinuities;
00206 
00207     Array<Float> itsCachedLag;
00208 
00209     FFTServer<Float, Complex> itsFFTServer;
00210 
00211     // remember the sampler level - used in a few places when processing the data
00212     Int itsCachedLevel;
00213 
00214     // van-vleck corr is done as a double for accuracy - this is a temp used for that
00215     Matrix<Double> itsVVTemp;
00216 
00217     // fourier transform is 2x size in first dimension - keep transients here
00218     // one spectra at a time
00219     Vector<Float> itsFTTemp;
00220     Vector<Complex> itsFFTResult;
00221 
00222     // weight to give the nlags elements in itsFTTemp when that is constructed
00223     // set by smooth but defaults to 0.0
00224     Float itsNlagWeight;
00225 
00226     // smoothing vector - only depends on the number of lags and type of smoothing
00227     Vector<Float> itsSmoother;
00228 
00229     // the power level for each spectra in a row is kept here, when requested
00230     Vector<Double> itsPowerLevels;
00231 
00232     RORecordFieldPtr<Array<Float> > itsDataField, itsIntegrat;
00233     RORecordFieldPtr<Double> itsDMJD, itsTimeMid;
00234 
00235     // A flag to indicate that at least one bad zero lag was detected.
00236     // This is set to true and an error message emited when a bad zero
00237     // lag is seen and reset to false in reopen.  In that way, the error
00238     // message is only seen once per input file.
00239     Bool itsHasBadData, itsErrorEmitted;
00240     // similarly for discontinuities
00241     Bool itsHasDiscontinuities, itsDiscErrorEmitted;
00242 
00243     // Is everything okay (appart from bad zero-lags)
00244     Bool isValid_p;
00245 
00246     // used in the data slicing
00247     IPosition itsStart, itsEnd;
00248     uInt itsSampAxis;
00249 
00250     String itsBank;
00251 
00252     // The BANK_{A,B} and  PORT_{A,B} column values from sampler()
00253     Vector<String> itsBankA, itsBankB;
00254     Vector<Int> itsPortA, itsPortB;
00255 
00256     String itsFixLagsLog;
00257 
00258     void vanVleck();
00259 
00260     // get information from an AC lag vector
00261     void getACInfo(Double &threshold, Double &dcbias, Int &biasfactor, Int nlevel, 
00262                    Vector<Double> &aclags);
00263 
00264     // This actually does the correction - in place
00265     void vanVleckLags(Vector<Double> &lags, Int nlevel, Double thresh1, Double thresh2,
00266                       Double dcbias1, Double dcbias2);
00267 
00268     // Functions related to the old vv correction
00269     // these are all in-place operations on itsCachedData
00270     // 3-level sampling
00271     void pow3lev();
00272     void vanvleck3lev();
00273     // 9-level sampling
00274     void pow9lev();
00275     void vanvleck9lev();
00276 
00277     void applyFFT();
00278 
00279     // smoothing functions
00280     void smooth();
00281     void hamming();
00282     void hanning();
00283 
00284     // check for bad data, sets itsCachedBadData and returns True if
00285     // there was any bad data.  
00286     Bool checkForBadData(Int nspectra);
00287     // Checks a lag set for any discontinuties in 1024-lag chunks.
00288     // A discontinuity is when a 1024-lag chunk is more than nsigma
00289     // from the mean of the 512-lags on either side of that chunk.
00290     // This will also fix it when one is found if itsFixLags is true.  
00291     // When fixed, that information is logged to the named logfile.  
00292     // If not fixed, then this should be flagged as bad.  Returns True 
00293     // if any discontinuities were found.  badData is set to True when 
00294     // the discontinuity was not fixed.  The first 1024-lag segment is 
00295     // not checked here.  If that is offset then it should get flagged
00296     // elsewhere because the zero lag will be unphysical.  The first
00297     // 1024 segement can not be fixed.
00298     Bool checkForDiscontinuities(Vector<Float> lags, Bool &badData, uInt spec, Bool isXcorr);
00299 
00300     // Generates a report for a given bad lag 
00301     void reportBadLags(uInt spec, String badChanStr, String comment="");
00302 
00303     // initializes shapes and the cachedLevel, clears the booleans and
00304     // the cached row number - does not read any data.
00305     Bool init();
00306 
00307     // utility functions
00308     String bankPortPhase(const String &bank, Int port, Int phase);
00309 
00310     // Undefined and inaccessible.
00311     GBTACSTable();
00312     GBTACSTable(const GBTACSTable &);
00313     GBTACSTable &operator=(const GBTACSTable &);
00314 };
00315 
00316 
00317 #endif