source: codes/icosagcm/trunk/src/spherical_geom.f90 @ 153

Last change on this file since 153 was 153, checked in by dubos, 11 years ago

Schmidt transform

File size: 4.9 KB
Line 
1MODULE spherical_geom_mod
2USE genmod
3
4
5
6CONTAINS
7
8
9
10
11SUBROUTINE lonlat2xyz(lon,lat,xyz)
12IMPLICIT NONE
13  REAL(rstd),INTENT(IN) :: lon
14  REAL(rstd),INTENT(IN) :: lat
15  REAL(rstd),INTENT(OUT) :: xyz(3)
16 
17  xyz(1)=cos(lon)*cos(lat)
18  xyz(2)=sin(lon)*cos(lat)
19  xyz(3)=sin(lat)
20
21END SUBROUTINE lonlat2xyz
22
23
24SUBROUTINE xyz2lonlat(xyz,lon,lat)
25IMPLICIT NONE
26  REAL(rstd),INTENT(IN) :: xyz(3)
27  REAL(rstd),INTENT(OUT) :: lon
28  REAL(rstd),INTENT(OUT) :: lat
29 
30  REAL(rstd) :: coslat
31  REAL(rstd) :: xyzn(3)
32 
33  xyzn(:)=xyz(:)/sqrt(sum(xyz(:)**2))
34 
35  lat=asin(xyzn(3))
36  lon=atan2(xyzn(2),xyzn(1))
37END SUBROUTINE xyz2lonlat
38
39! lat/lon with respect to a displaced pole (rotated basis) :
40!  ( cos(lon0)*sin(lat0), sin(lon0)*sin(lat0), -cos(lat0))
41!  (-sin(lon0),           cos(lon0),           0)
42!  ( cos(lon0)*cos(lat0), sin(lon0)*cos(lat0), sin(lat0))
43
44SUBROUTINE lonlat2xyz_relative(lon,lat,lon0,lat0, xyz)
45IMPLICIT NONE
46  REAL(rstd),INTENT(IN) :: lon0, lat0, lon,lat
47  REAL(rstd),INTENT(OUT) :: xyz(3)
48  REAL(rstd) :: xx,yy,zz
49  xx = cos(lon)*cos(lat)
50  yy = sin(lon)*cos(lat)
51  zz = sin(lat)
52  xyz(1) = cos(lon0)*(sin(lat0)*xx+cos(lat0)*zz)-sin(lon0)*yy
53  xyz(2) = sin(lon0)*(sin(lat0)*yy+cos(lat0)*zz)+cos(lon0)*yy
54  xyz(3) = sin(lat0)*zz-cos(lat0)*xx
55END SUBROUTINE lonlat2xyz_relative
56
57SUBROUTINE xyz2lonlat_relative(xyz,lon0,lat0, lon,lat)
58IMPLICIT NONE
59  REAL(rstd),INTENT(IN) :: xyz(3), lon0, lat0
60  REAL(rstd),INTENT(OUT) :: lon,lat
61  REAL(rstd) :: xx,yy,zz
62  xx = sin(lat0)*(xyz(1)*cos(lon0)+xyz(2)*sin(lon0))-cos(lat0)*xyz(3)
63  yy = xyz(2)*cos(lon0)-xyz(1)*sin(lon0)
64  zz = cos(lat0)*(xyz(1)*cos(lon0)+xyz(2)*sin(lon0))+sin(lat0)*xyz(3)
65  lon = atan2(yy,xx)
66  lat = asin(zz)
67END SUBROUTINE xyz2lonlat_relative
68
69
70
71SUBROUTINE dist_cart(A,B,d)
72USE vector
73IMPLICIT NONE
74  REAL(rstd),INTENT(IN)  :: A(3)
75  REAL(rstd),INTENT(IN)  :: B(3)
76  REAL(rstd),INTENT(OUT) :: d
77 
78   REAL(rstd)  :: n(3)
79   CALL cross_product2(A,B,n)
80   d=asin(sqrt(sum(n**2)))
81
82END SUBROUTINE dist_cart
83
84
85SUBROUTINE dist_lonlat(lonA,latA,lonB,latB,d)
86IMPLICIT NONE
87  REAL(rstd),INTENT(IN)  :: lonA
88  REAL(rstd),INTENT(IN)  :: latA
89  REAL(rstd),INTENT(IN)  :: lonB
90  REAL(rstd),INTENT(IN)  :: latB
91  REAL(rstd),INTENT(OUT) :: d
92 
93  d=acos(MAX(MIN(sin(latA)*sin(latB)+cos(latA)*cos(latB)*cos(lonA-lonB),1.),-1.))
94 
95END SUBROUTINE dist_lonlat
96
97SUBROUTINE surf_triangle(A,B,C,surf)
98  REAL(rstd),INTENT(IN)  :: A(3)
99  REAL(rstd),INTENT(IN)  :: B(3)
100  REAL(rstd),INTENT(IN)  :: C(3)
101  REAL(rstd),INTENT(OUT) :: Surf
102
103  REAL(rstd)  :: AB,AC,BC
104  REAL(rstd)  :: s,x
105 
106  CALL dist_cart(A,B,AB)
107  CALL dist_cart(A,C,AC)
108  CALL dist_cart(B,C,BC)
109 
110  s=(AB+AC+BC)/2
111  x=tan(s/2) * tan((s-AB)/2)  * tan((s-AC)/2) * tan((s-BC)/2)
112  IF (x<0) x=0.
113  surf=4*atan(sqrt( x))
114 
115END SUBROUTINE surf_triangle 
116
117
118SUBROUTINE div_arc(A,B,frac,C)
119IMPLICIT NONE
120  REAL(rstd),INTENT(IN)  :: A(3)
121  REAL(rstd),INTENT(IN)  :: B(3)
122  REAL(rstd),INTENT(IN)  :: frac
123  REAL(rstd),INTENT(OUT)  :: C(3)
124 
125  REAL(rstd) :: d
126  REAL(rstd) :: M(3,3)
127  REAL(rstd) :: alpha(3,3)
128  INTEGER    :: IPIV(3)
129  INTEGER    :: info
130  REAL(rstd) :: xa,xb,xc
131  REAL(rstd) :: ya,yb,yc
132  REAL(rstd) :: za,zb,zc
133  REAL(rstd) :: alpha_A,alpha_B,alpha_C
134  REAL(rstd) :: x,y,z
135  REAL(rstd) :: a1,a2,a3
136  REAL(rstd) :: b1,b2,b3
137 
138 
139  xa=A(1) ; ya=A(2) ; za=A(3)
140  xb=B(1) ; yb=B(2) ; zb=B(3)
141
142  CALL dist_cart(A,B,d)
143
144  C(1)=cos(frac*d) 
145  C(2)=cos((1-frac)*d)
146  C(3)=0.
147
148  xc=ya*zb-yb*za ; yc=-(xa*zb-xb*za) ; zc=xa*yb-xb*ya
149 
150  M(1,1)=xa ; M(1,2)=ya ; M(1,3)=za
151  M(2,1)=xb ; M(2,2)=yb ; M(2,3)=zb
152  M(3,1)=xc ; M(3,2)=yc ; M(3,3)=zc
153  stop 'STOP'
154!  CALL DGESV(3,1,M,3,IPIV,C,3,info)
155 
156END SUBROUTINE div_arc
157
158SUBROUTINE div_arc_bis(A,B,frac,C)
159IMPLICIT NONE
160  REAL(rstd),INTENT(IN)  :: A(3)
161  REAL(rstd),INTENT(IN)  :: B(3)
162  REAL(rstd),INTENT(IN)  :: frac
163  REAL(rstd),INTENT(OUT)  :: C(3)
164 
165   C=A*(1-frac)+B*frac 
166   C=C/sqrt(sum(C**2))
167END SUBROUTINE div_arc_bis
168
169
170  SUBROUTINE circumcenter(A0,B0,C0,Center)
171  USE vector
172  IMPLICIT NONE
173    REAL(rstd), INTENT(IN)  :: A0(3),B0(3),C0(3)
174    REAL(rstd), INTENT(OUT) :: Center(3)
175   
176    REAL(rstd)  :: a(3),b(3),c(3)
177   
178    a=A0/sqrt(sum(A0**2))
179    b=B0/sqrt(sum(B0**2))
180    c=C0/sqrt(sum(C0**2))
181   
182    CALL Cross_product2(b-a,c-b,center)
183    center=center/sqrt(sum(center**2))
184   
185  END SUBROUTINE circumcenter
186   
187
188  SUBROUTINE compute_centroid(points,n,centr)
189  USE vector
190  IMPLICIT NONE
191    INTEGER :: n
192    REAL(rstd), INTENT(IN)  :: points(3,n)
193    REAL(rstd), INTENT(OUT) :: Centr(3)
194   
195    REAL(rstd) :: p1(3),p2(3),cross(3)
196    REAL(rstd) :: norm_cross
197    INTEGER :: i,j
198     
199      Centr(:)=0
200      DO i=1,n
201        j=MOD(i,n)+1
202        p1=points(:,i)/norm(points(:,i))
203        p2=points(:,j)/norm(points(:,j))
204        CALL cross_product2(p1,p2,cross)
205        norm_cross=norm(cross)
206        if (norm_cross<1e-10) CYCLE
207       
208        Centr(:)=centr(:)+asin(norm_cross)*cross(:)/norm_cross
209      ENDDO
210     
211      Centr(:)=centr(:)/norm(centr(:))
212 
213  END SUBROUTINE compute_centroid
214
215END MODULE spherical_geom_mod
216
217
Note: See TracBrowser for help on using the repository browser.