00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 #ifndef SYNTHESIS_TRANSFORM2_CFBUFFER_H
00029 #define SYNTHESIS_TRANSFORM2_CFBUFFER_H
00030 #include <synthesis/TransformMachines/SynthesisError.h>
00031 #include <coordinates/Coordinates/CoordinateSystem.h>
00032 #include <synthesis/TransformMachines2/CFDefs.h>
00033 #include <synthesis/TransformMachines/CFCell.h>
00034 #include <synthesis/TransformMachines2/Utils.h>
00035 #include <images/Images/ImageInterface.h>
00036 #include <casa/Utilities/CountedPtr.h>
00037 #include <casa/Utilities/Sort.h>
00038 #include <casa/Logging/LogOrigin.h>
00039 #include <msvis/MSVis/VisBuffer2.h>
00040 #include <casa/Logging/LogSink.h>
00041 #include <casa/Logging/LogIO.h>
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 using namespace std;
00096 using namespace casa::vi;
00097 namespace casa { 
00098   
00099   typedef Complex TT;
00100   namespace refim{
00101     using namespace CFDefs;
00102 
00103   struct CFBStruct {
00104     CFCStruct *CFBStorage;
00105     int shape[3];
00106     Double *freqValues, *wValues, *pointingOffset;
00107     Double fIncr, wIncr;
00108     Int **muellerElementsIndex, **muellerElements, 
00109       **conjMuellerElementsIndex, **conjMuellerElements,
00110       **conjFreqNdxMap, **freqNdxMap;
00111     Int nMueller;
00112     
00113 
00114     CFCStruct* getCFB(int i, int j, int k)
00115     { return &(CFBStorage[i + (shape[1]-1)*j + (shape[1]-1)*(shape[2]-1)*k]);}
00116     
00117   } ;
00118 
00119   class CFBuffer
00120   {
00121   public:
00122     
00123     
00124     
00125     
00126     CFBuffer(): wValues_p(), maxXSupport_p(-1), maxYSupport_p(-1), pointingOffset_p(), cfHitsStats(),
00127                 freqNdxMapsReady_p(False), freqNdxMap_p(), conjFreqNdxMap_p()
00128     {};
00129     
00130     CFBuffer(Int maxXSup, Int maxYSup):
00131       wValues_p(), maxXSupport_p(maxXSup), maxYSupport_p(maxYSup), pointingOffset_p(), cfHitsStats(),
00132       freqNdxMapsReady_p(False), freqNdxMap_p(), conjFreqNdxMap_p()
00133     {
00134       
00135       
00136       
00137       
00138     };
00139     
00140     ~CFBuffer() 
00141     {
00142       
00143       
00144       
00145     };
00146     
00147     CountedPtr<CFBuffer> clone();
00148     void allocCells(const Cube<CountedPtr<CFCell> >& cells);
00149     void setParams(const CFBuffer& other);
00150     
00151     
00152     
00153     
00154     
00155     
00156     
00157     
00158     
00159     
00160     
00161     
00162     
00163     inline Int nChan() {return nChan_p;}
00164     inline Int nW() {return nW_p;}
00165     inline Int nMuellerElements() {return nPol_p;}
00166     inline IPosition shape() {IPosition shp(3,nChan_p, nW_p, nPol_p); return shp;}
00167     
00168     inline Vector<Double> getFreqList() {return freqValues_p;};
00169     inline Vector<Double> getWList() {return wValues_p;};
00170     
00171     CFCell& getCFCell(const Double& freqVal, const Double& wValue, 
00172                       const Int & muellerElement); 
00173     
00174     CountedPtr<CFCell>& getCFCellPtr(const Double& freqVal, const Double& wValue, 
00175                                      const Int & muellerElement); 
00176     CFCell& operator()(const Int& i, const Int& j, const Int& k) {return *cfCells_p(i,j,k);}
00177     CFCell& getCFCell(const Int& i, const Int& j, const Int& k);
00178 
00179     CountedPtr<CFCell >& getCFCellPtr(const Int& i, const Int& j, const Int& k);
00180     
00181     
00182     Array<TT>& getCF(const Double& freqVal, const Double& wValue, 
00183                      const Int & muellerElement)
00184     {return *(getCFCell(freqVal, wValue, muellerElement).storage_p);}
00185     
00186     
00187     CountedPtr<Array<TT> >& getCFPtr(const Double& freqVal, const Double& wValue, 
00188                                      const Int & muellerElement) 
00189     {return getCFCellPtr(freqVal, wValue, muellerElement)->storage_p;}
00190     
00191     Array<TT>& getCF(const Int& i, const Int& j, const Int& k)
00192     {return *(getCFCell(i,j,k).storage_p);}
00193     
00194     CountedPtr<Array<TT> >& getCFPtr(const Int& i, const Int& j, const Int& k)
00195     {return getCFCellPtr(i,j,k)->storage_p;}
00196     
00197     
00198     
00199     
00200     
00201     
00202     
00203     
00204     
00205     void getParams(CoordinateSystem& cs, Float& sampling, 
00206                    Int& xSupport, Int& ySupport, 
00207                    const Double& freqVal, const Double& wValue, 
00208                    const Int& muellerElement);
00209     
00210     
00211     inline void getParams(CoordinateSystem& cs, Float& sampling, 
00212                           Int& xSupport, Int& ySupport, 
00213                           const Int& i, const Int& j, const Int& k)
00214     {
00215       cs = cfCells_p(i,j,k)->coordSys_p;
00216       sampling = cfCells_p(i,j,k)->sampling_p;
00217       xSupport = cfCells_p(i,j,k)->xSupport_p;
00218       ySupport = cfCells_p(i,j,k)->ySupport_p;
00219     }
00220     void getParams(Double& freqVal, Float& sampling, 
00221                    Int& xSupport, Int& ySupport, 
00222                    const Int& iFreq, const Int& iW, const Int& iPol)
00223     {
00224       sampling = cfCells_p(iFreq,iW,iPol)->sampling_p;
00225       xSupport = cfCells_p(iFreq,iW,iPol)->xSupport_p;
00226       ySupport = cfCells_p(iFreq,iW,iPol)->ySupport_p;
00227       freqVal = freqValues_p(iFreq);
00228     }
00229     
00230     inline void getCoordList(Vector<Double>& freqValues, Vector<Double>& wValues,
00231                              PolMapType& muellerElementsIndex, PolMapType& muellerElements, 
00232                              PolMapType& conjMuellerElementsIndex, PolMapType& conjMuellerElements, 
00233                              Double& fIncr, Double& wIncr)
00234     {
00235       freqValues.assign(freqValues_p);wValues.assign(wValues_p);
00236       muellerElements.assign(muellerElements_p);         muellerElementsIndex.assign(muellerElementsIndex_p);
00237       conjMuellerElements.assign(conjMuellerElements_p); conjMuellerElementsIndex.assign(conjMuellerElementsIndex_p);
00238       fIncr = freqValIncr_p; wIncr = wValIncr_p;
00239     }
00240     
00241     Int nearestNdx(const Double& val, const Vector<Double>& valList, const Double& incr);
00242     
00243     Int nearestFreqNdx(const Double& freqVal) ;
00244     
00245     inline Int nearestWNdx(const Double& wVal) 
00246     {
00247       
00248       return max(0,min((int)(sqrt(wValIncr_p*abs(wVal))),wValues_p.nelements())-1);
00249       
00250       
00251       
00252       
00253     }
00254     
00255     Double nearest(Bool& found, const Double& val, const Vector<Double>& valList, const Double& incr);
00256     
00257     inline Double nearestFreq(Bool& found, const Double& freqVal)
00258     {return nearest(found, freqVal, freqValues_p, freqValIncr_p);}
00259     
00260     inline Double nearestWVal(Bool& found, const Double& wVal)
00261     {return nearest(found, wVal, wValues_p, wValIncr_p);}
00262     
00263     
00264     
00265     
00266     
00267     
00268     
00269     void makeCFBufferMap(const Vector<Double>& freqVals, 
00270                          const Vector<Double>& wValues,
00271                          const MuellerMatrixType& muellerElements);
00272     
00273     
00274     
00275     
00276     void addCF(Array<TT>*, 
00277                CoordinateSystem&,
00278                Float& ,
00279                Int& ,
00280                Int& ,
00281                Double& ,
00282                Double& ,
00283                Int& 
00284                )
00285     {throw(AipsError("CFBuffer::addCF called"));}
00286     
00287     
00288     void resize(const IPosition& size) {cfCells_p.resize(size);};
00289     void resize(const Double& wIncr, const Double& freqIncr,
00290                 const Vector<Double>& wValues, 
00291                 const Vector<Double>& freqValues,
00292                 const PolMapType& muellerElements,
00293                 const PolMapType& muellerElementsIndex,
00294                 const PolMapType& conjMuellerElements,
00295                 const PolMapType& conjMuellerElementsIndex);
00296     Int noOfMuellerElements(const PolMapType& muellerElements);
00297     
00298     
00299     
00300     RigidVector<Int, 3> setParams(const Int& i, const Int& j, const Int& ipx, const Int& ipy,
00301                                   CoordinateSystem& cs, Float& sampling,
00302                                   Int& xSupport, Int& ySupport,
00303                                   const Double& freqValue, const Double& wValue, 
00304                                   const Int& muellerElement,
00305                                   const String& fileName=String(),
00306                                   const Double& conjFreq=0.0,
00307                                   const Int& conjPol=-1,
00308                                   const String& telescopeName=String(),
00309                                   const Float& diameter=25.0);
00310     
00311     
00312     void setPointingOffset(const Vector<Double>& offset) 
00313     {pointingOffset_p.assign(offset);};
00314     Vector<Double> getPointingOffset() {return pointingOffset_p;};
00315     
00316     
00317     
00318     void setParams(Int& nx, Int& ny, CoordinateSystem& cs, Float& sampling, 
00319                    Int& xSupport, Int& ySupport, 
00320                    const Double& freqVal, const Double& wValue, 
00321                    const Int& muellerElement,
00322                    const String& fileName);
00323     void setPA(Float& pa);
00324     RigidVector<Int,3> getIndex(const Double& freqVal, const Double& wValue, 
00325                                 const Int& muellerElement);
00326     
00327     
00328     
00329     
00330     void copyParams(const CFBuffer& other)
00331     {
00332       cfCells_p = other.cfCells_p;
00333       
00334       
00335       maxXSupport_p=other.maxXSupport_p;  maxYSupport_p=other.maxYSupport_p; 
00336     }
00337     
00338     
00339     
00340     
00341     
00342     
00343     void show(const char *Mesg=NULL,ostream &os=cerr);
00344     
00345     
00346     
00347     Bool null() {return (cfCells_p.nelements() == 0);};
00348     
00349     Cube<CountedPtr<CFCell> >& getStorage() {return cfCells_p;};
00350     void makePersistent(const char *dir, const char *cfName="");
00351     
00352     void primeTheCache();
00353     void initMaps(const VisBuffer2& vb,const Matrix<Double>& freqSelection,const Double& imRefFreq);
00354     void initPolMaps(PolMapType& polMap, PolMapType& conjPolMap);
00355     
00356     
00357     
00358     void getFreqNdxMaps(Vector<Vector<Int> >& freqNdx, Vector<Vector<Int> >& conjFreqNdx);
00359     inline Int nearestFreqNdx(const Int& spw, const Int& chan, const Bool conj=False)
00360     {
00361       if (conj) return conjFreqNdxMap_p[spw][chan];
00362       else  return freqNdxMap_p[spw][chan];
00363     }
00364     
00365     void getAsStruct(CFBStruct& st);
00366     
00367     static void initCFBStruct(CFBStruct& cfbSt) 
00368     {
00369       cfbSt.CFBStorage=NULL;
00370       cfbSt.freqValues=NULL;
00371       cfbSt.wValues=NULL;
00372       cfbSt.muellerElementsIndex=NULL;
00373       cfbSt.muellerElements=NULL;
00374       cfbSt.conjMuellerElementsIndex=NULL;
00375       cfbSt.conjMuellerElements=NULL;
00376       cfbSt.shape[0]=cfbSt.shape[1]=cfbSt.shape[2]=0;
00377       cfbSt.fIncr=cfbSt.wIncr=0.0;
00378     }
00379     void fill(const Int& nx, const Int& ny, 
00380               const Vector<Double>& freqValues,
00381               const Vector<Double>& wValues,
00382               const PolMapType& muellerElements);
00383     
00384     IPosition getShape() {return cfCells_p.shape();}
00385     
00386     
00387     
00388     
00389   protected:
00390     
00391     
00392     
00393     
00394     
00395     
00396     Cube<CountedPtr<CFCell> > cfCells_p;
00397     Vector<Double> wValues_p, freqValues_p;
00398     PolMapType muellerElements_p, muellerElementsIndex_p,conjMuellerElements_p,conjMuellerElementsIndex_p; 
00399     Double wValIncr_p, freqValIncr_p;
00400     MuellerMatrixType muellerMask_p;
00401     
00402     Int nPol_p, nChan_p, nW_p, maxXSupport_p, maxYSupport_p;
00403     Vector<Double> pointingOffset_p;
00404     Cube<Int> cfHitsStats;
00405     Bool freqNdxMapsReady_p;
00406     Vector<Vector<Int> > freqNdxMap_p, conjFreqNdxMap_p;
00407     void ASSIGNVVofI(Int** &target,Vector<Vector<Int> >& source, Bool& doAlloc);
00408   };
00409 
00410   
00411     
00412   
00413   } 
00414 }
00415 #endif