|
| 1 | +program deflate_test |
| 2 | +!! unit tests and registration tests of HDF5 deflate compression write |
| 3 | +use, intrinsic:: iso_fortran_env, only: int32, real32, real64, stderr=>error_unit |
| 4 | + |
| 5 | +use nc4fortran, only: netcdf_file, toLower, strip_trailing_null, truncate_string_null |
| 6 | + |
| 7 | +implicit none (type, external) |
| 8 | + |
| 9 | +type(netcdf_file) :: h |
| 10 | +integer, parameter :: N=1000 |
| 11 | +integer :: crat, chunks(3) |
| 12 | +integer :: fsize |
| 13 | + |
| 14 | +integer, allocatable :: ibig2(:,:), ibig3(:,:,:) |
| 15 | +real(real32), allocatable :: big2(:,:), big3(:,:,:) |
| 16 | +character(*), parameter :: fn1='deflate1.nc', fn2='deflate2.nc', fn3='deflate3.nc', fn4='deflate4.nc' |
| 17 | + |
| 18 | +allocate(ibig2(N,N), ibig3(N,N,4), big2(N,N), big3(N,N,4)) |
| 19 | + |
| 20 | +ibig2 = 0 |
| 21 | +ibig3 = 0 |
| 22 | +big2 = 0 |
| 23 | +big3 = 0 |
| 24 | + |
| 25 | +call h%initialize(fn1, status='replace', comp_lvl=1, debug=.true.) |
| 26 | +call h%write('big2', big2, dims=['x','y']) |
| 27 | +call h%flush() |
| 28 | +!> turn compression off for following variables (must flush first) |
| 29 | +h%comp_lvl = 0 |
| 30 | +call h%write('small_contig', big2(:5,:5), dims=['q','r']) |
| 31 | +call h%finalize() |
| 32 | + |
| 33 | +inquire(file=fn1, size=fsize) |
| 34 | +crat = (N*N*storage_size(big2)/8) / fsize |
| 35 | +print '(A,F6.2,A,I6)','#1 filesize (Mbytes): ',fsize/1e6, ' 2D compression ratio:',crat |
| 36 | +if (crat < 10) error stop '#1 2D low compression' |
| 37 | + |
| 38 | +call h%initialize(fn1, status='old', action='r', debug=.false.) |
| 39 | + |
| 40 | +if(.not. h%is_chunked('big2')) error stop '#1 not chunked layout' |
| 41 | + |
| 42 | +call h%chunks('big2', chunks(:2)) |
| 43 | +if(any(chunks(:2) /= [1000, 1000])) error stop '#1 get_chunk mismatch' |
| 44 | + |
| 45 | +if(.not.h%is_contig('small_contig')) error stop '#1 not contig layout' |
| 46 | +call h%chunks('small_contig', chunks(:2)) |
| 47 | +if(any(chunks(:2) /= -1)) error stop '#1 get_chunk mismatch' |
| 48 | + |
| 49 | +call h%finalize() |
| 50 | + |
| 51 | +! ====================================== |
| 52 | + |
| 53 | +call h%initialize(fn2, status='replace',comp_lvl=1, debug=.true.) |
| 54 | +call h%write('big3', big3) |
| 55 | + |
| 56 | +call h%write('big3_autochunk', big3) |
| 57 | +call h%chunks('big3_autochunk', chunks) |
| 58 | +if(any(chunks /= [500,500,2])) then |
| 59 | + write(stderr,*) '#2 chunk size', chunks |
| 60 | + error stop '#2 auto chunk unexpected chunk size' |
| 61 | +endif |
| 62 | +call h%finalize() |
| 63 | + |
| 64 | +inquire(file=fn2, size=fsize) |
| 65 | +crat = (2*N*N*storage_size(big3)/8) / fsize |
| 66 | +print '(A,F6.2,A,I6)','#2 filesize (Mbytes): ',fsize/1e6, ' 3D compression ratio:',crat |
| 67 | +if (h%comp_lvl > 0 .and. crat < 10) error stop '#2 3D low compression' |
| 68 | + |
| 69 | +! ====================================== |
| 70 | + |
| 71 | +call h%initialize(fn3, status='replace',comp_lvl=1, debug=.true.) |
| 72 | + |
| 73 | +call h%write('ibig3', ibig3(:N-10,:N-20,:)) |
| 74 | +call h%chunks('ibig3', chunks) |
| 75 | +if(any(chunks /= [495,490,2])) then |
| 76 | + write(stderr,*) '#3 chunk size', chunks |
| 77 | + error stop '#3 auto chunk unexpected chunk size' |
| 78 | +endif |
| 79 | +call h%finalize() |
| 80 | + |
| 81 | +inquire(file=fn3, size=fsize) |
| 82 | +crat = (N*N*storage_size(ibig3)/8) / fsize |
| 83 | +print '(A,F6.2,A,I6)','#3 filesize (Mbytes): ',fsize/1e6, ' 3D compression ratio:',crat |
| 84 | +if (h%comp_lvl > 0 .and. crat < 10) error stop '#3 3D low compression' |
| 85 | +! !====================================== |
| 86 | + |
| 87 | +call h%initialize(fn4, status='replace', comp_lvl=1, debug=.true.) |
| 88 | +call h%write('ibig2', ibig2) |
| 89 | +call h%finalize() |
| 90 | + |
| 91 | +inquire(file=fn4, size=fsize) |
| 92 | +crat = (N*N*storage_size(ibig2)/8) / fsize |
| 93 | +print '(A,F6.2,A,I6)','#4 filesize (Mbytes): ',fsize/1e6, ' 3D compression ratio:',crat |
| 94 | +if (h%comp_lvl > 0 .and. crat < 10) error stop '#4 3D low compression' |
| 95 | + |
| 96 | +end program |
0 commit comments