-
Notifications
You must be signed in to change notification settings - Fork 4
/
himawari_nav.f90
414 lines (330 loc) · 12.8 KB
/
himawari_nav.f90
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
!******************************************************************************%
! *
! * Copyright (C) 2016-2019 Simon Proud <[email protected]>
! * License: CC BY-NC-ND 4.0
! *
! ******************************************************************************/
module himawari_navigation
use himawari
use iso_c_binding
use himawari_headerinfo
use omp_lib
implicit none
public :: AHI_Pix2Geo, &
AHI_DefaultNav, &
AHI_Calctime, &
AHI_Solpos
interface
integer(c_int) function get_sza_saa(year,month,day,hour,minute,lat,lon,sza,saa) bind(C, name = 'get_sza_saa')
use iso_c_binding
implicit none
integer(c_int), intent(in), value :: year
integer(c_int), intent(in), value :: month
integer(c_int), intent(in), value :: day
integer(c_int), intent(in), value :: hour
integer(c_int), intent(in), value :: minute
real(c_float), intent(in), value :: lat
real(c_float), intent(in), value :: lon
real(c_float), intent(out) :: sza
real(c_float), intent(out) :: saa
end function get_sza_saa
end interface
contains
integer function AHI_Solpos(year,month,day,hour,minute,lat,lon,sza,saa) result(status)
implicit none
integer, intent(in), value :: year
integer, intent(in), value :: month
integer, intent(in), value :: day
integer, intent(in), value :: hour
integer, intent(in), value :: minute
real(kind=ahi_sreal), intent(in), value :: lat
real(kind=ahi_sreal), intent(in), value :: lon
real(kind=ahi_sreal), intent(out) :: sza
real(kind=ahi_sreal), intent(out) :: saa
integer :: retval
saa = 0.
sza = 0.
retval = 0
retval = get_sza_saa(year,month,day,hour,minute,lat,lon,sza,saa)
if (retval /= 0) then
write(6, *) 'ERROR: get_sza_saa()'
return
end if
if (saa > 360.0) then
saa = him_sreal_fill_value
endif
if (saa < 0.0) then
saa = him_sreal_fill_value
endif
sza = abs(sza)
if (sza > 180.0) then
sza = him_sreal_fill_value
endif
if (sza < -180.0) then
sza = him_sreal_fill_value
endif
status = HIMAWARI_SUCCESS
return
end function AHI_Solpos
integer function AHI_DefaultNav(ahi_navi,him_nav,verbose) result(status)
type(himawari_t_navdata), intent(inout) :: ahi_navi
type(himawari_t_Proj_Info), intent(in) :: him_nav
logical, intent(in) :: verbose
ahi_navi%subLon = him_nav%subLon
ahi_navi%cfac = him_nav%cfac / HIMAWARI_DEGTORAD
ahi_navi%lfac = him_nav%lfac / HIMAWARI_DEGTORAD
ahi_navi%coff = him_nav%coff
ahi_navi%loff = him_nav%loff
ahi_navi%satDis = him_nav%satDis
ahi_navi%eqtrRadius = him_nav%eqtrRadius
ahi_navi%polrRadius = him_nav%polrRadius
ahi_navi%projParam1 = him_nav%projParam1
ahi_navi%projParam2 = him_nav%projParam2
ahi_navi%projParam3 = him_nav%projParam3
ahi_navi%projParamSd = him_nav%projParamSd
status = HIMAWARI_SUCCESS
return
end function AHI_DefaultNav
integer function AHI_Pix2Geo(ahi_main,verbose) result(status)
type(himawari_t_struct), intent(inout) :: ahi_main
logical, intent(in) :: verbose
! real :: Sd,Sn,S1,S2,S3,Sxy
integer :: c1, l1
integer :: xsize, ysize
integer :: cur_x, cur_y
real(kind=ahi_dreal),dimension(:,:),allocatable :: c,l,x,y
real(kind=ahi_dreal),dimension(:,:),allocatable :: Sd,Sn,S1,S2,S3,Sxy
xsize = HIMAWARI_IR_NLINES
ysize = HIMAWARI_IR_NCOLS
allocate(c(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(l(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(x(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(y(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(Sd(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(sn(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(s1(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(s2(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(s3(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
allocate(sxy(ahi_main%ahi_extent%x_size, ahi_main%ahi_extent%y_size))
ahi_main%ahi_data%lat = him_sreal_fill_value
ahi_main%ahi_data%lon = him_sreal_fill_value
do c1=ahi_main%ahi_extent%x_min, ahi_main%ahi_extent%x_max
cur_x = 1 + (c1 - ahi_main%ahi_extent%x_min)
do l1=ahi_main%ahi_extent%y_min, ahi_main%ahi_extent%y_max
cur_y = 1 + (l1 - ahi_main%ahi_extent%y_min)
c(cur_x,cur_y) = dble(c1)
l(cur_x,cur_y) = dble(l1)
enddo
enddo
x = ( c - ahi_main%ahi_navdata%coff) / ( HIMAWARI_SCLUNIT * ahi_main%ahi_navdata%cfac)
y = ( l - ahi_main%ahi_navdata%loff) / ( HIMAWARI_SCLUNIT * ahi_main%ahi_navdata%lfac)
sd = (ahi_main%ahi_navdata%satDis * cos(x) * cos(y)) * (ahi_main%ahi_navdata%satDis * cos(x) * cos(y)) -&
(cos(y) * cos(y) + ahi_main%ahi_navdata%projParam3 * sin(y) * sin(y)) *&
ahi_main%ahi_navdata%projParamSd
sd = sqrt(sd)
sn = (ahi_main%ahi_navdata%satDis * cos(x) * cos(y) -sd) / (cos(y) * cos(y) +&
ahi_main%ahi_navdata%projParam3 * sin(y) * sin(y))
s1 = ahi_main%ahi_navdata%satDis - (sn * cos(x) * cos(y))
s2 = sn * sin(x) * cos(y)
s3 =-sn * sin(y)
sxy=sqrt( s1 * s1 + s2 * s2)
ahi_main%ahi_data%lon = sngl(HIMAWARI_RADTODEG * atan(s2/s1) + ahi_main%ahi_navdata%subLon)
ahi_main%ahi_data%lat = sngl(atan(ahi_main%ahi_navdata%projParam3 * s3 / sxy) * HIMAWARI_RADTODEG)
where (ahi_main%ahi_data%lon > 180.0)
ahi_main%ahi_data%lon = ahi_main%ahi_data%lon-360.0
end where
where (ahi_main%ahi_data%lon < -180.0)
ahi_main%ahi_data%lon = ahi_main%ahi_data%lon+360.0
end where
! ahi_main%ahi_data%lat = ahi_main%ahi_data%lat*-1
! stop
where (ahi_main%ahi_data%lon > 180.0)
ahi_main%ahi_data%lon = him_sreal_fill_value
end where
where (ahi_main%ahi_data%lon < -180.0)
ahi_main%ahi_data%lon = him_sreal_fill_value
end where
where (ahi_main%ahi_data%lat > 90.0)
ahi_main%ahi_data%lat = him_sreal_fill_value
end where
where (ahi_main%ahi_data%lat < -90.0)
ahi_main%ahi_data%lat = him_sreal_fill_value
end where
deallocate(x)
deallocate(y)
deallocate(c)
deallocate(l)
deallocate(sn)
deallocate(sd)
deallocate(s1)
deallocate(s2)
deallocate(s3)
deallocate(sxy)
status = HIMAWARI_SUCCESS
return
end function AHI_Pix2Geo
integer function AHI_Calctime(ahi_main,verbose) result(status)
use omp_lib
type(himawari_t_struct), intent(inout) :: ahi_main
logical, intent(in) :: verbose
integer :: year,month,day,hour,minu,yearp,monthp,retval
real(kind=ahi_dreal) :: a,b,c,d,jd,tfact
integer :: x,y
real(kind=ahi_dreal) :: start_jd,end_jd
integer :: iye,mon,idy,ihr,min,ifail
integer :: iyyy,jy,jm,igreg,ja,ijul
integer :: idint2,n_threads,tnr,i,t
integer :: xmin,ymin,xmax,ymax
real(kind=ahi_dreal) :: julian
real(kind=ahi_sreal) :: sza,saa
real(kind=ahi_sreal) :: sec
real(kind=ahi_sreal) :: doy
real(kind=ahi_sreal), allocatable :: tmparr(:,:)
real(kind=ahi_sreal):: bob1,bob2
parameter (igreg=15+31*(10+12*1582))
sec=0
read(ahi_main%ahi_info%timeslot(1:4),'(i10)')iye
read(ahi_main%ahi_info%timeslot(5:6),'(i10)')mon
read(ahi_main%ahi_info%timeslot(7:8),'(i10)')idy
read(ahi_main%ahi_info%timeslot(9:10),'(i10)')ihr
read(ahi_main%ahi_info%timeslot(11:12),'(i10)')min
if(iye==0.or. iye<-4713) then
ifail=1
return
endif
if(iye<0) then
iyyy=iye+1
else
iyyy=iye
endif
if(mon>2) then
jy=iyyy
jm=mon+1
else
jy=iyyy-1
jm=mon+13
endif
ijul=idint(365.25d0*dble(jy))+idint(30.6001d0*dble(jm))+idy+1720995
if(idy+31*(mon+12*iyyy)>=igreg) then
ja=idint(0.01d0*dble(jy))
ijul=ijul+2-ja+idint(0.25d0*dble(ja))
endif
julian=dble(ijul)+dble(ihr)/24.d0+dble(min)/1440.d0+dble(sec)/86400.d0-0.5d0
tfact = 10.0/(24.0*60.0)
#ifdef _OPENMP
!$omp parallel DO PRIVATE(y)
#endif
do y=ahi_main%ahi_extent%y_min,ahi_main%ahi_extent%y_max
ahi_main%ahi_data%time(:,y - ahi_main%ahi_extent%y_min+1)=julian+tfact*(y/dble(HIMAWARI_IR_NLINES))
enddo
#ifdef _OPENMP
!$omp end parallel do
#endif
xmin = 1
ymin = 1
xmax = ahi_main%ahi_extent%x_max - ahi_main%ahi_extent%x_min + 1
ymax = ahi_main%ahi_extent%y_max - ahi_main%ahi_extent%y_min + 1
if(ahi_main%do_solar_angles .eqv. .true.) then
#ifdef _OPENMP
if (verbose) then
n_threads = omp_get_max_threads()
write(*,*) 'Processing solar geometry using',n_threads,'threads'
endif
!$omp parallel DO PRIVATE(i,x,y,sza,saa,tnr)
#endif
do y=ymin,ymax
do x=xmin,xmax
retval = AHI_Solpos(iye,mon,idy,ihr,min,ahi_main%ahi_data%lat(x,y),ahi_main%ahi_data%lon(x,y),sza,saa)
ahi_main%ahi_data%sza(x,y)=sza
ahi_main%ahi_data%saa(x,y)=saa
enddo
enddo
#ifdef _OPENMP
!$omp end parallel do
#endif
else
ahi_main%ahi_data%sza(:,:) = him_sreal_fill_value
ahi_main%ahi_data%saa(:,:) = him_sreal_fill_value
endif
status = HIMAWARI_SUCCESS
return
end function AHI_Calctime
integer function AHI_calc_satangs(ahi_main,verbose) result(status)
! This function calculates viewing angles for AHI.
! Loosely based on the calculations described here:
! http://celestrak.com/columns/
type(himawari_t_struct), intent(inout):: ahi_main
logical, intent(in) :: verbose
real(kind=ahi_dreal) :: sin_o_lat, sin_o_lon, cos_o_lat, cos_o_lon
real(kind=ahi_dreal) :: obs_x, obs_y, obs_z, obs_alt
real(kind=ahi_dreal) :: sat_x, sat_y, sat_z, sat_alt
real(kind=ahi_dreal) :: del_x, del_y, del_z
real(kind=ahi_dreal) :: a, b, r, satlon, satlat, flatten, ut1
real(kind=ahi_dreal) :: theta_o, theta_s
real(kind=ahi_dreal) :: top_s, top_e, top_z, az_, rg_, el_
real(kind=ahi_dreal) :: c, sq, achcp
integer :: xmin, ymin, xmax, ymax, xpos, ypos
integer :: n_threads
! Navigational data from the Himawari dataset
a = ahi_main%ahi_navdata%eqtrRadius
b = ahi_main%ahi_navdata%polrRadius
r = ahi_main%ahi_navdata%satdis - ahi_main%ahi_navdata%eqtrRadius
satlon = ahi_main%ahi_navdata%subLon
satlat = 0 ! We set this to zero, in reality it varies by <1 degree.
flatten = (a - b) / b
! Here we set the observer to be on the geoid surface
! This could be improved using a DEM or similar
obs_alt = 0
xmin = 1
ymin = 1
xmax = ahi_main%ahi_extent%x_max - ahi_main%ahi_extent%x_min + 1
ymax = ahi_main%ahi_extent%y_max - ahi_main%ahi_extent%y_min + 1
#ifdef _OPENMP
if (verbose) then
n_threads = omp_get_max_threads()
write(*,*) 'Processing viewing geometry using',n_threads,'threads'
endif
!$omp parallel DO PRIVATE(cos_o_lat, sin_o_lat, cos_o_lon, sin_o_lon, ut1, theta_o, c, sq, achcp, sat_x, sat_y, sat_z, obs_x, obs_y, obs_z, del_x, del_y, del_z, top_s, top_e, top_z, az_, rg_, el_)
#endif
do ypos=ymin,ymax
do xpos=xmin,xmax
cos_o_lat = cos(ahi_main%ahi_data%lat(xpos, ypos) * HIMAWARI_DEGTORAD)
sin_o_lat = sin(ahi_main%ahi_data%lat(xpos, ypos) * HIMAWARI_DEGTORAD)
cos_o_lon = cos(ahi_main%ahi_data%lon(xpos, ypos) * HIMAWARI_DEGTORAD)
sin_o_lon = sin(ahi_main%ahi_data%lon(xpos, ypos) * HIMAWARI_DEGTORAD)
ut1 = ahi_main%ahi_data%time(xpos, ypos) / 36525.0
! This calculates observer position
theta_o = 67310.54841 + ut1 * (876600.0 * 3600.0 + 8640184.812866 + ut1 * (0.093104 - ut1 * 6.2 * 10e-6))
theta_o = mod(theta_o / 240.0, 2 * HIMAWARI_PI)
theta_o = mod(theta_o + ahi_main%ahi_data%lon(xpos, ypos) * HIMAWARI_DEGTORAD, 2 * HIMAWARI_PI)
c = 1 / sqrt(1 + flatten * (flatten - 2) * sin_o_lat**2)
sq = c * (1 - flatten)**2
achcp = (a * c + obs_alt) * cos_o_lat
obs_x = achcp * cos(theta_o)
obs_y = achcp * sin(theta_o)
obs_z = (a * sq + obs_alt) * sin_o_lat
! This calculates satellite position
theta_s = 67310.54841 + ut1 * (876600.0 * 3600.0 + 8640184.812866 + ut1 * (0.093104 - ut1 * 6.2 * 10e-6))
theta_s = mod(theta_s / 240.0, 2 * HIMAWARI_PI)
theta_s = mod(theta_s + satlon * HIMAWARI_DEGTORAD, 2 * HIMAWARI_PI)
achcp = ahi_main%ahi_navdata%satdis * cos(satlat)
sat_x = achcp * cos(theta_s)
sat_y = achcp * sin(theta_s)
sat_z = ahi_main%ahi_navdata%satdis * sin(satlat)
del_x = sat_x - obs_x
del_y = sat_y - obs_y
del_z = sat_z - obs_z
top_s = sin_o_lat * cos(theta_o) * del_x + sin_o_lat * sin(theta_o) * del_y - cos_o_lat * del_z
top_e = -sin(theta_o) * del_x + cos(theta_o) * del_y
top_z = cos_o_lat * cos(theta_o) * del_x + cos_o_lat * sin(theta_o) * del_y + sin_o_lat * del_z
az_ = abs(atan2(-top_e, top_s) + HIMAWARI_PI)
rg_ = sqrt(del_x * del_x + del_y * del_y + del_z * del_z)
el_ = asin(top_z / rg_)
ahi_main%ahi_data%vza(xpos, ypos) = 90. - el_ * HIMAWARI_RADTODEG
ahi_main%ahi_data%vaa(xpos, ypos) = az_ * HIMAWARI_RADTODEG
enddo
enddo
status = HIMAWARI_SUCCESS
return
end function AHI_calc_satangs
end module himawari_navigation