-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNASA power extract climatology nc file.R
242 lines (180 loc) · 7.8 KB
/
NASA power extract climatology nc file.R
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#POWER_Global_Climatology Temperature tmax----
library(ncdf4)
library(reshape2)
library(dplyr)
library(ncdf4.helpers)
# Clear workspace
rm(list=ls())
ncpath <- "~/Dropbox/Labour & climate change/Data/climate/"
ncname <- "POWER_Global_Climatology_5c8af4ff"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
ncin <- nc_open(ncfname)
print(ncin)
nc<- nc_open(ncfname)
print(nc)
attributes(nc)$names
print(paste("The file has",nc$nvars,"variables,",nc$ndims,"dimensions and",nc$natts,"NetCDF attributes"))
attributes(nc$var)$names
pr_mean <- ncvar_get(nc, attributes(nc$var)$names[2])
dim(pr_mean)
attributes(nc$dim)$names
nc_lat <- ncvar_get( nc, attributes(nc$dim)$names[2])
nc_lon <- ncvar_get( nc, attributes(nc$dim)$names[1])
print(paste(dim(nc_lat), "latitudes and", dim(nc_lon), "longitudes"))
dim(pr_mean)
pr_mean[35:36, 0:3, 1:5]
nc_time <- ncvar_get( nc, attributes(nc$dim)$names[3])
nc_close(nc)
# set the dimension names and values of your matrix to the appropriate latitude and longitude values
dimnames(pr_mean) <- list(lon=nc_lon, lat=nc_lat, time=nc_time)
tmp_pr_df <- melt(pr_mean, id=c("lon", "lat", "time"),value.name = "maxtemp")
Egypt<- subset(tmp_pr_df, tmp_pr_df$lat>=22 & tmp_pr_df$lat<=32 & tmp_pr_df$lon>=24 & tmp_pr_df$lon<=37)
library(reshape2)
Egypt<- dcast(Egypt, lon + lat ~ time, value.var="maxtemp")
library("writexl")
write_xlsx(Egypt,"Egypt monthly longtmax 1984-1-1 2013-12-31.xlsx")
Tunisia<- subset(tmp_pr_df, tmp_pr_df$lat>=30 & tmp_pr_df$lat<=38 & tmp_pr_df$lon>=7 & tmp_pr_df$lon<=12)
library(reshape2)
Tunisia<- dcast(Tunisia, lon + lat ~ time, value.var="maxtemp")
library("writexl")
write_xlsx(Tunisia,"Tunisia monthly longtmax 1984-1-1 2013-12-31.xlsx")
Jordan<- subset(tmp_pr_df, tmp_pr_df$lat>=29 & tmp_pr_df$lat<=34 & tmp_pr_df$lon>=34 & tmp_pr_df$lon<=40)
library(reshape2)
Jordan<- dcast(Jordan, lon + lat ~ time, value.var="maxtemp")
library("writexl")
write_xlsx(Jordan,"Jordan monthly longtmax 1984-1-1 2013-12-31.xlsx")
#POWER_Global_Climatology Temperature average temperature----
library(ncdf4)
library(reshape2)
library(dplyr)
library(ncdf4.helpers)
# Clear workspace
rm(list=ls())
ncpath <- "~/Dropbox/Labour & climate change/Data/climate/"
ncname <- "POWER_Global_Climatology_5c8af4ff"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
ncin <- nc_open(ncfname)
print(ncin)
nc<- nc_open(ncfname)
print(nc)
attributes(nc)$names
print(paste("The file has",nc$nvars,"variables,",nc$ndims,"dimensions and",nc$natts,"NetCDF attributes"))
attributes(nc$var)$names
pr_mean <- ncvar_get(nc, attributes(nc$var)$names[3])
dim(pr_mean)
attributes(nc$dim)$names
nc_lat <- ncvar_get( nc, attributes(nc$dim)$names[2])
nc_lon <- ncvar_get( nc, attributes(nc$dim)$names[1])
print(paste(dim(nc_lat), "latitudes and", dim(nc_lon), "longitudes"))
dim(pr_mean)
pr_mean[35:36, 0:3, 1:5]
nc_time <- ncvar_get( nc, attributes(nc$dim)$names[3])
nc_close(nc)
# set the dimension names and values of your matrix to the appropriate latitude and longitude values
dimnames(pr_mean) <- list(lon=nc_lon, lat=nc_lat, time=nc_time)
tmp_pr_df <- melt(pr_mean, id=c("lon", "lat", "time"),value.name = "maxtemp")
Egypt<- subset(tmp_pr_df, tmp_pr_df$lat>=22 & tmp_pr_df$lat<=32 & tmp_pr_df$lon>=24 & tmp_pr_df$lon<=37)
library(reshape2)
Egypt<- dcast(Egypt, lon + lat ~ time, value.var="maxtemp")
library("writexl")
write_xlsx(Egypt,"Egypt monthly longaveragetemp 1984-1-1 2013-12-31.xlsx")
Tunisia<- subset(tmp_pr_df, tmp_pr_df$lat>=30 & tmp_pr_df$lat<=38 & tmp_pr_df$lon>=7 & tmp_pr_df$lon<=12)
library(reshape2)
Tunisia<- dcast(Tunisia, lon + lat ~ time, value.var="maxtemp")
library("writexl")
write_xlsx(Tunisia,"Tunisia monthly longaveragetemp 1984-1-1 2013-12-31.xlsx")
Jordan<- subset(tmp_pr_df, tmp_pr_df$lat>=29 & tmp_pr_df$lat<=34 & tmp_pr_df$lon>=34 & tmp_pr_df$lon<=40)
library(reshape2)
Jordan<- dcast(Jordan, lon + lat ~ time, value.var="maxtemp")
library("writexl")
write_xlsx(Jordan,"Jordan monthly longaveragetemp 1984-1-1 2013-12-31.xlsx")
#POWER_Global_Climatology Temperature precipitation----
library(ncdf4)
library(reshape2)
library(dplyr)
library(ncdf4.helpers)
# Clear workspace
rm(list=ls())
ncpath <- "~/Dropbox/Labour & climate change/Data/climate/"
ncname <- "POWER_Global_Climatology_6f8988d8"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
ncin <- nc_open(ncfname)
print(ncin)
nc<- nc_open(ncfname)
print(nc)
attributes(nc)$names
print(paste("The file has",nc$nvars,"variables,",nc$ndims,"dimensions and",nc$natts,"NetCDF attributes"))
attributes(nc$var)$names
pr_mean <- ncvar_get(nc, attributes(nc$var)$names[2])
dim(pr_mean)
attributes(nc$dim)$names
nc_lat <- ncvar_get( nc, attributes(nc$dim)$names[2])
nc_lon <- ncvar_get( nc, attributes(nc$dim)$names[1])
print(paste(dim(nc_lat), "latitudes and", dim(nc_lon), "longitudes"))
dim(pr_mean)
pr_mean[35:36, 0:3, 1:5]
nc_time <- ncvar_get( nc, attributes(nc$dim)$names[3])
nc_close(nc)
# set the dimension names and values of your matrix to the appropriate latitude and longitude values
dimnames(pr_mean) <- list(lon=nc_lon, lat=nc_lat, time=nc_time)
tmp_pr_df <- melt(pr_mean, id=c("lon", "lat", "time"),value.name = "precip")
Egypt<- subset(tmp_pr_df, tmp_pr_df$lat>=22 & tmp_pr_df$lat<=32 & tmp_pr_df$lon>=24 & tmp_pr_df$lon<=37)
library(reshape2)
Egypt<- dcast(Egypt, lon + lat ~ time, value.var="precip")
library("writexl")
write_xlsx(Egypt,"Egypt monthly longprecip 1984-1-1 2013-12-31.xlsx")
Tunisia<- subset(tmp_pr_df, tmp_pr_df$lat>=30 & tmp_pr_df$lat<=38 & tmp_pr_df$lon>=7 & tmp_pr_df$lon<=12)
library(reshape2)
Tunisia<- dcast(Tunisia, lon + lat ~ time, value.var="precip")
library("writexl")
write_xlsx(Tunisia,"Tunisia monthly longprecip 1984-1-1 2013-12-31.xlsx")
Jordan<- subset(tmp_pr_df, tmp_pr_df$lat>=29 & tmp_pr_df$lat<=34 & tmp_pr_df$lon>=34 & tmp_pr_df$lon<=40)
library(reshape2)
Jordan<- dcast(Jordan, lon + lat ~ time, value.var="precip")
library("writexl")
write_xlsx(Jordan,"Jordan monthly longprecip 1984-1-1 2013-12-31.xlsx")
#POWER_Global_Climatology Temperature humidity----
library(ncdf4)
library(reshape2)
library(dplyr)
library(ncdf4.helpers)
# Clear workspace
rm(list=ls())
ncpath <- "~/Dropbox/Labour & climate change/Data/climate/"
ncname <- "POWER_Global_Climatology_6f8988d8"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
ncin <- nc_open(ncfname)
print(ncin)
nc<- nc_open(ncfname)
print(nc)
attributes(nc)$names
print(paste("The file has",nc$nvars,"variables,",nc$ndims,"dimensions and",nc$natts,"NetCDF attributes"))
attributes(nc$var)$names
pr_mean <- ncvar_get(nc, attributes(nc$var)$names[1])
dim(pr_mean)
attributes(nc$dim)$names
nc_lat <- ncvar_get( nc, attributes(nc$dim)$names[2])
nc_lon <- ncvar_get( nc, attributes(nc$dim)$names[1])
print(paste(dim(nc_lat), "latitudes and", dim(nc_lon), "longitudes"))
dim(pr_mean)
pr_mean[35:36, 0:3, 1:5]
nc_time <- ncvar_get( nc, attributes(nc$dim)$names[3])
nc_close(nc)
# set the dimension names and values of your matrix to the appropriate latitude and longitude values
dimnames(pr_mean) <- list(lon=nc_lon, lat=nc_lat, time=nc_time)
tmp_pr_df <- melt(pr_mean, id=c("lon", "lat", "time"),value.name = "RH2M")
Egypt<- subset(tmp_pr_df, tmp_pr_df$lat>=22 & tmp_pr_df$lat<=32 & tmp_pr_df$lon>=24 & tmp_pr_df$lon<=37)
library(reshape2)
Egypt<- dcast(Egypt, lon + lat ~ time, value.var="RH2M")
library("writexl")
write_xlsx(Egypt,"Egypt monthly longRH2M 1984-1-1 2013-12-31.xlsx")
Tunisia<- subset(tmp_pr_df, tmp_pr_df$lat>=30 & tmp_pr_df$lat<=38 & tmp_pr_df$lon>=7 & tmp_pr_df$lon<=12)
library(reshape2)
Tunisia<- dcast(Tunisia, lon + lat ~ time, value.var="RH2M")
library("writexl")
write_xlsx(Tunisia,"Tunisia monthly longRH2M 1984-1-1 2013-12-31.xlsx")
Jordan<- subset(tmp_pr_df, tmp_pr_df$lat>=29 & tmp_pr_df$lat<=34 & tmp_pr_df$lon>=34 & tmp_pr_df$lon<=40)
library(reshape2)
Jordan<- dcast(Jordan, lon + lat ~ time, value.var="RH2M")
library("writexl")
write_xlsx(Jordan,"Jordan monthly longRH2M 1984-1-1 2013-12-31.xlsx")