Sun position
Sun position algorithm
skyio.h
Go to the documentation of this file.
1 #ifndef SKYIO_H
2 #define SKYIO_H
3 /*============================================================================*/
4 /*!\file
5  * \brief skyio.h - output and formatting routines and a read routine
6  *
7  * \author David Hoadley
8  *
9  * \details
10  * Routines for reading and writing out assorted astronomical things,
11  * such as:
12  * - formatting a radian angle, or an angle expressed in hours, into
13  * a string in Hours Minutes Seconds form, something only an
14  * astronomer would think of doing.
15  * - formatting a radian angle, or an angle expressed in degrees into
16  * a string in Degrees Minutes Seconds form, and doing it correctly.
17  * Quite a few people need this one.
18  * - reading an angle from a string
19  * - write out a Julian Date (in J2KD form - see
20  * \ref page-time-variables) as a calendar date and time.
21  *
22  * These routines were mainly developed for debugging.
23  *
24  *==============================================================================
25  */
26 /*
27  * Copyright (c) 2020, David Hoadley <vcrumble@westnet.com.au>
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions are met:
32  *
33  * * Redistributions of source code must retain the above copyright notice, this
34  * list of conditions and the following disclaimer.
35  * * Redistributions in binary form must reproduce the above copyright notice,
36  * this list of conditions and the following disclaimer in the documentation
37  * and/or other materials provided with the distribution.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
43  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  */
51 /*------------------------------------------------------------------------------
52  * Notes:
53  * Character set: UTF-8. (Non-ASCII characters appear in this file)
54  *----------------------------------------------------------------------------*/
55 
56 #include "general.h"
57 #include "astron.h"
58 
59 /*
60  * Global #defines and typedefs
61  */
62 #define NO_ANGLE (-1)
63 #define INVALID_ANGLE (-2)
64 
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /*
71  * Global functions available to be called by other modules
72  */
73 /* Convert from an angle (or a time) into a sexagesimal text string */
74 char *skyio_degToDmsStr(char destStr[],
75  size_t destStrSize,
76  double angle_deg,
77  unsigned decimals);
78 
79 char *skyio_hrsToHmsStr(char destStr[],
80  size_t destStrSize,
81  double angle_h,
82  unsigned decimals);
83 
84 /* Convert from a sexagesimal text string to an angle */
85 double skyio_sxStrToAng(const char angleStr[],
86  const char **endPtr,
87  int *error);
88 
89 
90 
91 /*! Routine to take an angle in radian and return a string in degrees,
92  arcminutes and arcseconds form - [±]DDD°MM′SS.sss″ -
93  correctly rounding according to the number of decimal places to be shown.
94  The angle is assumed to be within the range (-2*Pi, 2*Pi). Angles which
95  round to ±360° will be written out as 0°.
96  \returns Pointer to \a destStr
97  \param[out] destStr Destination character string
98  \param[in] destStrSize Size of destination string (max available length + 1)
99  \param[in] angle_rad The angle to be written out (radian).
100  Valid range:(-2*Pi, 2*Pi); larger numbers may well be
101  written OK, but there is a risk of overflowing an
102  intermediate variable. No error will be detected,
103  just a wrong answer will be written.
104  \param[in] decimals Number of digits after the decimal point to display.
105  Valid range: [0,9] (or [0,3] if long int is only a
106  32-bit number); numbers outside this range will be
107  clamped to this range.
108 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
109 #if defined (PREDEF_STANDARD_C_1999)
110 /* Compiler supports inline functions */
111 static inline char *skyio_radToDmsStr(char destStr[],
112  size_t destStrSize,
113  double angle_rad,
114  unsigned decimals)
115 {
116  return skyio_degToDmsStr(destStr, destStrSize, radToDeg(angle_rad),decimals);
117 }
118 #else
119  /* C89/C90 compiler - no inline functions. Need macros instead */
120  #define skyio_radToDmsStr(destStr__, destStrSize__, angle_rad__, decimals__) \
121  skyio_degToDmsStr(destStr__, destStrSize__, \
122  radToDeg(angle_rad__), decimals__)
123 #endif
124 
125 
126 /*! Routine to take an angle in radian and return a string in hours, minutes and
127  seconds form - "±HH:MM:SS.sss" -
128  correctly rounding according to the number of decimal places to be shown.
129  The angle is assumed to be within the range (-2*Pi, 2*Pi). Angles which
130  round to ±24:00:00 will be written out as 0:00:00.
131  \returns Pointer to \a destStr
132  \param[out] destStr Destination character string
133  \param[in] destStrSize Size of destination string (max available length + 1)
134  \param[in] angle_rad The angle to be written out (radian).
135  Valid range:(-2*Pi, 2*Pi); larger numbers may well be
136  written OK, but there is a risk of overflowing an
137  intermediate variable. No error will be detected,
138  just a wrong answer will be written.
139  \param[in] decimals Number of digits after the decimal point to display.
140  Valid range: [0,9] (or [0,3] if long int is only a
141  32-bit number); numbers outside this range will be
142  clamped to this range.
143 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144 #if defined (PREDEF_STANDARD_C_1999)
145 /* Compiler supports inline functions */
146 static inline char *skyio_radToHmsStr(char destStr[],
147  size_t destStrSize,
148  double angle_rad,
149  unsigned decimals)
150 {
151  return skyio_hrsToHmsStr(destStr, destStrSize, radToHrs(angle_rad),decimals);
152 }
153 #else
154  /* C89/C90 compiler - no inline functions. Need macros instead */
155  #define skyio_radToHmsStr(destStr__, destStrSize__, angle_rad__, decimals__) \
156  skyio_hrsToHmsStr(destStr__, destStrSize__, \
157  radToHrs(angle_rad__), decimals__)
158 #endif
159 
160 
161 void skyio_printJ2kd(double j2kd);
162 
163 /*
164  * Global variables accessible by other modules
165  */
166 
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* SKYIO_H */
173 
general.h
general.h - definitions of general use to (standard) C programs
skyio_radToDmsStr
static char * skyio_radToDmsStr(char destStr[], size_t destStrSize, double angle_rad, unsigned decimals)
Routine to take an angle in radian and return a string in degrees, arcminutes and arcseconds form - [...
Definition: skyio.h:111
skyio_radToHmsStr
static char * skyio_radToHmsStr(char destStr[], size_t destStrSize, double angle_rad, unsigned decimals)
Routine to take an angle in radian and return a string in hours, minutes and seconds form - "±HH:MM:S...
Definition: skyio.h:146
astron.h
astron.h - assorted definitions useful for astronomy
skyio_hrsToHmsStr
char * skyio_hrsToHmsStr(char destStr[], size_t destStrSize, double angle_h, unsigned decimals)
Routine to take an angle in hours and return a string in hours, minutes and seconds form - "±HH:MM:SS...
Definition: skyio.c:193
skyio_sxStrToAng
double skyio_sxStrToAng(const char angleStr[], const char **endPtr, int *error)
Convert a string containing an angle (or a time) in sexagesimal format to the angle's value.
Definition: skyio.c:288
radToHrs
static double radToHrs(double angle_rad)
Returns angle_rad converted from radians to hours.
Definition: astron.h:57
skyio_printJ2kd
void skyio_printJ2kd(double j2kd)
Write out a J2KD as a calendar date and time.
Definition: skyio.c:426
skyio_degToDmsStr
char * skyio_degToDmsStr(char destStr[], size_t destStrSize, double angle_deg, unsigned decimals)
Routine to take an angle in degrees and return a string of the form [±]DDD°MM′SS.sss″ correctly round...
Definition: skyio.c:88
radToDeg
static double radToDeg(double angle_rad)
Returns angle_rad converted from radians to degrees.
Definition: general.h:182