-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNasa_Insight_API.R
More file actions
118 lines (86 loc) · 2.9 KB
/
Nasa_Insight_API.R
File metadata and controls
118 lines (86 loc) · 2.9 KB
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
library(jsonlite)
library(ggplot2)
library(readxl)
library(jpeg)
library(grid)
library(png)
nasa_api <- "https://mars.nasa.gov/rss/api/?feed=weather&category=insight&feedtype=json"
insight <- fromJSON(nasa_api, flatten=TRUE)
insight$validity_checks <- NULL
minDay <- min(as.numeric(insight$sol_keys))
maxDay <- max(as.numeric(insight$sol_keys))
insight$sol_keys <- NULL
tempToF <- function(degreesC) {
degreesF <- ((9/5*degreesC)+32)
return(degreesF)
}
# tempToC <- function(degreesF) {
# tryCatch(expr = (degreesC <- ((5/9*degreesF) - 32))
#
# )
#
# return(degreesC)
# }
# `$`(`$`(insight, `87`), `AT`)$`av`
# tempToF(degreesC =insight$`82`$AT$av[1])
countDays <- length(names(insight))
insightdata <- NULL
insightdata$x <- as.numeric(names(insight))
for (j in 1:4) {
at <- NULL
for (i in 1:countDays) {
# message(paste0(j, ":", i))
at2 <- insight[[i]][["AT"]][[j]]
at <- c(at, at2)}
varNames <- names(insight[[1]][["AT"]])
eval(parse(text = paste0("insightdata$y",varNames[j],"<- at")))
# insightdata$y <- at
}
insightdata <- as.data.frame(insightdata)
insightdataC <- insightdata
insightdataF <- as.data.frame(cbind(insightdata$x,
tempToF(insightdata$yav),
tempToF(insightdata$yct),
tempToF(insightdata$ymn),
tempToF(insightdata$ymx)))
names(insightdataF) <- names(insightdataC)
avShow = TRUE
mnShow = TRUE
mxShow = TRUE
# imageParameters ----
z <- "logo.jpg"
img <- readJPEG("logo.jpg")
g <- rasterGrob(img, interpolate = T, gp = gpar(alpha = 0.5))
imgMin <- (minDay + maxDay)/2 - 1
imgMax <- (minDay + maxDay)/2 + 1
tempNeed = "C"
# if (tempNeed == "F") {
# p <- ggplot(data = insightdataF)
# } else {
# p <- ggplot(data = insightdataC)
# }
#
# # Graphing ----
# p <- p +
# {if(avShow) geom_line(mapping = aes(x = x, y = yav),
# size = 2, color = "yellow", alpha = 0.75)} +
# {if(mnShow) geom_line(mapping = aes(x = x, y = ymn),
# size = 2, color = "red", alpha = 0.75)} +
# {if(mxShow) geom_line(mapping = aes(x = x, y = ymx),
# size = 2, color = "blue", alpha =0.75)} +
# theme_minimal() +
# annotation_custom(g, xmin=imgMin, xmax=imgMax, ymin=-50, ymax=-25)+
# xlab("Earth Day") +
# ylab("Temperature in degrees chosen") +
# ggtitle("Air Temperature") +
# # scale_y_continuous(minor_breaks = seq(-20, 0, 1)) +
# # scale_x_continuous(minor_breaks = seq(80, 100, 1)) +
# theme(title = element_text(hjust = 0),
# plot.title = element_text(hjust = 0.5),
# axis.title = element_text(face = "bold", size = 12),
# axis.text = element_text(size = 15, angle = 45, face = "bold" ),
# panel.background = element_rect(fill = "maroon"))
#
# print(p)
# Json Output ----
write_json(insight,path = paste0("InsightOutput.", Sys.Date(),"-min,", minDay,"-max,",maxDay, ".json"))