From 74eea066d34245d0d3c800266b38995b6de9cc92 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 14:47:57 +0100 Subject: [PATCH 01/16] - added function getAttributeNames to NcAttributable type to get all attribute names --- src/mo_netcdf.f90 | 34 +++++++++++++++++++++++++++++++++- src/mo_netcdf.fypp | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/mo_netcdf.f90 b/src/mo_netcdf.f90 index 003fd73..4a86396 100644 --- a/src/mo_netcdf.f90 +++ b/src/mo_netcdf.f90 @@ -30,7 +30,7 @@ module mo_netcdf use netcdf, only : & nf90_open, nf90_close, nf90_strerror, nf90_def_dim, nf90_def_var, & - nf90_put_var, nf90_get_var, nf90_put_att, nf90_get_att, & + nf90_put_var, nf90_get_var, nf90_put_att, nf90_get_att, nf90_inq_attname, & nf90_inquire, nf90_inq_dimid, nf90_inquire_dimension, & nf90_inq_varid, nf90_inq_varids, nf90_inquire_variable, nf90_inquire_attribute, & nf90_inq_ncid, nf90_inq_grp_parent, nf90_inq_grpname, nf90_def_grp, & @@ -72,6 +72,7 @@ module mo_netcdf procedure, public :: hasAttribute procedure, public :: renameAttribute procedure, private :: getAttributableIds + procedure, public :: getAttributeNames procedure, private :: setAttribute_0d_sp generic, public :: setAttribute => setAttribute_0d_sp @@ -1075,6 +1076,37 @@ contains hasAttribute = (status == NF90_NOERR) end function hasAttribute + function getAttributeNames(self) result(attributeNames) + class(NcAttributable), intent(in) :: self + character(256), dimension(:), allocatable :: attributeNames + + integer(i4), parameter :: maxNames = 100_i4 + integer(i4) :: nAtts = 0_i4 + character(256) :: attName + + ! assume a maximum number of 100 attributes that are checked + allocate(attributeNames(nAtts)) + do while (nAtts < maxNames) + select type (self) + class is (NcGroup) + call check(nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & + "Could not inquire attributes for NcGroup: " // self%getName()) + class is (NcVariable) + call check(nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & + "Could not inquire attributes for NcVariable: " // self%getName()) + end select + ! if the attribute name is not defined, exit loop, else increase counter + if (len_trim(attName) == 0_i4) then + exit + else + nAtts = nAtts + 1_i4 + end if + end do + ! select only valid names + attributeNames = attributeNames(1:nAtts) + + end function getAttributeNames + subroutine setAttribute_0d_sp(self, name, data) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 5cf3d7d..f914412 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -34,7 +34,7 @@ module mo_netcdf use netcdf, only : & nf90_open, nf90_close, nf90_strerror, nf90_def_dim, nf90_def_var, & - nf90_put_var, nf90_get_var, nf90_put_att, nf90_get_att, & + nf90_put_var, nf90_get_var, nf90_put_att, nf90_get_att, nf90_inq_attname, & nf90_inquire, nf90_inq_dimid, nf90_inquire_dimension, & nf90_inq_varid, nf90_inq_varids, nf90_inquire_variable, nf90_inquire_attribute, & nf90_inq_ncid, nf90_inq_grp_parent, nf90_inq_grpname, nf90_def_grp, & @@ -76,6 +76,7 @@ module mo_netcdf procedure, public :: hasAttribute procedure, public :: renameAttribute procedure, private :: getAttributableIds + procedure, public :: getAttributeNames #:for kind, type in REAL_KINDS_TYPES + INT_KINDS_TYPES #:for rank in [0, 1] @@ -853,6 +854,37 @@ contains hasAttribute = (status == NF90_NOERR) end function hasAttribute + function getAttributeNames(self) result(attributeNames) + class(NcAttributable), intent(in) :: self + character(256), dimension(:), allocatable :: attributeNames + + integer(i4), parameter :: maxNames = 100_i4 + integer(i4) :: nAtts = 0_i4 + character(256) :: attName + + ! assume a maximum number of 100 attributes that are checked + allocate(attributeNames(nAtts)) + do while (nAtts < maxNames) + select type (self) + class is (NcGroup) + call check(nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & + "Could not inquire attributes for NcGroup: " // self%getName()) + class is (NcVariable) + call check(nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & + "Could not inquire attributes for NcVariable: " // self%getName()) + end select + ! if the attribute name is not defined, exit loop, else increase counter + if (len_trim(attName) == 0_i4) then + exit + else + nAtts = nAtts + 1_i4 + end if + end do + ! select only valid names + attributeNames = attributeNames(1:nAtts) + + end function getAttributeNames + #:def setAttribute_template(kind, type, rank) subroutine setAttribute_${rank}$d_${kind}$(self, name, data) class(NcAttributable), intent(in) :: self -- GitLab From 6e30c3a31660b9efc52c219d9581d4d9d2237b3a Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 15:06:59 +0100 Subject: [PATCH 02/16] - added tests for new functions in mo_netcdf --- src/pf_tests/test_mo_netcdf.pf | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index d36efba..cd9a5ba 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -13,6 +13,8 @@ module test_mo_netcdf character(*), parameter :: fname="netcdf_make_check_test_file" character(*), parameter :: vname_time="time", vname_lat="lat", vname_lon="lon", vname_data="data" + character(64), dimension(2), parameter :: waglobalnames('Author', 'Year'), wavarnames('units', 'scaling') + character(64), dimension(:), allocatable :: raglobalnames, ravarnames character(64) :: wavalue, ravalue type(NcDataset) :: nc @@ -86,12 +88,12 @@ contains end do ! add some more variable attributes - call var_data%setAttribute("units", "mm/d") - call var_data%setAttribute("scaling", 0.1_dp) + call var_data%setAttribute(trim(wavarnames(1)), "mm/d") + call var_data%setAttribute(trim(wavarnames(2)), 0.1_dp) ! add global attributes - call nc%setAttribute("Author", wavalue) - call nc%setAttribute("Year", 2099_i4) + call nc%setAttribute(trim(waglobalnames(1)), wavalue) + call nc%setAttribute(trim(waglobalnames(2)), 2099_i4) ! close the file call nc%close() @@ -113,12 +115,14 @@ contains call var_lat%getData(rlat) call var_lon%getData(rlon) call var_data%getData(rdata) + call var_data%getAttributeNames(ravarnames) ! read the fill value call var_data%getFillValue(rfvalue) ! read a global attribute - call nc%getAttribute("Author", ravalue) + call nc%getAttributeNames(raglobalnames) + call nc%getAttribute(trim(waglobalnames(1)), ravalue) ! close dataset call nc%close() @@ -131,6 +135,8 @@ contains @assertEqual(rdata, wdata(:,:,1:ntime)) @assertEqual(rfvalue, rfvalue) @assertEqual(ravalue, wavalue) + @assertEqual(ravarnames, wavarnames) + @assertEqual(raglobalnames, waglobalnames) end subroutine test_netcdf_create_new -- GitLab From ee1549eda0b1e04a3ef9965fcc582f35286f3cff Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 15:18:44 +0100 Subject: [PATCH 03/16] - fixed bugs --- src/mo_netcdf.fypp | 2 +- src/pf_tests/test_mo_netcdf.pf | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index f914412..9ba284e 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -863,7 +863,7 @@ contains character(256) :: attName ! assume a maximum number of 100 attributes that are checked - allocate(attributeNames(nAtts)) + allocate(attributeNames(maxNames)) do while (nAtts < maxNames) select type (self) class is (NcGroup) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index cd9a5ba..906854a 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -115,14 +115,14 @@ contains call var_lat%getData(rlat) call var_lon%getData(rlon) call var_data%getData(rdata) - call var_data%getAttributeNames(ravarnames) + call var_data%getAttributeNames(wavarnames) ! read the fill value call var_data%getFillValue(rfvalue) ! read a global attribute - call nc%getAttributeNames(raglobalnames) - call nc%getAttribute(trim(waglobalnames(1)), ravalue) + call nc%getAttributeNames(waglobalnames) + call nc%getAttribute(trim(raglobalnames(1)), ravalue) ! close dataset call nc%close() -- GitLab From 565f6ba85663d0b78a324e4b222bacfa325d0dff Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 15:31:20 +0100 Subject: [PATCH 04/16] - fixed bugs 2 --- src/pf_tests/test_mo_netcdf.pf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 906854a..6ce7782 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -13,7 +13,8 @@ module test_mo_netcdf character(*), parameter :: fname="netcdf_make_check_test_file" character(*), parameter :: vname_time="time", vname_lat="lat", vname_lon="lon", vname_data="data" - character(64), dimension(2), parameter :: waglobalnames('Author', 'Year'), wavarnames('units', 'scaling') + character(64), dimension(2), parameter :: waglobalnames = [character(64) :: 'Author', 'Year'] + character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] character(64), dimension(:), allocatable :: raglobalnames, ravarnames character(64) :: wavalue, ravalue -- GitLab From a7f72338dee1861ff130cdd6e7aa3d623d9d7575 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 16:02:07 +0100 Subject: [PATCH 05/16] - fixed bugs 3 - added more arguments to wrapper hasAttribute function (so inquiry of length and type is possible) - added tests for that --- src/mo_netcdf.f90 | 15 +++++++++++---- src/mo_netcdf.fypp | 15 +++++++++++---- src/pf_tests/test_mo_netcdf.pf | 21 +++++++++++++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/mo_netcdf.f90 b/src/mo_netcdf.f90 index 4a86396..4d23afa 100644 --- a/src/mo_netcdf.f90 +++ b/src/mo_netcdf.f90 @@ -36,7 +36,7 @@ module mo_netcdf nf90_inq_ncid, nf90_inq_grp_parent, nf90_inq_grpname, nf90_def_grp, & nf90_rename_dim, nf90_rename_var, nf90_rename_att, nf90_sync, & NF90_OPEN, NF90_NETCDF4, NF90_CREATE, NF90_WRITE, NF90_NOWRITE, & - NF90_BYTE, NF90_SHORT, NF90_INT, NF90_INT64, NF90_FLOAT, NF90_DOUBLE, & + NF90_BYTE, NF90_SHORT, NF90_INT, NF90_INT64, NF90_FLOAT, NF90_DOUBLE, NF90_CHAR, & NF90_FILL_BYTE, NF90_FILL_SHORT, NF90_FILL_INT, NF90_FILL_FLOAT, NF90_FILL_DOUBLE, & NF90_NOERR, NF90_UNLIMITED, NF90_GLOBAL, NF90_SHARE, NF90_HDF5, & NF90_64BIT_OFFSET, NF90_CLASSIC_MODEL @@ -1061,16 +1061,19 @@ contains end do end function isUnlimitedVariable - logical function hasAttribute(self, name) + logical function hasAttribute(self, name, xtype, len, attnum) class(NcAttributable), intent(in) :: self character(*), intent(in) :: name + integer(i4), intent(out), optional :: xtype + integer(i4), intent(out), optional :: len + integer(i4), intent(out), optional :: attnum integer(i4) :: status select type (self) class is (NcGroup) - status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name) + status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype, len, attnum) class is (NcVariable) - status = nf90_inquire_attribute(self%parent%id, self%id, name) + status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype, len, attnum) end select hasAttribute = (status == NF90_NOERR) @@ -3606,6 +3609,8 @@ contains getDtypeFromString = NF90_INT case("i64") getDtypeFromString = NF90_INT64 + case("char") + getDtypeFromString = NF90_CHAR case default write(*,*) "Datatype not understood: ", dtype stop 1 @@ -3629,6 +3634,8 @@ contains getDtypeFromInteger = "i32" case(NF90_INT64) getDtypeFromInteger = "i64" + case(NF90_CHAR) + getDtypeFromInteger = "char" case default write(*,*) "Datatype not understood: ", dtype stop 1 diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 9ba284e..73cfe41 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -40,7 +40,7 @@ module mo_netcdf nf90_inq_ncid, nf90_inq_grp_parent, nf90_inq_grpname, nf90_def_grp, & nf90_rename_dim, nf90_rename_var, nf90_rename_att, nf90_sync, & NF90_OPEN, NF90_NETCDF4, NF90_CREATE, NF90_WRITE, NF90_NOWRITE, & - NF90_BYTE, NF90_SHORT, NF90_INT, NF90_INT64, NF90_FLOAT, NF90_DOUBLE, & + NF90_BYTE, NF90_SHORT, NF90_INT, NF90_INT64, NF90_FLOAT, NF90_DOUBLE, NF90_CHAR, & NF90_FILL_BYTE, NF90_FILL_SHORT, NF90_FILL_INT, NF90_FILL_FLOAT, NF90_FILL_DOUBLE, & NF90_NOERR, NF90_UNLIMITED, NF90_GLOBAL, NF90_SHARE, NF90_HDF5, & NF90_64BIT_OFFSET, NF90_CLASSIC_MODEL @@ -839,16 +839,19 @@ contains end do end function isUnlimitedVariable - logical function hasAttribute(self, name) + logical function hasAttribute(self, name, xtype, len, attnum) class(NcAttributable), intent(in) :: self character(*), intent(in) :: name + integer(i4), intent(out), optional :: xtype + integer(i4), intent(out), optional :: len + integer(i4), intent(out), optional :: attnum integer(i4) :: status select type (self) class is (NcGroup) - status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name) + status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype, len, attnum) class is (NcVariable) - status = nf90_inquire_attribute(self%parent%id, self%id, name) + status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype, len, attnum) end select hasAttribute = (status == NF90_NOERR) @@ -1172,6 +1175,8 @@ $: ' allocate(mask(' + ('datashape({}), '*rank).format(*list(range(1, rank+ getDtypeFromString = NF90_INT case("i64") getDtypeFromString = NF90_INT64 + case("char") + getDtypeFromString = NF90_CHAR case default write(*,*) "Datatype not understood: ", dtype stop 1 @@ -1195,6 +1200,8 @@ $: ' allocate(mask(' + ('datashape({}), '*rank).format(*list(range(1, rank+ getDtypeFromInteger = "i32" case(NF90_INT64) getDtypeFromInteger = "i64" + case(NF90_CHAR) + getDtypeFromInteger = "char" case default write(*,*) "Datatype not understood: ", dtype stop 1 diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 6ce7782..944a8e0 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -14,6 +14,9 @@ module test_mo_netcdf character(*), parameter :: fname="netcdf_make_check_test_file" character(*), parameter :: vname_time="time", vname_lat="lat", vname_lon="lon", vname_data="data" character(64), dimension(2), parameter :: waglobalnames = [character(64) :: 'Author', 'Year'] + integer(i4), dimension(2), parameter :: waglobaltype = [1_i4, 1_i4] + integer(i4), dimension(2), parameter :: wagloballen = [1_i4, 1_i4] + logical, dimension(2), parameter :: wahasatt = [.true., .true.] character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] character(64), dimension(:), allocatable :: raglobalnames, ravarnames character(64) :: wavalue, ravalue @@ -116,13 +119,16 @@ contains call var_lat%getData(rlat) call var_lon%getData(rlon) call var_data%getData(rdata) - call var_data%getAttributeNames(wavarnames) + wavarnames = var_data%getAttributeNames() ! read the fill value call var_data%getFillValue(rfvalue) ! read a global attribute - call nc%getAttributeNames(waglobalnames) + waglobalnames = nc%getAttributeNames() + wahasatt(1) = nc%hasAttribute(raglobalnames(1), waglobaltype(1), wagloballen(1)) + wahasatt(2) = nc%hasAttribute(raglobalnames(2), waglobaltype(2), wagloballen(2)) + call nc%getAttribute(trim(raglobalnames(1)), ravalue) ! close dataset @@ -136,8 +142,15 @@ contains @assertEqual(rdata, wdata(:,:,1:ntime)) @assertEqual(rfvalue, rfvalue) @assertEqual(ravalue, wavalue) - @assertEqual(ravarnames, wavarnames) - @assertEqual(raglobalnames, waglobalnames) + do i=1, size(wavarnames) + @assertEqual(ravarnames(i), wavarnames(i)) + end do + do i=1, size(waglobalnames) + @assertEqual(raglobalnames(i), waglobalnames(i)) + @assertEqual(rahasatt(i), wahasatt(i)) + @assertEqual(raglobaltype(i), waglobaltype(i)) + @assertEqual(ragloballen(i), wagloballen(i)) + end do end subroutine test_netcdf_create_new -- GitLab From 4fd26346200d3f8f8b063512aa5b1e842698ed80 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 16:15:28 +0100 Subject: [PATCH 06/16] - fixed bugs 4 --- src/pf_tests/test_mo_netcdf.pf | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 944a8e0..2db6af4 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -14,11 +14,14 @@ module test_mo_netcdf character(*), parameter :: fname="netcdf_make_check_test_file" character(*), parameter :: vname_time="time", vname_lat="lat", vname_lon="lon", vname_data="data" character(64), dimension(2), parameter :: waglobalnames = [character(64) :: 'Author', 'Year'] - integer(i4), dimension(2), parameter :: waglobaltype = [1_i4, 1_i4] - integer(i4), dimension(2), parameter :: wagloballen = [1_i4, 1_i4] + ! see netcdf-fortran repo: /fortran/netcdf_constants.f90 + integer(i4), dimension(2), parameter :: waglobaltype = [2_i4, 4_i4] + integer(i4), dimension(2), parameter :: wagloballen = [14_i4, 1_i4] logical, dimension(2), parameter :: wahasatt = [.true., .true.] character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] character(64), dimension(:), allocatable :: raglobalnames, ravarnames + integer(i4), dimension(:), allocatable :: raglobaltype, ragloballen + logical, dimension(:), allocatable :: rahasatt character(64) :: wavalue, ravalue type(NcDataset) :: nc @@ -119,17 +122,17 @@ contains call var_lat%getData(rlat) call var_lon%getData(rlon) call var_data%getData(rdata) - wavarnames = var_data%getAttributeNames() + ravarnames = var_data%getAttributeNames() ! read the fill value call var_data%getFillValue(rfvalue) ! read a global attribute - waglobalnames = nc%getAttributeNames() - wahasatt(1) = nc%hasAttribute(raglobalnames(1), waglobaltype(1), wagloballen(1)) - wahasatt(2) = nc%hasAttribute(raglobalnames(2), waglobaltype(2), wagloballen(2)) + raglobalnames = nc%getAttributeNames() + rahasatt(1) = nc%hasAttribute(waglobalnames(1), raglobaltype(1), ragloballen(1)) + rahasatt(2) = nc%hasAttribute(waglobalnames(2), raglobaltype(2), ragloballen(2)) - call nc%getAttribute(trim(raglobalnames(1)), ravalue) + call nc%getAttribute(trim(waglobalnames(1)), ravalue) ! close dataset call nc%close() -- GitLab From d5cd0db70aadd3e2ec873036631380391fa5083a Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 16:42:56 +0100 Subject: [PATCH 07/16] - fixed bugs 5 --- src/mo_netcdf.f90 | 13 ++++++------- src/mo_netcdf.fypp | 11 +++++------ src/pf_tests/test_mo_netcdf.pf | 16 +++++++++------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/mo_netcdf.f90 b/src/mo_netcdf.f90 index 4d23afa..d2d1698 100644 --- a/src/mo_netcdf.f90 +++ b/src/mo_netcdf.f90 @@ -1086,20 +1086,19 @@ contains integer(i4), parameter :: maxNames = 100_i4 integer(i4) :: nAtts = 0_i4 character(256) :: attName + integer(i4) :: status ! assume a maximum number of 100 attributes that are checked - allocate(attributeNames(nAtts)) + allocate(attributeNames(maxNames)) do while (nAtts < maxNames) select type (self) class is (NcGroup) - call check(nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & - "Could not inquire attributes for NcGroup: " // self%getName()) + status = nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) class is (NcVariable) - call check(nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & - "Could not inquire attributes for NcVariable: " // self%getName()) + status = nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) end select - ! if the attribute name is not defined, exit loop, else increase counter - if (len_trim(attName) == 0_i4) then + ! if the status is negative, exit loop, else increase counter + if (status /= NF90_NOERR) then exit else nAtts = nAtts + 1_i4 diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 73cfe41..a196788 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -864,20 +864,19 @@ contains integer(i4), parameter :: maxNames = 100_i4 integer(i4) :: nAtts = 0_i4 character(256) :: attName + integer(i4) :: status ! assume a maximum number of 100 attributes that are checked allocate(attributeNames(maxNames)) do while (nAtts < maxNames) select type (self) class is (NcGroup) - call check(nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & - "Could not inquire attributes for NcGroup: " // self%getName()) + status = nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) class is (NcVariable) - call check(nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)), & - "Could not inquire attributes for NcVariable: " // self%getName()) + status = nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) end select - ! if the attribute name is not defined, exit loop, else increase counter - if (len_trim(attName) == 0_i4) then + ! if the status is negative, exit loop, else increase counter + if (status /= NF90_NOERR) then exit else nAtts = nAtts + 1_i4 diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 2db6af4..0794f27 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -13,11 +13,11 @@ module test_mo_netcdf character(*), parameter :: fname="netcdf_make_check_test_file" character(*), parameter :: vname_time="time", vname_lat="lat", vname_lon="lon", vname_data="data" - character(64), dimension(2), parameter :: waglobalnames = [character(64) :: 'Author', 'Year'] + character(64), dimension(3), parameter :: waglobalnames = [character(64) :: 'Author', 'Year', 'intentionally_fail'] ! see netcdf-fortran repo: /fortran/netcdf_constants.f90 - integer(i4), dimension(2), parameter :: waglobaltype = [2_i4, 4_i4] - integer(i4), dimension(2), parameter :: wagloballen = [14_i4, 1_i4] - logical, dimension(2), parameter :: wahasatt = [.true., .true.] + integer(i4), dimension(3), parameter :: waglobaltype = [2_i4, 4_i4, -1_i4] + integer(i4), dimension(3), parameter :: wagloballen = [14_i4, 1_i4, -1_i4] + logical, dimension(3), parameter :: wahasatt = [.true., .true., .false.] character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] character(64), dimension(:), allocatable :: raglobalnames, ravarnames integer(i4), dimension(:), allocatable :: raglobaltype, ragloballen @@ -149,10 +149,12 @@ contains @assertEqual(ravarnames(i), wavarnames(i)) end do do i=1, size(waglobalnames) - @assertEqual(raglobalnames(i), waglobalnames(i)) @assertEqual(rahasatt(i), wahasatt(i)) - @assertEqual(raglobaltype(i), waglobaltype(i)) - @assertEqual(ragloballen(i), wagloballen(i)) + if (rahasatt(i)) then + @assertEqual(raglobalnames(i), waglobalnames(i)) + @assertEqual(raglobaltype(i), waglobaltype(i)) + @assertEqual(ragloballen(i), wagloballen(i)) + end if end do end subroutine test_netcdf_create_new -- GitLab From 798b1c3df82a61a51b73f79c360f9316233b3e94 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 17:14:23 +0100 Subject: [PATCH 08/16] - fixed bugs 6 --- src/mo_netcdf.f90 | 4 ++-- src/mo_netcdf.fypp | 4 ++-- src/pf_tests/test_mo_netcdf.pf | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/mo_netcdf.f90 b/src/mo_netcdf.f90 index d2d1698..bb5c359 100644 --- a/src/mo_netcdf.f90 +++ b/src/mo_netcdf.f90 @@ -1071,9 +1071,9 @@ contains select type (self) class is (NcGroup) - status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype, len, attnum) + status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype=xtype, len=len, attnum=attnum) class is (NcVariable) - status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype, len, attnum) + status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype=xtype, len=len, attnum=attnum) end select hasAttribute = (status == NF90_NOERR) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index a196788..1d92967 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -849,9 +849,9 @@ contains select type (self) class is (NcGroup) - status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype, len, attnum) + status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype=xtype, len=len, attnum=attnum) class is (NcVariable) - status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype, len, attnum) + status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype=xtype, len=len, attnum=attnum) end select hasAttribute = (status == NF90_NOERR) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 0794f27..7d68e98 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -129,8 +129,9 @@ contains ! read a global attribute raglobalnames = nc%getAttributeNames() - rahasatt(1) = nc%hasAttribute(waglobalnames(1), raglobaltype(1), ragloballen(1)) - rahasatt(2) = nc%hasAttribute(waglobalnames(2), raglobaltype(2), ragloballen(2)) + rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1)) + rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2)) + rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3)) call nc%getAttribute(trim(waglobalnames(1)), ravalue) -- GitLab From 2c3795f87c3749c09c8a117aa1135e02a2f25790 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 17:23:27 +0100 Subject: [PATCH 09/16] - fixed bugs 7 --- src/pf_tests/test_mo_netcdf.pf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 7d68e98..6a3de51 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -15,13 +15,13 @@ module test_mo_netcdf character(*), parameter :: vname_time="time", vname_lat="lat", vname_lon="lon", vname_data="data" character(64), dimension(3), parameter :: waglobalnames = [character(64) :: 'Author', 'Year', 'intentionally_fail'] ! see netcdf-fortran repo: /fortran/netcdf_constants.f90 + logical, dimension(3), parameter :: wahasatt = [.true., .true., .false.] integer(i4), dimension(3), parameter :: waglobaltype = [2_i4, 4_i4, -1_i4] integer(i4), dimension(3), parameter :: wagloballen = [14_i4, 1_i4, -1_i4] - logical, dimension(3), parameter :: wahasatt = [.true., .true., .false.] + integer(i4), dimension(3) :: raglobaltype, ragloballen + logical, dimension(3) :: rahasatt character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] character(64), dimension(:), allocatable :: raglobalnames, ravarnames - integer(i4), dimension(:), allocatable :: raglobaltype, ragloballen - logical, dimension(:), allocatable :: rahasatt character(64) :: wavalue, ravalue type(NcDataset) :: nc -- GitLab From bc433dcf301404c301b47f92c40fcad504d3d016 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 17:49:51 +0100 Subject: [PATCH 10/16] - fixed bugs 7 (moved attribute units setting to after other attributes) --- src/pf_tests/test_mo_netcdf.pf | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index 6a3de51..acf5a37 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -81,6 +81,10 @@ contains ! add some variable attributes call var_time%setAttribute("units", "days since 1989-12-31 12:00:00") + ! add some variable attributes + call var_data%setAttribute(trim(wavarnames(1)), "mm/d") + call var_data%setAttribute(trim(wavarnames(2)), 0.1_dp) + ! set fill value before any data is written call var_data%setFillValue(wfvalue) @@ -94,10 +98,6 @@ contains call var_data%setData(wdata(:,:,i), start=(/1,1,i/)) end do - ! add some more variable attributes - call var_data%setAttribute(trim(wavarnames(1)), "mm/d") - call var_data%setAttribute(trim(wavarnames(2)), 0.1_dp) - ! add global attributes call nc%setAttribute(trim(waglobalnames(1)), wavalue) call nc%setAttribute(trim(waglobalnames(2)), 2099_i4) -- GitLab From 7a409015680822969871c186863a5c7cb29cfc7f Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 18:08:36 +0100 Subject: [PATCH 11/16] - something is wrong with getAttributesNames in test case... print diagnostics - switched actual and expected arf in assertEqual calls for mo_netcdf --- src/pf_tests/test_mo_netcdf.pf | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index acf5a37..c3d4f55 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -123,12 +123,15 @@ contains call var_lon%getData(rlon) call var_data%getData(rdata) ravarnames = var_data%getAttributeNames() + print*, 'data var', size(ravarnames), ravarnames ! read the fill value call var_data%getFillValue(rfvalue) ! read a global attribute raglobalnames = nc%getAttributeNames() + print*, 'global', size(raglobalnames), raglobalnames + print*, 'time var', var_time%getAttributeNames() rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1)) rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2)) rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3)) @@ -140,21 +143,21 @@ contains ! 1.3 Check ! --------- - @assertEqual(rtime, wtime(1:ntime)) - @assertEqual(rlat, wlat) - @assertEqual(rlon, wlon) - @assertEqual(rdata, wdata(:,:,1:ntime)) + @assertEqual(wtime(1:ntime), rtime) + @assertEqual(wlat, rlat) + @assertEqual(wlon, rlon) + @assertEqual(wdata(:,:,1:ntime), rdata) @assertEqual(rfvalue, rfvalue) - @assertEqual(ravalue, wavalue) + @assertEqual(wavalue, ravalue) do i=1, size(wavarnames) - @assertEqual(ravarnames(i), wavarnames(i)) + @assertEqual(wavarnames(i), ravarnames(i)) end do do i=1, size(waglobalnames) - @assertEqual(rahasatt(i), wahasatt(i)) + @assertEqual(wahasatt(i), rahasatt(i)) if (rahasatt(i)) then - @assertEqual(raglobalnames(i), waglobalnames(i)) - @assertEqual(raglobaltype(i), waglobaltype(i)) - @assertEqual(ragloballen(i), wagloballen(i)) + @assertEqual(waglobalnames(i), raglobalnames(i)) + @assertEqual(waglobaltype(i), raglobaltype(i)) + @assertEqual(wagloballen(i), ragloballen(i)) end if end do @@ -205,8 +208,8 @@ contains ! 2.3 Check ! --------- - @assertEqual(rtime(1:nadd), wtime(ntime+1:ntime+nadd)) - @assertEqual(rdata(:,:,1:nadd), wdata(:,:,ntime+1:ntime+nadd)) + @assertEqual(wtime(ntime+1:ntime+nadd), rtime(1:nadd)) + @assertEqual(wdata(:,:,ntime+1:ntime+nadd), rdata(:,:,1:nadd)) end subroutine test_netcdf_append @@ -275,8 +278,8 @@ contains ! 3.3 check ! --------- - @assertEqual(rtime, wtime) - @assertEqual(rdata, wdata) + @assertEqual(wtime, rtime) + @assertEqual(wdata, rdata) end subroutine test_netcdf_dump -- GitLab From 78f53d1fe3ed0c2db8aec47f64f18667987e9611 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 20:46:01 +0100 Subject: [PATCH 12/16] - something is wrong with getAttributesNames in test case... print diagnostics --- src/mo_netcdf.fypp | 4 ++++ src/pf_tests/test_mo_netcdf.pf | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 1d92967..1490b14 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -849,8 +849,10 @@ contains select type (self) class is (NcGroup) + print*, 'is group' status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype=xtype, len=len, attnum=attnum) class is (NcVariable) + print*, 'is var' status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype=xtype, len=len, attnum=attnum) end select @@ -871,8 +873,10 @@ contains do while (nAtts < maxNames) select type (self) class is (NcGroup) + print*, 'is group' status = nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) class is (NcVariable) + print*, 'is variable' status = nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) end select ! if the status is negative, exit loop, else increase counter diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index c3d4f55..a134675 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -18,7 +18,7 @@ module test_mo_netcdf logical, dimension(3), parameter :: wahasatt = [.true., .true., .false.] integer(i4), dimension(3), parameter :: waglobaltype = [2_i4, 4_i4, -1_i4] integer(i4), dimension(3), parameter :: wagloballen = [14_i4, 1_i4, -1_i4] - integer(i4), dimension(3) :: raglobaltype, ragloballen + integer(i4), dimension(3) :: raglobaltype, ragloballen, raglobalnum logical, dimension(3) :: rahasatt character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] character(64), dimension(:), allocatable :: raglobalnames, ravarnames @@ -130,11 +130,14 @@ contains ! read a global attribute raglobalnames = nc%getAttributeNames() - print*, 'global', size(raglobalnames), raglobalnames print*, 'time var', var_time%getAttributeNames() - rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1)) - rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2)) - rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3)) + rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1), attnum=raglobalnum(1)) + rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2), attnum=raglobalnum(2)) + rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3), attnum=raglobalnum(3)) + print*, 'extracted global names ',raglobalnames + do i=1, 3 + print*, 'hasAttribute ', iAtt, trim(waglobalnames(i)), raglobaltype(i), ragloballen(i), raglobalnum(i) + end do call nc%getAttribute(trim(waglobalnames(1)), ravalue) -- GitLab From dc0a28f804e64848450370972b0605355c46fa9c Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 21:00:50 +0100 Subject: [PATCH 13/16] - something is wrong with getAttributesNames in test case... print diagnostics --- src/pf_tests/test_mo_netcdf.pf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index a134675..fca8e9e 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -136,7 +136,7 @@ contains rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3), attnum=raglobalnum(3)) print*, 'extracted global names ',raglobalnames do i=1, 3 - print*, 'hasAttribute ', iAtt, trim(waglobalnames(i)), raglobaltype(i), ragloballen(i), raglobalnum(i) + print*, 'hasAttribute ', i, trim(waglobalnames(i)), raglobaltype(i), ragloballen(i), raglobalnum(i) end do call nc%getAttribute(trim(waglobalnames(1)), ravalue) -- GitLab From 2c28bf58cf56b49946566bb4c5194a59e0649d42 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 21:25:55 +0100 Subject: [PATCH 14/16] - something is wrong with getAttributesNames in test case... print diagnostics --- src/mo_netcdf.fypp | 8 ++++---- src/pf_tests/test_mo_netcdf.pf | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 1490b14..5c19d88 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -849,10 +849,10 @@ contains select type (self) class is (NcGroup) - print*, 'is group' + print*, 'hasAttribute group' status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype=xtype, len=len, attnum=attnum) class is (NcVariable) - print*, 'is var' + print*, 'hasAttribute var' status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype=xtype, len=len, attnum=attnum) end select @@ -873,10 +873,10 @@ contains do while (nAtts < maxNames) select type (self) class is (NcGroup) - print*, 'is group' + print*, 'is group', nAtts + 1_i4 status = nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) class is (NcVariable) - print*, 'is variable' + print*, 'is variable', nAtts + 1_i4 status = nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) end select ! if the status is negative, exit loop, else increase counter diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index fca8e9e..b3e8fe8 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -122,19 +122,29 @@ contains call var_lat%getData(rlat) call var_lon%getData(rlon) call var_data%getData(rdata) + + print*, 'getting var_data attributes...' ravarnames = var_data%getAttributeNames() - print*, 'data var', size(ravarnames), ravarnames + do i=1, size(ravarnames) + print*, 'got attribute ', i, ' named', trim(ravarnames(i)) + end do + print*, 'getting var_data fillvalue...' ! read the fill value call var_data%getFillValue(rfvalue) + print*, 'getting global attributes...' ! read a global attribute raglobalnames = nc%getAttributeNames() - print*, 'time var', var_time%getAttributeNames() + do i=1, size(raglobalnames) + print*, 'got attribute ', i, ' named', trim(raglobalnames(i)) + end do + + print*, 'checking global attributes...' rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1), attnum=raglobalnum(1)) rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2), attnum=raglobalnum(2)) rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3), attnum=raglobalnum(3)) - print*, 'extracted global names ',raglobalnames + do i=1, 3 print*, 'hasAttribute ', i, trim(waglobalnames(i)), raglobaltype(i), ragloballen(i), raglobalnum(i) end do -- GitLab From 692d7e2516a25714a45f8a9ead92c822d62334ea Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 21:36:42 +0100 Subject: [PATCH 15/16] - something is wrong with getAttributesNames in test case... print diagnostics --- src/mo_netcdf.fypp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 5c19d88..25de136 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -864,12 +864,12 @@ contains character(256), dimension(:), allocatable :: attributeNames integer(i4), parameter :: maxNames = 100_i4 - integer(i4) :: nAtts = 0_i4 - character(256) :: attName + integer(i4) :: nAtts integer(i4) :: status ! assume a maximum number of 100 attributes that are checked allocate(attributeNames(maxNames)) + nAtts = 0_i4 do while (nAtts < maxNames) select type (self) class is (NcGroup) -- GitLab From b34f55e6d4dca12b36905065835a78b72a81c9b8 Mon Sep 17 00:00:00 2001 From: Robert Schweppe Date: Thu, 25 Nov 2021 21:57:19 +0100 Subject: [PATCH 16/16] - should work now --- src/mo_netcdf.f90 | 4 ++-- src/mo_netcdf.fypp | 4 ---- src/pf_tests/test_mo_netcdf.pf | 22 ++++------------------ 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/mo_netcdf.f90 b/src/mo_netcdf.f90 index bb5c359..2826cd5 100644 --- a/src/mo_netcdf.f90 +++ b/src/mo_netcdf.f90 @@ -1084,12 +1084,12 @@ contains character(256), dimension(:), allocatable :: attributeNames integer(i4), parameter :: maxNames = 100_i4 - integer(i4) :: nAtts = 0_i4 - character(256) :: attName + integer(i4) :: nAtts integer(i4) :: status ! assume a maximum number of 100 attributes that are checked allocate(attributeNames(maxNames)) + nAtts = 0_i4 do while (nAtts < maxNames) select type (self) class is (NcGroup) diff --git a/src/mo_netcdf.fypp b/src/mo_netcdf.fypp index 25de136..8410b77 100644 --- a/src/mo_netcdf.fypp +++ b/src/mo_netcdf.fypp @@ -849,10 +849,8 @@ contains select type (self) class is (NcGroup) - print*, 'hasAttribute group' status = nf90_inquire_attribute(self%id, NF90_GLOBAL, name, xtype=xtype, len=len, attnum=attnum) class is (NcVariable) - print*, 'hasAttribute var' status = nf90_inquire_attribute(self%parent%id, self%id, name, xtype=xtype, len=len, attnum=attnum) end select @@ -873,10 +871,8 @@ contains do while (nAtts < maxNames) select type (self) class is (NcGroup) - print*, 'is group', nAtts + 1_i4 status = nf90_inq_attname(self%id, NF90_GLOBAL, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) class is (NcVariable) - print*, 'is variable', nAtts + 1_i4 status = nf90_inq_attname(self%parent%id, self%id, nAtts + 1_i4, attributeNames(nAtts + 1_i4)) end select ! if the status is negative, exit loop, else increase counter diff --git a/src/pf_tests/test_mo_netcdf.pf b/src/pf_tests/test_mo_netcdf.pf index b3e8fe8..9f520ba 100644 --- a/src/pf_tests/test_mo_netcdf.pf +++ b/src/pf_tests/test_mo_netcdf.pf @@ -17,7 +17,7 @@ module test_mo_netcdf ! see netcdf-fortran repo: /fortran/netcdf_constants.f90 logical, dimension(3), parameter :: wahasatt = [.true., .true., .false.] integer(i4), dimension(3), parameter :: waglobaltype = [2_i4, 4_i4, -1_i4] - integer(i4), dimension(3), parameter :: wagloballen = [14_i4, 1_i4, -1_i4] + integer(i4), dimension(3), parameter :: wagloballen = [64_i4, 1_i4, -1_i4] integer(i4), dimension(3) :: raglobaltype, ragloballen, raglobalnum logical, dimension(3) :: rahasatt character(64), dimension(2), parameter :: wavarnames = [character(64) :: 'units', 'scaling'] @@ -123,31 +123,17 @@ contains call var_lon%getData(rlon) call var_data%getData(rdata) - print*, 'getting var_data attributes...' ravarnames = var_data%getAttributeNames() - do i=1, size(ravarnames) - print*, 'got attribute ', i, ' named', trim(ravarnames(i)) - end do - print*, 'getting var_data fillvalue...' ! read the fill value call var_data%getFillValue(rfvalue) - print*, 'getting global attributes...' ! read a global attribute raglobalnames = nc%getAttributeNames() - do i=1, size(raglobalnames) - print*, 'got attribute ', i, ' named', trim(raglobalnames(i)) - end do - print*, 'checking global attributes...' - rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1), attnum=raglobalnum(1)) - rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2), attnum=raglobalnum(2)) - rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3), attnum=raglobalnum(3)) - - do i=1, 3 - print*, 'hasAttribute ', i, trim(waglobalnames(i)), raglobaltype(i), ragloballen(i), raglobalnum(i) - end do + rahasatt(1) = nc%hasAttribute(name=waglobalnames(1), xtype=raglobaltype(1), len=ragloballen(1)) + rahasatt(2) = nc%hasAttribute(name=waglobalnames(2), xtype=raglobaltype(2), len=ragloballen(2)) + rahasatt(3) = nc%hasAttribute(name=waglobalnames(3), xtype=raglobaltype(3), len=ragloballen(3)) call nc%getAttribute(trim(waglobalnames(1)), ravalue) -- GitLab