|
1 | | -program scalar_test |
| 1 | +program test_scalar |
2 | 2 |
|
3 | 3 | use, intrinsic:: iso_fortran_env, only: int64, int32, real32, real64, stderr=>error_unit |
| 4 | + |
4 | 5 | use nc4fortran, only : netcdf_file |
| 6 | +use netcdf, only : NF90_INT, NF90_INT64 |
5 | 7 |
|
6 | 8 | implicit none (type, external) |
7 | 9 |
|
| 10 | +character(*), parameter :: fn = 'test_scalar.nc' |
| 11 | + |
| 12 | +call test_scalar_write(fn) |
| 13 | +print *, 'OK: scalar and vector: write and rewrite' |
| 14 | + |
| 15 | +call test_scalar_read(fN) |
| 16 | +print *, 'OK: scalar and vector: read' |
| 17 | + |
| 18 | +contains |
| 19 | + |
| 20 | +subroutine test_scalar_write(fn) |
| 21 | + |
| 22 | +character(*), intent(in) :: fn |
| 23 | + |
8 | 24 | type(netcdf_file) :: h |
9 | | -real(real32), allocatable :: rr1(:) |
10 | | -real(real32) :: rt, r1(4) |
11 | | -integer(int32) :: it, i1(4) |
12 | | -integer(int32), allocatable :: i1t(:) |
13 | | -integer, allocatable :: dims(:) |
| 25 | + |
| 26 | +real(real32) :: r1(4) |
| 27 | +integer(int32) :: i1(4) |
| 28 | +integer(int64) :: i1_64(4) |
| 29 | + |
14 | 30 | integer :: i |
15 | | -character(*), parameter :: fn = 'test_scalar.nc' |
16 | 31 |
|
17 | 32 | do i = 1,size(i1) |
18 | 33 | i1(i) = i |
19 | 34 | enddo |
20 | 35 |
|
21 | 36 | r1 = i1 |
| 37 | +i1_64 = i1 |
22 | 38 |
|
23 | 39 | !> write |
24 | 40 | call h%open(fn, action='w') |
25 | 41 | !> scalar tests |
26 | 42 | call h%write('scalar_int32', 42_int32) |
27 | | - |
| 43 | +call h%write('scalar_int64', 42_int64) |
28 | 44 | call h%write('scalar_real32', -1._real32) |
29 | | -call h%write('scalar_real32', 42._real32) |
| 45 | +call h%write('scalar_real64', -1._real64) |
30 | 46 |
|
31 | 47 | !> vector |
32 | | -call h%write('vector_scalar_real', [37.]) |
33 | 48 | call h%write('1d_real', r1) |
| 49 | +call h%write('vector_scalar_real', [37.]) |
34 | 50 |
|
| 51 | +!> create then write |
| 52 | +call h%create('1d_int32', NF90_INT, dims=shape(i1)) |
35 | 53 | call h%write('1d_int32', i1) |
36 | 54 |
|
37 | | -!> create then write: not an nc4fortran feature yet |
| 55 | +call h%create('1d_int64', NF90_INT64, dims=shape(i1_64)) |
| 56 | +call h%write('1d_int64', i1_64) |
38 | 57 |
|
39 | | -print *, 'PASSED: vector write' |
40 | 58 | !> test rewrite |
41 | | -call h%write('scalar_real32', 42.) |
| 59 | +call h%write('scalar_real32', 42._real32) |
| 60 | +call h%write('scalar_real64', 42._real64) |
42 | 61 | call h%write('scalar_int32', 42_int32) |
| 62 | +call h%write('scalar_int64', 42_int64) |
43 | 63 | call h%close() |
44 | 64 |
|
| 65 | +end subroutine test_scalar_write |
| 66 | + |
| 67 | + |
| 68 | +subroutine test_scalar_read(fn) |
| 69 | + |
| 70 | +character(*), intent(in) :: fn |
| 71 | + |
| 72 | +type(netcdf_file) :: h |
| 73 | + |
| 74 | +real(real32) :: rt, r1(4) |
| 75 | +integer(int32) :: i, it, i1(4) |
| 76 | +integer(int32), allocatable :: i1t(:) |
| 77 | +integer(int64) :: it_64, i1_64(4) |
| 78 | +integer(int64), allocatable :: i1t_64(:) |
| 79 | +real(real32), allocatable :: rr1_32(:) |
| 80 | +integer, allocatable :: dims(:) |
| 81 | + |
| 82 | +!> test data |
| 83 | +do i = 1,size(i1) |
| 84 | + i1(i) = i |
| 85 | +enddo |
| 86 | + |
| 87 | +r1 = i1 |
| 88 | +i1_64 = i1 |
| 89 | + |
45 | 90 | !> read |
46 | 91 |
|
47 | 92 | call h%open(fn, action='r') |
48 | 93 |
|
49 | 94 | call h%read('scalar_int32', it) |
50 | | -call h%read('scalar_real32', rt) |
| 95 | +call h%read('scalar_int64', it_64) |
51 | 96 |
|
| 97 | +call h%read('scalar_real32', rt) |
52 | 98 | if (.not.(rt==it .and. it==42)) then |
53 | 99 | write(stderr,*) it,'/=',rt |
54 | 100 | error stop 'scalar real / int: not equal 42' |
55 | 101 | endif |
56 | | -print *, 'PASSED: scalar read/write' |
57 | | - |
58 | | -!> read casting -- real to int and int to real |
59 | | -call h%read('scalar_real32', it) |
60 | | -if(it/=42) error stop 'scalar cast real => int' |
61 | | -call h%read('scalar_int32', rt) |
62 | | -if(rt/=42) error stop 'scalar cast int32 => real' |
63 | | -print *, 'PASSED: scalar cast on read' |
64 | 102 |
|
65 | 103 | !> read vector length 1 as scalar |
66 | 104 | call h%shape('vector_scalar_real', dims) |
67 | | -if (any(dims /= [1])) then |
68 | | - write(stderr,*) "expected dims 1, got dims:", dims, "rank:", rank(dims) |
69 | | - error stop "vector_scalar: expected vector length 1" |
70 | | -endif |
| 105 | +if (any(dims /= [1])) error stop "vector_scalar: expected vector length 1" |
71 | 106 |
|
72 | 107 | call h%read('vector_scalar_real', rt) |
73 | | -if(rt/=37) error stop 'vector_scalar: 1d length 1 => scalar' |
| 108 | +if(rt /= 37) error stop 'vector_scalar: 1d length 1 => scalar' |
74 | 109 |
|
75 | | -!> 1D vector read write |
| 110 | +!> 1D vector read |
76 | 111 | call h%shape('1d_real', dims) |
77 | | -allocate(rr1(dims(1))) |
78 | | -print *, "OK: 1d shape read real32" |
79 | | -call h%read('1d_real', rr1) |
80 | | -if (.not.all(r1 == rr1)) error stop 'real 1-D: read does not match write' |
81 | | -print *, "OK: 1d read real32" |
| 112 | +allocate(rr1_32(dims(1))) |
| 113 | + |
| 114 | +call h%read('1d_real', rr1_32) |
| 115 | +if (.not.all(r1 == rr1_32)) error stop 'real32 1-D: read does not match write' |
82 | 116 |
|
83 | 117 | call h%shape('1d_int32',dims) |
84 | 118 | allocate(i1t(dims(1))) |
85 | | -call h%read('1d_int32',i1t) |
| 119 | +call h%read('1d_int32', i1t) |
86 | 120 | if (.not.all(i1==i1t)) error stop 'int32 1-D: read does not match write' |
87 | 121 |
|
88 | | -print *, 'PASSED: 1D read/write' |
89 | | - |
90 | | -!> 1D vector read casting -- real to int and int to real |
91 | | -call h%read('1d_real', i1t) |
92 | | -if (.not.all(r1==i1t)) error stop '1Dcast real => int32' |
93 | | -call h%read('1d_int32', rr1) |
94 | | -if (.not.all(i1==rr1)) error stop '1D cast int32 => real' |
| 122 | +allocate(i1t_64(dims(1))) |
| 123 | +call h%read('1d_int64', i1t_64) |
| 124 | +if (.not.all(i1_64==i1t_64)) error stop 'int64 1-D: read does not match write' |
95 | 125 |
|
96 | 126 | !> check filename property |
97 | 127 | if (.not. h%filename == fn) error stop h%filename // ' mismatch filename' |
98 | 128 |
|
99 | 129 | call h%close() |
100 | 130 |
|
| 131 | +end subroutine test_scalar_read |
| 132 | + |
101 | 133 | end program |
0 commit comments