-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdphregion.m
141 lines (137 loc) · 4.06 KB
/
dphregion.m
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
function varargout=dphregion(th,N,region)
% [phint,thp,php]=DPHREGION(th,N,region)
%
% Finds the longitude crossings at a given colatitude of the coastlines
% of some region that is to be treated as occupying flat Cartesian
% geometry (which is, admittedly, a bit of a limitation).
%
% INPUT:
%
% th Colatitudes where you want it evaluated [degrees]
% N Smoothness of the spline version [only for region strings]
% region A string: 'england', 'africa', 'namerica', 'greenland',
% 'australia', 'eurasia', etc. OR
% A matrix with XY/[lon,lat] coordinates [degrees]
% note that this can be several closed curves separated by
% a row of [NaN NaN]
%
% OUTPUT:
%
% phint Longitudinal interval(s) defining the integration domain
% thp Colatitude matrix for plotting
% php Longitude matrix for plotting
%
% EXAMPLE:
%
% dphregion('demo1',[],'africa')
% dphregion('demo1',[],'contshelves')
% dphregion('demo2',[],'eurasia')
% dphregion('demo2',10)
%
% Last modified by fjsimons-at-alum.mit.edu, 11/11/2023
if ~isstr(th)
% First, get the coordinates of region; these are sorted
defval('N',10);
if isstr(region)
XY=eval(sprintf('%s(%i)',region,N));
else
XY=region;
end
% Input now in colatitude
XY(:,2)=90-XY(:,2);
% Calculate the crossings of XY at th
[phint,thp,php]=phicurve([XY(:,2) XY(:,1)],th);
% Distribute output
varns={phint,thp,php};
varargout=varns(1:nargout);
% AFTER THIS NOTHING BUT DEMOS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
elseif strcmp(th,'demo1')
defval('N',10)
defval('region','africa')
eval(sprintf('%s(%i)',region,N));
eval(sprintf('XY=%s(%i);',region,N));
thN=90-max(XY(:,2));
thS=90-min(XY(:,2));
XY(:,2)=90-XY(:,2);
TH=thN+rand*(thS-thN);
hold on
XL=xlim;
[phint,thp,php]=phicurve([XY(:,2) XY(:,1)],TH);
pl=plot(php,90-thp,'g-','LineW',2);
plot(XL,[90-TH 90-TH],'k--')
p=plot(phint,90-TH,'o');
set(p,'MarkerE','k','MarkerF','y')
t=title(num2str(length(phint)),'FontSize',30);
hold off
if nargout==1
phint=dphi;
end
elseif strcmp(th,'demo2')
% Plot this for the time being
defval('region','africa')
defval('N',200)
ah(2)=subplot(212);
ah(1)=subplot(211);
Nk=100;
eval(sprintf('%s(%i)',region,Nk));
eval(sprintf('XY=%s(%i);',region,Nk));
thN=90-max(XY(:,2));
thS=90-min(XY(:,2));
XY(:,2)=90-XY(:,2);
thetas=linspace(thN,thS,N);
hold on; p=[];
XL=xlim;
for ind=1:length(thetas)
TH=thetas(ind);
l=plot(XL,[90-TH 90-TH],'k-');
% Add eps so it is never zero, essentially
xmth=XY(:,2)-TH;
dsx=diff(sign(xmth));
% Now it can be the one before, or after the crossing, how about
colf=find(dsx);
% This now returns the one on the negative side of the line
colx=colf+(dsx(colf)==-2);
colx2=colx-(dsx(colf)==-2)+(dsx(colf)==2);
L(ind)=length(colx);
if mod(L(ind),2)
warning(sprintf('Cannot find pairs of crossings at th=%8.3f',TH))
end
axes(ah(2))
plot(XY(:,1),xmth,'-+'); hold on; grid on
xls=[min(XY(colx,1))-range(XY(colx,1))/3 ...
max(XY(colx,1))+range(XY(colx,1))/3];
% We want the interpolated point at which it becomes exactly zero
xlim(xls)
plot(xls,[0 0],'k')
ylim([-1 1]/100)
% Then one point was exactly hit, this is the thN case
if all(colx==colx2)
dph=XY([colx(2) colx2(2)]); % This works
else
for ond=1:L(ind)
dph(ond)=interp1(xmth([colx(ond) colx2(ond)]),...
XY([colx(ond) colx2(ond)],1),0,'linear');
end
end
plot(XY(colx,1),xmth(colx),'bs');
plot(XY(colx2,1),xmth(colx2),'rv');
plot(dph,repmat(0,size(dph)),'g*');
clear dph
hold off
for indo=1:length(colx)
axes(ah(1))
p(indo)=plot(XY(colx(indo),1),90-XY(colx(indo),2),'o');
end
if ~isempty(p)
set(p,'MarkerE','k','MarkerF','y')
% t=text(362,58,num2str(L(ind)),'FontSize',30);
t=title(num2str(L(ind)),'FontSize',30);
pause(0.25)
delete([l p t]); p=[];
end
end
hold off
disp(sprintf('Number of uneven crosses %i',sum(mod(L,2))))
else
error('Speficy valid demo')
end