OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
leapsecond.c
Go to the documentation of this file.
1 #define LEAPSEC_DAT "leapsec.dat"
2 #define JD_1993 2448988.5
3 #define SECONDS_IN_DAY 86400
4 #define MAX_LEAP_SECONDS 32
5 #define UNIX_TIME_AT_1993 725846400.0
6 
7 static double leap_seconds_tai93[MAX_LEAP_SECONDS] = {0};
8 static double leap_seconds_unix[MAX_LEAP_SECONDS] = {0};
9 
10 #include <timeutils.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 
15  char *varRoot;
16  char leapsecdat[FILENAME_MAX];
17 
18  if (!leap_seconds_tai93[0]) {
19  if ((varRoot = getenv("OCVARROOT")) == NULL) {
20  printf("-E- OCVARROOT environment variable is not defined.\n");
21  exit(EXIT_FAILURE);
22  }
23  snprintf(leapsecdat, FILENAME_MAX, "%s/common/tai-utc.dat", varRoot);
24  FILE *file_h = fopen(leapsecdat, "rb");
25  if (file_h == NULL) {
26  snprintf(leapsecdat, FILENAME_MAX, "%s/viirsn/IETTime.dat", varRoot);
27  file_h = fopen(leapsecdat, "rb");
28  if (file_h == NULL) {
29  snprintf(leapsecdat, FILENAME_MAX, "%s/modis/leapsec.dat", varRoot);
30  file_h = fopen(leapsecdat, "rb");
31  if (file_h == NULL) {
32  printf("-E- %s:%d - leap second file (%s/common/tai-utc.dat) not found.\n", __FILE__, __LINE__, varRoot);
33  exit(EXIT_FAILURE);
34  }
35  }
36  }
37  int i = 0, year;
38  float jd;
39  while (fgetc(file_h) != '\n')
40  ;
41  while (fscanf(file_h, " %d %*s %*d =JD %f%*[^\n]", &year, &jd) == 2) {
42  if (year >= 1993) {
43  leap_seconds_tai93[i] = ((jd - JD_1993) * SECONDS_IN_DAY) + i;
44  leap_seconds_unix[i] = ((jd - JD_1993) * SECONDS_IN_DAY) + UNIX_TIME_AT_1993;
45  i++;
46  if (i >= MAX_LEAP_SECONDS) {
47  printf("-E- %s:%d - max leap seconds reached in %s\n", __FILE__, __LINE__, leapsecdat);
48  exit(EXIT_FAILURE);
49  }
50  }
51  }
52  fclose(file_h);
53  }
54 
55 }
56 
57 
58 int leapseconds_since_1993(double tai93time) {
59 
61 
62  int i;
63  for (i = 0; i < MAX_LEAP_SECONDS - 1 && leap_seconds_tai93[i]; i++) {
64  if (tai93time < leap_seconds_tai93[i]) {
65  return i;
66  }
67  }
68  return i;
69 }
70 
71 int leapseconds_since_1993_unix(double unixtime) {
72 
74 
75  int i;
76  for (i = 0; i < MAX_LEAP_SECONDS - 1 && leap_seconds_unix[i]; i++) {
77  if (unixtime < leap_seconds_unix[i]) {
78  return i;
79  }
80  }
81  return i;
82 }
void init_leapsecond_arrays()
Definition: leapsecond.c:14
function jd(i, j, k)
Definition: jd.f:2
#define NULL
Definition: decode_rs.h:63
#define UNIX_TIME_AT_1993
Definition: leapsecond.c:5
int leapseconds_since_1993_unix(double unixtime)
Definition: leapsecond.c:71
#define JD_1993
Definition: leapsecond.c:2
#define MAX_LEAP_SECONDS
Definition: leapsecond.c:4
int leapseconds_since_1993(double tai93time)
Definition: leapsecond.c:58
#define SECONDS_IN_DAY
Definition: leapsecond.c:3
Definition: jd.py:1
int i
Definition: decode_rs.h:71