Line data Source code
1 : /* 2 : * ALMA - Atacama Large Millimeter Array 3 : * (c) European Southern Observatory, 2002 4 : * (c) Associated Universities Inc., 2002 5 : * Copyright by ESO (in the framework of the ALMA collaboration), 6 : * Copyright by AUI (in the framework of the ALMA collaboration), 7 : * All rights reserved. 8 : * 9 : * This library is free software; you can redistribute it and/or 10 : * modify it under the terms of the GNU Lesser General Public 11 : * License as published by the Free software Foundation; either 12 : * version 2.1 of the License, or (at your option) any later version. 13 : * 14 : * This library is distributed in the hope that it will be useful, 15 : * but WITHOUT ANY WARRANTY, without even the implied warranty of 16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 : * Lesser General Public License for more details. 18 : * 19 : * You should have received a copy of the GNU Lesser General Public 20 : * License along with this library; if not, write to the Free Software 21 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 22 : * MA 02111-1307 USA 23 : * 24 : * File ArrayTimeInterval.h 25 : */ 26 : 27 : #ifndef ArrayTimeInterval_CLASS 28 : #define ArrayTimeInterval_CLASS 29 : 30 : #include <stdint.h> 31 : #include <alma/ASDM/LongWrapper.h> 32 : #include <alma/ASDM/ArrayTime.h> 33 : #include <alma/ASDM/Interval.h> 34 : 35 : #ifndef WITHOUT_ACS 36 : #include <asdmIDLTypesC.h> 37 : #endif 38 : 39 : namespace asdm { 40 : 41 : /** 42 : * The ArrayTimeInterval class aggregates an instance of ArrayTime 43 : * and an instance of Interval. Instances of ArrayTimeInterval are 44 : * used in many ASDM tables whose rows contain information relevant 45 : * during a certain laps of time and starting at a given date. 46 : */ 47 : class ArrayTimeInterval { 48 : friend std::ostream & operator << ( std::ostream &, ArrayTimeInterval& ); 49 : friend std::istream & operator >> ( std::istream &, ArrayTimeInterval& ); 50 : private: 51 : 52 : ArrayTime start; 53 : Interval duration; 54 : 55 : static bool readStartTimeDurationInBin_; 56 : static bool readStartTimeDurationInXML_; 57 : 58 : public: 59 : 60 : 61 : // Constructors 62 : ArrayTimeInterval(); 63 : ArrayTimeInterval(ArrayTime start, 64 : Interval duration); 65 : 66 : ArrayTimeInterval(double startInMJD, 67 : double durationInDays); 68 : 69 : ArrayTimeInterval(int64_t startInNanoSeconds, 70 : int64_t durationInNanoSeconds); 71 : 72 : ArrayTimeInterval(ArrayTime start); 73 : ArrayTimeInterval(double startInMJD); 74 : ArrayTimeInterval(int64_t startInNanoSeconds); 75 : 76 : #ifndef WITHOUT_ACS 77 : ArrayTimeInterval (asdmIDLTypes::IDLArrayTimeInterval t); 78 : #endif 79 : // Setters 80 : void setStart(ArrayTime start); 81 : void setStart(double start); 82 : void setStart(int64_t start); 83 : 84 : void setDuration(Interval duration); 85 : void setDuration(int64_t nanoSeconds); 86 : void setDuration(double days); 87 : 88 : // Getters 89 : ArrayTime getStart() const ; 90 : ArrayTime getMidPoint() const ; 91 : double getStartInMJD() const ; 92 : int64_t getStartInNanoSeconds() const ; 93 : 94 : Interval getDuration() const ; 95 : int64_t getDurationInNanoSeconds() const ; 96 : double getDurationInDays() const ; 97 : 98 : // Checkers 99 : bool equals(ArrayTimeInterval ati); 100 : bool overlaps(ArrayTimeInterval ati); 101 : bool contains(ArrayTimeInterval ati); 102 : bool contains(ArrayTime at); 103 : 104 : // Operators 105 : bool operator == (ArrayTimeInterval&); 106 : bool operator != (ArrayTimeInterval&); 107 : 108 : #ifndef WITHOUT_ACS 109 : // To IDL conversion 110 : const asdmIDLTypes::IDLArrayTimeInterval toIDLArrayTimeInterval() const; 111 : #endif 112 : /** 113 : * Defines how the representation of an ArrayTimeInterval found in subsequent reads of 114 : * a document containing table exported in binary must be interpreted. The interpretation depends on the value of the argument b : 115 : * b == true means that it must be interpreted as (startTime, duration) 116 : * b == false means that it must be interpreted as (midPoint, duration) 117 : * 118 : * @param b a boolean value. 119 : */ 120 : static void readStartTimeDurationInBin(bool b); 121 : 122 : /** 123 : * Returns a boolean value whose meaning is defined as follows: 124 : * true <=> the representation of ArrayTimeInterval object found in any binary table will be considered as (startTime, duration). 125 : * false <=> the representation of ArrayTimeInterval object found in any binary table will be considered as (midPoint, duration). 126 : * 127 : */ 128 : static bool readStartTimeDurationInBin(); 129 : 130 : /** 131 : * Defines how the representation of an ArrayTimeInterval found in subsequent reads of 132 : * a document containing table exported in XML must be interpreted. The interpretation depends on the value of the argument b : 133 : * b == true means that it must be interpreted as (startTime, duration) 134 : * b == false means that it must be interpreted as (midPoint, duration) 135 : * 136 : * @param b a boolean value. 137 : */ 138 : static void readStartTimeDurationInXML(bool b); 139 : 140 : /** 141 : * Returns a boolean value whose meaning is defined as follows: 142 : * true <=> the representation of ArrayTimeInterval object found in any binary table will be considered as (startTime, duration). 143 : * false <=> the representation of ArrayTimeInterval object found in any binary table will be considered as (midPoint, duration). 144 : * 145 : */ 146 : static bool readStartTimeDurationInXML(); 147 : 148 : /** 149 : * Write the binary representation of this to a EndianOSStream. 150 : * It writes successively the midpoint , the duration. 151 : */ 152 : void toBin(EndianOSStream& eoss) const ; 153 : 154 : /** 155 : * Write the binary representation of a vector of ArrayTime to a EndianOSStream. 156 : * @param arrayTimeInterval the vector of ArrayTime to be written 157 : * @param eoss the EndianOSStream to be written to 158 : */ 159 : static void toBin(const std::vector<ArrayTimeInterval>& arrayTimeInterval, EndianOSStream& eoss); 160 : 161 : /** 162 : * Write the binary representation of a vector of vector of ArrayTimeInterval to a EndianOSStream. 163 : * @param arrayTimeInterval the vector of vector of ArrayTimeInterval to be written 164 : * @param eoss the EndianOSStream to be written to 165 : */ 166 : static void toBin(const std::vector<std::vector<ArrayTimeInterval> >& arrayTimeInterval, EndianOSStream& eoss); 167 : 168 : /** 169 : * Write the binary representation of a vector of vector of vector of ArrayTimeInterval to a EndianOSStream. 170 : * @param arrayTimeInterval the vector of vector of vector of ArrayTimeInterval to be written 171 : * @param eoss the EndianOSStream to be written to 172 : */ 173 : static void toBin(const std::vector<std::vector<std::vector<ArrayTimeInterval> > >& arrayTimeInterval, EndianOSStream& eoss); 174 : 175 : /** 176 : * Read the binary representation of an ArrayTimeInterval from a EndianIStream 177 : * and use the read value to set an ArrayTimeInterval. 178 : * @param eis the EndianStream to be read 179 : * @return an ArrayTimeInterval 180 : */ 181 : static ArrayTimeInterval fromBin(EndianIStream& eis); 182 : 183 : /** 184 : * Read the binary representation of a vector of ArrayTimeInterval from an EndianIStream 185 : * and use the read value to set a vector of ArrayTimeInterval. 186 : * @param dis the EndianIStream to be read 187 : * @return a vector of ArrayTimeInterval 188 : */ 189 : static std::vector<ArrayTimeInterval> from1DBin(EndianIStream & eis); 190 : 191 : /** 192 : * Read the binary representation of a vector of vector of ArrayTimeInterval from an EndianIStream 193 : * and use the read value to set a vector of vector of ArrayTimeInterval. 194 : * @param eiis the EndianIStream to be read 195 : * @return a vector of vector of ArrayTimeInterval 196 : */ 197 : static std::vector<std::vector<ArrayTimeInterval> > from2DBin(EndianIStream & eis); 198 : 199 : /** 200 : * Read the binary representation of a vector of vector of vector of ArrayTimeInterval from an EndianIStream 201 : * and use the read value to set a vector of vector of vector of ArrayTimeInterval. 202 : * @param eis the EndianIStream to be read 203 : * @return a vector of vector of vector of ArrayTimeInterval 204 : */ 205 : static std::vector<std::vector<std::vector<ArrayTimeInterval> > > from3DBin(EndianIStream & eis); 206 : 207 : /** 208 : * Returns a string representation of this. 209 : */ 210 : std::string toString() const ; 211 : }; 212 : 213 : 214 : // inline constructors 215 : 216 0 : inline ArrayTimeInterval::ArrayTimeInterval(): start((int64_t)0), duration(0) {} 217 0 : inline ArrayTimeInterval::ArrayTimeInterval(ArrayTime start_, Interval duration_) { 218 0 : start = start_; 219 0 : duration = Interval(std::min(duration_.get(), Long::MAX_VALUE - start.get())); 220 0 : } 221 : 222 : inline ArrayTimeInterval::ArrayTimeInterval(double startInMJD, double durationInDays) : 223 : start(startInMJD), 224 : duration((int64_t) (ArrayTime::unitsInADay * durationInDays)){} 225 : 226 0 : inline ArrayTimeInterval::ArrayTimeInterval(int64_t startInNanoSeconds, 227 0 : int64_t durationInNanoSeconds){ 228 0 : start = startInNanoSeconds; 229 0 : duration = std::min(durationInNanoSeconds, Long::MAX_VALUE - startInNanoSeconds); 230 0 : } 231 : 232 0 : inline ArrayTimeInterval::ArrayTimeInterval(ArrayTime start_): 233 0 : start(start_) { 234 0 : duration = Interval(Long::MAX_VALUE - start.get()); 235 0 : } 236 : 237 : inline ArrayTimeInterval::ArrayTimeInterval(double startInMJD): 238 : start(startInMJD) { 239 : this->duration = Interval(Long::MAX_VALUE - start.get()); 240 : } 241 : 242 : inline ArrayTimeInterval::ArrayTimeInterval(int64_t startInNanoSeconds): 243 : start(startInNanoSeconds) { 244 : this->duration = Interval(Long::MAX_VALUE - start.get()); 245 : } 246 : 247 : // Inline setters 248 : inline void ArrayTimeInterval::setStart(ArrayTime start) { 249 : this->start = ArrayTime(start); 250 : } 251 : 252 : inline void ArrayTimeInterval::setStart(double start) { 253 : this->start = ArrayTime(start); 254 : } 255 : 256 : inline void ArrayTimeInterval::setStart(int64_t start) { 257 : this->start = ArrayTime(start); 258 : } 259 : 260 0 : inline void ArrayTimeInterval::setDuration(Interval duration) { 261 0 : this->duration = Interval(duration); 262 0 : } 263 : 264 : 265 0 : inline void ArrayTimeInterval::setDuration(double duration) { 266 0 : this->duration = Interval((int64_t) (ArrayTime::unitsInADay * duration)); 267 0 : } 268 : 269 : inline void ArrayTimeInterval::setDuration(int64_t duration) { 270 : this->duration = Interval(duration); 271 : } 272 : 273 : // inline getters 274 0 : inline ArrayTime ArrayTimeInterval::getStart() const { 275 0 : return start; 276 : } 277 : 278 0 : inline ArrayTime ArrayTimeInterval::getMidPoint() const { 279 0 : return ArrayTime(start.get() + duration.get() / 2); 280 : } 281 : 282 0 : inline double ArrayTimeInterval::getStartInMJD() const { 283 0 : return start.getMJD(); 284 : } 285 : 286 : inline int64_t ArrayTimeInterval::getStartInNanoSeconds() const { 287 : return start.get(); 288 : } 289 : 290 0 : inline Interval ArrayTimeInterval::getDuration() const { 291 0 : return duration; 292 : } 293 : 294 0 : inline double ArrayTimeInterval::getDurationInDays() const { 295 0 : return (((double) duration.get()) / ArrayTime::unitsInADay); 296 : } 297 : 298 : inline int64_t ArrayTimeInterval::getDurationInNanoSeconds() const { 299 : return duration.get(); 300 : } 301 : 302 : // inline checkers 303 : inline bool ArrayTimeInterval::equals(ArrayTimeInterval ati) { 304 : return ((start.get() == ati.getStart().get()) && 305 : (duration.get() == ati.getDuration().get())); 306 : } 307 : 308 0 : inline bool ArrayTimeInterval::overlaps(ArrayTimeInterval ati) { 309 0 : int64_t start1 = start.get(); 310 0 : int64_t end1 = start1 + duration.get(); 311 : 312 : 313 0 : int64_t start2 = ati.getStart().get(); 314 0 : int64_t end2 = start2 + ati.getDuration().get(); 315 : 316 0 : return (start2 <= start1 && end2 >= start1) || 317 0 : (start2 >= start1 && start2 <= end1); 318 : } 319 : 320 0 : inline bool ArrayTimeInterval::contains(ArrayTimeInterval ati) { 321 0 : int64_t start1 = start.get();; 322 0 : int64_t end1 = start1 + duration.get(); 323 : 324 0 : int64_t start2 = ati.getStart().get(); 325 0 : int64_t end2 = start2 + ati.getDuration().get(); 326 : 327 0 : return (start2>=start1 && end2<=end1); 328 : } 329 : 330 0 : inline bool ArrayTimeInterval::contains(ArrayTime ati) { 331 0 : int64_t start1 = start.get(); 332 0 : int64_t end1 = start1 + duration.get(); 333 : 334 0 : int64_t time = ati.get(); 335 0 : return (time >= start1 && time < end1); 336 : } 337 : 338 : inline bool ArrayTimeInterval::operator == (ArrayTimeInterval &ati){ 339 : return (start == ati.start) && (duration == ati.duration); 340 : } 341 : 342 : inline bool ArrayTimeInterval::operator != (ArrayTimeInterval &ati){ 343 : return (start != ati.start) || (duration != ati.duration); 344 : } 345 : 346 : inline std::ostream & operator << ( std::ostream &o, ArrayTimeInterval &ati ) { 347 : o << "(start=" << ati.getStart().get() << ",duration=" << ati.getDuration().get() << ")"; 348 : return o; 349 : } 350 : 351 0 : inline std::istream & operator >> ( std::istream &i, ArrayTimeInterval &ati) { 352 0 : i >> ati.start; 353 0 : i >> ati.duration; 354 0 : return i; 355 : } 356 : } // End namespace asdm 357 : #endif // ArrayTimeInterval_CLASS