casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SDDIndex.h
Go to the documentation of this file.
00001 //# SDDIndex.h: classes contining SDDFile index information
00002 //# Copyright (C) 1999,2001
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_SDDINDEX_H
00030 #define NRAO_SDDINDEX_H
00031 
00032 #include <casa/aips.h>
00033 #include <nrao/SDD/SDDBootStrap.h>
00034 #include <nrao/SDD/SDDBlock.h>
00035 #include <casa/OS/Time.h>
00036 #include <casa/BasicSL/String.h>
00037 #include <casa/Containers/Block.h>
00038 #include <casa/Utilities/Fallible.h>
00039 
00040 #include <casa/namespace.h>
00041 // <summary>
00042 // SDDIndexRep is the individual index field in an SDDIndex
00043 // </summary>
00044 
00045 // <use visibility=export>
00046 
00047 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00048 // </reviewed>
00049 
00050 // <prerequisite>
00051 //   <li> SDDFile
00052 //   <li> SDDIndex
00053 //   <li> SDDHeader
00054 // </prerequisite>
00055 //
00056 // <etymology>
00057 // </etymology>
00058 //
00059 // <synopsis>
00060 // </synopsis>
00061 //
00062 // <example>
00063 // </example>
00064 //
00065 // <motivation>
00066 // </motivation>
00067 //
00068 
00069 class SDDIndexRep {
00070 public:
00071     // construct an empty index entry
00072     SDDIndexRep();
00073 
00074     // construct one from another
00075     SDDIndexRep(const SDDIndexRep& other);
00076 
00077     ~SDDIndexRep() {;}
00078 
00079     // assinment operator, true copy
00080     SDDIndexRep& operator=(const SDDIndexRep& other);
00081 
00082     // operator== returns True if EVERY value of this is the same as other
00083     Bool operator==(const SDDIndexRep& other) const;
00084     // operator!= returns False if ANY value of this is not the same as other
00085     Bool operator!=(const SDDIndexRep& other) const;
00086 
00087     // returns True if this IndexRep has changed
00088     // remains True until a clearChangeFlag has been called
00089     Bool hasChanged() const { return hasChanged_p;}
00090     void clearChangeFlag() { hasChanged_p = False;}
00091 
00092     // these function return the values stored in this index entry
00093     // The starting record for this scan - 0 relative
00094     uInt firstRecord() const { return firstRecord_p; }
00095     void setFirstRecord(uInt recnum) 
00096     { hasChanged_p = True; firstRecord_p = recnum;}
00097     // The total number of records used for this scan
00098     uInt nRecords() const { return nRecords_p; }
00099     void setnRecords(uInt nrecs) 
00100     { hasChanged_p = True; nRecords_p = nrecs;}
00101 
00102     // the Coordinates of this scan in the indicated coordinate system
00103     // Units are radians.  The coordinate system is the 8 character
00104     // unipops coordinate string.
00105     Float hCoord() const { return hCoord_p; }
00106     void sethCoord(Float hpos) { hasChanged_p = True; hCoord_p = hpos; }
00107     Float vCoord() const { return vCoord_p; }
00108     void setvCoord(Float vpos) { hasChanged_p = True; vCoord_p = vpos; }
00109     String coordSys() const { return posCodeMap_p[coordSys_p]; }
00110     uShort coordCode() const { return coordSys_p; }
00111     // If there are not exactly 8 chars in the new coordSys or
00112     // the coordSys string is unrecognized, it is set to the null string
00113     void setcoordSys(const String& new_coordSys);
00114     void setcoordSys(uShort new_coordCode);
00115 
00116     // The source name, must be less than 16 characters
00117     String source() const { return source_p; }
00118     // If there are more than 16 characters, they are not used
00119     void setsource(const String& new_source);
00120 
00121     // The scan number, must be less than 100000
00122     uInt scan() const { return scan_p; }
00123     // If it is out of range, it gets set to zero (unused)
00124     void setscan(uInt newScan);
00125 
00126     // The subscan number, must be between 1 and 99
00127     uInt subScan() const { return subScan_p; }
00128     // If it is out of range, it and scan are set to zero (unused)
00129     void setsubScan(uInt newSubScan);
00130 
00131     //  For LINE scans (isLine() is True) freqResolution and restFrequency
00132     //  are available.  For CONT scans (isLine() is False), slewRate and
00133     // intTimePerPoint are available.
00134     // If a value is not-available - an IEEE NaN is returned.
00135     // If an attempt is made to set something that isn't otherwise available,
00136     // that will switch the type (i.e. setting slewRate while isLine() is True
00137     // changes isLine() to False).  Any values currently stored in the now
00138     // unavailable locations are lost (i.e. if slewRate is set, any value in
00139     // freqResolution and restFrequency is permanently lost).
00140     Float freqResolution() const { return freqRes_p; }
00141     void setfreqResolution(Float new_freqRes);
00142     Float slewRate() const { return slewRate_p; }
00143     void setslewRate(Float new_slewRate);
00144     Double restFrequency() const { return restFreq_p; }
00145     void setrestFrequency(Double new_restFreq);
00146     Double intTimePerPoint() const { return intTime_p; }
00147     void setintTimePerPoint(Double new_intTime);
00148 
00149     // LST in hours
00150     Float lst() const { return lst_p; }
00151     void setlst(float new_lst) { hasChanged_p = True; lst_p = new_lst; }
00152 
00153     // UT data as a Time object
00154     Time utDate() const { return utDate_p; }
00155     void setutDate(const Time& new_utDate) 
00156     { hasChanged_p = True; utDate_p = new_utDate;}
00157 
00158     // The 4 character observing mode
00159     String mode() const { return obsModeMap_p[mode_p]; }
00160     uShort modeCode() const { return mode_p; }
00161     // If the mode is not recognized it is set to the null string
00162     void setmode(const String& new_mode);
00163     void setmode(uShort new_modeCode);
00164 
00165     // For RECORDS data (currently only available for GB data)
00166     // this returns the record number for the indexed data.
00167     // A negative number implies that this is not records data
00168     // and also turns off intPhase (loosing any current value)
00169     Int record() const { return record_p; }
00170     void setrecord(Int new_record);
00171     // Also for RECORDS data, this returns the integration phase
00172     // number for the indexed data.  It must be less than 64.
00173     // A negative number implies that this is not records data
00174     // and also turns off record (loosing any current value)
00175     // Attempting to set this to an invalid number (>=64) is
00176     // equivalent to setting it to a negative number.
00177     Int intPhase() const { return intPhase_p; }
00178     void setintPhase(Int new_intPhase);
00179 
00180     // Returns TRUE if this indexes spectral line data
00181     // otherwise False
00182     Bool isLine() const { return isLine_p; }
00183     
00184     // change this entry to be a spectral line entry
00185     // This looses any current value of slewRate or intTimePerPoint
00186     void setLine();
00187     // change this entry to be a continuum entry
00188     // This looses any current value of freqResolution or restFrequency
00189     void setCont();
00190 
00191 private:
00192     Bool isLine_p, hasChanged_p;
00193     uShort coordSys_p, mode_p;
00194     uInt firstRecord_p, nRecords_p, scan_p, subScan_p;
00195     Int record_p, intPhase_p;
00196     Float hCoord_p, vCoord_p, freqRes_p, slewRate_p, lst_p;
00197     Double restFreq_p, intTime_p;
00198     String source_p;
00199     Time utDate_p;
00200 
00201     void init_maps();
00202 
00203     static Block<String> posCodeMap_p;
00204     static Block<String> obsModeMap_p;
00205 };
00206     
00207 
00208 // <summary>
00209 // SDDIndex is the index of an SDDFile, a set of SDDIndexReps
00210 // </summary>
00211 
00212 // <use visibility=export>
00213 
00214 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00215 // </reviewed>
00216 
00217 // <prerequisite>
00218 //   <li> SDDFile
00219 //   <li> SDDIndex
00220 //   <li> SDDHeader
00221 // </prerequisite>
00222 //
00223 // <etymology>
00224 // </etymology>
00225 //
00226 // <synopsis>
00227 // </synopsis>
00228 //
00229 // <example>
00230 // </example>
00231 //
00232 // <motivation>
00233 // </motivation>
00234 //
00235 
00236 class SDDIndex{
00237 public:
00238     // construct index from an SDDbootStrap - reads from file if present
00239     SDDIndex(const SDDBootStrap& bs);
00240     // copy constructor
00241     SDDIndex(const SDDIndex& other);
00242 
00243     // clean up the pointers
00244     ~SDDIndex();
00245 
00246     // assignment operator, true copy
00247     SDDIndex& operator=(const SDDIndex& other);
00248 
00249     // For both types of update, if the boostrap block is the same,
00250     // nothing actually changes in the index.  Each returns True if an
00251     // actual update occured and false if things remain the same.
00252     // do a full update from the disk file
00253     Bool fullUpdate();
00254     // do an incremental update : assumes that the file has mearly grown
00255     // Attempts to verify that assumption by checking the values of the last
00256     // 8 known index entries with their counterparts on disk and verifying
00257     // that they are still the same. 
00258     // Return True if incremental was the correct model (or nothing has changed)
00259     // Returns False if incremental was NOT the correct model, a fullUpdate 
00260     // could then be tried if desirable
00261     Bool incrementalUpdate();
00262 
00263     // attach to a new file via an SDDBooStrap
00264     void attach(const SDDBootStrap& bs);
00265 
00266     // write the full index to the attached file
00267     void write();
00268     // write nEntries entries starting with firstEntry
00269     void write(uInt firstEntry, uInt nEntries = 1);
00270 
00271     // Return a reference to SDDIndexRep
00272     SDDIndexRep& operator()(uInt entry);
00273     const SDDIndexRep& operator()(uInt entry) const;
00274 
00275     // Return a reference to the Bootstrap
00276     const SDDBootStrap& bootstrap() const { return bootstrap_p;}
00277     SDDBootStrap& bootstrap() { return bootstrap_p;}
00278 
00279     // max entry in use, invalid if non in use
00280     Fallible<uInt> maxEntryInUse() const;
00281     // number of available entries
00282     uInt nrEntry() const;
00283     // check if an entry is in use (non-zero scan number)
00284     Bool inUse(uInt entry) const;
00285 
00286     // set everything in an entry to 0
00287     void clear(uInt entry);
00288 
00289     // switch version
00290     void setVersion(SDDBootStrap::SDD_VERSION newVersion);
00291     
00292 private:
00293     enum ShortFields { OLD_FIRST = 0,
00294                        OLD_LAST  = 1,
00295                        OLD_MAGIC = 2,     // not used
00296                        OLD_POSCODE = 3,
00297                        OBSMODE = 28,
00298                        RECORD_PHASE = 29,
00299                        NEW_POSCODE = 30};
00300 
00301     enum IntFields { NEW_FIRST = 0,
00302                      NEW_LAST  = 1};
00303 
00304     enum FloatFields { HCOORD = 2,
00305                        VCOORD = 3,
00306                        SCAN_NUMBER = 8,
00307                        FRES_SLEWRATE = 9,
00308                        LST = 12,
00309                        UTDATE = 13};
00310 
00311     enum DoubleFields { RESTF_INTTIME = 5 };
00312 
00313     enum CharFields { SOURCE = 16};
00314                        
00315     Block<SDDIndexRep> *index_p;
00316     SDDBlock sddBlock;
00317     SDDBootStrap bootstrap_p;
00318     uInt currentRecord_p;
00319     Bool needsFlushed_p;
00320 
00321     void flush();
00322     void readEntry(SDDIndexRep& rep, uInt entry);
00323     void writeEntry(SDDIndexRep& rep, uInt entry);
00324     
00325     Char& valueRef(CharFields field, uInt entry, uInt position);
00326     const Char& valueRef(CharFields field, uInt entry, uInt position) const;
00327     Short& valueRef(ShortFields field, uInt entry);
00328     const Short& valueRef(ShortFields field, uInt entry) const;
00329     Int& valueRef(IntFields field, uInt entry);
00330     const Int& valueRef(IntFields field, uInt entry) const;
00331     float& valueRef(FloatFields field, uInt entry);
00332     const float& valueRef(FloatFields field, uInt entry) const;
00333     double& valueRef(DoubleFields field, uInt entry);
00334     const double& valueRef(DoubleFields field, uInt entry) const;
00335 
00336     
00337     
00338 };
00339 
00340 #endif
00341