Skip to content

Commit a4ded74

Browse files
authored
Remove commented-out ESMF coordinate extraction block from cece_cap.F90
Apply PR #34 changes across all three files and remove the large commented-out block of the previous ESMF-coordinate extraction logic from src/cece_cap.F90 as requested in review comment r3399235184.
1 parent 98f9d5b commit a4ded74

3 files changed

Lines changed: 53 additions & 40 deletions

File tree

src/cece_cap.F90

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -643,48 +643,37 @@ subroutine CECE_InitializeRealize(comp, importState, exportState, clock, rc)
643643
g_start_time_seconds = real(yy*365*24*3600 + mm*30*24*3600 + dd*24*3600 + &
644644
h*3600 + m*60 + s, c_double)
645645

646-
! Extract coordinates from ESMF grid and call enhanced writer initialization
646+
647+
! Build 1-D cell-center coordinates directly from configured YAML grid bounds.
648+
! This keeps standalone NetCDF output lon/lat metadata consistent with the
649+
! configured CECE output grid and avoids falling back to the legacy writer,
650+
! which hard-codes global -180..180 / -90..90 coordinates.
647651
block
648-
real(ESMF_KIND_R8), pointer :: grid_lon(:,:), grid_lat(:,:)
649652
real(c_double), allocatable :: lon_coords(:), lat_coords(:)
653+
real(c_double) :: dlon, dlat
650654
integer :: i
651655

652-
! Get coordinates from ESMF grid (2D arrays)
653-
call ESMF_GridGetCoord(grid, coordDim=1, localDE=0, staggerloc=ESMF_STAGGERLOC_CENTER, &
654-
farrayptr=grid_lon, rc=rc)
655-
if (rc /= ESMF_SUCCESS) then
656-
write(*,'(A,I0)') "WARNING: [CECE] Failed to get longitude coordinates: rc=", rc
657-
! Fall back to legacy initialization
658-
call cece_core_writer_initialize(g_cece_data_ptr, int(nx, c_int), int(ny, c_int), &
659-
int(nz, c_int), start_time_str, int(len_trim(start_time_str), c_int), c_rc)
660-
else
661-
call ESMF_GridGetCoord(grid, coordDim=2, localDE=0, staggerloc=ESMF_STAGGERLOC_CENTER, &
662-
farrayptr=grid_lat, rc=rc)
663-
if (rc /= ESMF_SUCCESS) then
664-
write(*,'(A,I0)') "WARNING: [CECE] Failed to get latitude coordinates: rc=", rc
665-
! Fall back to legacy initialization
666-
call cece_core_writer_initialize(g_cece_data_ptr, int(nx, c_int), int(ny, c_int), &
667-
int(nz, c_int), start_time_str, int(len_trim(start_time_str), c_int), c_rc)
668-
else
669-
! Extract 1D coordinate arrays from 2D ESMF grid coordinates
670-
allocate(lon_coords(nx), lat_coords(ny))
671-
do i = 1, nx
672-
lon_coords(i) = real(grid_lon(i,1), c_double) ! First row, all columns
673-
end do
674-
do i = 1, ny
675-
lat_coords(i) = real(grid_lat(1,i), c_double) ! First column, all rows
676-
end do
677-
678-
write(*,'(A,2F10.3)') "INFO: [CECE] Grid longitude range: ", lon_coords(1), lon_coords(nx)
679-
write(*,'(A,2F10.3)') "INFO: [CECE] Grid latitude range: ", lat_coords(1), lat_coords(ny)
680-
681-
! Call enhanced writer initialization with coordinates
682-
call cece_core_writer_initialize_with_coords(g_cece_data_ptr, int(nx, c_int), int(ny, c_int), &
683-
int(nz, c_int), lon_coords, lat_coords, &
684-
start_time_str, int(len_trim(start_time_str), c_int), c_rc)
685-
deallocate(lon_coords, lat_coords)
686-
end if
687-
end if
656+
allocate(lon_coords(nx), lat_coords(ny))
657+
658+
dlon = real(lon_max - lon_min, c_double) / real(nx, c_double)
659+
dlat = real(lat_max - lat_min, c_double) / real(ny, c_double)
660+
661+
do i = 1, nx
662+
lon_coords(i) = real(lon_min, c_double) + dlon * (real(i, c_double) - 0.5_c_double)
663+
end do
664+
665+
do i = 1, ny
666+
lat_coords(i) = real(lat_min, c_double) + dlat * (real(i, c_double) - 0.5_c_double)
667+
end do
668+
669+
write(*,'(A,2F12.5)') "INFO: [CECE] Writer longitude range: ", lon_coords(1), lon_coords(nx)
670+
write(*,'(A,2F12.5)') "INFO: [CECE] Writer latitude range: ", lat_coords(1), lat_coords(ny)
671+
672+
call cece_core_writer_initialize_with_coords(g_cece_data_ptr, int(nx, c_int), int(ny, c_int), &
673+
int(nz, c_int), lon_coords, lat_coords, &
674+
start_time_str, int(len_trim(start_time_str), c_int), c_rc)
675+
676+
deallocate(lon_coords, lat_coords)
688677
end block
689678

690679
rc = int(c_rc)

src/io/cece_standalone_writer.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ int CeceStandaloneWriter::Initialize(const std::string& start_time_iso8601, int
104104
ny_ = ny;
105105
nz_ = nz;
106106

107+
// Legacy dimensions-only initialization: clear any prior coordinate state.
108+
lon_coords_.clear();
109+
lat_coords_.clear();
110+
use_custom_coords_ = false;
111+
107112
CECE_LOG_INFO("[CECE] Initializing standalone writer with start time: " + start_time_iso8601);
108113

109114
// Create output directory if it doesn't exist
@@ -125,6 +130,11 @@ int CeceStandaloneWriter::InitializeWithCoords(const std::string& start_time_iso
125130
const std::vector<double>& lat_coords) {
126131
if (!config_.enabled) return 0;
127132

133+
if (static_cast<int>(lon_coords.size()) != nx || static_cast<int>(lat_coords.size()) != ny) {
134+
CECE_LOG_ERROR("[CECE] Coordinate array sizes do not match writer dimensions");
135+
return -1;
136+
}
137+
128138
start_time_iso8601_ = start_time_iso8601;
129139
nx_ = nx;
130140
ny_ = ny;
@@ -251,7 +261,8 @@ int CeceStandaloneWriter::WriteTimeStep(const std::unordered_map<std::string, Du
251261

252262
// Write coordinate arrays
253263
if (use_custom_coords_) {
254-
// Use coordinates provided from ESMF grid
264+
// -- Use coordinates provided from ESMF grid
265+
// ++ Use coordinates provided by the cap from the configured output grid.
255266
check_nc(nc_put_var_double(ncid, var_lon, lon_coords_.data()), "put_var lon");
256267
check_nc(nc_put_var_double(ncid, var_lat, lat_coords_.data()), "put_var lat");
257268
} else {

src/io/tide/streams/dshr_strdata_mod.F90

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,9 @@ subroutine shr_strdata_init(sdat, model_clock, stream_name, rc)
683683
! single point stream data, no action required.
684684
else if (trim(sdat%stream(ns)%mapalgo) == 'passthrough') then
685685
! Data is already on the model grid - skip regridding entirely.
686+
! passthrough does not interpolate, remap, or realign longitude.
687+
! It is only valid when source and destination grids match in size,
688+
! coordinate convention, and index order.
686689
! A size check against the destination field is performed at first read.
687690
else
688691
call shr_sys_abort('ERROR: map algo '//trim(sdat%stream(ns)%mapalgo)//' is not supported')
@@ -1978,17 +1981,27 @@ subroutine shr_strdata_readstrm(sdat, per_stream, stream, fldbun_data, &
19781981
if (chkerr(rc,__LINE__,u_FILE_u)) return
19791982

19801983
if (trim(stream%mapalgo) == 'passthrough') then
1981-
! No regridding: directly copy data and validate matching sizes.
1984+
! No regridding: directly copy data only when source and destination
1985+
! grids are already identical in size, coordinate convention, and
1986+
! index order. This branch does not interpolate, remap, or roll longitude.
19821987
block
19831988
real(r8), pointer :: dst_ptr(:) => null()
1989+
19841990
call dshr_field_getfldptr(field_dst, fldptr1=dst_ptr, rc=rc)
19851991
if (chkerr(rc,__LINE__,u_FILE_u)) return
1992+
19861993
if (size(dataptr1d) /= size(dst_ptr)) then
19871994
write(errmsg,'(a,i0,a,i0,a)') &
19881995
'ERROR: passthrough mapalgo: stream size (', size(dataptr1d), &
19891996
') does not match model grid size (', size(dst_ptr), ')'
19901997
call shr_sys_abort(trim(errmsg))
19911998
end if
1999+
2000+
! NOTE: passthrough intentionally does not perform coordinate
2001+
! convention conversion. If source and destination grids differ
2002+
! by 0..360 vs -180..180, use a real regridding mapalgo such as
2003+
! nearest, bilinear, or conservative remapping, or preprocess the
2004+
! input so it matches the destination grid.
19922005
dst_ptr(:) = dataptr1d(:)
19932006
end block
19942007
else if (trim(stream%mapalgo) == 'none') then

0 commit comments

Comments
 (0)