-
Notifications
You must be signed in to change notification settings - Fork 2
/
epic.go
133 lines (117 loc) · 3.18 KB
/
epic.go
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
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
package nasa
import (
"encoding/json"
"fmt"
"strings"
)
const (
epicAPIURL = "https://api.nasa.gov/EPIC"
epicImageURLFormat = "%s/archive/%s/%s/%s/%s/%s/%s.%s?api_key=%s"
)
// EPICImage represents an image from the Earth Polychromatic Imaging Camera.
type EPICImage struct {
Date EPICDate `json:"date"`
Identifier string `json:"identifier"`
Caption string `json:"caption"`
Image string `json:"image"`
Version string `json:"version"`
Coords struct {
Centroid LatLon `json:"centroid_coordinates"`
Dscovr XYZ `json:"dscovr_j2000_position"`
Lunar XYZ `json:"lunar_j2000_position"`
Sun XYZ `json:"sun_j2000_position"`
Attitude Quaternions `json:"attitude_quaternions"`
} `json:"coords"`
URL struct {
Natural string
Enhanced string
Thumb struct {
Natural string
Enhanced string
} `json:"-"`
} `json:"-"`
}
// EPIC gets a response from the Earth Polychromatic Imaging Camera.
func EPIC(p ParamEncoder) (EPICImages, error) {
params, ok := p.(*EPICParams)
if !ok {
return EPICImages{}, ErrorParamsMismatch
}
query, err := p.Encode()
if err != nil {
return EPICImages{}, err
}
url := fmt.Sprintf("%s/%s", epicAPIURL, query)
content, err := getContent(url, nil)
if err != nil {
return EPICImages{}, err
}
images := EPICImages{}
err = json.Unmarshal(content, &images)
if err != nil {
return EPICImages{}, err
}
images.buildURLs(params)
return images, nil
}
// EPICImages is an array of pointers to EPICImage.
type EPICImages []*EPICImage
func (images EPICImages) buildURLs(p *EPICParams) {
for _, epic := range images {
epic.buildNaturalURLs(p)
epic.buildEnhancedURLs(p)
}
}
// Full: https://api.nasa.gov/EPIC/archive/natural/2020/04/24/png/epic_1b_20200424002712.png?api_key=DEMO_KEY
// Thumb: https://api.nasa.gov/EPIC/archive/natural/2020/04/24/thumbs/epic_1b_20200424002712.jpg?api_key=DEMO_KEY
func (e *EPICImage) buildNaturalURLs(p *EPICParams) {
e.URL.Natural = fmt.Sprintf(epicImageURLFormat,
epicAPIURL,
"natural",
e.Date.Format("2006"), // Year
e.Date.Format("01"), // Month
e.Date.Format("02"), // Day
"png",
e.Image,
"png",
p.APIKey,
)
e.URL.Thumb.Natural = fmt.Sprintf(epicImageURLFormat,
epicAPIURL,
"natural",
e.Date.Format("2006"), // Year
e.Date.Format("01"), // Month
e.Date.Format("02"), // Day
"thumbs",
e.Image,
"jpg",
p.APIKey,
)
}
// Full: https://api.nasa.gov/EPIC/archive/enhanced/2020/04/24/png/epic_RGB_20200424002712.png?api_key=DEMO_KEY
// Thumb: https://api.nasa.gov/EPIC/archive/enhanced/2020/04/24/thumbs/epic_RGB_20200424002712.jpg?api_key=DEMO_KEY
func (e *EPICImage) buildEnhancedURLs(p *EPICParams) {
enhancedID := strings.Replace(e.Image, "_1b_", "_RGB_", 1)
e.URL.Enhanced = fmt.Sprintf(epicImageURLFormat,
epicAPIURL,
"enhanced",
e.Date.Format("2006"), // Year
e.Date.Format("01"), // Month
e.Date.Format("02"), // Day
"png",
enhancedID,
"png",
p.APIKey,
)
e.URL.Thumb.Enhanced = fmt.Sprintf(epicImageURLFormat,
epicAPIURL,
"enhanced",
e.Date.Format("2006"), // Year
e.Date.Format("01"), // Month
e.Date.Format("02"), // Day
"thumbs",
enhancedID,
"jpg",
p.APIKey,
)
}