Skip to content

Commit

Permalink
Merge pull request #1978 from GEOS-ESM/feature/wjiang/add_var_formatter
Browse files Browse the repository at this point in the history
add variable through file formatter
  • Loading branch information
mathomp4 committed Feb 28, 2023
2 parents 3d00170 + 68e807c commit 8bef386
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added a subroutine add_variable to Netcdf4_Fileformatter
- Add a function to get the area of a spherical polygon to the spherical geometry module
- Created layout independent version of the "DownBit"/"pFIO_ShaveMantissa" routines when running in MPI codes
- Added subroutine `MAPL_SunGetLocalSolarHourAngle()` in `base/MAPL_sun_uc.F90`. This
Expand Down
29 changes: 27 additions & 2 deletions pfio/NetCDF4_FileFormatter.F90
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module pFIO_NetCDF4_FileFormatterMod
procedure :: close
procedure :: read
procedure :: write
procedure :: add_variable

#include "new_overload.macro"
procedure :: ___SUB(get_var,int32,0)
Expand Down Expand Up @@ -656,12 +657,31 @@ subroutine put_var_attributes(this, var, varid, unusable, rc)

end subroutine put_var_attributes

subroutine add_variable(this, cf, varname, unusable, rc)
class (NetCDF4_FileFormatter), intent(inout) :: this
type (FileMetadata), target, intent(in) :: cf
character(*), intent(in) :: varname
class (KeywordEnforcer), optional, intent(in) :: unusable
!integer, optional, intent(in) :: chunksizes(:)
integer, optional, intent(out) :: rc
integer:: status

status=nf90_redef(this%ncid)
_VERIFY(status)
call this%def_variables(cf, varname=varname, _RC)
status=nf90_enddef(this%ncid)
_VERIFY(status)
_RETURN(_SUCCESS)
_UNUSED_DUMMY(unusable)

end subroutine add_variable

subroutine def_variables(this, cf, unusable, rc)
subroutine def_variables(this, cf, unusable, varname, rc)
class (NetCDF4_FileFormatter), intent(inout) :: this
type (FileMetadata), target, intent(in) :: cf
class (KeywordEnforcer), optional, intent(in) :: unusable
!integer, optional, intent(in) :: chunksizes(:)
character(*), optional,intent(in) :: varname
integer, optional, intent(out) :: rc

integer :: status
Expand Down Expand Up @@ -693,6 +713,12 @@ subroutine def_variables(this, cf, unusable, rc)
var_iter = order%begin()
do while (var_iter /= order%end())
var_name => var_iter%get()
if ( present (varname)) then
if (var_name /= varname) then
call var_iter%next()
cycle
endif
endif
var => vars%at(var_name)
xtype = get_xtype(var%get_type(),rc=status)
_VERIFY(status)
Expand All @@ -710,7 +736,6 @@ subroutine def_variables(this, cf, unusable, rc)
idim = idim + 1
end do
_VERIFY(status)

!$omp critical
status = nf90_def_var(this%ncid, var_name, xtype, dimids, varid)
!$omp end critical
Expand Down
40 changes: 40 additions & 0 deletions pfio/tests/Test_NetCDF4_FileFormatter.pf
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,44 @@ contains

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', 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

end module Test_NetCDF4_FileFormatter

0 comments on commit 8bef386

Please sign in to comment.