Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/opsinputs/opsinputs_fill_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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( &
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/opsinputs/opsinputs_obsspace_f.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "oops/util/DateTime.h"
#include "oops/util/Duration.h"
#include "oops/util/missingValues.h"

namespace opsinputs {

Expand All @@ -30,8 +31,11 @@ void opsinputs_obsspace_get_db_datetime_offset_in_seconds_f(
std::vector<util::DateTime> 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<util::DateTime>();
const int64_t missingInt64 = util::missingValue<int64_t>();
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();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/opsinputs/opsinputs_obsspace_f.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace opsinputs {

extern "C" {

/// Return offsets in seconds, or util::missingValue<int64_t>() 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);
Expand Down
11 changes: 11 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions test/mains/TestFillVarNamesWithChannels.F90
Original file line number Diff line number Diff line change
@@ -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) = [integer(c_int) :: 3, 7, 11]
integer(c_int), parameter :: no_channels(0) = [integer(c_int) ::]
Comment thread
lewisn-met marked this conversation as resolved.
Resolved
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
15 changes: 13 additions & 2 deletions test/testinput/081_VarField_level_time.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
&VarobsControlNL
Varfields=81,82
/
76 changes: 76 additions & 0 deletions test/testinput/varobswriter_unsuffixed_metadata_channels.yaml
Original file line number Diff line number Diff line change
@@ -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