From 6851429cc87e263d8af2736885e7f2d3fdae85c7 Mon Sep 17 00:00:00 2001 From: lewisn-met Date: Fri, 4 Dec 2026 11:00:36 +0000 Subject: [PATCH 1/2] initial commit --- src/opsinputs/opsinputs_fill_mod.F90 | 28 +++---- src/opsinputs/opsinputs_obsspace_f.cc | 6 +- src/opsinputs/opsinputs_obsspace_f.h | 1 + test/CMakeLists.txt | 11 +++ test/mains/TestFillVarNamesWithChannels.F90 | 40 ++++++++++ test/testinput/081_VarField_level_time.yaml | 15 +++- .../Aircraft.nl | 3 + ...bswriter_unsuffixed_metadata_channels.yaml | 76 +++++++++++++++++++ 8 files changed, 164 insertions(+), 16 deletions(-) create mode 100644 test/mains/TestFillVarNamesWithChannels.F90 create mode 100644 test/testinput/VarObsWriterNamelists_unsuffixed_metadata_channels/Aircraft.nl create mode 100644 test/testinput/varobswriter_unsuffixed_metadata_channels.yaml diff --git a/src/opsinputs/opsinputs_fill_mod.F90 b/src/opsinputs/opsinputs_fill_mod.F90 index 575606c8..f6484a50 100644 --- a/src/opsinputs/opsinputs_fill_mod.F90 +++ b/src/opsinputs/opsinputs_fill_mod.F90 @@ -2079,10 +2079,13 @@ subroutine opsinputs_fill_filltimeoffsets( & ! Local declarations: integer(c_int64_t) :: VarValue(JediToOpsLayoutMapping % NumJediObs) -integer :: i +integer(c_int64_t) :: MissingInt64 +integer :: i, iJediObs ! Body: +MissingInt64 = missing_value(0_c_int64_t) + if (obsspace_has(ObsSpace, JediVarGroup, JediVarName)) then ! Retrieve data from JEDI call opsinputs_obsspace_get_db_datetime_offset_in_seconds( & @@ -2092,14 +2095,13 @@ subroutine opsinputs_fill_filltimeoffsets( & call Ops_Alloc(Hdr, OpsVarName, JediToOpsLayoutMapping % NumOpsObs, Real1) do i = 1, JediToOpsLayoutMapping % NumOpsObs if (JediToOpsLayoutMapping % ConvertRecordsToMultilevelObs) then - if (JediToOpsLayoutMapping % RecordStarts(i + 1) > JediToOpsLayoutMapping % RecordStarts(i)) then - ! This record is non-empty. Use the first location from that record. - Real1(i) = VarValue(JediToOpsLayoutMapping % LocationsOrderedByRecord( & - JediToOpsLayoutMapping % RecordStarts(i))) - end if + if (JediToOpsLayoutMapping % RecordStarts(i + 1) <= JediToOpsLayoutMapping % RecordStarts(i)) cycle + ! This record is non-empty. Use the first location from that record. + iJediObs = JediToOpsLayoutMapping % LocationsOrderedByRecord(JediToOpsLayoutMapping % RecordStarts(i)) else - Real1(i) = VarValue(i) + iJediObs = i end if + if (VarValue(iJediObs) /= MissingInt64) Real1(i) = VarValue(iJediObs) end do end if end subroutine opsinputs_fill_filltimeoffsets @@ -2160,13 +2162,13 @@ subroutine opsinputs_fill_filltimeoffsets2d_norecords( & ! Local declarations: integer(c_int64_t) :: VarValue(NumObs) -real(kind=c_double) :: MissingDouble +integer(c_int64_t) :: MissingInt64 character(len=max_varname_with_channel_length) :: JediVarNamesWithChannels(max(size(Channels), 1)) integer :: iChannel ! Body: -MissingDouble = missing_value(0.0_c_double) +MissingInt64 = missing_value(0_c_int64_t) JediVarNamesWithChannels = opsinputs_fill_varnames_with_channels( & JediVarName, Channels, JediVarHasChannels) @@ -2187,7 +2189,7 @@ subroutine opsinputs_fill_filltimeoffsets2d_norecords( & end if ! Fill the OPS data structures - where (VarValue /= MissingDouble) + where (VarValue /= MissingInt64) Real2(:, iChannel) = VarValue end where end do @@ -2242,12 +2244,12 @@ subroutine opsinputs_fill_filltimeoffsets2d_records( & ! Local declarations: integer(c_int64_t) :: VarValue(JediToOpsLayoutMapping % NumJediObs) -real(kind=c_double) :: MissingDouble +integer(c_int64_t) :: MissingInt64 integer :: iObs, iLevel, iJediObs, numLevels ! Body: -MissingDouble = missing_value(0.0_c_double) +MissingInt64 = missing_value(0_c_int64_t) if (obsspace_has(ObsSpace, JediVarGroup, JediVarName)) then ! Retrieve data from JEDI @@ -2279,7 +2281,7 @@ subroutine opsinputs_fill_filltimeoffsets2d_records( & do iLevel = 1, numLevels iJediObs = JediToOpsLayoutMapping % LocationsOrderedByRecord( & JediToOpsLayoutMapping % RecordStarts(iObs) + (iLevel - 1)) - if (VarValue(iJediObs) /= MissingDouble) then + if (VarValue(iJediObs) /= MissingInt64) then Real2(iObs, iLevel) = VarValue(iJediObs) end if end do diff --git a/src/opsinputs/opsinputs_obsspace_f.cc b/src/opsinputs/opsinputs_obsspace_f.cc index f7bd8287..22d39510 100644 --- a/src/opsinputs/opsinputs_obsspace_f.cc +++ b/src/opsinputs/opsinputs_obsspace_f.cc @@ -16,6 +16,7 @@ #include "oops/util/DateTime.h" #include "oops/util/Duration.h" +#include "oops/util/missingValues.h" namespace opsinputs { @@ -30,8 +31,11 @@ void opsinputs_obsspace_get_db_datetime_offset_in_seconds_f( std::vector datetimes(length, util::DateTime("0000-01-01T00:00:00Z")); obsspace.get_db(std::string(group), std::string(vname), datetimes); + const util::DateTime &missingDateTime = util::missingValue(); + const int64_t missingInt64 = util::missingValue(); for (std::size_t i = 0; i < length; i++) { - offsets[i] = (datetimes[i] - reference).toSeconds(); + offsets[i] = datetimes[i] == missingDateTime ? missingInt64 + : (datetimes[i] - reference).toSeconds(); } } diff --git a/src/opsinputs/opsinputs_obsspace_f.h b/src/opsinputs/opsinputs_obsspace_f.h index 186159c4..2534ef87 100644 --- a/src/opsinputs/opsinputs_obsspace_f.h +++ b/src/opsinputs/opsinputs_obsspace_f.h @@ -23,6 +23,7 @@ namespace opsinputs { extern "C" { + /// Return offsets in seconds, or util::missingValue() for missing datetimes. void opsinputs_obsspace_get_db_datetime_offset_in_seconds_f( const ioda::ObsSpace &obsspace, const char *group, const char *vname, const util::DateTime &reference, const size_t &length, int64_t *offsets); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 065fcc66..42e620c3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -330,6 +330,12 @@ ADD_WRITER_TEST(NAME varobswriter_083_VarField_level_lon NAMELIST VarObsWriterNamelists_083_VarField_level_lon/Sonde.nl DATA 078_VarField_theta.nc4) +ADD_WRITER_TEST(NAME varobswriter_unsuffixed_metadata_channels + LABELS varobswriter varfield + YAML varobswriter_unsuffixed_metadata_channels.yaml + NAMELIST VarObsWriterNamelists_unsuffixed_metadata_channels/Aircraft.nl + DATA 002_VarField_temperature_RadarZ.nc4) + # MPI tests ADD_WRITER_TEST(NAME varobswriter_002_VarField_temperature_RadarZ_MPI_1 LABELS mpi varobswriter varfield @@ -938,6 +944,11 @@ ADD_WRITER_TEST(NAME cxwriter_ukvnamelist_radar_reflectivity ### Tests of auxiliary classes +ecbuild_add_test( TARGET ${PROJECT_NAME}_fill_varnames_with_channels + SOURCES mains/TestFillVarNamesWithChannels.F90 + LINKER_LANGUAGE ${OPSINPUTS_LINKER_LANGUAGE} + LIBS ${PROJECT_NAME} ) + list( APPEND opsinputs_test_input empty.yaml ) ecbuild_add_test( TARGET ${PROJECT_NAME}_mpiexceptionsynchronizer SOURCES mains/TestMPIExceptionSynchronizer.cc diff --git a/test/mains/TestFillVarNamesWithChannels.F90 b/test/mains/TestFillVarNamesWithChannels.F90 new file mode 100644 index 00000000..1dab7610 --- /dev/null +++ b/test/mains/TestFillVarNamesWithChannels.F90 @@ -0,0 +1,40 @@ +! (C) Crown Copyright 2026 Met Office. All rights reserved. +! + +program test_fill_varnames_with_channels +use, intrinsic :: iso_c_binding, only: c_int +use opsinputs_fill_mod, only: opsinputs_fill_varnames_with_channels +use opsinputs_utils_mod, only: max_varname_with_channel_length +implicit none + +integer(c_int), parameter :: channels(3) = [3_c_int, 7_c_int, 11_c_int] +integer(c_int), parameter :: no_channels(0) = [integer(c_int) ::] +character(len=max_varname_with_channel_length), parameter :: suffixed_names(3) = & + [character(len=max_varname_with_channel_length) :: "pressure_3", "pressure_7", "pressure_11"] +character(len=max_varname_with_channel_length), allocatable :: names(:) + +names = opsinputs_fill_varnames_with_channels("pressure", channels, JediVarHasChannels=.false.) +if (size(names) /= size(channels)) error stop "Unsuffixed names: expected one name per channel" +if (.not. all(names == "pressure")) error stop "Unsuffixed names: expected pressure for every channel" + +names = opsinputs_fill_varnames_with_channels("pressure", channels) +if (size(names) /= size(channels)) error stop "Default names: expected one name per channel" +if (.not. all(names == suffixed_names)) error stop "Default names: incorrect channel suffixes" + +names = opsinputs_fill_varnames_with_channels("pressure", channels, JediVarHasChannels=.true.) +if (size(names) /= size(channels)) error stop "Suffixed names: expected one name per channel" +if (.not. all(names == suffixed_names)) error stop "Suffixed names: incorrect channel suffixes" + +names = opsinputs_fill_varnames_with_channels("pressure", no_channels) +if (size(names) /= 1) error stop "Empty channels, default: expected a single name" +if (names(1) /= "pressure") error stop "Empty channels, default: expected pressure" + +names = opsinputs_fill_varnames_with_channels("pressure", no_channels, JediVarHasChannels=.true.) +if (size(names) /= 1) error stop "Empty channels, true: expected a single name" +if (names(1) /= "pressure") error stop "Empty channels, true: expected pressure" + +names = opsinputs_fill_varnames_with_channels("pressure", no_channels, JediVarHasChannels=.false.) +if (size(names) /= 1) error stop "Empty channels, false: expected a single name" +if (names(1) /= "pressure") error stop "Empty channels, false: expected pressure" + +end program test_fill_varnames_with_channels diff --git a/test/testinput/081_VarField_level_time.yaml b/test/testinput/081_VarField_level_time.yaml index 27e12ecc..46aada4a 100644 --- a/test/testinput/081_VarField_level_time.yaml +++ b/test/testinput/081_VarField_level_time.yaml @@ -15,6 +15,17 @@ observations: sort order: "descending" simulated variables: [potentialTemperature] obs filters: + # Preserve missing times on the first and middle levels of record 2. + # Set them after time-window selection so those locations remain in the record. + - filter: Variable Assignment + where: + - variable: + name: MetaData/latitude + minvalue: -22.0 + maxvalue: -21.0 + assignments: + - name: MetaData/dateTime + value: missing # Double all observation errors: we want to check if error changes made by filters are # propagated to VarObs files - filter: BlackList @@ -52,13 +63,13 @@ observations: # Record 2 (first row) contains 3 levels, whereas record 4 (second row) contains only 2. # But in the VarObs file all observations need to have the same number of levels, so the # observation corresponding to record 4 is padded with missing values. - ob value: ["-3479.00000", "-3478.00000", "-3477.00000", + ob value: ["-1073741824.00000", "-1073741824.00000", "-3477.00000", "-3359.00000", "-3358.00000", "-1073741824.00000" ] lat: ["-21.00000", "-21.00000", "-21.00000", "-41.00000", "-41.00000", "-41.00000" ] lon: ["21.00000", "21.00000", "21.00000", "41.00000", "41.00000", "41.00000"] - time: ["-3479.00000", "-3479.00000", "-3479.00000", + time: ["-1073741824.00000", "-1073741824.00000", "-1073741824.00000", "-3359.00000", "-3359.00000", "-3359.00000"] Callsign: ["station_2", "station_2", "station_2", "station_4", "station_4", "station_4"] diff --git a/test/testinput/VarObsWriterNamelists_unsuffixed_metadata_channels/Aircraft.nl b/test/testinput/VarObsWriterNamelists_unsuffixed_metadata_channels/Aircraft.nl new file mode 100644 index 00000000..c1112d4d --- /dev/null +++ b/test/testinput/VarObsWriterNamelists_unsuffixed_metadata_channels/Aircraft.nl @@ -0,0 +1,3 @@ +&VarobsControlNL +Varfields=81,82 +/ diff --git a/test/testinput/varobswriter_unsuffixed_metadata_channels.yaml b/test/testinput/varobswriter_unsuffixed_metadata_channels.yaml new file mode 100644 index 00000000..05df43a3 --- /dev/null +++ b/test/testinput/varobswriter_unsuffixed_metadata_channels.yaml @@ -0,0 +1,76 @@ +time window: + begin: 2018-01-01T00:00:00Z + end: 2018-01-01T02:00:00Z + +observations: + - obs space: + name: Aircraft + obsdatain: + engine: + type: H5File + obsfile: Data/002_VarField_temperature_RadarZ.nc4 + simulated variables: [airTemperature] + channels: 1-3 + obs filters: + # Set missing metadata after time-window selection so the location is retained. + - filter: Variable Assignment + where: + - variable: + name: MetaData/stationIdentification + is_in: [station_2] + assignments: + - name: MetaData/pressure + value: missing + - name: MetaData/latitude + value: missing + - name: MetaData/dateTime + value: missing + - filter: Reset Flags to Pass + flags_to_reset: [10, 15] + - filter: VarObs Writer + namelist_directory: testinput/VarObsWriterNamelists_unsuffixed_metadata_channels + general_mode: debug + - filter: VarObs Checker + expected_header_fields: + Total number of obs: "4" + Number of levels: "3" + expected_main_table_columns: + # Each location has three time offsets (81), then three latitudes (82). + # Aircraft output includes pressure in Plevel on every channel's row. + field: ["81", "81", "81", "82", "82", "82", + "81", "81", "81", "82", "82", "82", + "81", "81", "81", "82", "82", "82", + "81", "81", "81", "82", "82", "82"] + level: ["1", "2", "3", "1", "2", "3", + "1", "2", "3", "1", "2", "3", + "1", "2", "3", "1", "2", "3", + "1", "2", "3", "1", "2", "3"] + ob value: ["-3540.00000", "-3540.00000", "-3540.00000", + "21.00000", "21.00000", "21.00000", + "-1073741824.00000", "-1073741824.00000", "-1073741824.00000", + "-1073741824.00000", "-1073741824.00000", "-1073741824.00000", + "-3420.00000", "-3420.00000", "-3420.00000", + "-23.00000", "-23.00000", "-23.00000", + "-3360.00000", "-3360.00000", "-3360.00000", + "24.00000", "24.00000", "24.00000"] + # The per-location time must also preserve missing dateTime values. + time: ["-3540.00000", "-3540.00000", "-3540.00000", + "-3540.00000", "-3540.00000", "-3540.00000", + "-1073741824.00000", "-1073741824.00000", "-1073741824.00000", + "-1073741824.00000", "-1073741824.00000", "-1073741824.00000", + "-3420.00000", "-3420.00000", "-3420.00000", + "-3420.00000", "-3420.00000", "-3420.00000", + "-3360.00000", "-3360.00000", "-3360.00000", + "-3360.00000", "-3360.00000", "-3360.00000"] + # PrintVarobs represents missing pressure as n/a. + Plevel: ["100100.0", "100100.0", "100100.0", "100100.0", "100100.0", "100100.0", + "n/a", "n/a", "n/a", "n/a", "n/a", "n/a", + "100300.0", "100300.0", "100300.0", "100300.0", "100300.0", "100300.0", + "100400.0", "100400.0", "100400.0", "100400.0", "100400.0", "100400.0"] + Callsign: ["station_1", "station_1", "station_1", "station_1", "station_1", "station_1", + "station_2", "station_2", "station_2", "station_2", "station_2", "station_2", + "station_3", "station_3", "station_3", "station_3", "station_3", "station_3", + "station_4", "station_4", "station_4", "station_4", "station_4", "station_4"] + HofX: ObsValue + benchmarkFlag: 1000 + flaggedBenchmark: 0 From 7c86da8369123c6674c9733f3e57b02085ca7013 Mon Sep 17 00:00:00 2001 From: lewisn-met Date: Fri, 4 Dec 2026 15:32:38 +0000 Subject: [PATCH 2/2] Code review --- test/mains/TestFillVarNamesWithChannels.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mains/TestFillVarNamesWithChannels.F90 b/test/mains/TestFillVarNamesWithChannels.F90 index 1dab7610..d40b5587 100644 --- a/test/mains/TestFillVarNamesWithChannels.F90 +++ b/test/mains/TestFillVarNamesWithChannels.F90 @@ -7,7 +7,7 @@ program test_fill_varnames_with_channels use opsinputs_utils_mod, only: max_varname_with_channel_length implicit none -integer(c_int), parameter :: channels(3) = [3_c_int, 7_c_int, 11_c_int] +integer(c_int), parameter :: channels(3) = [integer(c_int) :: 3, 7, 11] integer(c_int), parameter :: no_channels(0) = [integer(c_int) ::] character(len=max_varname_with_channel_length), parameter :: suffixed_names(3) = & [character(len=max_varname_with_channel_length) :: "pressure_3", "pressure_7", "pressure_11"]