/
extract.jl
274 lines (213 loc) · 7.89 KB
/
extract.jl
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
RegionGrid(
geo :: GeoRegion,
lon :: Union{Vector{<:Real},AbstractRange{<:Real},
lat :: Union{Vector{<:Real},AbstractRange{<:Real}
) -> RectGrid, PolyGrid
Creates a `RectGrid` or `PolyGrid` type based on the following arguments. This method is suitable for rectilinear grids of longitude/latitude output such as from Isca, or from satellite and reanalysis gridded datasets.
Arguments
=========
- `geo` : A GeoRegion of interest
- `lon` : A vector or `AbstractRange` containing the longitude points
- `lat` : A vector or `AbstractRange` containing the latitude points
"""
RegionGrid(
geo::RectRegion, lon::Vector{<:Real}, lat::Vector{<:Real}
) = RectGrid(geo,lon,lat)
RegionGrid(
geo::PolyRegion, lon::Vector{<:Real}, lat::Vector{<:Real}
) = PolyGrid(geo,lon,lat)
RegionGrid(
geo::RectRegion, lon::AbstractRange{<:Real}, lat::AbstractRange{<:Real}
) = RectGrid(geo,collect(lon),collect(lat))
RegionGrid(
geo::PolyRegion, lon::AbstractRange{<:Real}, lat::AbstractRange{<:Real}
) = PolyGrid(geo,collect(lon),collect(lat))
"""
RegionGrid(
geo :: GeoRegion,
lon :: Array{<:Real,2},
lat :: Array{<:Real,2}
) -> RegionGrid
Creates a `RegionMask` type based on the following arguments. This method is more suitable for non-rectilinear grids of longitude and latitude points, such as in the output of WRF or CESM.
Arguments
=========
- `geo` : A GeoRegion of interest
- `lon` : An array containing the longitude points
- `lat` : An array containing the latitude points
"""
function RegionGrid(
geo :: GeoRegion,
lon :: Array{<:Real,2},
lat :: Array{<:Real,2}
)
@info "$(modulelog()) - Creating a RegionMask for the $(geo.name) GeoRegion based on an array of longitude and latitude points"
if eltype(lon) <: AbstractFloat
FT = eltype(lon)
end
if size(lon) != size(lat)
error("$(modulelog()) - The size of the longitude and latitude arrays are not the same.")
end
mask = zeros(size(lon))
for ii in eachindex(lon)
ipnt = Point2(lon[ii],lat[ii])
if isinGeoRegion(ipnt,geo,throw=false)
mask[ii] = 1
else; mask[ii] = NaN
end
end
return RegionMask{FT}(lon,lat,mask)
end
function RectGrid(
geo :: RectRegion,
lon :: Vector{<:Real},
lat :: Vector{<:Real}
)
@info "$(modulelog()) - Creating a RegionGrid for the $(geo.name) GeoRegion"
@debug "$(modulelog()) - Determining indices of longitude and latitude boundaries in the given dataset ..."
N = geo.N
S = geo.S
E = geo.E
W = geo.W
igrid = regiongrid([N,S,E,W],lon,lat);
iN = igrid[1]; iS = igrid[2]; iE = igrid[3]; iW = igrid[4]
nlon = deepcopy(lon)
@info "$(modulelog()) - Creating vector of latitude indices to extract ..."
if iN < iS; iNS = iN : iS
elseif iS < iN; iNS = iS : iN
else; iNS = iN;
end
@info "$(modulelog()) - Creating vector of longitude indices to extract ..."
if iW < iE; iWE = vcat(iW:iE)
elseif iW > iE || (iW == iE && bounds[3] != bounds[4])
iWE = vcat(iW:length(lon),1:iE); nlon[1:(iW-1)] .+= 360
else; iWE = [iW];
end
while maximum(nlon) > 360; nlon .-= 360 end
nlon = nlon[iWE]
nlat = lat[iNS]
while maximum(nlon) > geo.E; nlon .-= 360 end
while minimum(nlon) < geo.W; nlon .+= 360 end
return RectGrid{eltype(lon)}(igrid,nlon,nlat,iWE,iNS)
end
function PolyGrid(
geo :: PolyRegion,
lon :: Vector{<:Real},
lat :: Vector{<:Real};
FT = Float64
)
@info "$(modulelog()) - Creating a RegionGrid for the $(geo.name) GeoRegion"
@debug "$(modulelog()) - Determining indices of longitude and latitude boundaries in the given dataset ..."
N = geo.N
S = geo.S
E = geo.E
W = geo.W
if eltype(lon) <: AbstractFloat
FT = eltype(lon)
end
igrid = regiongrid([N,S,E,W],lon,lat);
iN = igrid[1]; iS = igrid[2]; iE = igrid[3]; iW = igrid[4]
nlon = deepcopy(lon)
@info "$(modulelog()) - Creating vector of latitude indices to extract ..."
if iN < iS; iNS = vcat(iN:iS)
elseif iS < iN; iNS = vcat(iS:iN)
else; iNS = [iN];
end
@info "$(modulelog()) - Creating vector of longitude indices to extract ..."
if iW < iE; iWE = vcat(iW:iE)
elseif iW > iE || (iW == iE && bounds[3] != bounds[4])
iWE = vcat(iW:length(lon),1:iE); nlon[1:(iW-1)] .+= 360
else; iWE = [iW];
end
while maximum(nlon) > 360; nlon .-= 360 end
@info "$(modulelog()) - Since the $(geo.name) GeoRegion is a PolyRegion, we need to defined a mask as well ..."
nlon = nlon[iWE]
nlat = lat[iNS]
mask = Array{FT,2}(undef,length(nlon),length(nlat))
for ilat in eachindex(nlat), ilon in eachindex(nlon)
ipnt = Point2(nlon[ilon],nlat[ilat])
if isinGeoRegion(ipnt,geo,throw=false)
mask[ilon,ilat] = 1
else; mask[ilon,ilat] = NaN
end
end
while maximum(nlon) > geo.E; nlon .-= 360 end
while minimum(nlon) < geo.W; nlon .+= 360 end
return PolyGrid{FT}(igrid,nlon,nlat,iWE,iNS,mask)
end
function regionpoint(
plon::Real, plat::Real,
rlon::Vector{<:Real}, rlat::Vector{<:Real}
)
plon = mod(plon,360); ispointinregion(plon,plat,rlon,rlat); rlon = mod.(rlon,360);
ilon = argmin(abs.(rlon.-plon)); ilat = argmin(abs.(rlat.-plat));
return [ilon,ilat]
end
function regiongrid(gridbounds::Vector{<:Real},rlon::Vector{<:Real},rlat::Vector{<:Real})
N,S,E,W = gridbounds; isgridinregion(gridbounds,rlon,rlat)
if rlon[2] > rlon[1]; EgW = true; else; EgW = false end
if rlat[2] > rlat[1]; NgS = true; else; NgS = false end
E = mod(E,360); W = mod(W,360); nlon = mod.(rlon,360);
iN = argmin(abs.(rlat.-N)); iS = argmin(abs.(rlat.-S)); iW = argmin(abs.(nlon.-W));
if E == W;
if gridbounds[3] != gridbounds[4]
if iW != 1; iE = iW - 1; else; iE = length(nlon); end
else
if EgW
if iW > 1; iE = iW - 1; else; iE = length(rlon) end
else
if iW < length(rlon); iE = iW + 1; else; iE = 1 end
end
end
else; iE = argmin(abs.(nlon.-E));
end
if !(E==W) || (gridbounds[3] == gridbounds[4])
while mod(rlon[iW],360) < mod(W,360)
if EgW
iW += 1; if iW > length(rlon); iW = 1 end
else
iW -= 1; if iszero(iW); iW = length(rlon) end
end
end
while mod(rlon[iE],360) > mod(E,360)
if EgW
iE -= 1; if iszero(iE); iE = length(rlon) end
else
iE += 1; if iE > length(rlon); iE = 1 end
end
end
end
while rlat[iS] < S
if NgS; iS += 1; else; iS -= 1 end
end
while rlat[iN] > N
if NgS; iN -= 1; else; iN += 1 end
end
if (NgS && (iN < iS)) || (!NgS && (iS < iN))
error("$(modulelog()) - The bounds of the specified georegion do not contain any latitude points")
end
# Figure out how to ensure that bounds are respected
# if (EgW && (iE < iW)) || (!EgW && (iW < iE))
# error("$(modulelog()) - The bounds of the specified georegion do not contain any longitude points")
# end
return [iN,iS,iE,iW]
end
function regioninfo(gridbounds::Vector{<:Real},rlon::Vector{<:Real},rlat::Vector{<:Real})
iN,iS,iE,iW = regiongrid(gridbounds,rlon,rlat);
nlon = deepcopy(rlon)
if iN < iS; iNS = vcat(iN:iS)
elseif iS < iN; iNS = vcat(iS:iN)
else; iNS = [iN];
end
if iW < iE; iWE = vcat(iW:iE)
elseif iW > iE || (iW == iE && bounds[3] != bounds[4])
iWE = vcat(iW:length(lon),1:iE); nlon[1:(iW-1)] .+= 360
else; iWE = [iW];
end
while maximum(nlon); nlon .-= 360 end
nlon = nlon[iWE]
nlat = lat[iNS]
while maximum(nlon) > geo.E; nlon .-= 360 end
while minimum(nlon) < geo.W; nlon .+= 360 end
return RectGrid{FT}(igrid,iWE,iNS,nlon,nlat)
end