forked from scottransom/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal2mjd.c
78 lines (73 loc) · 1.99 KB
/
cal2mjd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double slaCldj(int iy, int im, int id, int *j);
/* Does not include jump discontinuities from leap seconds. */
/* I don't really know if it should ! */
#ifdef USEDMALLOC
#include "dmalloc.h"
#endif
int main(int argc, char *argv[])
{
int year, month = 1, day = 1, hour = 0, min = 0, err;
double MJD, fracday, sec = 0.0;
if (argc < 2) {
printf("\nUsage: 'cal2mjd YYYY MM DD HH MM SS.SSSSS'\n\n");
exit(1);
}
year = strtol(argv[1], NULL, 10);
if (argc > 2) {
month = strtol(argv[2], NULL, 10);
if (month < 1 || month > 12) {
printf("\nmonth = %d is out-of-range.\n", month);
exit(1);
}
}
if (argc > 3) {
day = strtol(argv[3], NULL, 10);
if (day < 1 || day > 31) {
printf("\nday = %d is out-of-range.\n", month);
exit(1);
}
}
if (argc > 4) {
hour = strtol(argv[4], NULL, 10);
if (hour == 24)
hour = 0;
if (hour < 0 || hour > 23) {
printf("\nhour = %d is out-of-range.\n", month);
exit(1);
}
}
if (argc > 5) {
min = strtol(argv[5], NULL, 10);
if (min < 0 || min > 59) {
printf("\nmin = %d is out-of-range.\n", month);
exit(1);
}
}
if (argc > 6) {
sec = strtod(argv[6], NULL);
if (sec < 0.0 || sec >= 60.0) {
printf("\nsec = %d is out-of-range.\n", month);
exit(1);
}
}
fracday = (hour + (min + (sec / 60.0)) / 60.0) / 24.0;
MJD = slaCldj(year, month, day, &err);
MJD += fracday;
if (err == 1) {
printf("\nTry again. Bad year.\n\n");
exit(1);
}
if (err == 2) {
printf("\nTry again. Bad month.\n\n");
exit(1);
}
if (err == 3) {
printf("\nTry again. Bad day.\n\n");
exit(1);
}
printf("\nMJD is %17.11f\n\n", MJD);
exit(0);
}