CARMA C++
source.h
1 #ifndef source_h
2 #define source_h
3 
4 #include "carma/szaarrayutils/input.h"
5 #include "carma/szaarrayutils/output.h"
6 #include "carma/szaarrayutils/quad.h"
7 #include "carma/szaarrayutils/astrom.h"
8 #include "carma/szaarrayutils/cache.h"
9 
10 /*
11  * In all of the following functions time is specified in the form
12  * of Modified Julian Dates (Julian Date - 2400000.5). If the time
13  * is specified as -1.0, then the current time will automatically
14  * be substituted. See astrom.h for time generation and manipulation
15  * functions. Also note that in some places where one should strictly
16  * use Barycentric Dynamical Time, Terrestrial Time is used instead.
17  * The two time systems differ by at most a couple of milli-seconds.
18  *
19  * Thus arguments named utc denote Universal Coordinated Time's, whereas
20  * arguments named tt denote Terrestrial Time.
21  */
22 
23 /*
24  * The opaque SourceCatalog type contains a catalog of sources, indexed
25  * by name and number. Its definition and implementation is private
26  * to source.c.
27  */
28 typedef struct SourceCatalog SourceCatalog;
29 
30 /*
31  * Create an empty source catalog.
32  */
33 SourceCatalog *new_SourceCatalog(void);
34 
35 /*
36  * Delete a source catalog.
37  */
38 SourceCatalog *del_SourceCatalog(SourceCatalog *sc);
39 
40 /*
41  * Read source catalog entries from an input stream. Note that
42  * the new sources are added to the existing catalog. If a new source
43  * has the same name as an existing catalog entry, the existing catalog
44  * entry is overwritten with the new one.
45  */
46 int input_SourceCatalog(SourceCatalog *sc, InputStream *stream);
47 
48 /*
49  * Read source catalog entries from a file. This is a wrapper around
50  * input_SourceCatalog().
51  */
52 int read_SourceCatalog(SourceCatalog *sc, char *dir, char *file);
53 
54 /*
55  * Write source catalog entries to an output stream, using the format
56  * expected by input_SourceCatalog().
57  */
58 int output_SourceCatalog(SourceCatalog *sc, OutputStream *stream);
59 
60 /*
61  * Write source catalog entries to a file. This is a wrapper around
62  * output_SourceCatalog().
63  */
64 int write_SourceCatalog(SourceCatalog *sc, char *dir, char *file);
65 
66 /*
67  * Specify a new UT1-UTC ephemeris file.
68  */
69 int sc_set_ut1utc_ephemeris(SourceCatalog *sc, char *dir, char *file);
70 
71 namespace sza {
72  namespace array {
73 
74  typedef union SourceUnion Source;
75 
76  /*
77  * Enumerate the supported types of sources.
78  */
79  enum SourceType {
80  SRC_J2000, /* A source at fixed J2000 RA and Dec */
81  SRC_EPHEM, /* An ephemeris source (eg. a planet) */
82  SRC_FIXED, /* A source at fixed azimuth and elevation */
83  SRC_ALIAS, /* An alternative name for another source */
84  SRC_UNKNOWN /* A type we can assign to unititalized
85  SourceType variables */
86  };
87 
88  /*
89  * Set the max length of a source name (including '\0'). This
90  * should be set equal to SRC_LEN in szaregs.h.
91  */
92  enum {SRC_NAME_MAX=12};
93 
94 
95  /*
96  * All source types share an initial member of the following type.
97  *
98  * When a given source name is enterred into a SourceCatalog for the
99  * first time, it is assigned a catalog index. This index can thereafter
100  * be used as a unique synonym for the source name, since it doesn't change
101  * even if the definition of the source associated with that name is
102  * modified.
103  */
104  struct SourceId {
105  SourceType type; /* The enumerated type of the source */
106  char name[SRC_NAME_MAX]; /* The name of the source */
107  int number; /* The catalog index tied to name[] */
108  };
109 
110  /*
111  * The following type of source is specified at a fixed J2000.0 Right
112  * Ascension and Declination.
113  */
114  struct J2000Source {
115  SourceId id; /* The generic source identification header */
116  double ra; /* The mean right-ascension at epoch J2000.0 (radians) */
117  double dec; /* The mean declination at epoch J2000.0 (radians) */
118  float pmra; /* The proper motion in Right Ascension (radians) */
119  float pmdec; /* The proper motion in Declination (radians) */
120  float mag; /* The magnitude of the star */
121  };
122 
123  /*
124  * The following type of source is located at a fixed azimuth and
125  * elevation.
126  */
127  struct FixedSource {
128  SourceId id; /* The generic source identification header */
129  unsigned axis_mask;/* A bitwise union of source.h::SourceAxes enumerators */
130  /* specifying which of the following axes are */
131  /* significant to this source. */
132  double az; /* The target azimuth (radians) */
133  double el; /* The target elevation (radians) */
134  double dk; /* The target deck-angle (radians) */
135  };
136 
137  struct EphemEntry {
138  double ra; /* The geocentric apparent Right Ascension (radians) */
139  double dec; /* The geocentric apparent Declination (radians) */
140  double dist; /* The geocentric distance of the source (AU) */
141  double tt; /* The terrestrial time of the entry (MJD) */
142  };
143 
144  /*
145  * The following source type is for non-sidereal sources whos positions
146  * are recorded in ephemeris files. The ephemeris file is not kept open
147  * at all times, instead up to EPHEM_CACHE_SIZE contemporary entries are
148  * kept in a cache. At any given time three points from this cache are
149  * loaded into quadratic interpolators, to provide more precise positions.
150  * The middle of these three entries is at element 'midpt' of the cache.
151  * When the cache has been exhausted, it will be refreshed from the
152  * ephemeris file.
153  */
154  struct EphemSource {
155  SourceId id; /* The generic source identification header */
156  char *file; /* The pathname of the ephemeris file */
157  EphemEntry *cache; /* A cache of size<=EPHEM_CACHE_SIZE entries */
158  int size; /* The number of elements in cache[] */
159  int midpt; /* The middle element of the interpolation triple */
160  QuadPath *ra; /* The Right Ascension interpolator */
161  QuadPath *dec; /* The Declination interpolator */
162  QuadPath *dist; /* The Distance interpolator */
163  };
164 
165  /*
166  * The following type of source is a symbolic link to another source.
167  */
168  struct AliasSource {
169  SourceId id; /* The generic source identification header */
170  Source **sptr; // The catalog entry of the source. Using
171  // this relies on the fact the only
172  // destructive operation that is allowed on
173  // an existing catalog entry is to replace
174  // its contents with a new source of the same
175  // name. We can't actually store the (Source
176  // *) pointer because that can change.
177  };
178 
179  /*
180  * The opaque Source datatype is a union of all source types.
181  * Its definition is private to source.c. Be careful about
182  * cacheing (Source *) pointers. If add_J2000Source() et al. is called
183  * to change the definition of an existing source (identified by name),
184  * then the original (Source *) pointer associated with that name will
185  * become invalid and should not be used. It is safer to record catalog
186  * numbers or source names (see get_SourceId).
187  */
188  union SourceUnion {
189  SourceId id; /* The first members of all source types */
190  J2000Source j2000; /* A source at fixed Ra and Dec (J2000) */
191  EphemSource ephem; /* An ephemeris source */
192  FixedSource fixed; /* A source at fixed azimuth and elevation */
193  AliasSource alias; /* A link to another source */
194  };
195 
196  /*
197  * Fixed sources directly set the position of one or more of the
198  * azimuth, elevation and deck axes. The following enumerators
199  * are used in a bitwise union to specify which of the axes are
200  * specified by the source.
201  */
202  enum SourceAxes {
203  SRC_AZ_AXIS = 1, /* The source specifies the azimuth axis */
204  SRC_EL_AXIS = 2, /* The source specifies the elevation axis */
205  SRC_DK_AXIS = 4, /* The source specifies the deck axis */
206  /* The source specifies all of the axes */
207  SRC_ALL_AXES = SRC_AZ_AXIS | SRC_EL_AXIS | SRC_DK_AXIS
208  };
209  };
210 };
211 
212 /*
213  * The following function returns non-zero if a given character is
214  * valid within a source name.
215  */
216 int valid_source_char(int c);
217 
218 /*
219  * Get a copy of the identification header of a given Source.
220  * On error it returns non-zero. If resolve is true and the
221  * source is an alias to another source, the id of the aliased
222  * source will be returned in its place (this rule is applied
223  * recursively).
224  */
225 int get_SourceId(sza::array::Source* src, int resolve, sza::array::SourceId *id);
226 
227 /*
228  * Return the source-catalog index of a given source (-1 on error).
229  * See the documentation of SourceId for the usage of such indexes.
230  * The resolve member is used as described for get_SourceId().
231  */
232 int get_Source_number(sza::array::Source* src, int resolve);
233 
234 /*
235  * Use the following functions to add sources to the catalog.
236  * Each function returns NULL on failure.
237  */
238 sza::array::Source* add_J2000Source(SourceCatalog *sc, char *name, double ra,
239  double dec, float pmra, float pmdec);
240 sza::array::Source* add_EphemSource(SourceCatalog *sc, char *name, char *file);
241 sza::array::Source* add_FixedSource(SourceCatalog *sc, char *name,
242  unsigned axis_mask, double az, double el,
243  double dk);
244 sza::array::Source* add_AliasSource(SourceCatalog *sc, char *name, char *source);
245 
246 /*
247  * Use the following functions to look up a source. The first is
248  * useful for finding a source named by a user. It uses a hash table
249  * so it should be relatively fast. The second uses the ordinal
250  * position of the source in the catalog. This should be more
251  * efficient for repeat lookups of the same source and can also be
252  * used to step through the catalog. Note that if you have a SourceId
253  * object, the ordinal number of the source is given by the 'number'
254  * field.
255  */
256 sza::array::Source* find_SourceByName(SourceCatalog *sc, char *name);
257 sza::array::Source* find_SourceByNumber(SourceCatalog *sc, int number);
258 
259 /*
260  * Return the number of sources that are currently in a source catalog.
261  */
262 int size_SourceCatalog(SourceCatalog *sc);
263 
264 /*
265  * The following types are used with the source_visibility() function
266  * to return the projected visibility of a given source with respect
267  * to a specified horizon elevation.
268  */
269 typedef enum {
270  SRC_NEVER_RISES, /* The source is always below the horizon */
271  SRC_NEVER_SETS, /* The source is always above the horizon */
272  SRC_ALTERNATES, /* The source periodically crosses the horizon */
273  SRC_IRREGULAR /* A source who's path is too different from */
274  /* sidereal for rise and set times to be determined. */
275 } SourceSched;
276 
277 /*
278  * The source_info() function takes a bit-wise union of the following
279  * options for its "options" argument. This argument tells the
280  * source_info() function which parameters you want it to compute.
281  */
282 typedef enum {
283  SIO_EQUAT = 1, /* Topocentric equatorial coordinates */
284  SIO_HORIZ = 2, /* Topocentric horizon coordinates and rates */
285  SIO_ALMANAC = 4, /* Rise and set times */
286  /*
287  * The following option selects all options.
288  */
289  SIO_ALL = SIO_EQUAT | SIO_HORIZ | SIO_ALMANAC
290 } SrcInfoOpt;
291 
292 /*
293  * Describe the structure that source_info() uses to return contemporary
294  * information about a given source. All angles are in radians.
295  */
296 typedef struct {
297  double az; /* The azimuth of a source (radians) */
298  double el; /* The elevation of a source (radians) */
299  double pa; /* The parallactic angle of a source (radians) */
300 } SourceAzElPa;
301 
302 typedef struct {
303  double utc; /* The UTC for which this information was computed (MJD)*/
304  double ut1; /* The UT1 equivalent of 'utc' (MJD) */
305  double tt; /* The corresponding Terrestrial Time (MJD) */
306  double lst; /* The corresponding Local Apparent Sidereal Time, */
307  /* measured in radians. */
308  double ra,dec; /* The topocentric apparent Right Ascension and Declination */
309  double raMean,decMean; /* The J2000 Right Ascension and Declination (where applicable) */
310  unsigned axis_mask; /* For fixed sources this mask contains a bitwise */
311  /* union of sza::util::Axis::Type enumerators
312  to specify which of the telescope axes are
313  specified by this source. */
314  SourceAzElPa coord; /* The horizon coordinates of the source */
315  SourceAzElPa rates; /* The az,el,pa motion of the source (radians/sec)*/
316  SourceSched sched; /* The visibility type of the source */
317  double rise,set; /* The rise and set times (UTC) of the source if */
318  /* sched==SRC_ALTERNATES */
319 } SourceInfo;
320 
321 /*
322  * Fill in a provided SourceInfo structure with details about a given
323  * source, as seen from a given site at a given time. The (Site *) datatype
324  * needed by the "site" argument is defined in astrom.h and initialized with
325  * set_Site(). The utc argument must be given either as a Modified Julian Date,
326  * or as the value -1 which selects the current time. The horizon argument is
327  * the local elevation of the horizon in radians. The options argument is a
328  * bit-wise union of the SrcInfoOpt options defined above.
329  */
330 int source_info(SourceCatalog *sc, sza::array::Site *site,
331  sza::array::Source* src,
332  double utc, double horizon, unsigned options, SourceInfo *info);
333 
334 /*
335  * Return the time window over which the 3 UT1-UTC interpolation
336  * entries for the time 'utc' are usable. The returned time
337  * limits are specified in UTC. If no UT1-UTC ephemeris has been
338  * specified via sc_set_ut1utc_ephemeris() then win->tmin and win->tmax
339  * will be set to 0.0.
340  */
341 int get_ut1utc_window(SourceCatalog *sc, double utc,
342  sza::array::CacheWindow *win);
343 
344 /*
345  * Return the contents of the quadratic interpolator of UT1-UTC at
346  * Universal Coordinated Time, 'utc'. The QuadData x values are UTC
347  * expressed as MJDs, and the y values are the values of UT1-UTC in
348  * seconds.
349  */
350 int get_ut1utc_QuadData(SourceCatalog *sc, double utc,
351  sza::array::QuadData *data);
352 
353 /*
354  * Return the time window over which the 3 equation-of-the-equinoxes
355  * interpolation entries for the time 'tt' are usable.
356  * The returned time limits are specified in TT.
357  */
358 int get_eqneqx_window(SourceCatalog *sc, double tt,
359  sza::array::CacheWindow *win);
360 
361 /*
362  * Return the contents of the quadratic interpolator of the equation of
363  * the equinoxes at time 'tt'. The QuadData x values are TT times
364  * expressed as MJDs, and the y values are the values of the equation
365  * of the equinoxes, measured in radians.
366  */
367 int get_eqneqx_QuadData(SourceCatalog *sc, double tt,
368  sza::array::QuadData *data);
369 
370 /*
371  * Given an ephemeris source, return the time window over which
372  * the 3 ephemeris in its quadratic interpolators, initialized for
373  * the Terrestrial Time 'tt', are usable. Since src must be an ephemeris
374  * source, you should call get_SourceId() to check its type before calling
375  * this function.
376  *
377  * The returned time limits are specified in TT.
378  */
379 int get_ephem_window(SourceCatalog *sc, sza::array::Source* src, double tt,
380  sza::array::CacheWindow *win);
381 
382 /*
383  * Return the contents of the quadratic interpolators of a specified
384  * ephemeris source, valid for the time 'tt'. Since src must be an
385  * ephemeris source, you should call get_SourceId() to check its type
386  * before calling this function. The QuadData x axis values are all
387  * Terrestrial Times, expressed as MJDs. The ra interpolator y values
388  * are geocentric Right Ascensions in radians, and the dec interpolator
389  * y values are geocentric Declinations in radians. The dist interpolator
390  * y values are geocentric source distances in AU. For sources who's
391  * distances are unknown, 0 is substituted.
392  */
393 int get_ephem_QuadData(SourceCatalog *sc, sza::array::Source* src, double tt,
394  sza::array::QuadData *ra,
395  sza::array::QuadData *dec,
396  sza::array::QuadData *dist);
397 
398 /*
399  * Return the time window over which the cached precession and nutation
400  * parameters for time (tt) are usable for J2000 sources. These are
401  * parameters used with the slalib slaMapqk/slaMapqkz functions to
402  * precess the geocentric Right Ascension and declination of a source
403  * from J2000 to the current epoch. The Right Ascensions of J2000 sources
404  * are updated whenever this cache is updated.
405  *
406  * The returned time limits are specified in TT.
407  */
408 int get_mapqk_window(SourceCatalog *sc, double tt,
409  sza::array::CacheWindow *win);
410 
411 /*
412  * Return the contents of the precession/nutation parameter cache for
413  * time 'tt'. See slaMapqk() and slaMapqkz() for details of how to use
414  * the parameters in the cache.
415  */
416 typedef struct {
417  double mappa[21]; /* The array of cached parameters. */
418  /* This is the array of parameters returned by */
419  /* the slalib slaMappa() function. */
420 } MapqkData;
421 
422 int get_mapqk_data(SourceCatalog *sc, double tt, MapqkData *data);
423 
424 /*
425  * Return the geocentric Right Ascension and Declination of a
426  * J2000 source precessed to time 'tt'. An error will be returned
427  * if 'src' is not a J2000 source, so be sure to use get_SourceId()
428  * to determine the type of the source before making this call.
429  */
430 int precess_J2000_source(SourceCatalog *sc, sza::array::Source* src, double tt,
431  double *ra, double *dec);
432 
433 /*
434  * Get the coordinates of a fixed source.
435  */
436 int describe_fixed_source(sza::array::Source* src, SourceAzElPa *coord,
437  unsigned *axis_mask);
438 
439 unsigned nSource(SourceCatalog* sc);
440 
441 #endif