Test_NetCDF4_FileFormatter.pf Source File


This file depends on

sourcefile~~test_netcdf4_fileformatter.pf~~EfferentGraph sourcefile~test_netcdf4_fileformatter.pf Test_NetCDF4_FileFormatter.pf sourcefile~filemetadata.f90 FileMetadata.F90 sourcefile~test_netcdf4_fileformatter.pf->sourcefile~filemetadata.f90 sourcefile~netcdf4_fileformatter.f90 NetCDF4_FileFormatter.F90 sourcefile~test_netcdf4_fileformatter.pf->sourcefile~netcdf4_fileformatter.f90 sourcefile~pfio_constants.f90 pFIO_Constants.F90 sourcefile~test_netcdf4_fileformatter.pf->sourcefile~pfio_constants.f90 sourcefile~variable.f90 Variable.F90 sourcefile~test_netcdf4_fileformatter.pf->sourcefile~variable.f90 sourcefile~filemetadata.f90->sourcefile~pfio_constants.f90 sourcefile~filemetadata.f90->sourcefile~variable.f90 sourcefile~attribute.f90 Attribute.F90 sourcefile~filemetadata.f90->sourcefile~attribute.f90 sourcefile~coordinatevariable.f90 CoordinateVariable.F90 sourcefile~filemetadata.f90->sourcefile~coordinatevariable.f90 sourcefile~mapl_exceptionhandling.f90 MAPL_ExceptionHandling.F90 sourcefile~filemetadata.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~mapl_keywordenforcer.f90 MAPL_KeywordEnforcer.F90 sourcefile~filemetadata.f90->sourcefile~mapl_keywordenforcer.f90 sourcefile~pfio_utilities.f90 pFIO_Utilities.F90 sourcefile~filemetadata.f90->sourcefile~pfio_utilities.f90 sourcefile~stringintegermaputil.f90 StringIntegerMapUtil.F90 sourcefile~filemetadata.f90->sourcefile~stringintegermaputil.f90 sourcefile~stringvariablemap.f90 StringVariableMap.F90 sourcefile~filemetadata.f90->sourcefile~stringvariablemap.f90 sourcefile~stringvectorutil.f90 StringVectorUtil.F90 sourcefile~filemetadata.f90->sourcefile~stringvectorutil.f90 sourcefile~unlimitedentity.f90 UnlimitedEntity.F90 sourcefile~filemetadata.f90->sourcefile~unlimitedentity.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~filemetadata.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~pfio_constants.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~variable.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~attribute.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~coordinatevariable.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~mapl_keywordenforcer.f90 sourcefile~netcdf_supplement.f90 NetCDF_Supplement.F90 sourcefile~netcdf4_fileformatter.f90->sourcefile~netcdf_supplement.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~stringvariablemap.f90 sourcefile~netcdf4_fileformatter.f90->sourcefile~unlimitedentity.f90 sourcefile~variable.f90->sourcefile~pfio_constants.f90 sourcefile~variable.f90->sourcefile~attribute.f90 sourcefile~variable.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~variable.f90->sourcefile~mapl_keywordenforcer.f90 sourcefile~variable.f90->sourcefile~pfio_utilities.f90 sourcefile~variable.f90->sourcefile~stringvectorutil.f90 sourcefile~variable.f90->sourcefile~unlimitedentity.f90 sourcefile~attribute.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~attribute.f90->sourcefile~pfio_utilities.f90 sourcefile~attribute.f90->sourcefile~unlimitedentity.f90 sourcefile~coordinatevariable.f90->sourcefile~pfio_constants.f90 sourcefile~coordinatevariable.f90->sourcefile~variable.f90 sourcefile~coordinatevariable.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~coordinatevariable.f90->sourcefile~mapl_keywordenforcer.f90 sourcefile~coordinatevariable.f90->sourcefile~pfio_utilities.f90 sourcefile~mapl_errorhandling.f90 MAPL_ErrorHandling.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~mapl_errorhandling.f90 sourcefile~mapl_throw.f90 MAPL_Throw.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~mapl_throw.f90 sourcefile~pfio_utilities.f90->sourcefile~pfio_constants.f90 sourcefile~pfio_utilities.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~stringintegermaputil.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~stringintegermaputil.f90->sourcefile~pfio_utilities.f90 sourcefile~stringvariablemap.f90->sourcefile~variable.f90 sourcefile~stringvariablemap.f90->sourcefile~coordinatevariable.f90 sourcefile~stringvariablemap.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~stringvariablemap.f90->sourcefile~pfio_utilities.f90 sourcefile~stringvectorutil.f90->sourcefile~attribute.f90 sourcefile~stringvectorutil.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~stringvectorutil.f90->sourcefile~pfio_utilities.f90 sourcefile~unlimitedentity.f90->sourcefile~pfio_constants.f90 sourcefile~unlimitedentity.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~unlimitedentity.f90->sourcefile~pfio_utilities.f90 sourcefile~mapl_errorhandling.f90->sourcefile~mapl_throw.f90

Source Code

module Test_NetCDF4_FileFormatter
   use pfunit
   use pFIO_NetCDF4_FileFormatterMod
   use pFIO_FileMetadataMod
   use pFIO_VariableMod
   use pFIO_ConstantsMod
   use netcdf
   implicit none


contains

   @before
   subroutine setup()
   end subroutine setup

   @after
   subroutine tearDown()
      integer :: unit

      open(newunit=unit, file='test.nc', status='old')
      close(unit, status='delete')
   end subroutine tearDown

   @test
   subroutine test_write_read_trivial()
      type (NetCDF4_FileFormatter) :: formatter
      type (FileMetadata) :: cf_expected
      type (FileMetadata) :: cf_found
      integer :: status

      call cf_expected%add_dimension('x',1)

      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(cf_expected, rc=status)
      @assertEqual(0, NF90_NOERR)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      call formatter%open('test.nc', mode=NF90_NOWRITE, rc=status)
      @assertEqual(0, NF90_NOERR)
      cf_found = formatter%read(rc=status)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      @assertTrue(cf_expected == cf_found)

   end subroutine test_write_read_trivial

   @test
   subroutine test_write_read_simple_variable()
      type (NetCDF4_FileFormatter) :: formatter
      type (FileMetadata) :: cf_expected
      type (FileMetadata) :: cf_found
      integer :: status

      call cf_expected%add_dimension('x',1)
      call cf_expected%add_dimension('y',2)
      call cf_expected%add_variable('var', Variable(type=pFIO_INT32, dimensions='x,y'))

      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(cf_expected, rc=status)
      @assertEqual(0, NF90_NOERR)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      call formatter%open('test.nc', mode=NF90_NOWRITE, rc=status)
      @assertEqual(0, NF90_NOERR)
      cf_found = formatter%read(rc=status)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      @assertTrue(cf_expected == cf_found)

   end subroutine test_write_read_simple_variable

   @test
   subroutine test_write_read_simple_attribute()
      type (NetCDF4_FileFormatter) :: formatter
      type (FileMetadata) :: cf_expected
      type (FileMetadata) :: cf_found
      integer :: status

      call cf_expected%add_dimension('x',1)
      call cf_expected%add_dimension('y',2)
      call cf_expected%add_attribute('attr', [1,2])

      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(cf_expected, rc=status)
      @assertEqual(0, NF90_NOERR)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      call formatter%open('test.nc', mode=NF90_NOWRITE, rc=status)
      @assertEqual(0, NF90_NOERR)
      cf_found = formatter%read(rc=status)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      @assertTrue(cf_expected == cf_found)

   end subroutine test_write_read_simple_attribute

   @test
   subroutine test_write_read_variable_attribute()
      type (NetCDF4_FileFormatter) :: formatter
      type (FileMetadata) :: cf_expected
      type (FileMetadata) :: cf_found
      type (Variable) :: v
      integer :: status

      call cf_expected%add_dimension('x',1)
      call cf_expected%add_dimension('y',2)

      v = Variable(type=pFIO_INT32, dimensions='x,y')
      call v%add_attribute('attr', [1.,2.,3.])
      call cf_expected%add_variable('var', v)

      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(cf_expected, rc=status)
      @assertEqual(0, NF90_NOERR)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      call formatter%open('test.nc', mode=NF90_NOWRITE, rc=status)
      @assertEqual(0, NF90_NOERR)
      cf_found = formatter%read(rc=status)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      @assertTrue(cf_expected == cf_found)

   end subroutine test_write_read_variable_attribute

   @test
   subroutine test_add_variable()
      type (NetCDF4_FileFormatter) :: formatter
      type (FileMetadata) :: cf_expected
      type (FileMetadata) :: cf_found
      type (Variable) :: v,v1
      integer :: status

      call cf_expected%add_dimension('x',1)
      call cf_expected%add_dimension('y',2)

      v = Variable(type=pFIO_INT32, dimensions='x,y')
      call v%add_attribute('attr', [1.,2.,3.])
      call cf_expected%add_variable('var', v)

      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(cf_expected, rc=status)
      @assertEqual(0, NF90_NOERR)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      call formatter%open('test.nc', mode=NF90_WRITE, rc=status)
      @assertEqual(NF90_NOERR, status)
      v1 = Variable(type=pFIO_INT32, dimensions='x,y')
      call cf_expected%add_variable('new_var', v1)
      call formatter%add_variable(cf_expected,'new_var', rc=status)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      call formatter%open('test.nc', mode=NF90_NOWRITE, rc=status)
      @assertEqual(0, NF90_NOERR)
      cf_found = formatter%read(rc=status)
      call formatter%close(rc=status)
      @assertEqual(0, NF90_NOERR)

      @assertTrue(cf_expected == cf_found)

   end subroutine test_add_variable

   @test
   subroutine test_read_write_0d_string()
      type (NetCDF4_FileFormatter) :: formatter
      character(len=:), allocatable :: char_write
      character(len=:), allocatable :: char_read
      type (FileMetadata) :: metadata
      integer :: status, length
      type (Variable) :: v

      v = Variable(type=pFIO_STRING)
      call metadata%add_variable('new_char',v)
      char_write = "I am 0d String"

      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(metadata, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%put_var('new_char', char_write)
      call formatter%close(rc=status)
      @assertEqual(NF90_NOERR, status)

      call formatter%open('test.nc', PFIO_READ, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%inq_var_string_length('new_char',length)
      allocate(character(len=length):: char_read)
      call formatter%get_var('new_char', char_read)
      call formatter%close()

      @assertTrue(char_write == char_read)

   end subroutine test_read_write_0d_string

   @test
   subroutine test_read_write_1d_string()
      type (NetCDF4_FileFormatter) :: formatter
      character(len=:), allocatable :: char_write(:)
      character(len=:), allocatable :: char_read(:)
      type (FileMetadata) :: metadata
      integer :: status, length, Ydim, my_dim
      type (Variable) :: v

      Ydim = 5
      v = Variable(type=pFIO_STRING, dimensions='Ydim')
      call metadata%add_dimension('Ydim', Ydim)
      call metadata%add_variable('new_char',v)
      char_write = ["I     ",  \
                    "am    ",  \
                    "a     ",  \
                    "1d    ",  \
                    "String"]
      call formatter%create('test.nc', mode=PFIO_CLOBBER, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%write(metadata, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%put_var('new_char', char_write, start=[1], count=[Ydim])
      call formatter%close(rc=status)
      @assertEqual(NF90_NOERR, status)

      call formatter%open('test.nc', PFIO_READ, rc=status)
      @assertEqual(NF90_NOERR, status)
      call formatter%inq_var_string_length('new_char',length)
      metadata = formatter%read()
      my_dim = metadata%get_dimension('Ydim')
      allocate(character(len=length):: char_read(my_dim))
      call formatter%get_var('new_char', char_read, start=[1], count=[my_dim])
      call formatter%close()

      @assertTrue(char_write == char_read)

   end subroutine test_read_write_1d_string

end module Test_NetCDF4_FileFormatter