-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbdydta.f90
636 lines (636 loc) · 25.6 KB
/
bdydta.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
MODULE bdydta
USE oce
USE dom_oce
USE phycst
USE sbcapr
USE sbctide
USE bdy_oce
USE bdytides
USE ice
USE icevar
USE fldread
USE iom
USE in_out_manager
USE timing
IMPLICIT NONE
PRIVATE
PUBLIC :: bdy_dta
PUBLIC :: bdy_dta_init
INTEGER, ALLOCATABLE, DIMENSION(:) :: nb_bdy_fld
INTEGER :: nb_bdy_fld_sum
LOGICAL, DIMENSION(jp_bdy) :: ln_full_vel_array
TYPE(FLD), PUBLIC, ALLOCATABLE, DIMENSION(:), TARGET :: bf
TYPE(MAP_POINTER), ALLOCATABLE, DIMENSION(:) :: nbmap_ptr
INTEGER :: nice_cat
INTEGER :: jfld_hti, jfld_hts, jfld_ai
INTEGER, DIMENSION(jp_bdy) :: jfld_htit, jfld_htst, jfld_ait
CONTAINS
SUBROUTINE bdy_dta(kt, jit, time_offset)
INTEGER, INTENT(IN) :: kt
INTEGER, INTENT(IN), OPTIONAL :: jit
INTEGER, INTENT(IN), OPTIONAL :: time_offset
INTEGER :: jbdy, jfld, jstart, jend, ib, jl
INTEGER :: ii, ij, ik, igrd
INTEGER, DIMENSION(jpbgrd) :: ilen1
INTEGER, POINTER, DIMENSION(:) :: nblen, nblenrim
TYPE(OBC_DATA), POINTER :: dta
IF (ln_timing) CALL timing_start('bdy_dta')
IF (kt == nit000 .AND. .NOT. PRESENT(jit)) THEN
DO jbdy = 1, nb_bdy
nblen => idx_bdy(jbdy) % nblen
nblenrim => idx_bdy(jbdy) % nblenrim
dta => dta_bdy(jbdy)
IF (nn_dyn2d_dta(jbdy) == 0) THEN
ilen1(:) = nblen(:)
IF (dta % ll_ssh) THEN
igrd = 1
DO ib = 1, ilen1(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % ssh(ib) = sshn(ii, ij) * tmask(ii, ij, 1)
END DO
END IF
IF (dta % ll_u2d) THEN
igrd = 2
DO ib = 1, ilen1(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % u2d(ib) = un_b(ii, ij) * umask(ii, ij, 1)
END DO
END IF
IF (dta % ll_v2d) THEN
igrd = 3
DO ib = 1, ilen1(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % v2d(ib) = vn_b(ii, ij) * vmask(ii, ij, 1)
END DO
END IF
END IF
IF (nn_dyn3d_dta(jbdy) == 0) THEN
ilen1(:) = nblen(:)
IF (dta % ll_u3d) THEN
igrd = 2
DO ib = 1, ilen1(igrd)
DO ik = 1, jpkm1
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % u3d(ib, ik) = (un(ii, ij, ik) - un_b(ii, ij)) * umask(ii, ij, ik)
END DO
END DO
END IF
IF (dta % ll_v3d) THEN
igrd = 3
DO ib = 1, ilen1(igrd)
DO ik = 1, jpkm1
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % v3d(ib, ik) = (vn(ii, ij, ik) - vn_b(ii, ij)) * vmask(ii, ij, ik)
END DO
END DO
END IF
END IF
IF (nn_tra_dta(jbdy) == 0) THEN
ilen1(:) = nblen(:)
IF (dta % ll_tem) THEN
igrd = 1
DO ib = 1, ilen1(igrd)
DO ik = 1, jpkm1
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % tem(ib, ik) = tsn(ii, ij, ik, jp_tem) * tmask(ii, ij, ik)
END DO
END DO
END IF
IF (dta % ll_sal) THEN
igrd = 1
DO ib = 1, ilen1(igrd)
DO ik = 1, jpkm1
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % sal(ib, ik) = tsn(ii, ij, ik, jp_sal) * tmask(ii, ij, ik)
END DO
END DO
END IF
END IF
IF (nn_ice_dta(jbdy) == 0) THEN
ilen1(:) = nblen(:)
IF (dta % ll_a_i) THEN
igrd = 1
DO jl = 1, jpl
DO ib = 1, ilen1(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % a_i(ib, jl) = a_i(ii, ij, jl) * tmask(ii, ij, 1)
END DO
END DO
END IF
IF (dta % ll_h_i) THEN
igrd = 1
DO jl = 1, jpl
DO ib = 1, ilen1(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % h_i(ib, jl) = h_i(ii, ij, jl) * tmask(ii, ij, 1)
END DO
END DO
END IF
IF (dta % ll_h_s) THEN
igrd = 1
DO jl = 1, jpl
DO ib = 1, ilen1(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % h_s(ib, jl) = h_s(ii, ij, jl) * tmask(ii, ij, 1)
END DO
END DO
END IF
END IF
END DO
END IF
jstart = 1
DO jbdy = 1, nb_bdy
dta => dta_bdy(jbdy)
IF (nn_dta(jbdy) == 1) THEN
IF (PRESENT(jit)) THEN
IF (cn_dyn2d(jbdy) /= 'none') THEN
IF (nn_dyn2d_dta(jbdy) == 2) THEN
IF (dta % ll_ssh) dta % ssh(:) = 0._wp
IF (dta % ll_u2d) dta % u2d(:) = 0._wp
IF (dta % ll_u3d) dta % v2d(:) = 0._wp
END IF
IF (cn_tra(jbdy) /= 'runoff') THEN
IF (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3) THEN
jend = jstart + dta % nread(2) - 1
IF (ln_full_vel_array(jbdy)) THEN
CALL fld_read(kt = kt, kn_fsbc = 1, sd = bf(jstart : jend), map = nbmap_ptr(jstart : jend), kit = jit, kt_offset = time_offset, jpk_bdy = nb_jpk_bdy, fvl = ln_full_vel_array(jbdy))
ELSE
CALL fld_read(kt = kt, kn_fsbc = 1, sd = bf(jstart : jend), map = nbmap_ptr(jstart : jend), kit = jit, kt_offset = time_offset)
END IF
IF (ln_full_vel_array(jbdy) .AND. (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3 .OR. nn_dyn3d_dta(jbdy) == 1)) THEN
igrd = 2
dta % u2d(:) = 0._wp
DO ib = 1, idx_bdy(jbdy) % nblen(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
DO ik = 1, jpkm1
dta % u2d(ib) = dta % u2d(ib) + e3u_n(ii, ij, ik) * umask(ii, ij, ik) * dta % u3d(ib, ik)
END DO
dta % u2d(ib) = dta % u2d(ib) * r1_hu_n(ii, ij)
END DO
igrd = 3
dta % v2d(:) = 0._wp
DO ib = 1, idx_bdy(jbdy) % nblen(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
DO ik = 1, jpkm1
dta % v2d(ib) = dta % v2d(ib) + e3v_n(ii, ij, ik) * vmask(ii, ij, ik) * dta % v3d(ib, ik)
END DO
dta % v2d(ib) = dta % v2d(ib) * r1_hv_n(ii, ij)
END DO
END IF
END IF
IF (nn_dyn2d_dta(jbdy) .GE. 2) THEN
CALL bdytide_update(kt = kt, idx = idx_bdy(jbdy), dta = dta, td = tides(jbdy), jit = jit, time_offset = time_offset)
END IF
END IF
END IF
ELSE
IF (cn_tra(jbdy) == 'runoff') THEN
jend = nb_bdy_fld(jbdy)
CALL fld_read(kt = kt, kn_fsbc = 1, sd = bf(jstart : jend), map = nbmap_ptr(jstart : jend), kt_offset = time_offset)
igrd = 2
DO ib = 1, idx_bdy(jbdy) % nblen(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta % u2d(ib) = dta % u2d(ib) / (e2u(ii, ij) * hu_0(ii, ij))
END DO
igrd = 3
DO ib = 1, idx_bdy(jbdy) % nblen(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta % v2d(ib) = dta % v2d(ib) / (e1v(ii, ij) * hv_0(ii, ij))
END DO
ELSE
IF (nn_dyn2d_dta(jbdy) == 2) THEN
IF (dta % ll_ssh) dta % ssh(:) = 0._wp
IF (dta % ll_u2d) dta % u2d(:) = 0._wp
IF (dta % ll_v2d) dta % v2d(:) = 0._wp
END IF
IF (dta % nread(1) .GT. 0) THEN
jend = jstart + dta % nread(1) - 1
CALL fld_read(kt = kt, kn_fsbc = 1, sd = bf(jstart : jend), map = nbmap_ptr(jstart : jend), kt_offset = time_offset, jpk_bdy = nb_jpk_bdy, fvl = ln_full_vel_array(jbdy))
END IF
IF (ln_full_vel_array(jbdy) .AND. (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3 .OR. nn_dyn3d_dta(jbdy) == 1)) THEN
igrd = 2
dta % u2d(:) = 0._wp
DO ib = 1, idx_bdy(jbdy) % nblen(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
DO ik = 1, jpkm1
dta % u2d(ib) = dta % u2d(ib) + e3u_n(ii, ij, ik) * umask(ii, ij, ik) * dta % u3d(ib, ik)
END DO
dta % u2d(ib) = dta % u2d(ib) * r1_hu_n(ii, ij)
DO ik = 1, jpkm1
dta % u3d(ib, ik) = dta % u3d(ib, ik) - dta % u2d(ib)
END DO
END DO
igrd = 3
dta % v2d(:) = 0._wp
DO ib = 1, idx_bdy(jbdy) % nblen(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
DO ik = 1, jpkm1
dta % v2d(ib) = dta % v2d(ib) + e3v_n(ii, ij, ik) * vmask(ii, ij, ik) * dta % v3d(ib, ik)
END DO
dta % v2d(ib) = dta % v2d(ib) * r1_hv_n(ii, ij)
DO ik = 1, jpkm1
dta % v3d(ib, ik) = dta % v3d(ib, ik) - dta % v2d(ib)
END DO
END DO
END IF
END IF
IF (cn_ice(jbdy) /= 'none' .AND. nn_ice_dta(jbdy) == 1) THEN
jfld_hti = jfld_htit(jbdy)
jfld_hts = jfld_htst(jbdy)
jfld_ai = jfld_ait(jbdy)
IF (jpl /= 1 .AND. nice_cat == 1) THEN
CALL ice_var_itd(bf(jfld_hti) % fnow(:, 1, 1), bf(jfld_hts) % fnow(:, 1, 1), bf(jfld_ai) % fnow(:, 1, 1), dta_bdy(jbdy) % h_i, dta_bdy(jbdy) % h_s, dta_bdy(jbdy) % a_i)
ELSE IF (jpl /= 1 .AND. nice_cat /= 1 .AND. nice_cat /= jpl) THEN
CALL ice_var_itd2(bf(jfld_hti) % fnow(:, 1, :), bf(jfld_hts) % fnow(:, 1, :), bf(jfld_ai) % fnow(:, 1, :), dta_bdy(jbdy) % h_i, dta_bdy(jbdy) % h_s, dta_bdy(jbdy) % a_i)
END IF
END IF
END IF
jstart = jstart + dta % nread(1)
END IF
END DO
IF (ln_tide) THEN
IF (ln_dynspg_ts) THEN
DO jbdy = 1, nb_bdy
IF (nn_dyn2d_dta(jbdy) .GE. 2) THEN
nblen => idx_bdy(jbdy) % nblen
nblenrim => idx_bdy(jbdy) % nblenrim
IF (cn_dyn2d(jbdy) == 'frs') THEN
ilen1(:) = nblen(:)
ELSE
ilen1(:) = nblenrim(:)
END IF
IF (dta_bdy(jbdy) % ll_ssh) dta_bdy_s(jbdy) % ssh(1 : ilen1(1)) = dta_bdy(jbdy) % ssh(1 : ilen1(1))
IF (dta_bdy(jbdy) % ll_u2d) dta_bdy_s(jbdy) % u2d(1 : ilen1(2)) = dta_bdy(jbdy) % u2d(1 : ilen1(2))
IF (dta_bdy(jbdy) % ll_v2d) dta_bdy_s(jbdy) % v2d(1 : ilen1(3)) = dta_bdy(jbdy) % v2d(1 : ilen1(3))
END IF
END DO
ELSE
CALL bdy_dta_tides(kt = kt, time_offset = time_offset)
END IF
END IF
IF (ln_apr_obc) THEN
DO jbdy = 1, nb_bdy
IF (cn_tra(jbdy) /= 'runoff') THEN
igrd = 1
DO ib = 1, idx_bdy(jbdy) % nblenrim(igrd)
ii = idx_bdy(jbdy) % nbi(ib, igrd)
ij = idx_bdy(jbdy) % nbj(ib, igrd)
dta_bdy(jbdy) % ssh(ib) = dta_bdy(jbdy) % ssh(ib) + ssh_ib(ii, ij)
END DO
END IF
END DO
END IF
IF (ln_timing) CALL timing_stop('bdy_dta')
END SUBROUTINE bdy_dta
SUBROUTINE bdy_dta_init
INTEGER :: jbdy, jfld, jstart, jend, ierror, ios
CHARACTER(LEN = 100) :: cn_dir
CHARACTER(LEN = 100), DIMENSION(nb_bdy) :: cn_dir_array
CHARACTER(LEN = 256) :: clname
LOGICAL :: ln_full_vel
INTEGER :: ilen_global
INTEGER, ALLOCATABLE, DIMENSION(:) :: ilen1, ilen3
INTEGER, ALLOCATABLE, DIMENSION(:) :: ibdy
INTEGER, ALLOCATABLE, DIMENSION(:) :: igrid
INTEGER, POINTER, DIMENSION(:) :: nblen, nblenrim
TYPE(OBC_DATA), POINTER :: dta
INTEGER :: kndims
INTEGER, DIMENSION(4) :: kdimsz
INTEGER :: inum, id1
TYPE(FLD_N), ALLOCATABLE, DIMENSION(:) :: blf_i
TYPE(FLD_N) :: bn_tem, bn_sal, bn_u3d, bn_v3d
TYPE(FLD_N) :: bn_ssh, bn_u2d, bn_v2d
TYPE(FLD_N) :: bn_a_i, bn_h_i, bn_h_s
NAMELIST /nambdy_dta/ cn_dir, bn_tem, bn_sal, bn_u3d, bn_v3d, bn_ssh, bn_u2d, bn_v2d
NAMELIST /nambdy_dta/ bn_a_i, bn_h_i, bn_h_s
NAMELIST /nambdy_dta/ ln_full_vel, nb_jpk_bdy
IF (lwp) WRITE(numout, FMT = *)
IF (lwp) WRITE(numout, FMT = *) 'bdy_dta_ini : initialization of data at the open boundaries'
IF (lwp) WRITE(numout, FMT = *) '~~~~~~~~~~'
IF (lwp) WRITE(numout, FMT = *) ''
!$ACC KERNELS
DO jbdy = 1, nb_bdy
nn_dta(jbdy) = MAX(nn_dyn2d_dta(jbdy), nn_dyn3d_dta(jbdy), nn_tra_dta(jbdy), nn_ice_dta(jbdy))
IF (nn_dta(jbdy) > 1) nn_dta(jbdy) = 1
END DO
!$ACC END KERNELS
ALLOCATE(nb_bdy_fld(nb_bdy))
nb_bdy_fld(:) = 0
DO jbdy = 1, nb_bdy
IF (cn_dyn2d(jbdy) /= 'none' .AND. (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3)) THEN
nb_bdy_fld(jbdy) = nb_bdy_fld(jbdy) + 3
END IF
IF (cn_dyn3d(jbdy) /= 'none' .AND. nn_dyn3d_dta(jbdy) == 1) THEN
nb_bdy_fld(jbdy) = nb_bdy_fld(jbdy) + 2
END IF
IF (cn_tra(jbdy) /= 'none' .AND. nn_tra_dta(jbdy) == 1) THEN
nb_bdy_fld(jbdy) = nb_bdy_fld(jbdy) + 2
END IF
IF (cn_ice(jbdy) /= 'none' .AND. nn_ice_dta(jbdy) == 1) THEN
nb_bdy_fld(jbdy) = nb_bdy_fld(jbdy) + 3
END IF
IF (lwp) WRITE(numout, FMT = *) 'Maximum number of files to open =', nb_bdy_fld(jbdy)
END DO
nb_bdy_fld_sum = SUM(nb_bdy_fld)
ALLOCATE(bf(nb_bdy_fld_sum), STAT = ierror)
IF (ierror > 0) THEN
CALL ctl_stop('bdy_dta: unable to allocate bf structure')
RETURN
END IF
ALLOCATE(blf_i(nb_bdy_fld_sum), STAT = ierror)
IF (ierror > 0) THEN
CALL ctl_stop('bdy_dta: unable to allocate blf_i structure')
RETURN
END IF
ALLOCATE(nbmap_ptr(nb_bdy_fld_sum), STAT = ierror)
IF (ierror > 0) THEN
CALL ctl_stop('bdy_dta: unable to allocate nbmap_ptr structure')
RETURN
END IF
ALLOCATE(ilen1(nb_bdy_fld_sum), ilen3(nb_bdy_fld_sum))
ALLOCATE(ibdy(nb_bdy_fld_sum))
ALLOCATE(igrid(nb_bdy_fld_sum))
REWIND(UNIT = numnam_ref)
REWIND(UNIT = numnam_cfg)
jfld = 0
DO jbdy = 1, nb_bdy
IF (nn_dta(jbdy) == 1) THEN
READ(numnam_ref, nambdy_dta, IOSTAT = ios, ERR = 901)
901 IF (ios /= 0) CALL ctl_nam(ios, 'nambdy_dta in reference namelist', lwp)
READ(numnam_cfg, nambdy_dta, IOSTAT = ios, ERR = 902)
902 IF (ios > 0) CALL ctl_nam(ios, 'nambdy_dta in configuration namelist', lwp)
IF (lwm) WRITE(numond, nambdy_dta)
cn_dir_array(jbdy) = cn_dir
ln_full_vel_array(jbdy) = ln_full_vel
nblen => idx_bdy(jbdy) % nblen
nblenrim => idx_bdy(jbdy) % nblenrim
dta => dta_bdy(jbdy)
dta % nread(2) = 0
IF (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3) THEN
IF (dta % ll_ssh) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in ssh field'
jfld = jfld + 1
blf_i(jfld) = bn_ssh
ibdy(jfld) = jbdy
igrid(jfld) = 1
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = 1
dta % nread(2) = dta % nread(2) + 1
END IF
IF (dta % ll_u2d .AND. .NOT. ln_full_vel_array(jbdy)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in u2d field'
jfld = jfld + 1
blf_i(jfld) = bn_u2d
ibdy(jfld) = jbdy
igrid(jfld) = 2
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = 1
dta % nread(2) = dta % nread(2) + 1
END IF
IF (dta % ll_v2d .AND. .NOT. ln_full_vel_array(jbdy)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in v2d field'
jfld = jfld + 1
blf_i(jfld) = bn_v2d
ibdy(jfld) = jbdy
igrid(jfld) = 3
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = 1
dta % nread(2) = dta % nread(2) + 1
END IF
END IF
IF (nn_dyn3d_dta(jbdy) == 1 .OR. (ln_full_vel_array(jbdy) .AND. (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3))) THEN
IF (dta % ll_u3d .OR. (ln_full_vel_array(jbdy) .AND. dta % ll_u2d)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in u3d field'
jfld = jfld + 1
blf_i(jfld) = bn_u3d
ibdy(jfld) = jbdy
igrid(jfld) = 2
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = jpk
IF (ln_full_vel_array(jbdy) .AND. dta % ll_u2d) dta % nread(2) = dta % nread(2) + 1
END IF
IF (dta % ll_v3d .OR. (ln_full_vel_array(jbdy) .AND. dta % ll_v2d)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in v3d field'
jfld = jfld + 1
blf_i(jfld) = bn_v3d
ibdy(jfld) = jbdy
igrid(jfld) = 3
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = jpk
IF (ln_full_vel_array(jbdy) .AND. dta % ll_v2d) dta % nread(2) = dta % nread(2) + 1
END IF
END IF
IF (nn_tra_dta(jbdy) == 1) THEN
IF (dta % ll_tem) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in tem field'
jfld = jfld + 1
blf_i(jfld) = bn_tem
ibdy(jfld) = jbdy
igrid(jfld) = 1
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = jpk
END IF
IF (dta % ll_sal) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ reading in sal field'
jfld = jfld + 1
blf_i(jfld) = bn_sal
ibdy(jfld) = jbdy
igrid(jfld) = 1
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = jpk
END IF
END IF
IF (nn_ice_dta(jbdy) == 1) THEN
clname = TRIM(cn_dir) // TRIM(bn_a_i % clname)
IF (.NOT. bn_a_i % ln_clim) THEN
WRITE(clname, FMT = '(a,"_y",i4.4)') TRIM(clname), nyear
IF (bn_a_i % cltype /= 'yearly') WRITE(clname, FMT = '(a,"m" ,i2.2)') TRIM(clname), nmonth
ELSE
IF (bn_a_i % cltype /= 'yearly') WRITE(clname, FMT = '(a,"_m",i2.2)') TRIM(clname), nmonth
END IF
IF (bn_a_i % cltype == 'daily' .OR. bn_a_i % cltype(1 : 4) == 'week') WRITE(clname, FMT = '(a,"d" ,i2.2)') TRIM(clname), nday
CALL iom_open(clname, inum)
id1 = iom_varid(inum, bn_a_i % clvar, kdimsz = kdimsz, kndims = kndims, ldstop = .FALSE.)
CALL iom_close(inum)
IF (kndims == 4) THEN
nice_cat = kdimsz(4)
ELSE
nice_cat = 1
END IF
IF (dta % ll_a_i) THEN
jfld = jfld + 1
blf_i(jfld) = bn_a_i
ibdy(jfld) = jbdy
igrid(jfld) = 1
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = nice_cat
END IF
IF (dta % ll_h_i) THEN
jfld = jfld + 1
blf_i(jfld) = bn_h_i
ibdy(jfld) = jbdy
igrid(jfld) = 1
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = nice_cat
END IF
IF (dta % ll_h_s) THEN
jfld = jfld + 1
blf_i(jfld) = bn_h_s
ibdy(jfld) = jbdy
igrid(jfld) = 1
ilen1(jfld) = nblen(igrid(jfld))
ilen3(jfld) = nice_cat
END IF
END IF
IF (jbdy == 1) THEN
nb_bdy_fld_sum = 0
nb_bdy_fld(jbdy) = jfld
nb_bdy_fld_sum = jfld
ELSE
nb_bdy_fld(jbdy) = jfld - nb_bdy_fld_sum
nb_bdy_fld_sum = nb_bdy_fld_sum + nb_bdy_fld(jbdy)
END IF
dta % nread(1) = nb_bdy_fld(jbdy)
END IF
END DO
DO jfld = 1, nb_bdy_fld_sum
ALLOCATE(bf(jfld) % fnow(ilen1(jfld), 1, ilen3(jfld)))
IF (blf_i(jfld) % ln_tint) ALLOCATE(bf(jfld) % fdta(ilen1(jfld), 1, ilen3(jfld), 2))
nbmap_ptr(jfld) % ptr => idx_bdy(ibdy(jfld)) % nbmap(:, igrid(jfld))
nbmap_ptr(jfld) % ll_unstruc = ln_coords_file(ibdy(jfld))
END DO
jstart = 1
DO jbdy = 1, nb_bdy
jend = jstart - 1 + nb_bdy_fld(jbdy)
CALL fld_fill(bf(jstart : jend), blf_i(jstart : jend), cn_dir_array(jbdy), 'bdy_dta', 'open boundary conditions', 'nambdy_dta')
jstart = jend + 1
END DO
DO jfld = 1, nb_bdy_fld_sum
bf(jfld) % igrd = igrid(jfld)
bf(jfld) % ibdy = ibdy(jfld)
END DO
jfld = 0
DO jbdy = 1, nb_bdy
nblen => idx_bdy(jbdy) % nblen
dta => dta_bdy(jbdy)
IF (lwp) THEN
WRITE(numout, FMT = *) '++++++ dta%ll_ssh = ', dta % ll_ssh
WRITE(numout, FMT = *) '++++++ dta%ll_u2d = ', dta % ll_u2d
WRITE(numout, FMT = *) '++++++ dta%ll_v2d = ', dta % ll_v2d
WRITE(numout, FMT = *) '++++++ dta%ll_u3d = ', dta % ll_u3d
WRITE(numout, FMT = *) '++++++ dta%ll_v3d = ', dta % ll_v3d
WRITE(numout, FMT = *) '++++++ dta%ll_tem = ', dta % ll_tem
WRITE(numout, FMT = *) '++++++ dta%ll_sal = ', dta % ll_sal
END IF
IF (nn_dyn2d_dta(jbdy) == 0 .OR. nn_dyn2d_dta(jbdy) == 2) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%ssh/u2d/u3d allocated space'
IF (dta % ll_ssh) ALLOCATE(dta % ssh(nblen(1)))
IF (dta % ll_u2d) ALLOCATE(dta % u2d(nblen(2)))
IF (dta % ll_v2d) ALLOCATE(dta % v2d(nblen(3)))
END IF
IF (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3) THEN
IF (dta % ll_ssh) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%ssh pointing to fnow'
jfld = jfld + 1
dta % ssh => bf(jfld) % fnow(:, 1, 1)
END IF
IF (dta % ll_u2d) THEN
IF (ln_full_vel_array(jbdy)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%u2d allocated space'
ALLOCATE(dta % u2d(nblen(2)))
ELSE
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%u2d pointing to fnow'
jfld = jfld + 1
dta % u2d => bf(jfld) % fnow(:, 1, 1)
END IF
END IF
IF (dta % ll_v2d) THEN
IF (ln_full_vel_array(jbdy)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%v2d allocated space'
ALLOCATE(dta % v2d(nblen(3)))
ELSE
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%v2d pointing to fnow'
jfld = jfld + 1
dta % v2d => bf(jfld) % fnow(:, 1, 1)
END IF
END IF
END IF
IF (nn_dyn3d_dta(jbdy) == 0) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%u3d/v3d allocated space'
IF (dta % ll_u3d) ALLOCATE(dta_bdy(jbdy) % u3d(nblen(2), jpk))
IF (dta % ll_v3d) ALLOCATE(dta_bdy(jbdy) % v3d(nblen(3), jpk))
END IF
IF (nn_dyn3d_dta(jbdy) == 1 .OR. (ln_full_vel_array(jbdy) .AND. (nn_dyn2d_dta(jbdy) == 1 .OR. nn_dyn2d_dta(jbdy) == 3))) THEN
IF (dta % ll_u3d .OR. (ln_full_vel_array(jbdy) .AND. dta % ll_u2d)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%u3d pointing to fnow'
jfld = jfld + 1
dta_bdy(jbdy) % u3d => bf(jfld) % fnow(:, 1, :)
END IF
IF (dta % ll_v3d .OR. (ln_full_vel_array(jbdy) .AND. dta % ll_v2d)) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%v3d pointing to fnow'
jfld = jfld + 1
dta_bdy(jbdy) % v3d => bf(jfld) % fnow(:, 1, :)
END IF
END IF
IF (nn_tra_dta(jbdy) == 0) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%tem/sal allocated space'
IF (dta % ll_tem) ALLOCATE(dta_bdy(jbdy) % tem(nblen(1), jpk))
IF (dta % ll_sal) ALLOCATE(dta_bdy(jbdy) % sal(nblen(1), jpk))
ELSE
IF (dta % ll_tem) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%tem pointing to fnow'
jfld = jfld + 1
dta_bdy(jbdy) % tem => bf(jfld) % fnow(:, 1, :)
END IF
IF (dta % ll_sal) THEN
IF (lwp) WRITE(numout, FMT = *) '++++++ dta%sal pointing to fnow'
jfld = jfld + 1
dta_bdy(jbdy) % sal => bf(jfld) % fnow(:, 1, :)
END IF
END IF
IF (cn_ice(jbdy) /= 'none') THEN
IF (nn_ice_dta(jbdy) == 0) THEN
ALLOCATE(dta_bdy(jbdy) % a_i(nblen(1), jpl))
ALLOCATE(dta_bdy(jbdy) % h_i(nblen(1), jpl))
ALLOCATE(dta_bdy(jbdy) % h_s(nblen(1), jpl))
ELSE
IF (nice_cat == jpl) THEN
jfld = jfld + 1
dta_bdy(jbdy) % a_i => bf(jfld) % fnow(:, 1, :)
jfld = jfld + 1
dta_bdy(jbdy) % h_i => bf(jfld) % fnow(:, 1, :)
jfld = jfld + 1
dta_bdy(jbdy) % h_s => bf(jfld) % fnow(:, 1, :)
ELSE
jfld_ait(jbdy) = jfld + 1
jfld_htit(jbdy) = jfld + 2
jfld_htst(jbdy) = jfld + 3
jfld = jfld + 3
ALLOCATE(dta_bdy(jbdy) % a_i(nblen(1), jpl))
ALLOCATE(dta_bdy(jbdy) % h_i(nblen(1), jpl))
ALLOCATE(dta_bdy(jbdy) % h_s(nblen(1), jpl))
dta_bdy(jbdy) % a_i(:, :) = 0._wp
dta_bdy(jbdy) % h_i(:, :) = 0._wp
dta_bdy(jbdy) % h_s(:, :) = 0._wp
END IF
END IF
END IF
END DO
END SUBROUTINE bdy_dta_init
END MODULE bdydta