source: trunk/FACT++/sofa/src/jdcalf.c@ 18358

Last change on this file since 18358 was 18346, checked in by tbretz, 11 years ago
File size: 7.2 KB
Line 
1#include "sofa.h"
2
3int iauJdcalf(int ndp, double dj1, double dj2, int iymdf[4])
4/*
5** - - - - - - - - - -
6** i a u 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** This function is part of the International Astronomical Union's
13** SOFA (Standards Of Fundamental Astronomy) software collection.
14**
15** Status: support function.
16**
17** Given:
18** ndp int number of decimal places of days in fraction
19** dj1,dj2 double dj1+dj2 = Julian Date (Note 1)
20**
21** Returned:
22** iymdf int[4] year, month, day, fraction in Gregorian
23** calendar
24**
25** Returned (function value):
26** int status:
27** -1 = date out of range
28** 0 = OK
29** +1 = NDP not 0-9 (interpreted as 0)
30**
31** Notes:
32**
33** 1) The Julian Date is apportioned in any convenient way between
34** the arguments dj1 and dj2. For example, JD=2450123.7 could
35** be expressed in any of these ways, among others:
36**
37** dj1 dj2
38**
39** 2450123.7 0.0 (JD method)
40** 2451545.0 -1421.3 (J2000 method)
41** 2400000.5 50123.2 (MJD method)
42** 2450123.5 0.2 (date & time method)
43**
44** 2) In early eras the conversion is from the "Proleptic Gregorian
45** Calendar"; no account is taken of the date(s) of adoption of
46** the Gregorian Calendar, nor is the AD/BC numbering convention
47** observed.
48**
49** 3) Refer to the function iauJd2cal.
50**
51** 4) NDP should be 4 or less if internal overflows are to be
52** avoided on machines which use 16-bit integers.
53**
54** Called:
55** iauJd2cal JD to Gregorian calendar
56**
57** Reference:
58**
59** Explanatory Supplement to the Astronomical Almanac,
60** P. Kenneth Seidelmann (ed), University Science Books (1992),
61** Section 12.92 (p604).
62**
63** This revision: 2013 June 18
64**
65** SOFA release 2015-02-09
66**
67** Copyright (C) 2015 IAU SOFA Board. See notes at end.
68*/
69{
70 int j, js;
71 double denom, d1, d2, f1, f2, f;
72
73/* Denominator of fraction (e.g. 100 for 2 decimal places). */
74 if ((ndp >= 0) && (ndp <= 9)) {
75 j = 0;
76 denom = pow(10.0, ndp);
77 } else {
78 j = 1;
79 denom = 1.0;
80 }
81
82/* Copy the date, big then small, and realign to midnight. */
83 if (dj1 >= dj2) {
84 d1 = dj1;
85 d2 = dj2;
86 } else {
87 d1 = dj2;
88 d2 = dj1;
89 }
90 d2 -= 0.5;
91
92/* Separate days and fractions. */
93 f1 = fmod(d1, 1.0);
94 f2 = fmod(d2, 1.0);
95 d1 = floor(d1 - f1);
96 d2 = floor(d2 - f2);
97
98/* Round the total fraction to the specified number of places. */
99 f = floor((f1+f2)*denom + 0.5) / denom;
100
101/* Re-assemble the rounded date and re-align to noon. */
102 d2 += f + 0.5;
103
104/* Convert to Gregorian calendar. */
105 js = iauJd2cal(d1, d2, &iymdf[0], &iymdf[1], &iymdf[2], &f);
106 if (js == 0) {
107 iymdf[3] = (int) (f * denom);
108 } else {
109 j = js;
110 }
111
112/* Return the status. */
113 return j;
114
115/*----------------------------------------------------------------------
116**
117** Copyright (C) 2015
118** Standards Of Fundamental Astronomy Board
119** of the International Astronomical Union.
120**
121** =====================
122** SOFA Software License
123** =====================
124**
125** NOTICE TO USER:
126**
127** BY USING THIS SOFTWARE YOU ACCEPT THE FOLLOWING SIX TERMS AND
128** CONDITIONS WHICH APPLY TO ITS USE.
129**
130** 1. The Software is owned by the IAU SOFA Board ("SOFA").
131**
132** 2. Permission is granted to anyone to use the SOFA software for any
133** purpose, including commercial applications, free of charge and
134** without payment of royalties, subject to the conditions and
135** restrictions listed below.
136**
137** 3. You (the user) may copy and distribute SOFA source code to others,
138** and use and adapt its code and algorithms in your own software,
139** on a world-wide, royalty-free basis. That portion of your
140** distribution that does not consist of intact and unchanged copies
141** of SOFA source code files is a "derived work" that must comply
142** with the following requirements:
143**
144** a) Your work shall be marked or carry a statement that it
145** (i) uses routines and computations derived by you from
146** software provided by SOFA under license to you; and
147** (ii) does not itself constitute software provided by and/or
148** endorsed by SOFA.
149**
150** b) The source code of your derived work must contain descriptions
151** of how the derived work is based upon, contains and/or differs
152** from the original SOFA software.
153**
154** c) The names of all routines in your derived work shall not
155** include the prefix "iau" or "sofa" or trivial modifications
156** thereof such as changes of case.
157**
158** d) The origin of the SOFA components of your derived work must
159** not be misrepresented; you must not claim that you wrote the
160** original software, nor file a patent application for SOFA
161** software or algorithms embedded in the SOFA software.
162**
163** e) These requirements must be reproduced intact in any source
164** distribution and shall apply to anyone to whom you have
165** granted a further right to modify the source code of your
166** derived work.
167**
168** Note that, as originally distributed, the SOFA software is
169** intended to be a definitive implementation of the IAU standards,
170** and consequently third-party modifications are discouraged. All
171** variations, no matter how minor, must be explicitly marked as
172** such, as explained above.
173**
174** 4. You shall not cause the SOFA software to be brought into
175** disrepute, either by misuse, or use for inappropriate tasks, or
176** by inappropriate modification.
177**
178** 5. The SOFA software is provided "as is" and SOFA makes no warranty
179** as to its use or performance. SOFA does not and cannot warrant
180** the performance or results which the user may obtain by using the
181** SOFA software. SOFA makes no warranties, express or implied, as
182** to non-infringement of third party rights, merchantability, or
183** fitness for any particular purpose. In no event will SOFA be
184** liable to the user for any consequential, incidental, or special
185** damages, including any lost profits or lost savings, even if a
186** SOFA representative has been advised of such damages, or for any
187** claim by any third party.
188**
189** 6. The provision of any version of the SOFA software under the terms
190** and conditions specified herein does not imply that future
191** versions will also be made available under the same terms and
192** conditions.
193*
194** In any published work or commercial product which uses the SOFA
195** software directly, acknowledgement (see www.iausofa.org) is
196** appreciated.
197**
198** Correspondence concerning SOFA software should be addressed as
199** follows:
200**
201** By email: sofa@ukho.gov.uk
202** By post: IAU SOFA Center
203** HM Nautical Almanac Office
204** UK Hydrographic Office
205** Admiralty Way, Taunton
206** Somerset, TA1 2DN
207** United Kingdom
208**
209**--------------------------------------------------------------------*/
210}
Note: See TracBrowser for help on using the repository browser.