source: trunk/FACT++/erfa/src/jdcalf.c@ 18679

Last change on this file since 18679 was 18348, checked in by tbretz, 9 years ago
File size: 5.8 KB
Line 
1#include "erfa.h"
2
3int eraJdcalf(int ndp, double dj1, double dj2, int iymdf[4])
4/*
5** - - - - - - - - - -
6** e r a J d c a l f
7** - - - - - - - - - -
8**
9** Julian Date to Gregorian Calendar, expressed in a form convenient
10** for formatting messages: rounded to a specified precision.
11**
12** Given:
13** ndp int number of decimal places of days in fraction
14** dj1,dj2 double dj1+dj2 = Julian Date (Note 1)
15**
16** Returned:
17** iymdf int[4] year, month, day, fraction in Gregorian
18** calendar
19**
20** Returned (function value):
21** int status:
22** -1 = date out of range
23** 0 = OK
24** +1 = NDP not 0-9 (interpreted as 0)
25**
26** Notes:
27**
28** 1) The Julian Date is apportioned in any convenient way between
29** the arguments dj1 and dj2. For example, JD=2450123.7 could
30** be expressed in any of these ways, among others:
31**
32** dj1 dj2
33**
34** 2450123.7 0.0 (JD method)
35** 2451545.0 -1421.3 (J2000 method)
36** 2400000.5 50123.2 (MJD method)
37** 2450123.5 0.2 (date & time method)
38**
39** 2) In early eras the conversion is from the "Proleptic Gregorian
40** Calendar"; no account is taken of the date(s) of adoption of
41** the Gregorian Calendar, nor is the AD/BC numbering convention
42** observed.
43**
44** 3) Refer to the function eraJd2cal.
45**
46** 4) NDP should be 4 or less if internal overflows are to be
47** avoided on machines which use 16-bit integers.
48**
49** Called:
50** eraJd2cal JD to Gregorian calendar
51**
52** Reference:
53**
54** Explanatory Supplement to the Astronomical Almanac,
55** P. Kenneth Seidelmann (ed), University Science Books (1992),
56** Section 12.92 (p604).
57**
58** Copyright (C) 2013-2015, NumFOCUS Foundation.
59** Derived, with permission, from the SOFA library. See notes at end of file.
60*/
61{
62 int j, js;
63 double denom, d1, d2, f1, f2, f;
64
65/* Denominator of fraction (e.g. 100 for 2 decimal places). */
66 if ((ndp >= 0) && (ndp <= 9)) {
67 j = 0;
68 denom = pow(10.0, ndp);
69 } else {
70 j = 1;
71 denom = 1.0;
72 }
73
74/* Copy the date, big then small, and realign to midnight. */
75 if (dj1 >= dj2) {
76 d1 = dj1;
77 d2 = dj2;
78 } else {
79 d1 = dj2;
80 d2 = dj1;
81 }
82 d2 -= 0.5;
83
84/* Separate days and fractions. */
85 f1 = fmod(d1, 1.0);
86 f2 = fmod(d2, 1.0);
87 d1 = floor(d1 - f1);
88 d2 = floor(d2 - f2);
89
90/* Round the total fraction to the specified number of places. */
91 f = floor((f1+f2)*denom + 0.5) / denom;
92
93/* Re-assemble the rounded date and re-align to noon. */
94 d2 += f + 0.5;
95
96/* Convert to Gregorian calendar. */
97 js = eraJd2cal(d1, d2, &iymdf[0], &iymdf[1], &iymdf[2], &f);
98 if (js == 0) {
99 iymdf[3] = (int) (f * denom);
100 } else {
101 j = js;
102 }
103
104/* Return the status. */
105 return j;
106
107}
108/*----------------------------------------------------------------------
109**
110**
111** Copyright (C) 2013-2015, NumFOCUS Foundation.
112** All rights reserved.
113**
114** This library is derived, with permission, from the International
115** Astronomical Union's "Standards of Fundamental Astronomy" library,
116** available from http://www.iausofa.org.
117**
118** The ERFA version is intended to retain identical functionality to
119** the SOFA library, but made distinct through different function and
120** file names, as set out in the SOFA license conditions. The SOFA
121** original has a role as a reference standard for the IAU and IERS,
122** and consequently redistribution is permitted only in its unaltered
123** state. The ERFA version is not subject to this restriction and
124** therefore can be included in distributions which do not support the
125** concept of "read only" software.
126**
127** Although the intent is to replicate the SOFA API (other than
128** replacement of prefix names) and results (with the exception of
129** bugs; any that are discovered will be fixed), SOFA is not
130** responsible for any errors found in this version of the library.
131**
132** If you wish to acknowledge the SOFA heritage, please acknowledge
133** that you are using a library derived from SOFA, rather than SOFA
134** itself.
135**
136**
137** TERMS AND CONDITIONS
138**
139** Redistribution and use in source and binary forms, with or without
140** modification, are permitted provided that the following conditions
141** are met:
142**
143** 1 Redistributions of source code must retain the above copyright
144** notice, this list of conditions and the following disclaimer.
145**
146** 2 Redistributions in binary form must reproduce the above copyright
147** notice, this list of conditions and the following disclaimer in
148** the documentation and/or other materials provided with the
149** distribution.
150**
151** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
152** the International Astronomical Union nor the names of its
153** contributors may be used to endorse or promote products derived
154** from this software without specific prior written permission.
155**
156** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
157** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
158** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
159** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
160** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
161** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
162** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
163** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
164** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
165** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
166** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
167** POSSIBILITY OF SUCH DAMAGE.
168**
169*/
Note: See TracBrowser for help on using the repository browser.