-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGong.jl
203 lines (172 loc) · 6.27 KB
/
Gong.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
module Gong
using Statistics, Dates
#export gc_distance, rad2deg, deg2rad, histc, meshgrid, nan, findNaNmin, findNaNmax
function gc_distance(lat1deg::Float64,lon1deg::Float64,lat2deg::Float64,lon2deg::Float64)
# This code implements Vincenty 1975: https://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
# In the future, need to implement: Karney 2013 https://arxiv.org/abs/1109.4448
a = 6378.1370; # Earth semi major axis in km
b = 6356.7523142; # Earth semi-minor axis in km
R1 = 1/3 * (2*a + b);
f = (a - b) / a; # flattening parameter for an elipsoid
#f = 1/298.257223563; # defined by WGS84
lat1 = deg2rad(lat1deg);
lon1 = deg2rad(lon1deg);
lat2 = deg2rad(lat2deg);
lon2 = deg2rad(lon2deg);
beta1 = atan((1-f)*tan(lat1)); # reduced latitude 1
beta2 = atan((1-f)*tan(lat2)); # reduced latitude 2
P = (beta1 + beta2)/2;
Q = (beta2 - beta1)/2;
lambda = abs(lon1 - lon2);
sigma = atan(sqrt((cos(beta2)*sin(lambda))^2 + (cos(beta1)*sin(beta2) - sin(beta1)*cos(beta2)*cos(lambda))^2) / (sin(beta1)*sin(beta2) + cos(beta1)*cos(beta2)*cos(lambda)));
alpha = asin(cos(beta1) * cos(beta2) * sin(lambda)/sin(sigma));
sigmam = acos(cos(sigma) - 2*sin(beta1)*sin(beta2) / cos(alpha)^2)/2;
X = (sigma - sin(sigma)) * (sin(P)^2 * cos(Q)^2) / (cos(sigma/2)^2)
Y = (sigma + sin(sigma)) * (cos(P)^2 * sin(Q)^2) / (sin(sigma/2)^2)
dist = a*(sigma - f/2 * (X+Y));
return dist
end
function rad2deg(rad)
return rad * 180.0/pi;
end
function deg2rad(deg)
return deg * pi/180.0;
end
# define a histogram index function similar to matlab histc
function histc(x, xnodes)
bin = Array{Number}(undef,length(x));
for i = 1:length(x)
a = findlast(x[i] .>= xnodes);
if !isnothing(a)
bin[i] = a;
end
end
return bin;
end
# emulate the behavior of Matlab's meshgrid
function meshgrid(xgrid::Array{<:AbstractFloat,1},ygrid::Array{<:AbstractFloat,1})
nx = length(xgrid);
ny = length(ygrid);
minx = minimum(xgrid);
maxx = maximum(xgrid);
miny = minimum(ygrid);
maxy = maximum(ygrid);
dx = (maxx .- minx) ./ (nx-1);
dy = (maxy .- miny) ./ (ny-1);
i = [i for j in miny:dy:maxy, i in minx:dx:maxx];
j = [j for j in miny:dy:maxy, i in minx:dx:maxx];
return (i,j)
end
function meshgrid(xgrid::Array{<:Integer,1},ygrid::Array{<:Integer,1})
minx = minimum(xgrid);
maxx = maximum(xgrid);
miny = minimum(ygrid);
maxy = maximum(ygrid);
i = [i for j in miny:maxy, i in minx:maxx];
j = [j for j in miny:maxy, i in minx:maxx];
return (i,j)
end
function meshgrid(xgrid::UnitRange{<:Integer},ygrid::UnitRange{<:Integer})
#minx = minimum(xgrid);
#maxx = maximum(xgrid);
#miny = minimum(ygrid);
#maxy = maximum(ygrid);
i = [i for j in ygrid[1]:ygrid[end], i in xgrid[1]:xgrid[end]];
j = [j for j in ygrid[1]:ygrid[end], i in xgrid[1]:xgrid[end]];
return (i,j)
end
# emulate the behavior of Matlab's nan function
function nan(m::Unsigned,n::Unsigned)
nanarray=Array{Float64,2}(undef,m,n);
nanarray .= NaN;
end
# adopted from https://github.com/mlubin/NaNMath.jl/issues/31
function findNaNmax(x::Array{<:AbstractFloat,1})
result = convert(eltype(x), NaN)
indmax = 0
@inbounds @simd for i in eachindex(x)
v = x[i]
if !isnan(v)
if (isnan(result) || v > result)
result = v
indmax = i
end
end
end
return (result,indmax) # note the order of result and ind may be different that what's proposed by floswald as of 2019-12-03
end
# adopted from https://github.com/mlubin/NaNMath.jl/issues/31
function findNaNmin(x::Array{<:AbstractFloat,1})
result = convert(eltype(x), NaN)
indmin = 0
@inbounds @simd for i in eachindex(x)
v = x[i]
if !isnan(v)
if (isnan(result) || v < result)
result = v
indmin = i
end
end
end
return (result,indmin) # note the order of result and ind may be different that what's proposed by floswald as of 2019-12-03
end
function dg_pol2cart(magnitude::AbstractFloat, compassdir::AbstractFloat)
theta = mod.(360.0 .- compassdir .+ 90.0, 360) .* pi/180.0;
u = magnitude .* cos.(theta);
v = magnitude .* sin.(theta);
return u + v*im
end
# this does not work, left in for reference. can you 'repeat' function with splat or Tuple() to reproduce Matlab's repmat behavior
#function repmat(x, shape)
# xsize = size(x);
# xout = reshape(repeat(x, outer=prod(shape)),[collect(size(x));collect(shape)]...);
# return xout;
#end
# linearly scale between xmin and xmax using the range provided by xscl
function minmaxscaler(x, xscl; zeromin::Bool=false)
if zeromin == false
xmin = minimum(skipmissing(x));
else
xmin = 0.0;
end
xmax = maximum(skipmissing(x));
if xmin != xmax
x_std = (x .- xmin) ./ (xmax - xmin);
x_scl = x_std .* (maximum(xscl) - minimum(xscl)) .+ minimum(xscl);
else
x_scl = x;
end
return x_scl, xmin, xmax;
end
# linearly scale between xmin and xmax using the range provided in xscl
function minmaxscaler(x, xscl, xminmax)
xmin = minimum(skipmissing(xminmax));
xmax = maximum(skipmissing(xminmax));
if minimum(skipmissing(x)) != maximum(skipmissing(x))
x_std = (x .- xmin) ./ (xmax - xmin);
x_scl = x_std .* (maximum(xscl) - minimum(xscl)) .+ minimum(xscl);
else
x_scl = x;
end
return x_scl, xmin, xmax;
end
# find standard deviation of a variable, then scale to +/- n std. dev.
function varscaler(varin,nstd=2.0)
varstd = std(skipmissing(varin));
varmean = mean(skipmissing(varin));
varscl = (varin .- varmean) ./ (nstd * varstd);
return varscl, varmean, varstd, nstd;
end
# manually scale the input variable using specified mean and standard deviation
function varscaler_manual(varin, varmean, varstd, nstd=2.0)
return (varin .- varmean) ./ (nstd * varstd);
end
# use specified mean and standard deviation to 'unscale' the input variable
function varunscaler(varscl, varmean, varstd, nstd)
return varscl .* (nstd * varstd) .+ varmean;
end
# this function returns the indices for stock within a specified time period
function findtind(tt,trange)
return findall(minimum(trange) .<= tt .<= maximum(trange));
end
end