-
Notifications
You must be signed in to change notification settings - Fork 3
/
seviri_neural_net.F90
426 lines (374 loc) · 18.6 KB
/
seviri_neural_net.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
414
415
416
417
418
419
420
421
422
423
424
425
426
!------------------------------------------------------------------------------
! Name: seviri_neural_net.F90
!
! Purpose:
! Module for SEVIRI neural network cloud detection and cloud phase
! determination. Neural network prediction is done in Python using
! the Keras library with Tensorflow or Theano backends. This module
! establishes an interface to a C layer from which the Python neural
! network module is called. Neural net input (radiances and auxuiliary
! data) are passed to the Python module through the C interface. The
! predicted field is then passed back to this module.
!
! History:
! 2020/07/20, DP: Initial version
! 2020/09/30, DP: Moved assignment of NN results to Fortran arrays to C.
! Fixed memory leak. Major Simplifications + improvements
! + cleanup. Revised subroutine header.
!
! Bugs:
! None known.
!------------------------------------------------------------------------------
module seviri_neural_net_m
use iso_c_binding
implicit none
interface
! interface to C function py_ann_cot_cph
subroutine py_ann_cma(vis006, vis008, nir016, ir039, &
ir062, ir073, ir087, ir108, ir120, &
ir134, lsm, skt, solzen, satzen, nx, ny, reg_cot, &
bin_cot, unc_cot, msg_index, &
undo_true_reflectances) bind(C, name="py_ann_cma")
import :: c_ptr
import :: c_int
import :: c_float
import :: c_signed_char
import :: c_bool
type(c_ptr), value :: vis006
type(c_ptr), value :: vis008
type(c_ptr), value :: nir016
type(c_ptr), value :: ir039
type(c_ptr), value :: ir062
type(c_ptr), value :: ir073
type(c_ptr), value :: ir087
type(c_ptr), value :: ir108
type(c_ptr), value :: ir120
type(c_ptr), value :: ir134
type(c_ptr), value :: lsm
type(c_ptr), value :: skt
type(c_ptr), value :: solzen
type(c_ptr), value :: satzen
real(c_float), dimension(*), intent(out) :: reg_cot, unc_cot
integer(c_signed_char), dimension(*), intent(out) :: bin_cot
integer(c_int) :: nx, ny
integer(c_signed_char) :: msg_index
logical(c_bool) :: undo_true_reflectances
end subroutine py_ann_cma
! interface to C function py_ann_cot_cph
subroutine py_ann_cph(vis006, vis008, nir016, ir039, &
ir062, ir073, ir087, ir108, ir120, &
ir134, lsm, skt, solzen, satzen, nx, ny, &
reg_cph, bin_cph, unc_cph, cldmask, msg_index, &
undo_true_reflectances) bind(C, name="py_ann_cph")
import :: c_ptr
import :: c_int
import :: c_float
import :: c_signed_char
import :: c_bool
type(c_ptr), value :: vis006
type(c_ptr), value :: vis008
type(c_ptr), value :: nir016
type(c_ptr), value :: ir039
type(c_ptr), value :: ir062
type(c_ptr), value :: ir073
type(c_ptr), value :: ir087
type(c_ptr), value :: ir108
type(c_ptr), value :: ir120
type(c_ptr), value :: ir134
type(c_ptr), value :: lsm
type(c_ptr), value :: skt
type(c_ptr), value :: solzen
type(c_ptr), value :: satzen
type(c_ptr), value :: cldmask
real(c_float), dimension(*), intent(out) :: reg_cph, unc_cph
integer(c_signed_char), dimension(*), intent(out) :: bin_cph
integer(c_int) :: nx, ny
integer(c_signed_char) :: msg_index
logical(c_bool) :: undo_true_reflectances
end subroutine py_ann_cph
! interface to C function py_ann_ctp
subroutine py_ann_ctp(vis006, vis008, nir016, ir039, &
ir062, ir073, ir087, ir108, ir120, &
ir134, lsm, skt, solzen, satzen, nx, ny, ctp, &
ctp_unc, cldmask, msg_index, &
undo_true_reflectances) bind(C, name="py_ann_ctp")
import :: c_ptr
import :: c_int
import :: c_float
import :: c_signed_char
import :: c_bool
type(c_ptr), value :: vis006
type(c_ptr), value :: vis008
type(c_ptr), value :: nir016
type(c_ptr), value :: ir039
type(c_ptr), value :: ir062
type(c_ptr), value :: ir073
type(c_ptr), value :: ir087
type(c_ptr), value :: ir108
type(c_ptr), value :: ir120
type(c_ptr), value :: ir134
type(c_ptr), value :: lsm
type(c_ptr), value :: skt
type(c_ptr), value :: solzen
type(c_ptr), value :: satzen
type(c_ptr), value :: cldmask
real(c_float), dimension(*), intent(out) :: ctp, ctp_unc
integer(c_int) :: nx, ny
integer(c_signed_char) :: msg_index
logical(c_bool) :: undo_true_reflectances
end subroutine py_ann_ctp
! interface to C function py_ann_ctt
subroutine py_ann_ctt(vis006, vis008, nir016, ir039, &
ir062, ir073, ir087, ir108, ir120, &
ir134, lsm, skt, solzen, satzen, nx, ny, ctt, &
ctt_unc, cldmask, msg_index, &
undo_true_reflectances) bind(C, name="py_ann_ctt")
import :: c_ptr
import :: c_int
import :: c_float
import :: c_signed_char
import :: c_bool
type(c_ptr), value :: vis006
type(c_ptr), value :: vis008
type(c_ptr), value :: nir016
type(c_ptr), value :: ir039
type(c_ptr), value :: ir062
type(c_ptr), value :: ir073
type(c_ptr), value :: ir087
type(c_ptr), value :: ir108
type(c_ptr), value :: ir120
type(c_ptr), value :: ir134
type(c_ptr), value :: lsm
type(c_ptr), value :: skt
type(c_ptr), value :: solzen
type(c_ptr), value :: satzen
type(c_ptr), value :: cldmask
real(c_float), dimension(*), intent(out) :: ctt, ctt_unc
integer(c_int) :: nx, ny
integer(c_signed_char) :: msg_index
logical(c_bool) :: undo_true_reflectances
end subroutine py_ann_ctt
! interface to C function py_ann_ctt
subroutine py_ann_cbh(ir108, ir120, ir134, solzen, satzen, nx, &
ny, cbh, cbh_unc, cldmask) bind(C, name="py_ann_cbh")
import :: c_ptr
import :: c_int
import :: c_float
import :: c_char
import :: c_bool
type(c_ptr), value :: ir108
type(c_ptr), value :: ir120
type(c_ptr), value :: ir134
type(c_ptr), value :: solzen
type(c_ptr), value :: satzen
type(c_ptr), value :: cldmask
real(c_float), dimension(*), intent(out) :: cbh, cbh_unc
integer(c_int) :: nx, ny
end subroutine py_ann_cbh
! interface to C function py_ann_mlay
subroutine py_ann_mlay(vis006, vis008, nir016, ir039, &
ir062, ir073, ir087, ir108, ir120, &
ir134, lsm, skt, solzen, satzen, nx, ny, &
mlay_reg, mlay_bin, mlay_unc, cldmask, msg_index, &
undo_true_reflectances) bind(C, name="py_ann_mlay")
import :: c_ptr
import :: c_int
import :: c_float
import :: c_signed_char
import :: c_bool
type(c_ptr), value :: vis006
type(c_ptr), value :: vis008
type(c_ptr), value :: nir016
type(c_ptr), value :: ir039
type(c_ptr), value :: ir062
type(c_ptr), value :: ir073
type(c_ptr), value :: ir087
type(c_ptr), value :: ir108
type(c_ptr), value :: ir120
type(c_ptr), value :: ir134
type(c_ptr), value :: lsm
type(c_ptr), value :: skt
type(c_ptr), value :: solzen
type(c_ptr), value :: satzen
type(c_ptr), value :: cldmask
real(c_float), dimension(*), intent(out) :: mlay_reg, mlay_unc
integer(c_signed_char), dimension(*), intent(out) :: mlay_bin
integer(c_int) :: nx, ny
integer(c_signed_char) :: msg_index
logical(c_bool) :: undo_true_reflectances
end subroutine py_ann_mlay
end interface
contains
!------------------------------------------------------------------------------
! Name: seviri_ann_cph_cot
!
! Purpose:
! Subroutine accepting neural network input data from ORAC which are
! passed to C and the Python neural network subsequently. Calls the C
! interface function
!
! Arguments:
! Name Type I/O Description
!------------------------------------------------------------------------------
! nx int In Dimension in x direction
! ny int In Dimension in y direction
! vis006 2darr In SEVIRI VIS006 measurements
! vis008 2darr In SEVIRI VIS008 measurements
! nir016 2darr In SEVIRI NIR016 measurements
! ir039 2darr In SEVIRI IR039 measurements
! ir062 2darr In SEVIRI IR062 measurements
! ir073 2darr In SEVIRI IR073 measurements
! ir087 2darr In SEVIRI IR087 measurements
! ir108 2darr In SEVIRI IR108 measurements
! ir120 2darr In SEVIRI IR120 measurements
! ir134 2darr In SEVIRI IR134 measurements
! lsm 2darr In Land-sea mask
! skt 2darr In Skin temperature
! solzen 2darr In Solar Zenith Angle
! regression_cot 2darr Out COT NN regression value
! binary_cot 2darr Out COT binary value after thresholding (CMA)
! uncertainty_cot 2darr Out COT uncertainty of CMA
! regression_cph 2darr Out CPH NN regression value
! binary_cph 2darr Out CPH binary value after thresholding
! uncertainty_cph 2darr Out CPH uncertainty of CPH
!------------------------------------------------------------------------------
subroutine seviri_ann_cma(nx, ny, vis006, vis008, nir016, ir039, ir062, ir073, &
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen, &
regression_cot, binary_cot, uncertainty_cot, &
msg_index, undo_true_reflectances)
use iso_c_binding
! output arrays
real(c_float), intent(out) :: regression_cot(:,:), uncertainty_cot(:,:)
integer(c_signed_char), intent(out) :: binary_cot(:,:)
! C-types
integer(c_int) :: nx ,ny
integer(c_signed_char) :: msg_index
real(c_float), dimension(nx,ny), target :: vis006, vis008, nir016, ir039, &
& ir062, ir073, ir087, ir108, &
& ir120, ir134, skt, solzen, &
& satzen
integer(c_signed_char), dimension(nx,ny), target :: lsm
logical(kind=1) :: undo_true_reflectances
! Call Python neural network via Python C-API
call py_ann_cma(c_loc(vis006(1,1)), c_loc(vis008(1,1)), c_loc(nir016(1,1)), &
c_loc(ir039(1,1)), c_loc(ir062(1,1)), c_loc(ir073(1,1)), &
c_loc(ir087(1,1)), c_loc(ir108(1,1)), c_loc(ir120(1,1)), &
c_loc(ir134(1,1)), c_loc(lsm(1,1)), c_loc(skt(1,1)), &
c_loc(solzen(1,1)), c_loc(satzen(1,1)), nx, ny, &
regression_cot, binary_cot, uncertainty_cot, &
msg_index, undo_true_reflectances)
end subroutine seviri_ann_cma
subroutine seviri_ann_cph(nx, ny, vis006, vis008, nir016, ir039, ir062, ir073, &
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen, &
regression_cph, binary_cph, uncertainty_cph, cldmask, &
msg_index, undo_true_reflectances)
use iso_c_binding
! output arrays
real(c_float), intent(out) :: regression_cph(:,:), uncertainty_cph(:,:)
integer(c_signed_char), intent(out) :: binary_cph(:,:)
! C-types
integer(c_int) :: nx ,ny
integer(c_signed_char) :: msg_index
real(c_float), dimension(nx,ny), target :: vis006, vis008, nir016, ir039, &
& ir062, ir073, ir087, ir108, &
& ir120, ir134, skt, solzen, &
& satzen
integer(c_signed_char), dimension(nx,ny), target :: lsm, cldmask
logical(kind=1) :: undo_true_reflectances
! Call Python neural network via Python C-API
call py_ann_cph(c_loc(vis006(1,1)), c_loc(vis008(1,1)), c_loc(nir016(1,1)), &
c_loc(ir039(1,1)), c_loc(ir062(1,1)), c_loc(ir073(1,1)), &
c_loc(ir087(1,1)), c_loc(ir108(1,1)), c_loc(ir120(1,1)), &
c_loc(ir134(1,1)), c_loc(lsm(1,1)), c_loc(skt(1,1)), &
c_loc(solzen(1,1)), c_loc(satzen(1,1)), &
nx, ny, regression_cph, binary_cph, uncertainty_cph, &
c_loc(cldmask(1,1)), msg_index, undo_true_reflectances)
end subroutine seviri_ann_cph
subroutine seviri_ann_ctp(nx, ny, vis006, vis008, nir016, ir039, ir062, ir073, &
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen, &
ctp, ctp_unc, cldmask, msg_index, undo_true_reflectances)
use iso_c_binding
! output arrays
real(c_float), intent(out) :: ctp(:,:), ctp_unc(:,:)
! C-types
integer(c_int) :: nx ,ny
integer(c_signed_char) :: msg_index
real(c_float), dimension(nx,ny), target :: vis006, vis008, nir016, ir039, &
& ir062, ir073, ir087, ir108, &
& ir120, ir134, skt, solzen, &
& satzen
integer(c_signed_char), dimension(nx,ny), target :: lsm, cldmask
logical(kind=1) :: undo_true_reflectances
! Call Python neural network via Python C-API
call py_ann_ctp(c_loc(vis006(1,1)), c_loc(vis008(1,1)),c_loc(nir016(1,1)), &
c_loc(ir039(1,1)), c_loc(ir062(1,1)), c_loc(ir073(1,1)), &
c_loc(ir087(1,1)), c_loc(ir108(1,1)), c_loc(ir120(1,1)), &
c_loc(ir134(1,1)), c_loc(lsm(1,1)), c_loc(skt(1,1)), &
c_loc(solzen(1,1)), c_loc(satzen(1,1)),nx, ny, ctp, &
ctp_unc, c_loc(cldmask(1,1)), msg_index, undo_true_reflectances)
end subroutine seviri_ann_ctp
subroutine seviri_ann_ctt(nx, ny, vis006, vis008, nir016, ir039, ir062, ir073, &
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen, &
ctt, ctt_unc, cldmask, msg_index, undo_true_reflectances)
use iso_c_binding
! output arrays
real(c_float), intent(out) :: ctt(:,:), ctt_unc(:,:)
! C-types
integer(c_int) :: nx ,ny
integer(c_signed_char) :: msg_index
real(c_float), dimension(nx,ny), target :: vis006, vis008, nir016, ir039, &
& ir062, ir073, ir087, ir108, &
& ir120, ir134, skt, solzen, &
& satzen
integer(c_signed_char), dimension(nx,ny), target :: lsm, cldmask
logical(kind=1) :: undo_true_reflectances
! Call Python neural network via Python C-API
call py_ann_ctt(c_loc(vis006(1,1)), c_loc(vis008(1,1)),c_loc(nir016(1,1)), &
c_loc(ir039(1,1)), c_loc(ir062(1,1)), c_loc(ir073(1,1)), &
c_loc(ir087(1,1)), c_loc(ir108(1,1)), c_loc(ir120(1,1)), &
c_loc(ir134(1,1)), c_loc(lsm(1,1)), c_loc(skt(1,1)), &
c_loc(solzen(1,1)), c_loc(satzen(1,1)),nx, ny, ctt, &
ctt_unc, c_loc(cldmask(1,1)), msg_index, undo_true_reflectances)
end subroutine seviri_ann_ctt
subroutine seviri_ann_cbh(nx, ny, ir108, ir120, ir134, solzen, satzen, cbh, cbh_unc, &
cldmask)
use iso_c_binding
! output arrays
real(c_float), intent(out) :: cbh(:,:), cbh_unc(:,:)
! C-types
integer(c_int) :: nx ,ny
integer(c_signed_char) :: msg_index
real(c_float), dimension(nx,ny), target :: ir108, ir120, ir134, solzen, satzen
integer(c_signed_char), dimension(nx,ny), target :: cldmask
! Call Python neural network via Python C-API
call py_ann_cbh(c_loc(ir108(1,1)), c_loc(ir120(1,1)), c_loc(ir134(1,1)), &
c_loc(solzen(1,1)), c_loc(satzen(1,1)),nx, ny, cbh, &
cbh_unc, c_loc(cldmask(1,1)))
end subroutine seviri_ann_cbh
subroutine seviri_ann_mlay(nx, ny, vis006, vis008, nir016, ir039, ir062, ir073, &
ir087, ir108, ir120, ir134, lsm, skt, solzen, satzen, &
regression_mlay, binary_mlay, uncertainty_mlay, cldmask, &
msg_index, undo_true_reflectances)
use iso_c_binding
! output arrays
real(c_float), intent(out) :: regression_mlay(:,:), uncertainty_mlay(:,:)
integer(c_signed_char), intent(out) :: binary_mlay(:,:)
! C-types
integer(c_int) :: nx ,ny
integer(c_signed_char) :: msg_index
real(c_float), dimension(nx,ny), target :: vis006, vis008, nir016, ir039, &
& ir062, ir073, ir087, ir108, &
& ir120, ir134, skt, solzen, &
& satzen
integer(c_signed_char), dimension(nx,ny), target :: lsm, cldmask
logical(kind=1) :: undo_true_reflectances
! Call Python neural network via Python C-API
call py_ann_mlay(c_loc(vis006(1,1)), c_loc(vis008(1,1)), c_loc(nir016(1,1)), &
c_loc(ir039(1,1)), c_loc(ir062(1,1)), c_loc(ir073(1,1)), &
c_loc(ir087(1,1)), c_loc(ir108(1,1)), c_loc(ir120(1,1)), &
c_loc(ir134(1,1)), c_loc(lsm(1,1)), c_loc(skt(1,1)), &
c_loc(solzen(1,1)), c_loc(satzen(1,1)), nx, ny, &
regression_mlay, binary_mlay, uncertainty_mlay, &
c_loc(cldmask(1,1)), msg_index, undo_true_reflectances)
end subroutine seviri_ann_mlay
end module seviri_neural_net_m