aHimawariFile.cxx 3.65 KB
Newer Older
RKGarcia's avatar
RKGarcia committed
1
2
3
#include <ctime>
#include <cstring>
#include <strings.h>
4
5
6
#include <stdint.h>
#include <stdlib.h> // size_t, offset_t

R. Keoni Garcia's avatar
R. Keoni Garcia committed
7
#include "HimawariHSDFile.hxx"
Scott Mindock's avatar
Scott Mindock committed
8
9
#include "HCastFile.hxx"

R. Keoni Garcia's avatar
R. Keoni Garcia committed
10
11
12
13
14
#include "erfa.h" // eraJd2cal()
using namespace std;

// factory function. 
aHimawariFile *aHimawariFile::attach(const char *pathname, const struct HimawariSentinels *optional_sentinels) {
Scott Mindock's avatar
Scott Mindock committed
15

Scott Mindock's avatar
Scott Mindock committed
16
17
18

    aHimawariFile *ret=NULL;
	ret = attachHCast(pathname, optional_sentinels);
19
	if (ret != 0)
Scott Mindock's avatar
Scott Mindock committed
20
21
		return ret;

R. Keoni Garcia's avatar
R. Keoni Garcia committed
22
	// for now, just attempt til we succeed
Scott Mindock's avatar
Scott Mindock committed
23
	ret = attachHSD(pathname, optional_sentinels);
24
	if (ret != 0)
R. Keoni Garcia's avatar
R. Keoni Garcia committed
25
		return ret;
26
	// add other file types here
Scott Mindock's avatar
Scott Mindock committed
27
28


R. Keoni Garcia's avatar
R. Keoni Garcia committed
29
30
31
32
	return 0;
}


R. Keoni Garcia's avatar
R. Keoni Garcia committed
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
int aHimawariFile::lineCount() const {
    intpair lr = lineRange();
    return (lr.second - lr.first + 1);
}


HimawariBandType aHimawariFile::bandType() const {
    return standardHimawariBandType(bandNumber());
}

HimawariBandType standardHimawariBandType(int band) {
    switch(band) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
            return AHI_BAND_ALBEDO;
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
        case 13:
        case 14:
        case 15:
        case 16:
            return AHI_BAND_BRIGHTNESS_TEMP;
        default:
            return AHI_BAND_UNKNOWN;
    }
}


R. Keoni Garcia's avatar
R. Keoni Garcia committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
// 
// Time support routines 
//
*/

const HimawariTime NULL_TIME = { 
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    NAN,
    NAN
};


// We use ERFA library to convert MJD to Gregorian calendar date
// see erfa.c: eraJd2cal() 
const double MJD_BASE = 2400000.5; 



static time_t dateToUnix(int year, int month, int day, int seconds_into_day,
                         int &hour, int &minute, int &second, int &doy) {
    struct tm inp;
    bzero(&inp, sizeof(inp));
    inp.tm_year = year>=1900? year - 1900 : year;
    inp.tm_mon = month - 1;
    inp.tm_mday = day;
    inp.tm_yday = 0; // note this is 0-based in C
    inp.tm_isdst = 0;
    inp.tm_zone = 0;
    inp.tm_gmtoff = 0;
    
    time_t when = timegm(&inp) + seconds_into_day;

    gmtime_r(&when, &inp);
    doy = inp.tm_yday + 1;
    hour = inp.tm_hour;
    minute = inp.tm_min;
    second = inp.tm_sec;

    return when;
}

// ref http://tycho.usno.navy.mil/mjd.html
// MJD = JD - 2400000.5
// http://pds-rings.seti.org/toolkits/julian_133_html/aareadme.html
// test pattern at 
//   http://aa.usno.navy.mil/data/docs/JulianDate.php
// Add 2400000.5 (MJD_BASE) to MJD to get JD
// FIXME: review whether this adequately answers for leap seconds!
// Test pattern:
//  MJD=56772.0 ./ahi_scene
//  [2014-04-25T00:00:00.000000Z (2014.115) (U 1398384000.000000) (MJD 56772.000000)]
//  MJD=56772.7 ./ahi_scene
//  [2014-04-25T16:48:00.000000Z (2014.115) (U 1398444480.000000) (MJD 56772.700000)]
//  MJD=56483.1 ./ahi_scene
//  [2013-07-10T02:24:00.000000Z (2013.191) (U 1373423040.000000) (MJD 56483.100000)]
struct HimawariTime mjdToSceneTime(double mjd) {
    HimawariTime zult = NULL_TIME;
    double day_fraction = NAN;
    int rc = eraJd2cal(MJD_BASE, mjd, 
        &zult.year, &zult.month, &zult.day, &day_fraction);
    if (rc!=0)
        return NULL_TIME;
    
    double seconds = day_fraction * 86400.0 + 0.0000005; // seconds within the calendar day, intent to round to microseconds
    double intpart_seconds, fracpart_seconds;    
    fracpart_seconds = modf(seconds, &intpart_seconds);
    zult.microsecond = int(fracpart_seconds * 1000000.0);
    
    time_t when = dateToUnix(zult.year, zult.month, zult.day, int(intpart_seconds),
                                zult.hour, zult.minute, zult.second, zult.doy);
    zult.unix_time = double(when) + fracpart_seconds;    
    zult.mjd = mjd;
    return zult;
}