-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathmpas_stream_manager.F
More file actions
6450 lines (5314 loc) · 280 KB
/
Copy pathmpas_stream_manager.F
File metadata and controls
6450 lines (5314 loc) · 280 KB
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module mpas_stream_manager
#define COMMA ,
#define STREAM_DEBUG_WRITE(M) ! call mpas_log_write(M)
#define STREAM_WARNING_WRITE(M) call mpas_log_write( M , messageType=MPAS_LOG_WARN)
#define STREAM_ERROR_WRITE(M) call mpas_log_write( M , messageType=MPAS_LOG_ERR)
use mpas_kind_types
use mpas_derived_types
use mpas_field_routines
use mpas_pool_routines
use mpas_timekeeping
use mpas_log
use mpas_io_streams
use mpas_stream_list
use mpas_sort
use mpas_threading
public :: MPAS_stream_mgr_init, &
MPAS_stream_mgr_finalize, &
MPAS_stream_mgr_create_stream, &
MPAS_stream_mgr_validate_streams, &
MPAS_stream_mgr_destroy_stream, &
MPAS_stream_mgr_get_clock, &
MPAS_stream_mgr_set_property, &
MPAS_stream_mgr_get_property, &
MPAS_stream_mgr_add_pkg, &
MPAS_stream_mgr_remove_pkg, &
MPAS_stream_mgr_add_pool, &
MPAS_stream_mgr_add_field, &
MPAS_stream_mgr_add_stream_fields, &
MPAS_stream_mgr_remove_field, &
MPAS_stream_mgr_add_alarm, &
MPAS_stream_mgr_remove_alarm, &
MPAS_stream_mgr_reset_alarms, &
MPAS_stream_mgr_ringing_alarms, &
MPAS_stream_mgr_add_att, &
MPAS_stream_mgr_write, &
MPAS_stream_mgr_block_write, &
MPAS_stream_mgr_read, &
MPAS_stream_mgr_begin_iteration, &
MPAS_stream_mgr_get_next_stream, &
MPAS_stream_mgr_get_next_field, &
MPAS_stream_mgr_stream_exists, &
MPAS_stream_mgr_get_stream_interval, &
MPAS_get_stream_filename, &
MPAS_build_stream_filename
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! IMPORTANT NOTE for {pre,post}write_reindex
!
! Caution is needed if calling the {pre,post}write_reindex routines directly
! from outside of the stream manager. These two routines make use of module
! state to save and retrieve pointers to the original indexing arrays. Problems
! can arise, for example, if external code calls prewrite_reindex, then makes
! a call to write output streams via the stream manager: in this case, pointers
! set by the external call to prewrite_reindex will be overwritten by the
! internal call to prewrite_reindex within mpas_stream_mgr_write.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public :: prewrite_reindex, & ! Please see note above...
postwrite_reindex, & ! Please see note above...
postread_reindex
private
interface MPAS_stream_mgr_set_property
module procedure MPAS_stream_mgr_set_property_int
module procedure MPAS_stream_mgr_set_property_char
module procedure MPAS_stream_mgr_set_property_logical
end interface
interface MPAS_stream_mgr_get_property
module procedure MPAS_stream_mgr_get_property_int
module procedure MPAS_stream_mgr_get_property_char
module procedure MPAS_stream_mgr_get_property_logical
end interface
interface MPAS_stream_mgr_add_att
module procedure MPAS_stream_mgr_add_att_int
module procedure MPAS_stream_mgr_add_att_real
module procedure MPAS_stream_mgr_add_att_char
module procedure MPAS_stream_mgr_add_att_logical
end interface
!
! Used for reindexing connectivity arrays during stream writes by the routines prewrite_reindex() and postwrite_reindex().
! Before a stream is written, we set the pointers here to be the heads of linked lists of locally-indexed connectivity fields.
! After a stream is written, we reset the arrays for connectivity fields in the stream to these pointers.
!
type (field2DInteger), pointer :: cellsOnCell_save
type (field2DInteger), pointer :: edgesOnCell_save
type (field2DInteger), pointer :: verticesOnCell_save
type (field2DInteger), pointer :: cellsOnEdge_save
type (field2DInteger), pointer :: verticesOnEdge_save
type (field2DInteger), pointer :: edgesOnEdge_save
type (field2DInteger), pointer :: cellsOnVertex_save
type (field2DInteger), pointer :: edgesOnVertex_save
contains
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_init
!
!> \brief Initialize a new MPAS stream manager.
!> \author Michael Duda, Doug Jacobsen
!> \date 13 June 2014
!> \details
!> Instantiates and initializes a streamManager type with a timekeeping
!> clock and a pool from which fields may be drawn and added to streams.
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_init(manager, ioContext, clock, allFields, allPackages, allStructs, ierr)!{{{
implicit none
interface
subroutine seed_random() bind(c)
end subroutine seed_random
end interface
character (len=*), parameter :: sub = 'MPAS_stream_mgr_init'
type (MPAS_streamManager_type), pointer :: manager
type (MPAS_IO_context_type), pointer :: ioContext
type (MPAS_Clock_type), pointer :: clock
type (MPAS_Pool_type), pointer :: allFields
type (MPAS_Pool_type), pointer :: allPackages
type (MPAS_Pool_type), pointer :: allStructs
integer, intent(out), optional :: ierr
integer :: err_local, threadNum
threadNum = mpas_threading_get_thread_num()
if ( threadNum == 0 ) then
call seed_random()
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_init()')
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
allocate(manager)
manager % ioContext => ioContext
manager % allFields => allFields
manager % allPackages => allPackages
manager % allStructs => allStructs
manager % streamClock => clock
manager % numStreams = 0
manager % errorLevel = MPAS_STREAM_ERR_SILENT
!
! Set up linked list of streams
!
call MPAS_stream_list_create(manager % streams, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while creating stream list')
return
end if
!
! Set up linked list of input alarms
!
call MPAS_stream_list_create(manager % alarms_in, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while creating input alarm list')
return
end if
!
! Set up linked list of output alarms
!
call MPAS_stream_list_create(manager % alarms_out, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while creating output alarm list')
return
end if
!
! Create a pool to hold default global attributes that every stream will have
!
call mpas_pool_create_pool(manager % defaultAtts)
end if
end subroutine MPAS_stream_mgr_init!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_finalize
!
!> \brief Free all memory associated with an MPAS stream manager.
!> \author Michael Duda, Doug Jacobsen
!> \date 13 June 2014
!> \details
!> Destroys a streamManager type, freeing all memory that was created as
!> part of the manager; the external clock and field pool associated with
!> the streamManager are unaffected.
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_finalize(manager, ierr)!{{{
implicit none
character (len=*), parameter :: sub = 'MPAS_stream_mgr_finalize'
type (MPAS_streamManager_type), pointer:: manager
integer, intent(out), optional :: ierr
integer :: err_local, threadNum
type (MPAS_stream_list_type), pointer :: stream_cursor
threadNum = mpas_threading_get_thread_num()
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_finalize()')
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
if ( threadNum == 0 ) then
!
! Remove all streams
!
stream_cursor => manager % streams % head
do while (associated(stream_cursor))
STREAM_DEBUG_WRITE(' -- deleting stream '//trim(stream_cursor % name))
call MPAS_stream_mgr_destroy_stream(manager, stream_cursor % name, ierr=err_local)
stream_cursor => manager % streams % head
end do
!
! Free up list of streams
!
call MPAS_stream_list_destroy(manager % streams, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while destroying stream list')
end if
!
! Remove all items from manager % alarms_in(put) list
!
stream_cursor => manager % alarms_in % head
do while (associated(stream_cursor))
call MPAS_stream_list_destroy(stream_cursor % streamList, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while destroying input alarms item')
end if
stream_cursor => stream_cursor % next
end do
!
! Free up list of input alarms
!
call MPAS_stream_list_destroy(manager % alarms_in, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while destroying input alarms list')
end if
!
! Remove all items from manager % alarms_out(put) list
!
stream_cursor => manager % alarms_out % head
do while (associated(stream_cursor))
call MPAS_stream_list_destroy(stream_cursor % streamList, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while destroying output alarms list')
end if
stream_cursor => stream_cursor % next
end do
!
! Free up list of output alarms
!
call MPAS_stream_list_destroy(manager % alarms_out, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while destroying output alarms list')
end if
!
! Free up default attribute pool
!
call mpas_pool_destroy_pool(manager % defaultAtts)
deallocate(manager)
end if
end subroutine MPAS_stream_mgr_finalize!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_create_stream
!
!> \brief Instantiate a new stream within an MPAS stream manager.
!> \author Michael Duda, Doug Jacobsen
!> \date 13 June 2014
!> \details
!> Creates a new stream within the stream manager. The "direction"
!> argument may be either MPAS_STREAM_INPUT, MPAS_STREAM_OUTPUT,
!> MPAS_STREAM_INPUT_OUTPUT, or MPAS_STREAM_NONE. The "filename" argument
!> is the template of the filenames that are associated with the stream.
!> Knowing the interval between files, and
!> the filename template, a "referenceTime" argument must be provided to
!> specify the first timestamp appearing in any of the files associated with
!> the stream, thereby determining where the "file breaks" will occur between
!> timestamps. If no "referenceTime" is specified, the start time of the
!> clock associated with the stream handler will be used as the reference
!> time. Additionally, the interval between records in the file may be
!> specified using the optional "recordInterval" argument; if this argument
!> is not supplied, the stream manager will assume that this interval is
!> equal to the shortest period of any periodic alarm attached to the stream.
!> The optional argument 'realPrecision' specifies the precision of
!> real-valued fields in the files associated with the stream; this
!> argument may take on values MPAS_IO_SINGLE_PRECISION,
!> MPAS_IO_DOUBLE_PRECISION, or MPAS_IO_NATIVE_PRECISION; if this argument is
!> not supplied, native precision is assumed.
!> The optional argument clobberMode determines how the stream manager will
!> deal with existing files; possible options include MPAS_STREAM_CLOBBER_NEVER,
!> MPAS_STREAM_CLOBBER_APPEND, MPAS_STREAM_CLOBBER_TRUNCATE,
!> and MPAS_STREAM_CLOBBER_OVERWRITE. The default behavior is to never modify
!> existing files (MPAS_STREAM_CLOBBER_NEVER).
!> The optional argument ioType specifies the I/O type to use for the
!> stream; possible values are defined by constants in the mpas_io module and
!> include: MPAS_IO_NETCDF, MPAS_IO_NETCDF4, MPAS_IO_PNETCDF, and
!> MPAS_IO_PNETCDF5. If not specified, the io_type will default to
!> MPAS_IO_PNETCDF.
!> NOTE: This routine does not support regular expressions for StreamID
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_create_stream(manager, streamID, direction, filename, &
filenameInterval, referenceTime, recordInterval, &
realPrecision, clobberMode, ioType, ierr) !{{{
use mpas_io, only : MPAS_IO_PNETCDF
implicit none
character (len=*), parameter :: sub = 'MPAS_stream_mgr_create_stream'
type (MPAS_streamManager_type), intent(inout) :: manager
character (len=*), intent(in) :: streamID
integer, intent(in) :: direction
character (len=*), intent(in) :: filename
character (len=*), intent(in), optional :: filenameInterval
type (MPAS_Time_type), intent(in), optional :: referenceTime
type (MPAS_TimeInterval_type), intent(in), optional :: recordInterval
integer, intent(in), optional :: realPrecision
integer, intent(in), optional :: clobberMode
integer, intent(in), optional :: ioType
integer, intent(out), optional :: ierr
type (MPAS_stream_list_type), pointer :: new_stream
integer :: err_local, threadNum
threadNum = mpas_threading_get_thread_num()
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_create_stream() for '//trim(streamID))
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
if ( threadNum == 0 ) then
!
! Check that the stream does not already exist
!
nullify(new_stream)
if (MPAS_stream_list_query(manager % streams, streamID, new_stream, ierr=err_local)) then
STREAM_DEBUG_WRITE('-- Stream '//trim(streamID)//' already exist in stream manager')
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
!
! Allocate a stream node to store the new stream
!
allocate(new_stream)
new_stream % name = streamID
new_stream % direction = direction
new_stream % valid = .false.
!TODO: ensure that filename does not contain ':' characters, which PNETCDF does not like...
new_stream % filename_template = filename
! Filename interval is 'none' by deault, but is set through the set_property routine.
if (present(filenameInterval)) then
new_stream % filename_interval = filenameInterval
else
new_stream % filename_interval = 'none'
end if
new_stream % nRecords = 0
if (present(clobberMode)) then
new_stream % clobber_mode = clobberMode
else
new_stream % clobber_mode = MPAS_STREAM_CLOBBER_NEVER
end if
if (present(ioType)) then
new_stream % io_type = ioType
else
new_stream % io_type = MPAS_IO_PNETCDF
end if
allocate(new_stream % referenceTime)
if (present(referenceTime)) then
new_stream % referenceTime = referenceTime
else
new_stream % referenceTime = mpas_get_clock_time(manager % streamClock, MPAS_START_TIME)
end if
if (present(recordInterval)) then
allocate(new_stream % recordInterval)
new_stream % recordInterval = recordInterval
end if
if (present(realPrecision)) then
new_stream % precision = realPrecision
end if
call MPAS_stream_list_create(new_stream % alarmList_in, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while creating input alarm list')
deallocate(new_stream)
return
end if
call MPAS_stream_list_create(new_stream % alarmList_out, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while creating output alarm list')
deallocate(new_stream)
return
end if
call mpas_pool_create_pool(new_stream % att_pool)
call mpas_pool_clone_pool(manager % defaultAtts, new_stream % att_pool)
call mpas_pool_create_pool(new_stream % field_pool)
call mpas_pool_create_pool(new_stream % field_pkg_pool)
call mpas_pool_create_pool(new_stream % pkg_pool)
nullify(new_stream % next)
!
! Add stream to list
!
call MPAS_stream_list_insert(manager % streams, new_stream, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while adding stream to list')
return
end if
manager % numStreams = manager % numStreams + 1
end if
end subroutine MPAS_stream_mgr_create_stream!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_validate_streams
!
!> \brief Validate all streams or one stream, if optional argument streamID is present.
!> \author Dom Heinzeller
!> \date 12 September 2017
!> \details
!> Validates all streams of a stream manager, or one stream if optional argument
!> streamID is specified. A low-level validation of streams happens within
!> xml_stream_parser in xml_stream_parser.c (e.g. immutable streams must not be
!> specified as mutable streams etc.). MPAS_stream_mgr_validate_streams performs
!> higher-level checks that should stay separate from the low-level checks and
!> that may require additional logic or settings not necessarily present when
!> calling xml_stream_parser.
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_validate_streams(manager, streamID, ierr)!{{{
implicit none
type (MPAS_streamManager_type), intent(inout) :: manager
character (len=*), intent(in), optional :: streamID
integer, intent(out), optional :: ierr
character(len=StrKIND) :: stream_name
character(len=StrKIND) :: filename_interval
integer :: direction, io_type
character (len=StrKIND) :: message
integer :: err_local
! For future validation of SIONlib data - need to check that there is only one timestamp per file
type (MPAS_TimeInterval_type) :: time_interval_zero, time_interval_file, time_interval_input, time_interval_output
if (present(streamID)) then
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_validate_streams() for '//trim(streamID))
else
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_validate_streams() for all streams')
end if
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
call mpas_set_timeInterval(time_interval_zero, dt = 0.0_RKIND)
if (present(streamID)) then
call MPAS_stream_mgr_begin_iteration(manager, streamID, ierr=err_local)
if (err_local .eq. MPAS_STREAM_MGR_ERROR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
end if
call MPAS_stream_mgr_begin_iteration(manager)
do while (MPAS_stream_mgr_get_next_stream(manager, streamID = stream_name, directionProperty = direction, &
filenameIntervalProperty = filename_interval))
if (present(streamID)) then
if (trim(streamID) .ne. trim(stream_name)) cycle
end if
STREAM_DEBUG_WRITE('-- Validating stream '//trim(stream_name))
call MPAS_stream_mgr_get_property(manager, stream_name, MPAS_STREAM_PROPERTY_IOTYPE, io_type)
#if 0
! Placeholder for testing additional consistency checks for future SIONlib implementation,
! and demo of how to use MPAS_stream_mgr_get_stream_interval for directions input/output.
if (io_type == MPAS_IO_SIONLIB) then
if (trim(filename_interval) /= 'none') then
call mpas_set_timeInterval(time_interval_file, timeString=filename_interval)
else
call mpas_set_timeInterval(time_interval_file, dt = 0.0_RKIND)
end if
if (direction == MPAS_STREAM_INPUT .or. direction == MPAS_STREAM_INPUT_OUTPUT) then
time_interval_input = MPAS_stream_mgr_get_stream_interval(manager, stream_name, MPAS_STREAM_INPUT, err_local)
if (err_local /= MPAS_STREAM_MGR_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
if (time_interval_input .gt. time_interval_zero) then
if ( (time_interval_file .eq. time_interval_zero) .or. (time_interval_file .gt. time_interval_input) ) then
message = 'Filename interval is larger than input interval for SIONlib stream ''' // trim(stream_name) //''' (only one time stamp per file allowed).'
call mpas_log_write(message, MPAS_LOG_CRIT)
end if
end if
end if
if (direction == MPAS_STREAM_OUTPUT .or. direction == MPAS_STREAM_INPUT_OUTPUT) then
time_interval_output = MPAS_stream_mgr_get_stream_interval(manager, stream_name, MPAS_STREAM_OUTPUT, err_local)
if (err_local /= MPAS_STREAM_MGR_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
if (time_interval_output .gt. time_interval_zero) then
if ( (time_interval_file .eq. time_interval_zero) .or. (time_interval_file .gt. time_interval_output) ) then
message = 'Filename interval is larger than output interval for SIONlib stream ''' // trim(stream_name) //''' (only one time stamp per file allowed).'
call mpas_log_write(message, MPAS_LOG_CRIT)
end if
end if
end if
end if
#endif
end do
! Only check filename_template uniqueness when validating all streams
if (.not. present(streamID)) then
call MPAS_stream_mgr_check_filename_template(manager, ierr=err_local)
if (present(ierr)) ierr = err_local
end if
end subroutine MPAS_stream_mgr_validate_streams!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_check_filename_template
!
!> \brief Check for identical filename templates in active output streams.
!> \author Abishek Gopal
!> \date June 3 2026
!> \details
!> Checks that there are no identical filename templates among active output
!> streams in the stream manager, which may lead to file conflicts. This
!> routine can be called from within MPAS_stream_mgr_validate_streams or
!> separately as needed.
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_check_filename_template(manager, ierr)!{{{
implicit none
type (MPAS_streamManager_type), intent(inout) :: manager
integer, intent(out), optional :: ierr
integer :: threadNum
character (len=StrKIND) :: message, streamID
type (MPAS_stream_list_type), pointer :: stream1_cursor, stream2_cursor
logical :: stream1_pkg_active, stream2_pkg_active
logical :: stream1_output, stream2_output
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_check_filename_template() for all streams')
if (present(ierr)) ierr = MPAS_STREAM_LIST_NOERR
threadNum = mpas_threading_get_thread_num()
if ( threadNum == 0 ) then
streamID = '.*' ! query all streams
nullify(stream1_cursor)
do while (MPAS_stream_list_query(manager % streams, streamID, stream1_cursor))
stream1_pkg_active = stream_active_pkg_check(stream1_cursor)
stream1_output = (stream1_cursor % direction == MPAS_STREAM_OUTPUT) .or. &
(stream1_cursor % direction == MPAS_STREAM_INPUT_OUTPUT)
call mpas_log_write('Stream 1 '//trim(stream1_cursor % name)//' filename '//trim(stream1_cursor % filename_template)//' &
& active = $l output = $l packages_active $l',logicArgs=(/stream1_cursor % active_stream, stream1_output, stream1_pkg_active/))
if (.not. stream1_cursor % active_stream .or. .not. stream1_output &
.or. .not. stream1_pkg_active) then
cycle
end if
nullify(stream2_cursor)
do while (MPAS_stream_list_query(manager % streams, streamID, stream2_cursor))
stream2_pkg_active = stream_active_pkg_check(stream2_cursor)
stream2_output = (stream2_cursor % direction == MPAS_STREAM_OUTPUT) .or. &
(stream2_cursor % direction == MPAS_STREAM_INPUT_OUTPUT)
! uniqueness_check has already checked that two different streams do not have the same name
if (trim(stream1_cursor % name) == trim(stream2_cursor % name) &
.or. .not. stream2_cursor % active_stream &
.or. .not. stream2_output &
.or. .not. stream2_pkg_active) then
cycle
end if
call mpas_log_write('Stream 2 '//trim(stream2_cursor % name)//' filename '//trim(stream2_cursor % filename_template)//' &
& active = $l output = $l pkg_active: $l',logicArgs=(/stream2_cursor % active_stream, stream2_output, stream2_pkg_active/))
if (trim(stream1_cursor % filename_template) == trim(stream2_cursor % filename_template)) then
message = 'Found identical values of the filename_template attribute for multiple &
active output streams (' // trim(stream1_cursor % name) // ' and ' // &
trim(stream2_cursor % name) // ') in streams.<CORE>. This may result in &
file conflicts.'
call mpas_log_write(message)
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
end do
end do
end if
end subroutine MPAS_stream_mgr_check_filename_template!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_destroy_stream
!
!> \brief Free all memory associated with a stream in an MPAS stream manager.
!> \author Michael Duda, Doug Jacobsen
!> \date 13 June 2014
!> \details
!> Destroy the stream, including freeing all memory explicitly associated with the stream.
!> This will not deallocate the memory associated with the fields in the stream.
!> NOTE: This routine does not support regular expressions for StreamID
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_destroy_stream(manager, streamID, ierr)!{{{
implicit none
character (len=*), parameter :: sub = 'MPAS_stream_mgr_destroy_stream'
type (MPAS_streamManager_type), intent(inout) :: manager
character (len=*), intent(in) :: streamID
integer, intent(out), optional :: ierr
integer :: err_local, threadNum
type (MPAS_stream_list_type), pointer :: stream, alarm_cursor, delete_me
threadNum = mpas_threading_get_thread_num()
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_destroy_stream()')
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
if ( threadNum == 0 ) then
!
! Remove stream from list
!
call MPAS_stream_list_remove(manager % streams, streamID, stream, ierr=err_local)
if (err_local /= MPAS_STREAM_LIST_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while removing stream from list')
return
end if
!
! Unlink stream from input alarms
!
alarm_cursor => stream % alarmList_in % head
do while (associated(alarm_cursor))
call MPAS_stream_list_remove(alarm_cursor % xref % streamList, streamID, delete_me, ierr=err_local)
if (err_local == MPAS_STREAM_LIST_NOERR) then
deallocate(delete_me)
else
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while removing stream from list of input alarm')
return
end if
alarm_cursor => alarm_cursor % next
end do
!
! Unlink stream from output alarms
!
alarm_cursor => stream % alarmList_out % head
do while (associated(alarm_cursor))
call MPAS_stream_list_remove(alarm_cursor % xref % streamList, streamID, delete_me, ierr=err_local)
if (err_local == MPAS_STREAM_LIST_NOERR) then
deallocate(delete_me)
else
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while removing stream from list of output alarm')
return
end if
alarm_cursor => alarm_cursor % next
end do
!
! Free up stream storage -- reverse of whatever was done when allocating the stream
!
call MPAS_stream_list_destroy(stream % alarmList_in, ierr=err_local)
call MPAS_stream_list_destroy(stream % alarmList_out, ierr=err_local)
call mpas_pool_destroy_pool(stream % att_pool)
call mpas_pool_destroy_pool(stream % field_pool)
call mpas_pool_destroy_pool(stream % field_pkg_pool)
call mpas_pool_destroy_pool(stream % pkg_pool)
if (associated(stream % referenceTime)) then
deallocate(stream % referenceTime)
end if
if (associated(stream % recordInterval)) then
deallocate(stream % recordInterval)
end if
if (stream % valid) then
call MPAS_closeStream(stream % stream, ierr=err_local)
if (err_local /= MPAS_STREAM_NOERR) then
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
STREAM_ERROR_WRITE('Problems while closing stream '//trim(stream % name))
end if
deallocate(stream % stream)
end if
deallocate(stream)
manager % numStreams = manager % numStreams - 1
end if
end subroutine MPAS_stream_mgr_destroy_stream!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_get_clock
!
!> \brief Retrieves the clock used by the stream manager.
!> \author Michael Duda
!> \date 22 August 2014
!> \details
!> Returns a pointer to the clock associated with the stream manager,
!> in which any stream alarms should be defined before being added to
!> the stream manager via the MPAS_stream_mgr_add_alarm() routine.
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_get_clock(manager, clock, ierr) !{{{
implicit none
type (MPAS_streamManager_type), intent(in) :: manager
type (MPAS_Clock_type), pointer :: clock
integer, intent(out), optional :: ierr
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_get_clock()')
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
clock => manager % streamClock
end subroutine MPAS_stream_mgr_get_clock !}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_add_pool
!
!> \brief Add a pool of fields to the specified stream in an MPAS stream manager.
!> \author Doug Jacobsen, Michael Duda
!> \date 09/15/2014
!> \details
!> Adds a pool from the allStructs pool to the specified stream in an MPAS
!> stream manager. Currently, it adds only explicitly named var's and
!> var_array's to the stream, but commented code will allow adding all nested
!> structs as well. If the optional 'packages' argument is supplied, the
!> specified packages will be attached to all var and var_array members
!> added to the stream.
!> NOTE: This routine does not support regular expressions for StreamID
!
!-----------------------------------------------------------------------
recursive subroutine MPAS_stream_mgr_add_pool(manager, streamID, poolName, packages, ierr)!{{{
implicit none
character (len=*), parameter :: sub = 'MPAS_stream_mgr_add_pool'
type (MPAS_streamManager_type), intent(inout) :: manager
character (len=*), intent(in) :: streamID
character (len=*), intent(in) :: poolName
character (len=*), intent(in), optional :: packages
integer, intent(out), optional :: ierr
type (MPAS_stream_list_type), pointer :: stream
type (mpas_pool_field_info_type) :: info
integer, pointer :: test_ptr
integer :: err_local, threadNum
type (mpas_pool_type), pointer :: fieldPool
type (mpas_pool_iterator_type) :: poolItr
type (field0DReal), pointer :: real0DField
type (field1DReal), pointer :: real1DField
type (field2DReal), pointer :: real2DField
type (field3DReal), pointer :: real3DField
type (field4DReal), pointer :: real4DField
type (field5DReal), pointer :: real5DField
type (field0DInteger), pointer :: int0DField
type (field1DInteger), pointer :: int1DField
type (field2DInteger), pointer :: int2DField
type (field3DInteger), pointer :: int3DField
type (field0DChar), pointer :: char0DField
type (field1DChar), pointer :: char1DField
threadNum = mpas_threading_get_thread_num()
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_add_pool()')
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
if ( threadNum == 0 ) then
!
! Check that stream exists
!
nullify(stream)
if (.not. MPAS_stream_list_query(manager % streams, streamID, stream, ierr=err_local)) then
STREAM_ERROR_WRITE('Requested stream '//trim(streamID)//' does not exist in stream manager')
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
!
! Don't modify an immutable stream
!
if (stream % immutable) then
STREAM_ERROR_WRITE('Requested stream '//trim(streamID)//' is immutable.')
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
!
! Check that the pool exists
!
call mpas_pool_get_subpool(manager % allStructs, poolName, fieldPool)
if (.not. associated(fieldPool) ) then
STREAM_ERROR_WRITE('Requested pool '//trim(poolName)//' does not exist.')
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
!
! Iterate over pool, adding each field to the stream, and recursively calling this subroutine for each subpool
!
call mpas_pool_begin_iteration(fieldPool)
do while (mpas_pool_get_next_member(fieldPool, poolItr))
if (poolItr % memberType == MPAS_POOL_SUBPOOL) then
STREAM_DEBUG_WRITE('-- Try to add subpool...')
! call mpas_stream_mgr_add_pool(manager, streamId, poolItr % memberName, packages=packages, ierr=ierr)
else if (poolItr % memberType == MPAS_POOL_FIELD) then
if (poolItr % dataType == MPAS_POOL_REAL) then
if (poolItr % nDims == 0) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, real0DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, real0DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 1) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, real1DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, real1DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 2) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, real2DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, real2DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 3) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, real3DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, real3DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 4) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, real4DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, real4DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 5) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, real5DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, real5DField % fieldName, packages=packages, ierr=ierr)
end if
else if (poolItr % dataType == MPAS_POOL_INTEGER) then
if (poolItr % nDims == 0) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, int0DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, int0DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 1) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, int1DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, int1DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 2) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, int2DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, int2DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 3) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, int3DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, int3DField % fieldName, packages=packages, ierr=ierr)
end if
else if (poolItr % dataType == MPAS_POOL_CHARACTER) then
if (poolItr % nDims == 0) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, char0DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, char0DField % fieldName, packages=packages, ierr=ierr)
else if (poolItr % nDims == 1) then
call mpas_pool_get_field(fieldPool, poolItr % memberName, char1DField, 1)
call mpas_stream_mgr_add_field(manager, streamID, char1DField % fieldName, packages=packages, ierr=ierr)
end if
end if
end if
end do
end if
end subroutine MPAS_stream_mgr_add_pool!}}}
!-----------------------------------------------------------------------
! routine MPAS_stream_mgr_add_field
!
!> \brief Add a field to the specified stream in an MPAS stream manager.
!> \author Michael Duda, Doug Jacobsen
!> \date 13 June 2014
!> \details
!> Adds a field from the allFields pool to a stream. If the optional
!> argument 'packages' is present, those packages will be attached to
!> the field as well.
!> NOTE: This routine does not support regular expressions for StreamID
!
!-----------------------------------------------------------------------
subroutine MPAS_stream_mgr_add_field(manager, streamID, fieldName, packages, ierr)!{{{
implicit none
character (len=*), parameter :: sub = 'MPAS_stream_mgr_add_field'
type (MPAS_streamManager_type), intent(inout) :: manager
character (len=*), intent(in) :: streamID
character (len=*), intent(in) :: fieldName
character (len=*), intent(in), optional :: packages
integer, intent(out), optional :: ierr
type (MPAS_stream_list_type), pointer :: stream
type (mpas_pool_field_info_type) :: info
character (len=StrKIND) :: field_pkg
integer, pointer :: test_ptr
logical :: test_logical
integer :: err_level
integer :: err_local, threadNum
threadNum = mpas_threading_get_thread_num()
STREAM_DEBUG_WRITE('-- Called MPAS_stream_mgr_add_field()')
if (present(ierr)) ierr = MPAS_STREAM_MGR_NOERR
if ( threadNum == 0 ) then
!
! Check that stream exists
!
nullify(stream)
if (.not. MPAS_stream_list_query(manager % streams, streamID, stream, ierr=err_local)) then
STREAM_ERROR_WRITE('Requested stream '//trim(streamID)//' does not exist in stream manager')
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
!
! Don't modify an immutable stream
!
if (stream % immutable) then
STREAM_ERROR_WRITE('Requested stream '//trim(streamID)//' is immutable.')
if (present(ierr)) ierr = MPAS_STREAM_MGR_ERROR
return
end if
!