File: read.f90

package info (click to toggle)
odc 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,140 kB
  • sloc: cpp: 21,984; f90: 3,707; sh: 966; ansic: 477; python: 389; makefile: 33
file content (1173 lines) | stat: -rw-r--r-- 49,782 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173

module fapi_read_tests

    use odc
    use odc_config
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding, only: c_loc,c_null_char
    implicit none

contains

    subroutine check_call(err, msg, success)
        integer, intent(in) :: err
        character(*), intent(in) :: msg
        logical, intent(inout) :: success

        if (err /= ODC_SUCCESS) then
            write(error_unit, *) 'Failed API call: ', msg
            write(error_unit, *) 'Error: ', odc_error_string(err)
            success = .false.
        end if
    end subroutine

    function test_count_lines() result(success)

        ! Test that we obtain the expected version number

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer(8) :: frame_count, row_count, tmp_8
        integer :: err, tmp_4
        logical :: success

        success = .true.
        call check_call(reader%open_path("../2000010106-reduced.odb"), "open ODB", success)
        call check_call(frame%initialise(reader), "initialise frame", success)

        frame_count = 0
        row_count = 0

        err = frame%next()
        do while (err == ODC_SUCCESS)

            call check_call(frame%row_count(tmp_8), "row count", success)

            frame_count = frame_count + 1
            row_count = row_count + tmp_8

            call check_call(frame%column_count(tmp_4), "column count", success)
            if (tmp_4 /= 51) then
                write(error_unit, *) 'Unexpected column count: ', tmp_4, ' /= 51'
                success = .false.
            endif

            err = frame%next()
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, "next frame", success)

        if (frame_count /= 5) then
            write(error_unit, *) 'Unexpected frame count: ', frame_count, ' /= 5'
            success = .false.
        endif

        if (row_count /= 50000) then
            write(error_unit, *) 'Unexpected row count: ', row_count, ' /= 50000'
            success = .false.
        endif

        call check_call(reader%close(), "close reader", success)

    end function

    function test_column_details() result(success)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        character(:), allocatable :: column_name, field_name
        integer :: ncols, col, column_type, field, field_size, expected_offset, field_offset
        integer :: element_size, element_size_doubles, bitfield_count
        logical :: success

        character(23), parameter :: example_column_names(*) = [ character(23) :: &
            "expver@desc", "andate@desc", "antime@desc", "seqno@hdr", "obstype@hdr", &
            "obschar@hdr", "subtype@hdr", "date@hdr", "time@hdr", "rdbflag@hdr", &
            "status@hdr", "event1@hdr", "blacklist@hdr", "sortbox@hdr", "sitedep@hdr", &
            "statid@hdr", "ident@hdr", "lat@hdr", "lon@hdr", "stalt@hdr", &
            "modoro@hdr", "trlat@hdr", "trlon@hdr", "instspec@hdr", "event2@hdr", &
            "anemoht@hdr", "baroht@hdr", "sensor@hdr", "numlev@hdr", "varno_presence@hdr", &
            "varno@body", "vertco_type@body", "rdbflag@body", "anflag@body", "status@body", &
            "event1@body", "blacklist@body", "entryno@body", "press@body", "press_rl@body", &
            "obsvalue@body", "aux1@body", "event2@body", "ppcode@body", "level@body", &
            "biascorr@body", "final_obs_error@errstat", "obs_error@errstat", "repres_error@errstat", &
            "pers_error@errstat", "fg_error@errstat"]

        integer, parameter :: example_column_types(*) = [ &
            ODC_STRING, ODC_INTEGER, ODC_INTEGER, ODC_INTEGER, ODC_INTEGER, ODC_BITFIELD , &
            ODC_INTEGER, ODC_INTEGER, ODC_INTEGER, ODC_BITFIELD , ODC_BITFIELD , ODC_BITFIELD , &
            ODC_BITFIELD , ODC_INTEGER, ODC_INTEGER, ODC_STRING, ODC_INTEGER, ODC_REAL, &
            ODC_REAL, ODC_REAL, ODC_REAL, ODC_REAL, ODC_REAL, ODC_INTEGER, &
            ODC_INTEGER, ODC_REAL, ODC_REAL, ODC_INTEGER, ODC_INTEGER, ODC_BITFIELD , &
            ODC_INTEGER, ODC_INTEGER, ODC_BITFIELD , ODC_BITFIELD , ODC_BITFIELD , ODC_BITFIELD , &
            ODC_BITFIELD , ODC_INTEGER, ODC_REAL, ODC_REAL, ODC_REAL, ODC_REAL, &
            ODC_INTEGER, ODC_INTEGER, ODC_BITFIELD , ODC_REAL, ODC_REAL, ODC_REAL, &
            ODC_REAL, ODC_REAL, ODC_REAL]


        character(14), parameter :: column_10_bitfield_names(*) = [ character(14) :: &
            "lat_humon", "lat_qcsub", "lat_override", "lat_flag", "lat_hqc_flag", "lon_humon", "lon_qcsub", &
            "lon_override", "lon_flag", "lon_hqc_flag", "date_humon", "date_qcsub", "date_override", &
            "date_flag", "date_hqc_flag", "time_humon", "time_qcsub", "time_override", "time_flag", &
            "time_hqc_flag", "stalt_humon", "stalt_qcsub", "stalt_override", "stalt_flag", "stalt_hqc_flag" &
        ]

        integer, parameter :: column_10_bitfield_sizes(*) = [ &
            1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1]

        success = .true.
        call check_call(reader%open_path("../2000010106-reduced.odb"), "open reader", success)
        call check_call(frame%initialise(reader), "initialise frame", success)

        call check_call(frame%next(), "get the first frame", success)

        call check_call(frame%column_count(ncols), "column count", success)
        if (ncols /= 51) then
            write(error_unit, *) 'Expected 51 columns'
            success = .false.
        endif

        ! n.b. -- 1-based indexing!
        do col = 1, ncols

            call check_call(frame%column_attributes(col, &
                                                    name=column_name, &
                                                    type=column_type, &
                                                    element_size=element_size, &
                                                    element_size_doubles=element_size_doubles, &
                                                    bitfield_count=bitfield_count), "column attrs", success)

            if (column_name /= trim(example_column_names(col))) then
                write(error_unit,'(3a,i2,3a)') 'Unexpected column name ', column_name, &
                           ' for column ', col, ' (expected ', trim(example_column_names(col)), ')'
                success = .false.
            end if

            if (column_type /= example_column_types(col)) then
                write(error_unit, '(a,i1,a,i2,a,i1,a)') 'Unexpected column type ', column_type, &
                        ' for column ', col, ' (expected ', example_column_types(col), ')'
                success = .false.
            end if

            if (element_size /= 8) then
                write(error_unit, '(a,i1,a,i2,a)') 'Unexpected column data size ', element_size, &
                        ' for column ', col, ' (expected 8)'
                success = .false.
            end if

            if (element_size_doubles /= 1) then
                write(error_unit, '(a,i1,a,i2,a)') 'Unexpected column doubles data size ', &
                        element_size_doubles, ' for column ', col, ' (expected 1)'
                success = .false.
            end if

            if (column_type == ODC_BITFIELD) then
                if (bitfield_count <= 0) then
                    write(error_unit, *) "Bitfields expected for bitfield column"
                    success = .false.
                end if
            else
                if (bitfield_count /= 0) then
                    write(error_unit, *) "Unexpected bitfields for non-bitfield column"
                    success = .false.
                end if
            end if

        end do

        ! Test bitfields for column 10

        call check_call(frame%column_attributes(10, bitfield_count=bitfield_count), "bitfield count", success)
        if (bitfield_count /= 25) then
            write(error_unit, *) "Expected 25 bitfield fields for column 10. Got ", bitfield_count
            success = .false.
        end if

        expected_offset = 0
        do field = 1, 25

            ! Look at column 10
            call check_call(frame%bitfield_attributes(10, field, &
                                                      name=field_name, &
                                                      offset=field_offset, &
                                                      size=field_size), "bitfield attrs", success)

            if (field_name /= trim(column_10_bitfield_names(field))) then
                write(error_unit, '(3a,i2,3a)') 'Unexpected field name ', field_name, ' for field ', &
                        field, ' (expected ', trim(column_10_bitfield_names(field)), ')'
                success = .false.
            end if

            if (field_size /= column_10_bitfield_sizes(field)) then
                write(error_unit, '(a,i2,a,i2,a,i2,a)') 'Unexpected field size ', field_size, &
                        ' for field ', field, ' (expected ', column_10_bitfield_sizes(field), ')'
                success = .false.
            end if

            if (field_offset /= expected_offset) then
                write(error_unit, '(a,i2,a,i2,a,i2,a)') 'Unexpected field offset ', field_offset, &
                        ' for field ', field, ' (expected ', expected_offset, ')'
                success = .false.
            end if

            expected_offset = expected_offset + field_size
        end do

        call check_call(frame%free(), "free frame", success)
        call check_call(reader%close(), "close reader", success)
    end function

    function check_frame_2_values(array_data) result(success)

        real(8) :: array_data(:,:)
        logical :: success

        integer :: row, i
        integer, parameter :: expected_seqno(*) = [6106691, 6002945, 6003233, 6105819]
        integer, parameter :: expected_obschar(*) = [537918674, 135265490, 135265490, 537918674]
        integer(8) :: missing_integer
        real(8) :: missing_double

        success = .true.

        call check_call(odc_missing_integer(missing_integer), "missing integer", success)
        call check_call(odc_missing_double(missing_double), "missing double", success)

        do i = 1, 4
            row = 1 + ((i-1) * 765)

            ! Expver
            if (trim(transfer(array_data(row, 1), "        ")) /= "0018") then
                write(error_unit, *) 'unexpected expver in row ', row, ' (expected 0018, got ', &
                                     transfer(array_data(row, 1), "        ") ,')'
                success = .false.
            end if

            ! Test seqno (INTEGER)
            if (int(array_data(row, 4)) /= expected_seqno(i)) then
                write(error_unit, *) 'Unexpected seqno value. row=', row, ", expected=", &
                                     expected_seqno(i), ", got=", int(array_data(row, 4))
                success = .false.
            end if

            ! obschar (BITFIELD)
            if (int(array_data(row, 6)) /= expected_obschar(i)) then
                write(error_unit, *) 'Unexpected obschar value. row=', row, ", expected=", &
                                     expected_obschar(i), ", got=", int(array_data(row, 6))
                success = .false.
            end if

            ! Sortbox (INTEGER, missing)
            if (int(array_data(row, 14)) /= missing_integer) then
                write(error_unit, *) 'Expected value with set missing value. Got ', int(array_data(row, 14)), ', &
                                     &expected ', missing_integer
                success = .false.
            end if

            ! repres_error (REAL, missing)
            if (array_data(row, 49) /= missing_double) then
                write(error_unit, *) 'Expected value with set missing value. Got ', array_data(row, 49), ', &
                                     &expected ', missing_double
                success = .false.
            end if
        end do

    end function

    function test_decode_columns_allocate() result(success)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        type(odc_decoder) :: decoder
        integer(8) :: nrows, nrows2
        integer :: ncols
        logical :: success, column_major
        real(8), pointer :: array_data(:,:)

        success =.true.
        call check_call(reader%open_path("../2000010106-reduced.odb"), "open reader", success)
        call check_call(frame%initialise(reader), "initialise frame", success)

        ! Read the second frame, because why not.
        call check_call(frame%next(), "get first frame", success)
        call check_call(frame%next(), "get second frame", success)

        call check_call(decoder%initialise(), "initialise decoder", success)
        call check_call(decoder%defaults_from_frame(frame), "decoder from frame", success)
        call check_call(decoder%decode(frame, nrows), "do decode", success)

        if (nrows /= 10000) then
            write(error_unit, *) 'Unexpected number of rows decoded'
            success = .false.
        end if

        call check_call(decoder%row_count(nrows2), "decoder row count", success)
        if (nrows2 /= 10000) then
            write(error_unit, *) 'Got row count ', nrows, ' not 10000'
            success = .false.
        end if

        call check_call(decoder%column_count(ncols), "decoder column count", success)
        if (ncols /= 51) then
            write(error_unit, *) 'Got column count ', ncols, ' not 51'
            success = .false.
        end if

        call check_call(decoder%data(array_data, column_major), "get decoded data", success)

        if (any(shape(array_data) /= [10000, 51])) then
            write(error_unit, *) 'Unexpected data dimensions'
            success = .false.
        end if

        if (.not. column_major) then
            write(error_unit, *) 'Expected column major by default'
            success = .false.
        end if

        success = success .and. check_frame_2_values(array_data)

        call check_call(decoder%free(), "free decoder", success)
        call check_call(reader%close(), "free reader", success)

    end function

    function test_decode_array_reuse() result(success)

        use, intrinsic :: iso_c_binding

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        type(odc_decoder) :: decoder
        integer(8) :: rows_decoded, nrows
        integer :: ncols
        logical :: success
        real(8), target :: array_data(11000, 51)

        success = .true.
        call check_call(reader%open_path("../2000010106-reduced.odb"), "open reader", success)
        call check_call(frame%initialise(reader), "initialise frame", success)

        call check_call(frame%next(), "get first frame", success)

        call check_call(decoder%initialise(), "initialise decoder", success)
        call check_call(decoder%defaults_from_frame(frame), "decoder frame defaults", success)
        call check_call(decoder%set_data(array_data), "set array data", success)
        call check_call(decoder%decode(frame, rows_decoded), "decode first frame", success)

        if (rows_decoded /= 10000) then
            write(error_unit, *) 'Unexpected number of rows decoded'
            success = .false.
        end if

        call check_call(frame%next(), "get second frame", success)

        call check_call(decoder%decode(frame, rows_decoded), "decode second frame", success)

        if (rows_decoded /= 10000) then
            write(error_unit, *) 'Unexpected number of rows decoded'
            success = .false.
        end if

        call check_call(decoder%row_count(nrows), "decoder row count", success)
        if (nrows /= 11000) then
            write(error_unit, *) 'Got row count ', nrows, ' not 11000'
            write(error_unit, *) 'Row count should be related to the size of the decode target, not the decode data'
            success = .false.
        end if

        call check_call(decoder%column_count(ncols), "decoder column count", success)
        if (ncols /= 51) then
            write(error_unit, *) 'Got column count ', ncols, ' not 51'
            success = .false.
        end if

        success = success .and. check_frame_2_values(array_data)

        call check_call(decoder%free(), "free decoder", success)
        call check_call(reader%close(), "close reader", success)

    end function

    function test_decode_aggregate() result(success)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        type(odc_decoder) :: decoder
        integer(8) :: rows_decoded, nrows
        integer :: ncols
        logical :: success
        real(8), pointer :: array_data(:,:)

        success = .true.
        call check_call(reader%open_path("../2000010106-reduced.odb"), "open reader", success)
        call check_call(frame%initialise(reader), "initialise frame", success)

        call check_call(frame%next(maximum_rows=99999_8), "get first (aggregate) frame", success)

        call check_call(decoder%initialise(), "initialise decoder", success)
        call check_call(decoder%defaults_from_frame(frame), "decoder frame defaults", success)
        call check_call(decoder%decode(frame, rows_decoded, nthreads=4), "decode threaded", success)

        if (rows_decoded /= 50000) then
            write(error_unit, *) 'Unexpected number of rows decoded'
            success = .false.
        end if

        call check_call(decoder%row_count(nrows), "decoder row count", success)
        if (nrows /= 50000) then
            write(error_unit, *) 'Got row count ', nrows, ' not 50000'
            success = .false.
        end if

        call check_call(decoder%column_count(ncols), "decoder column count", success)
        if (ncols /= 51) then
            write(error_unit, *) 'Got column count ', ncols, ' not 51'
            success = .false.
        end if

        call check_call(decoder%data(array_data), "get array data", success)

        if (any(shape(array_data) /= [50000, 51])) then
            write(error_unit, *) 'Unexpected data dimensions'
            success = .false.
        end if

        call check_call(decoder%free(), "free decoder", success)
        call check_call(reader%close(), "close reader", success)

    end function

    function test_frame_properties_1_non_aggregated() result(success)

        ! Where the properties in the two frames are distinct (non-aggregated)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer :: err, nframes = 1, nproperties, idx
        logical :: aggregated = .false.
        character(:), allocatable, target :: version, key, val
        character(255) :: version_str
        logical :: success, exists
        success = .true.

        call test_generate_odb('properties-1.odb', 1, success)

        call check_call(reader%open_path('properties-1.odb'), 'opening path', success)
        call check_call(frame%initialise(reader), 'initialising frame', success)

        call check_call(odc_version(version), 'getting version number', success)
        write(version_str, *) 'odc version ', version
        version_str = trim(adjustl(version_str))

        ! Advance to the first frame in the stream in non-aggregated mode
        err = frame%next(aggregated)

        do while (err == ODC_SUCCESS)
            call check_call(frame%properties_count(nproperties), 'getting properties count', success)

            ! Check properties count
            if (nproperties /= 2) then
                write(error_unit, *) 'unexpected number of properties:', nproperties, '/=', 2
                success = .false.
            end if

            do idx = 1, 2
                call check_call(frame%property_idx(idx, key, val), 'getting property by index', success)

                ! Check getting properties by index
                if ((idx == 1 .and. nframes == 1) .or. (idx == 2 .and. nframes == 2)) then
                    if (key /= 'encoder' .or. val /= version_str) then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= encoder => ', version_str
                        success = .false.
                    end if
                else if (nframes == 1) then
                    if (key /= 'foo' .or. val /= 'bar') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => bar'
                        success = .false.
                    end if
                else
                    if (key /= 'baz' .or. val /= 'qux') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= baz => qux'
                        success = .false.
                    end if
                end if

                ! Check getting property values by key
                if (nframes == 1) then
                    call check_call(frame%property('encoder', val), 'getting property by key', success)
                    if (val /= version_str) then
                        write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                        success = .false.
                    end if

                    call check_call(frame%property('foo', val), 'getting property by key', success)
                    if (val /= 'bar') then
                        write(error_unit, *) 'unexpected property value for foo: ', val , ' /= bar'
                    end if
                else
                    call check_call(frame%property('encoder', val), 'getting property by key', success)
                    if (val /= version_str) then
                        write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                        success = .false.
                    end if

                    call check_call(frame%property('baz', val), 'getting property by key', success)
                    if (val /= 'qux') then
                        write(error_unit, *) 'unexpected property value for baz: ', val , ' /= qux'
                        success = .false.
                    end if
                end if

                ! Check for reading of non-existent properties
                call check_call(frame%property('non-existent', exists=exists), 'getting property by key', success)
                if (exists) then
                    write(error_unit, *) 'unexpected non-existent property: ', exists, ' /= .false.'
                    success = .false.
                end if
            end do

            nframes = nframes + 1

            ! Advances to the next frame in the stream in non-aggregated mode
            err = frame%next(aggregated)
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, 'get next frame', success)

        call check_call(reader%close(), 'closing reader', success)

        ! Check number of frames
        if (nframes - 1 /= 2) then
            write(error_unit, *) 'unexpected number of frames:', nframes - 1, '/=', 2
            success = .false.
        end if

    end function

    function test_frame_properties_1_aggregated() result(success)

        ! Where the properties in the two frames are distinct (aggregated)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer :: err, nframes = 1, nproperties, idx
        logical :: aggregated = .true.
        character(:), allocatable, target :: version, key, val
        character(255) :: version_str
        logical :: success, exists
        success = .true.

        call test_check_file_exists('properties-1.odb', success)

        call check_call(reader%open_path('properties-1.odb'), 'opening path', success)
        call check_call(frame%initialise(reader), 'initialising frame', success)

        call check_call(odc_version(version), 'getting version number', success)
        write(version_str, *) 'odc version ', version
        version_str = trim(adjustl(version_str))

        ! Advance to the first frame in the stream in aggregated mode
        err = frame%next(aggregated)

        do while (err == ODC_SUCCESS)
            call check_call(frame%properties_count(nproperties), 'getting properties count', success)

            ! Check properties count
            if (nproperties /= 3) then
                write(error_unit, *) 'unexpected number of properties:', nproperties, '/=', 3
                success = .false.
            end if

            do idx = 1, 3
                call check_call(frame%property_idx(idx, key, val), 'getting property by index', success)

                ! Check getting properties by index
                if (idx == 1) then
                    if (key /= 'baz' .or. val /= 'qux') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= baz => qux'
                        success = .false.
                    end if
                else if (idx == 2) then
                    if (key /= 'encoder' .or. val /= version_str) then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= encoder => ', version_str
                        success = .false.
                    end if
                else
                    if (key /= 'foo' .or. val /= 'bar') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => bar'
                        success = .false.
                    end if
                end if

                ! Check getting property values by key
                call check_call(frame%property('encoder', val), 'getting property by key', success)
                if (val /= version_str) then
                    write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                    success = .false.
                end if

                call check_call(frame%property('foo', val), 'getting property by key', success)
                if (val /= 'bar') then
                    write(error_unit, *) 'unexpected property value for foo: ', val , ' /= bar'
                    success = .false.
                end if

                call check_call(frame%property('baz', val), 'getting property by key', success)
                if (val /= 'qux') then
                    write(error_unit, *) 'unexpected property value for baz: ', val , ' /= qux'
                    success = .false.
                end if

                ! Check for reading of non-existent properties
                call check_call(frame%property('non-existent', exists=exists), 'getting property by key', success)
                if (exists) then
                    write(error_unit, *) 'unexpected non-existent property: ', exists, ' /= .false.'
                    success = .false.
                end if
            end do

            nframes = nframes + 1

            ! Advances to the next frame in the stream in aggregated mode
            err = frame%next(aggregated)
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, 'get next frame', success)

        call check_call(reader%close(), 'closing reader', success)

        ! Check number of frames
        if (nframes - 1 /= 1) then
            write(error_unit, *) 'unexpected number of frames:', nframes - 1, '/=', 1
            success = .false.
        end if

    end function

    function test_frame_properties_2_non_aggregated() result(success)

        ! Where the properties in the two frames overlap with entries that are the same (non-aggregated)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer :: err, nframes = 1, nproperties, idx
        logical :: aggregated = .false.
        character(:), allocatable, target :: version, key, val
        character(255) :: version_str
        logical :: success, exists
        success = .true.

        call test_generate_odb('properties-2.odb', 2, success)

        call check_call(reader%open_path('properties-2.odb'), 'opening path', success)
        call check_call(frame%initialise(reader), 'initialising frame', success)

        call check_call(odc_version(version), 'getting version number', success)
        write(version_str, *) 'odc version ', version
        version_str = trim(adjustl(version_str))

        ! Advance to the first frame in the stream in non-aggregated mode
        err = frame%next(aggregated)

        do while (err == ODC_SUCCESS)
            call check_call(frame%properties_count(nproperties), 'getting properties count', success)

            ! Check properties count
            if (nproperties /= 3) then
                write(error_unit, *) 'unexpected number of properties:', nproperties, '/=', 3
                success = .false.
            end if

            do idx = 1, 3
                call check_call(frame%property_idx(idx, key, val), 'getting property by index', success)

                ! Check getting properties by index
                if (idx == 1) then
                    if (key /= 'baz' .or. val /= 'qux') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= baz => qux'
                        success = .false.
                    end if
                else if (idx == 2) then
                    if (key /= 'encoder' .or. val /= version_str) then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= encoder => ', version_str
                        success = .false.
                    end if
                else
                    if (key /= 'foo' .or. val /= 'bar') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => bar'
                        success = .false.
                    end if
                end if

                ! Check getting property values by key
                call check_call(frame%property('encoder', val), 'getting property by key', success)
                if (val /= version_str) then
                    write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                    success = .false.
                end if

                call check_call(frame%property('foo', val), 'getting property by key', success)
                if (val /= 'bar') then
                    write(error_unit, *) 'unexpected property value for foo: ', val , ' /= bar'
                    success = .false.
                end if

                call check_call(frame%property('baz', val), 'getting property by key', success)
                if (val /= 'qux') then
                    write(error_unit, *) 'unexpected property value for baz: ', val , ' /= qux'
                    success = .false.
                end if

                ! Check for reading of non-existent properties
                call check_call(frame%property('non-existent', exists=exists), 'getting property by key', success)
                if (exists) then
                    write(error_unit, *) 'unexpected non-existent property: ', exists, ' /= .false.'
                    success = .false.
                end if
            end do

            nframes = nframes + 1

            ! Advances to the next frame in the stream in non-aggregated mode
            err = frame%next(aggregated)
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, 'get next frame', success)

        call check_call(reader%close(), 'closing reader', success)

        ! Check number of frames
        if (nframes - 1 /= 2) then
            write(error_unit, *) 'unexpected number of frames:', nframes - 1, '/=', 2
            success = .false.
        end if

    end function

    function test_frame_properties_2_aggregated() result(success)

        ! Where the properties in the two frames overlap with entries that are the same (aggregated)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer :: err, nframes = 1, nproperties, idx
        logical :: aggregated = .true.
        character(:), allocatable, target :: version, key, val
        character(255) :: version_str
        logical :: success, exists
        success = .true.

        call test_check_file_exists('properties-2.odb', success)

        call check_call(reader%open_path('properties-2.odb'), 'opening path', success)
        call check_call(frame%initialise(reader), 'initialising frame', success)

        call check_call(odc_version(version), 'getting version number', success)
        write(version_str, *) 'odc version ', version
        version_str = trim(adjustl(version_str))

        ! Advance to the first frame in the stream in aggregated mode
        err = frame%next(aggregated)

        do while (err == ODC_SUCCESS)
            call check_call(frame%properties_count(nproperties), 'getting properties count', success)

            ! Check properties count
            if (nproperties /= 3) then
                write(error_unit, *) 'unexpected number of properties:', nproperties, '/=', 3
                success = .false.
            end if

            do idx = 1, 3
                call check_call(frame%property_idx(idx, key, val), 'getting property by index', success)

                ! Check getting properties by index
                if (idx == 1) then
                    if (key /= 'baz' .or. val /= 'qux') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= baz => qux'
                        success = .false.
                    end if
                else if (idx == 2) then
                    if (key /= 'encoder' .or. val /= version_str) then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= encoder => ', version_str
                        success = .false.
                    end if
                else
                    if (key /= 'foo' .or. val /= 'bar') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => bar'
                        success = .false.
                    end if
                end if

                ! Check getting property values by key
                call check_call(frame%property('encoder', val), 'getting property by key', success)
                if (val /= version_str) then
                    write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                    success = .false.
                end if

                call check_call(frame%property('foo', val), 'getting property by key', success)
                if (val /= 'bar') then
                    write(error_unit, *) 'unexpected property value for foo: ', val , ' /= bar'
                    success = .false.
                end if

                call check_call(frame%property('baz', val), 'getting property by key', success)
                if (val /= 'qux') then
                    write(error_unit, *) 'unexpected property value for baz: ', val , ' /= qux'
                    success = .false.
                end if

                ! Check for reading of non-existent properties
                call check_call(frame%property('non-existent', exists=exists), 'getting property by key', success)
                if (exists) then
                    write(error_unit, *) 'unexpected non-existent property: ', exists, ' /= .false.'
                    success = .false.
                end if
            end do

            nframes = nframes + 1

            ! Advances to the next frame in the stream in aggregated mode
            err = frame%next(aggregated)
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, 'get next frame', success)

        call check_call(reader%close(), 'closing reader', success)

        ! Check number of frames
        if (nframes - 1 /= 1) then
            write(error_unit, *) 'unexpected number of frames:', nframes - 1, '/=', 1
            success = .false.
        end if

    end function

    function test_frame_properties_3_non_aggregated() result(success)

        ! Where the properties overlap with entries whose keys are the same, but the values different (non-aggregated)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer :: err, nframes = 1, nproperties, idx
        logical :: aggregated = .false.
        character(:), allocatable, target :: version, key, val
        character(255) :: version_str
        logical :: success, exists
        success = .true.

        call test_generate_odb('properties-3.odb', 3, success)

        call check_call(reader%open_path('properties-3.odb'), 'opening path', success)
        call check_call(frame%initialise(reader), 'initialising frame', success)

        call check_call(odc_version(version), 'getting version number', success)
        write(version_str, *) 'odc version ', version
        version_str = trim(adjustl(version_str))

        ! Advance to the first frame in the stream in non-aggregated mode
        err = frame%next(aggregated)

        do while (err == ODC_SUCCESS)
            call check_call(frame%properties_count(nproperties), 'getting properties count', success)

            ! Check properties count
            if (nproperties /= 2) then
                write(error_unit, *) 'unexpected number of properties:', nproperties, '/=', 2
                success = .false.
            end if

            do idx = 1, 2
                call check_call(frame%property_idx(idx, key, val), 'getting property by index', success)

                ! Check getting properties by index
                if (idx == 1) then
                    if (key /= 'encoder' .or. val /= version_str) then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= encoder => ', version_str
                        success = .false.
                    end if
                else if (nframes == 1) then
                    if (key /= 'foo' .or. val /= 'bar') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => bar'
                        success = .false.
                    end if
                else
                    if (key /= 'foo' .or. val /= 'baz') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => baz'
                        success = .false.
                    end if
                end if

                ! Check getting property values by key
                if (nframes == 1) then
                    call check_call(frame%property('encoder', val), 'getting property by key', success)
                    if (val /= version_str) then
                        write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                        success = .false.
                    end if

                    call check_call(frame%property('foo', val), 'getting property by key', success)
                    if (val /= 'bar') then
                        write(error_unit, *) 'unexpected property value for foo: ', val , ' /= bar'
                        success = .false.
                    end if
                else
                    call check_call(frame%property('encoder', val), 'getting property by key', success)
                    if (val /= version_str) then
                        write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                        success = .false.
                    end if

                    call check_call(frame%property('foo', val), 'getting property by key', success)
                    if (val /= 'baz') then
                        write(error_unit, *) 'unexpected property value for foo: ', val , ' /= baz'
                        success = .false.
                    end if
                end if

                ! Check for reading of non-existent properties
                call check_call(frame%property('non-existent', exists=exists), 'getting property by key', success)
                if (exists) then
                    write(error_unit, *) 'unexpected non-existent property: ', exists, ' /= F'
                    success = .false.
                end if
            end do

            nframes = nframes + 1

            ! Advances to the next frame in the stream in non-aggregated mode
            err = frame%next(aggregated)
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, 'get next frame', success)

        call check_call(reader%close(), 'closing reader', success)

        ! Check number of frames
        if (nframes - 1 /= 2) then
            write(error_unit, *) 'unexpected number of frames:', nframes - 1, '/=', 2
            success = .false.
        end if

    end function

    function test_frame_properties_3_aggregated() result(success)

        ! Where the properties overlap with entries whose keys are the same, but the values different (aggregated)

        type(odc_reader) :: reader
        type(odc_frame) :: frame
        integer :: err, nframes = 1, nproperties, idx
        logical :: aggregated = .true.
        character(:), allocatable, target :: version, key, val
        character(255) :: version_str
        logical :: success, exists
        success = .true.

        call test_check_file_exists('properties-3.odb', success)

        call check_call(reader%open_path('properties-3.odb'), 'opening path', success)
        call check_call(frame%initialise(reader), 'initialising frame', success)

        call check_call(odc_version(version), 'getting version number', success)
        write(version_str, *) 'odc version ', version
        version_str = trim(adjustl(version_str))

        ! Advance to the first frame in the stream in aggregated mode
        err = frame%next(aggregated)

        do while (err == ODC_SUCCESS)
            call check_call(frame%properties_count(nproperties), 'getting properties count', success)

            ! Check properties count
            if (nproperties /= 2) then
                write(error_unit, *) 'unexpected number of properties:', nproperties, '/=', 2
                success = .false.
            end if

            do idx = 1, 2
                call check_call(frame%property_idx(idx, key, val), 'getting property by index', success)

                ! Check getting properties by index
                if (idx == 1) then
                    if (key /= 'encoder' .or. val /= version_str) then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= encoder => ', version_str
                        success = .false.
                    end if
                else
                    ! Value from the first frame will win
                    if (key /= 'foo' .or. val /= 'bar') then
                        write(error_unit, *) 'unexpected property: ', key, ' => ', val , ' /= foo => bar'
                        success = .false.
                    end if
                end if

                ! Check getting property values by key
                call check_call(frame%property('encoder', val), 'getting property by key', success)
                if (val /= version_str) then
                    write(error_unit, *) 'unexpected property value for encoder: ', val , ' /= ', version_str
                    success = .false.
                end if

                call check_call(frame%property('foo', val), 'getting property by key', success)

                ! Value from the first frame will win
                if (val /= 'bar') then
                    write(error_unit, *) 'unexpected property value for foo: ', val , ' /= bar'
                    success = .false.
                end if

                ! Check for reading of non-existent properties
                call check_call(frame%property('non-existent', exists=exists), 'getting property by key', success)
                if (exists) then
                    write(error_unit, *) 'unexpected non-existent property: ', exists, ' /= .false.'
                    success = .false.
                end if
            end do

            nframes = nframes + 1

            ! Advances to the next frame in the stream in aggregated mode
            err = frame%next(aggregated)
        end do

        if (err /= ODC_ITERATION_COMPLETE) call check_call(err, 'get next frame', success)

        call check_call(reader%close(), 'closing reader', success)

        ! Check number of frames
        if (nframes - 1 /= 1) then
            write(error_unit, *) 'unexpected number of frames:', nframes - 1, '/=', 1
            success = .false.
        end if

    end function

    subroutine test_check_file_exists(path, success)
        character(*), intent(in) :: path
        logical, intent(inout) :: success

        inquire(file=path, exist=success)

        if (.not. success) then
            write(error_unit, *) 'unexpected missing file: ', path
        end if
    end subroutine

    subroutine test_generate_odb(path, properties_mode, success)
        character(*), intent(in) :: path
        integer, intent(in) :: properties_mode
        logical, intent(inout) :: success

        integer(8), parameter :: nrows = 10
        character(8), target :: data1(nrows)
        integer(8), target :: data2(nrows)
        real(8), target :: data3(nrows)

        character(4) :: expver_str = 'xxxx'
        integer(8) :: date = 20210401
        integer :: i

        type(odc_encoder) :: encoder
        integer, target :: outunit
        integer(8), target :: bytes_written

        ! Set treatment of integers as longs
        call check_call(odc_integer_behaviour(ODC_INTEGERS_AS_LONGS), 'setting integer behaviour to longs', success)

        ! Fill in the passed data arrays with scratch values
        do i = 1, nrows
            data1(i) = expver_str // c_null_char  ! expver
            data2(i) = date  ! date@hdr
            data3(i) = 12.3456 * (i - 1)  ! obsvalue@body
        end do

        ! Encode ODB-2 into a file
        open(newunit=outunit, file=path, access='stream', form='unformatted', status='replace')

        ! Encode two ODB-2 frames with the same data
        do i = 1, 2

            ! Initialise encoder
            call check_call(encoder%initialise(), 'initialising encoder', success)

            ! Set number of rows to allocate in the encoder
            call check_call(encoder%set_row_count(nrows), 'setting number of rows', success)

            ! Define all column names and their types
            call check_call(encoder%add_column('expver', ODC_STRING), 'adding expver column', success)
            call check_call(encoder%add_column('date@hdr', ODC_INTEGER), 'adding date@hdr column', success)
            call check_call(encoder%add_column('obsvalue@body', ODC_REAL), 'adding obsvalue@body column', success)

            ! Set a custom data layout and data array for each column
            call check_call(encoder%column_set_data_array(1, 8, stride=8, data=c_loc(data1)), 'setting expver array', success)
            call check_call(encoder%column_set_data_array(2, 8, stride=8, data=c_loc(data2)), 'setting date array', success)
            call check_call(encoder%column_set_data_array(3, 8, stride=8, data=c_loc(data3)), 'setting obsvalue array', success)

            ! Encode additional properties depending on the current mode

            select case(properties_mode)

                ! Where the properties in the two frames are distinct
                case (1)
                    if (i == 1) then
                        call check_call(encoder%add_property('foo', 'bar'), 'adding property', success)
                    else
                        call check_call(encoder%add_property('baz', 'qux'), 'adding property', success)
                    end if

                ! Where the properties in the two frames overlap with entries that are the same
                case (2)
                    call check_call(encoder%add_property('foo', 'bar'), 'adding property', success)
                    call check_call(encoder%add_property('baz', 'qux'), 'adding property', success)

                ! Where the properties overlap with entries whose keys are the same, but the values different
                case (3)
                    if (i == 1) then
                        call check_call(encoder%add_property('foo', 'bar'), 'adding property', success)
                    else
                        call check_call(encoder%add_property('foo', 'baz'), 'adding property', success)
                    end if

            end select

            call check_call(encoder%encode(outunit, bytes_written), 'do encode', success)

            ! Deallocate memory used up by the encoder
            call check_call(encoder%free(), 'cleaning up encoder', success)
        end do

        close(outunit)

    end subroutine

end module


program fapi_general

    use fapi_read_tests
    implicit none

    logical :: success

    success = .true.
    call check_call(odc_initialise_api(), "initialise api", success)

    success = test_count_lines() .and. success
    success = test_column_details() .and. success
    success = test_decode_columns_allocate() .and. success
    success = test_decode_array_reuse() .and. success
    success = test_decode_aggregate() .and. success
    success = test_frame_properties_1_non_aggregated() .and. success
    success = test_frame_properties_1_aggregated() .and. success
    success = test_frame_properties_2_non_aggregated() .and. success
    success = test_frame_properties_2_aggregated() .and. success
    success = test_frame_properties_3_non_aggregated() .and. success
    success = test_frame_properties_3_aggregated() .and. success

    if (.not. success) stop -1

end program