updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / var / da / da_radiance / amsr2time_.c
blob5c612be850ca34f220ea01d0dab363c377336331
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
6 typedef struct{
7 double tai93sec;
8 short year;
9 short month;
10 short day;
11 short hour;
12 short minute;
13 short second;
14 short ms;
15 short reserve;
16 } AM2_COMMON_SCANTIME;
18 void amsr2time_(int *num, double *tai93, AM2_COMMON_SCANTIME *st){
19 // structure for leap second
20 typedef struct{
21 short year;
22 short month;
23 double tai93sec;
24 } LEAP_SECOND;
26 // fixed value
27 char *fn = "leapsec.dat"; // leap second file name
29 // interface variable
30 int i, j; // loop variable
31 char *ret; // return status
32 char buf[512]; // text buffer
33 short ibuf; // integer buffer
34 double rbuf; // real buffer
35 FILE *hnd; // file handle
37 // leap second data
38 short lnum; // number of leap second from 1993
39 LEAP_SECOND *ldat; // leap second data from 1993
41 // leap second working variable
42 int lcnt; // leap second count
43 int flag; // strike flag
44 time_t utime; // unix epoch time
45 struct tm *stmp; // struct tm pointer
47 // open
48 hnd = fopen(fn, "r");
49 if(hnd == NULL){
50 printf("amsr2time: leap second file leapsec.dat open error\n");
51 exit(1);
54 // count leap second entry
55 lnum = 0;
56 while(1){
57 // read
58 ret = fgets(buf, 512, hnd);
60 // end check
61 if(ret == NULL) break;
63 // check & count
64 if(buf[0] != '/'){
65 sscanf(buf, "%hd", &ibuf);
66 if(ibuf >= 1993){
67 ++lnum;
72 // read leap second data
73 ldat = malloc(sizeof(LEAP_SECOND) * lnum);
74 rewind(hnd);
75 i = 0;
76 while(1){
77 // read
78 ret = fgets(buf, 512, hnd);
80 // end check
81 if(ret == NULL) break;
83 // check & count
84 if(buf[0] != '/'){
85 sscanf(buf, "%hd", &ibuf);
86 if(ibuf >= 1993){
87 sscanf(buf, "%hd %hd %lf %lf %lf %lf"
88 , &ldat[i].year
89 , &ldat[i].month
90 , &rbuf
91 , &rbuf
92 , &rbuf
93 , &ldat[i].tai93sec
95 // printf("amsr2time: year=%4d month=%2d tai93sec=%14.2lf\n"
96 // , ldat[i].year
97 // , ldat[i].month
98 // , ldat[i].tai93sec
99 // );
100 ++i;
104 // printf("amsr2time: number of leap second = %d\n", lnum);
106 // close
107 fclose(hnd);
109 // convert
110 for(i = 0; i < *num; ++i){
111 // negative value is warning
112 if(tai93[i] < 0){
113 printf("amsr2time: negative value warning: "
114 "%14.2lf (scan_by_1origin=%04d)\n", tai93[i],i+1);
115 st[i].tai93sec = tai93[i];
116 st[i].year = 0;
117 st[i].month = 0;
118 st[i].day = 0;
119 st[i].hour = 0;
120 st[i].minute = 0;
121 st[i].second = 0;
122 st[i].ms = 0;
123 st[i].reserve = 0;
124 continue;
127 // check leap second & strike
128 lcnt = 0;
129 flag = 0;
130 for(j = lnum - 1; j >= 0; --j){
131 if(tai93[i] >= ldat[j].tai93sec){
132 lcnt = j + 1;
133 break;
135 else if(tai93[i] >= ldat[j].tai93sec - 1){
136 lcnt = j;
137 flag = 1;
138 break;
142 // convert unix epoch time
143 utime = tai93[i] + 725846400 - lcnt;
145 // convert struct tm
146 stmp = gmtime(&utime);
148 // store result in AM2_COMMON_SCANTIME
149 st[i].tai93sec = tai93[i];
150 st[i].year = stmp->tm_year + 1900;
151 st[i].month = stmp->tm_mon + 1;
152 st[i].day = stmp->tm_mday;
153 st[i].hour = stmp->tm_hour;
154 st[i].minute = stmp->tm_min;
155 st[i].second = stmp->tm_sec;
156 st[i].ms = (int)((tai93[i] - (long int)tai93[i]) * 1000 + 0.5);
157 st[i].reserve = 0;
159 // deal for strike
160 if(flag){
161 // convert unix epoch time
162 utime = tai93[i] + 725846400 - lcnt - 1;
164 // convert struct tm
165 stmp = gmtime(&utime);
167 // store result in AM2_COMMON_SCANTIME
168 st[i].tai93sec = tai93[i];
169 st[i].year = stmp->tm_year + 1900;
170 st[i].month = stmp->tm_mon + 1;
171 st[i].day = stmp->tm_mday;
172 st[i].hour = stmp->tm_hour;
173 st[i].minute = stmp->tm_min;
174 st[i].second = 60;
175 st[i].ms = (tai93[i] - (long int)tai93[i]) * 1000 + 0.5;
176 st[i].reserve = 0;
180 // free
181 free(ldat);