|
| 1 | +program scalar_test |
| 2 | + |
| 3 | +use, intrinsic:: iso_fortran_env, only: int64, int32, real32, real64, stderr=>error_unit |
| 4 | +use nc4fortran, only : netcdf_file |
| 5 | + |
| 6 | +implicit none (type, external) |
| 7 | + |
| 8 | +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(:) |
| 14 | +integer :: i |
| 15 | +character(*), parameter :: fn = 'test_scalar.nc' |
| 16 | + |
| 17 | +do i = 1,size(i1) |
| 18 | + i1(i) = i |
| 19 | +enddo |
| 20 | + |
| 21 | +r1 = i1 |
| 22 | + |
| 23 | +!> write |
| 24 | +call h%initialize(fn, status='replace') |
| 25 | +!! scalar tests |
| 26 | +call h%write('scalar_int', 42_int32) |
| 27 | +call h%write('scalar_real', -1._real32) |
| 28 | +call h%write('scalar_real', 42._real32) |
| 29 | +call h%write('1d_real', r1) |
| 30 | +call h%write('1d_int', i1) |
| 31 | +!! test rewrite |
| 32 | +call h%write('scalar_real', 42.) |
| 33 | +call h%write('scalar_int', 42) |
| 34 | +call h%finalize() |
| 35 | + |
| 36 | +!> read |
| 37 | + |
| 38 | +call h%initialize(fn, status='old', action='r') |
| 39 | + |
| 40 | +call h%read('scalar_int', it) |
| 41 | +call h%read('scalar_real', rt) |
| 42 | +if (.not.(rt==it .and. it==42)) then |
| 43 | + write(stderr,*) it,'/=',rt |
| 44 | + error stop 'scalar real / int: not equal 42' |
| 45 | +endif |
| 46 | + |
| 47 | +call h%shape('1d_real',dims) |
| 48 | +allocate(rr1(dims(1))) |
| 49 | +call h%read('1d_real',rr1) |
| 50 | +if (.not.all(r1 == rr1)) error stop 'real 1-D: read does not match write' |
| 51 | + |
| 52 | +call h%shape('1d_int',dims) |
| 53 | +allocate(i1t(dims(1))) |
| 54 | +call h%read('1d_int',i1t) |
| 55 | +if (.not.all(i1==i1t)) error stop 'integer 1-D: read does not match write' |
| 56 | + |
| 57 | +if (.not. h%filename == fn) then |
| 58 | + write(stderr,*) h%filename // ' mismatch filename' |
| 59 | + error stop |
| 60 | +endif |
| 61 | + |
| 62 | +call h%finalize() |
| 63 | + |
| 64 | +end program |
0 commit comments