casacore
Loading...
Searching...
No Matches
MVAngle.h
Go to the documentation of this file.
1//# MVAngle.h: Class to handle angle type conversions and I/O
2//# Copyright (C) 1996,1997,1998,1999,2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_MVANGLE_H
27#define CASA_MVANGLE_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Quanta/Quantum.h>
33#include <casacore/casa/iosfwd.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37//# Forward Declarations
38class String;
39class MUString;
40
41//# Constants (SUN compiler does not accept non-simple default arguments)
42
43// <summary>
44// Class to handle angle type conversions and I/O
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tMeasure" demos="">
50// </reviewed>
51
52// <prerequisite>
53// <li> <linkto class=Quantum>Quantum</linkto>
54// </prerequisite>
55//
56// <etymology>
57// From Measure, Value and Angle
58// </etymology>
59//
60// <synopsis>
61// An MVAngle is a simple Double, to be used for angle conversions and I/O.
62// It can be constructed from a Double (in which case radians are assumed),
63// or from a Quantity (<src>Quantum<Double></src>). Quantities must be in
64// either angle or time units.<br>
65// It has an automatic conversion to Double, so all standard mathematical
66// operations can operate on it.<br>
67// The class has a number of special member operations:
68// <ul>
69// <li> <src>MVAngle operator()</src> will normalise the angle between
70// -180 and +180(inclusive) degrees and return the value
71// <li> <src>MVAngle operator(Double)</src> will normalise the angle
72// using the value specified (and return the value)
73// in fractions of a circle (this was chosen rather than radians to make
74// for easier and more precise programming) as a lower bound. I.e.
75// (-0.5) will normalise between -180 and +180 degrees, (0.) between
76// 0 and 360 degrees, (-0.25) between -90 and +270 degrees,
77// (5.) between 1800 and 2160 dgrees.
78// <li> <src>MVAngle operator(MVAngle)</src> will normalise (and
79// return the normalised value) to within 180 degrees of the
80// argument value. This is useful for making a range of angles
81// contiguous.
82// <li> <src>MVAngle binorm(Double)</src> will normalise the angle in
83// steps of 180 degrees.
84// using the value specified (and return the value)
85// in fractions of 180 degrees (this was chosen rather than radians to make
86// for easier and more precise programming) as a lower bound. I.e.
87// (-0.5) will normalise between -90 and +90 degrees, (0.) between
88// 0 and 180 degrees, (10.) between 1800 and 1980 dgrees.
89// <li> <src>Double radian()</src> will return value in radians
90// <li> <src>Double degree()</src> will return value in degrees
91// <li> <src>Double circle()</src> will return value in fraction of circles
92// <li> <src>MVAngle coAngle()</src> will return 90-angle (or rather
93// pi/2 - angle), with (0) normalisation.
94// <li> <src>Quantity get()</src> will return radians
95// <li> <src>Quantity get(Unit)</src> will return in specified units
96// (angle or time)
97// </ul>
98// Output formatting is done with the <src><<</src> statement, with the
99// following rules:
100// <ul>
101// <li> standard output is done in the following format:
102// <src>+ddd.mm.ss.tt</src> with a floating sign. The number of
103// digits presented will be based on the precision attached to the
104// current stream
105// <li> output can be formatted by using either the <src>setFormat()</src>
106// method for global angle format setting, or the output of
107// <src>MVAngle::Format()</src> data for a once off change (see later).
108// Formats have a first argument which
109// determines the type (default, if not given, MVAngle::ANGLE, other
110// possibility MVAngle::TIME (as hh:mm:ss.tt..),
111// the second the number of digits wanted (default stream precision),
112// with a value:
113// <ul>
114// <li> <3 : ddd.. only
115// <li> <5 : ddd.mm.
116// <li> <7 : ddd.mm.ss
117// <li> >6 : with precision-6 t's added
118// </ul>
119// comparable for time. <note role=tip> The added periods are to enable input
120// checking of the format. Look at the 'clean' types to bypass them.
121// </note>
122// The output format can be modified with modifiers (specify as
123// MVAngle::ANGLE | MVAngle::MOD (or + MVAngle::MOD)).
124// <note role=caution>
125// For overloading/casting problems with some compilers, the
126// use of modifiers necessitates either the presence of a precision
127// (i.e. <src>(A|B, prec)</src>), or an explicit cast:
128// <src>((MVAngle::formatTypes)(A|B))</src>, or make use of
129// the provided <src>ANGLE[_CLEAN][_NO_D[M]]</src> and
130// <src>TIME[_CLEAN][_NO_H[M]].</src>
131// </note>
132//
133// The modifiers can be:
134// <ul>
135// <li> <src>MVAngle::CLEAN</src> to suppress leading or trailing
136// periods (or colons for TIME), + and leading zeroes in degree
137// field for angle representation will be replaced with a space.
138// Note that the result can not be read automatically.
139// <li> <src>MVAngle::NO_D</src> (or <src>NO_H</src>) to suppress
140// the output of degrees (or hours): useful for offsets
141// <li> <src>MVAngle::NO_DM</src> (or <src>NO_HM</src>), to
142// suppress the degrees and minutes.
143// <li> <src>MVAngle::DIG2</src> to allow only 2 digits for degrees,
144// or -12 - +12 range for hours
145// <li> <src>MVAngle::LOCAL</src> to indicate local time to FITS
146// formatting only
147// <li> <src>MVAngle::FITS</src> to produce, if
148// LOCAL set, the time zone (note that if local set
149// here, as opposed to in MVTime) the angle is supposed
150// to be in local time already).
151// <li> <src>MVAngle::ALPHA</src> to use d (or h) and m instead of
152// periods or colons.
153// </ul>
154// Output in formats like <src>20'</src> can be done via the standard
155// Quantum output (e.g. <src> stream << angle.get("'") </src>).
156// <li> Available formats:
157// <ul>
158// <li> MVAngle::ANGLE in +ddd.mm.ss.ttt format
159// <li> MVAngle::TIME in hh:mm:ss.ttt format
160// <li> MVAngle::[ANGLE|TIME]_CLEAN format without superfluous periods
161// <li> MVAngle::[ANGLE|TIME][_CLEAN]_NO_[D|H][M] in format with
162// leading zero fields left empty.
163// <li> MVAngle::CLEAN modifier for suppressing superfluous periods
164// <li> MVAngle::NO_[D|H][M] modifier to suppress first field(s)
165// <li> MVAngle::DIG2 modifier to output in +dd.mm.ss.ttt format or
166// in time format in range -12 to +12h
167// </ul>
168// </ul>
169// The default formatting can be overwritten by a
170// <src> MVAngle::setFormat(); </src> statement; which returns an
171// MVAngle::Format
172// structure, that can be used in a subsequent one to reset to previous.
173// The format set holds for all MVAngle output on all streams.<br>
174// Temporary formats (i.e. for one MVAngle output only), can be set by
175// outputting a format (i.e. <src> stream << MVAngle::Format() << ... </src>).
176// <note role=caution> A setFormat() will also reset any lingering temporary format.
177// A setFormat(getFormat()) will reset without changing. Problems could
178// arise in parallel processors. </note>
179// Input can be read if the values are in any of the above (non-clean) output
180// formats. <br>
181// For other formatting practice, the output can be written to a String with
182// the string() member function.<br>
183// Note that using a temporary format is inherently thread-unsafe because
184// the format is kept in a static variable. Another thread may overwrite
185// the format just set. The only thread-safe way to format an MVTime is using
186// a <src>print</src> or <src>string</src> that accepts a Format object.
187//
188// Strings and input can be converted to an MVAngle (or Quantity) by
189// <src>Bool read(Quantity &out, const String &in)</src> and
190// <src> istream >> MVAngle &</src>. In the latter case the actual
191// reading is done by the String read, which reads between white-spaces.<br>
192// The following input formats (note no blanks allowed) are supported
193// (+stands for an optional + or -; v for an unsigned integer; dv for a
194// floating number. [] indicate optional values. Separating codes are
195// case insensitive):
196// <ul>
197// <li> +[v].[v].[dv] -- value in deg, arcmin, arcsec
198// <li> +[v]D[v[M[dv]]] -- value in deg, arcmin, arcsec
199// <li> +[v]:[v[:[dv]]] -- value in h, min, s
200// <li> +[v]H[v[M[dv]]] -- value in h, min, s
201// <li> +[v]{D|H|:}[dv] -- value in deg (or h), arcmin (or min)
202// <li> +dv[unit string] -- value in time or angle units. rad default
203// </ul>
204// Examples of valid strings:
205// <srcblock>
206// 5::2.59 5h + 0min + 2.59 s
207// 5..2.59 5deg + 0arcmin + 2.59arcsec
208// 5.259 5.259 rad
209// 5..259 5deg + 259arcsec
210// 5.259a 5.259 * pi * 2 *365.25 rad (normalised)
211// </srcblock>
212// <note role=caution> In general the input will be read as a Quantity.
213// Reading of Quantities will always try to read special formats (like
214// MVAngle, MVTime) first. In
215// that case problems could arise converting strings like 5d, 5::, 5hm, 5dm.
216// In 'angle' mode they could have meant to be
217// 5d0m, 5:0:, 5h0m, 5d0m, but they could have
218// meant: days, min, hectometre, decimetre. In the same vain 5d2 could have
219// meant 5d2m or 5 d<sup>2</sup>.
220// To try to guess the general use, the following interpretation is made:
221// <ul>
222// <li> 5d, 5:: == 5deg, 5h0m; make float (like 5.d) to make it days/min
223// <li> 5dm, 5hm == decimetre, hectometre; use 5d0m 5h0m for
224// angle
225// <li> 5d2, 5h2, 5:2 == 5d2m, 5h2m, 5:2:; use float 5 or explicit () for
226// other interpretation
227// </ul>
228// </note>
229// </synopsis>
230//
231// <example>
232// See synopsis
233// </example>
234//
235// <motivation>
236// To be able to format angle-like values in user-required ways.
237// </motivation>
238//
239// <todo asof="1997/09/16">
240// <li> Use AipsrcData once moved to aips from trial
241// </todo>
242
243class MVAngle {
244
245 public:
246
247 //# Enumerations (should mimic those in MVTime)
248 // Format types
273
274 //# Local structure
275 // Format structure
276 class Format {
277 public:
278 friend class MVAngle;
280 uInt inprec = 0) :
281 typ(intyp), prec(inprec) {;};
282 Format(uInt inprec) :
283 typ(MVAngle::ANGLE), prec(inprec) {;};
284 // Construct from type and precision (present due to overlaoding problems)
285 Format(uInt intyp, uInt inprec) :
286 typ((MVAngle::formatTypes) intyp), prec(inprec) {;};
287 private:
290 };
291
292 //# Friends
293 // Output an angle
294 friend ostream &operator<<(ostream &os, const MVAngle &meas);
295 // Input an angle
296 friend istream &operator>>(istream &is, MVAngle &meas);
297 // Set a temporary format
298 friend ostream &operator<<(ostream &os, const MVAngle::Format &form);
299
300 //# Constructors
301 // Default constructor: generate a zero value
303 // Copy constructor
304 MVAngle(const MVAngle &other);
305 // Copy assignment
306 MVAngle &operator=(const MVAngle &other);
307 // Constructor from Double
309 // Constructor from Quantum : value can be an angle or time
310 // <thrown>
311 // <li> AipsError if not a time or angle
312 // </thrown>
313 MVAngle(const Quantity &other);
314
315 // Destructor
317
318 //# Operators
319 // Conversion operator
320 operator Double() const;
321 // Normalisation between -180 and +180 degrees (-pi and +pi)
323 // Normalisation between 2pi*norm and 2pi*norm + 2pi
325 // Normalisation between norm-pi and norm+pi
326 const MVAngle &operator()(const MVAngle &norm);
327
328 //# General member functions
329 // Normalisation between pi*norm and pi*norm + pi
330 const MVAngle &binorm(Double norm);
331 // Check if String unit
332 static Bool unitString(UnitVal &uv, String &us, MUString &in);
333
334 // Make res angle Quantity from string in angle/time-like format. In the
335 // case of String input, also quantities are recognised.
336 // chk=True means that the entire string should be consumed.
337 // throwExcp=True means that an exception is thrown in case of an error.
338 // <group>
339 static Bool read(Quantity &res, const String &in, Bool chk=True);
340 static Bool read(Quantity &res, MUString &in, Bool chk=True);
341 static Bool read(Quantity &res, const String &in, Bool chk, Bool throwExcp);
342 static Bool read(Quantity &res, MUString &in, Bool chk, Bool throwExcp);
343 // </group>
344 // Handle a read error. An exception is thrown if indicated so.
345 // Otherwise in.pop() is called and False is returned.
346 static Bool handleReadError(MUString& in, Bool throwExcp);
347
348 // Make co-angle (e.g. zenith distance from elevation)
350 // Get value in given unit
351 // <group>
352 Double radian() const;
353 Double degree() const;
354 Double circle() const;
355 Quantity get() const;
356 Quantity get(const Unit &inunit) const;
357 // </group>
358 // Output data
359 // <note role=warning>
360 // The first function below is thread-unsafe because it uses the result of
361 // the setFormat function which changes a static class member.
362 // The other functions are thread-safe because the format is directly given.
363 // </note>
364 // <group>
365 String string() const;
366 String string(MVAngle::formatTypes intyp, uInt inprec = 0) const;
367 String string(uInt intyp, uInt inprec) const;
368 String string(uInt inprec) const;
369 String string(const MVAngle::Format &form) const;
370 void print(ostream &oss, const MVAngle::Format &form) const;
371 void print(ostream &oss, const MVAngle::Format &form, Bool loc) const;
372 // </group>
373 // Set default format
374 // <note role=warning>
375 // It is thread-unsafe to print using the setFormat functions because they
376 // change a static class member. The only thred-safe way to print a time is
377 // to use the print function above.
378 // </note>
379 // <group>
381 uInt inprec = 0);
382 static Format setFormat(uInt intyp, uInt inprec);
383 static Format setFormat(uInt inprec = 0);
384 static Format setFormat(const Format &form);
385 // </group>
386 // Get default format
388 // Get code belonging to string. 0 if not known
390 // Get time zone offset (in days)
391 static Double timeZone();
392
393 private:
394 //# Data
395 // Value
397 // Default format
399 // Temporary format
400 // <group>
403 // </group>
404
405 //# Member functions
406};
407
408// Global functions
409// <summary> Global output/input functions </summary>
410// Output/Input
411// <group name=output>
412ostream &operator<<(ostream &os, const MVAngle &meas);
413istream &operator>>(istream &is, MVAngle &meas);
414// Set a temporary format (thread-unsafe).
415ostream &operator<<(ostream &os, const MVAngle::Format &form);
416// </group>
418
419} //# NAMESPACE CASACORE - END
420
421#endif
static functions and enumerations
Definition fits.h:159
Format structure.
Definition MVAngle.h:276
MVAngle::formatTypes typ
Definition MVAngle.h:288
Format(MVAngle::formatTypes intyp=MVAngle::ANGLE, uInt inprec=0)
Definition MVAngle.h:279
Format(uInt intyp, uInt inprec)
Construct from type and precision (present due to overlaoding problems)
Definition MVAngle.h:285
String string(const MVAngle::Format &form) const
static Bool read(Quantity &res, MUString &in, Bool chk, Bool throwExcp)
String string(uInt intyp, uInt inprec) const
String string(uInt inprec) const
String string() const
Output data Warning: The first function below is thread-unsafe because it uses the result of the set...
~MVAngle()
Destructor.
Double circle() const
void print(ostream &oss, const MVAngle::Format &form) const
MVAngle(const MVAngle &other)
Copy constructor.
static Format setFormat(MVAngle::formatTypes intyp, uInt inprec=0)
Set default format Warning: It is thread-unsafe to print using the setFormat functions because they ...
const MVAngle & operator()(Double norm)
Normalisation between 2pi*norm and 2pi*norm + 2pi.
static Double timeZone()
Get time zone offset (in days)
static Format getFormat()
Get default format.
static Bool read(Quantity &res, const String &in, Bool chk=True)
Make res angle Quantity from string in angle/time-like format.
MVAngle(const Quantity &other)
Constructor from Quantum : value can be an angle or time.
friend ostream & operator<<(ostream &os, const MVAngle::Format &form)
Set a temporary format.
MVAngle(Double d)
Constructor from Double.
static Bool read(Quantity &res, const String &in, Bool chk, Bool throwExcp)
MVAngle()
Default constructor: generate a zero value.
formatTypes
Format types.
Definition MVAngle.h:249
MVAngle coAngle() const
Make co-angle (e.g.
static Bool unitString(UnitVal &uv, String &us, MUString &in)
Check if String unit.
static MVAngle::formatTypes giveMe(const String &in)
Get code belonging to string.
static MVAngle::Format interimFormat
Temporary format.
Definition MVAngle.h:401
const MVAngle & binorm(Double norm)
Normalisation between pi*norm and pi*norm + pi.
friend istream & operator>>(istream &is, MVAngle &meas)
Input an angle.
static MVAngle::Format defaultFormat
Default format.
Definition MVAngle.h:398
static Format setFormat(uInt intyp, uInt inprec)
void print(ostream &oss, const MVAngle::Format &form, Bool loc) const
Quantity get() const
Double val
Value.
Definition MVAngle.h:396
Double radian() const
Get value in given unit.
Double degree() const
Quantity get(const Unit &inunit) const
MVAngle & operator=(const MVAngle &other)
Copy assignment.
const MVAngle & operator()(const MVAngle &norm)
Normalisation between norm-pi and norm+pi.
static Format setFormat(const Format &form)
String string(MVAngle::formatTypes intyp, uInt inprec=0) const
static Bool read(Quantity &res, MUString &in, Bool chk=True)
static Bool interimSet
Definition MVAngle.h:402
static Format setFormat(uInt inprec=0)
friend ostream & operator<<(ostream &os, const MVAngle &meas)
Output an angle.
const MVAngle & operator()()
Normalisation between -180 and +180 degrees (-pi and +pi)
static Bool handleReadError(MUString &in, Bool throwExcp)
Handle a read error.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
AipsIO & operator>>(AipsIO &os, Record &rec)
Definition Record.h:462
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53
ostream & operator<<(ostream &os, const MVAngle::Format &form)
Set a temporary format (thread-unsafe).
istream & operator>>(istream &is, MVAngle &meas)
ostream & operator<<(ostream &os, const MVAngle &meas)