From 62d161fda026c66eec956ffb8ec3ef0215d31234 Mon Sep 17 00:00:00 2001 From: Mark Piper Date: Tue, 6 Feb 2024 15:44:07 -0700 Subject: [PATCH] Implement get_grid_coordinate function --- bmi_heat/bmi_heat_geo.f90 | 22 +++++++++++++++++++++- example/bmi_geospatial_ex.f90 | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/bmi_heat/bmi_heat_geo.f90 b/bmi_heat/bmi_heat_geo.f90 index 5ec4916..c585f9c 100644 --- a/bmi_heat/bmi_heat_geo.f90 +++ b/bmi_heat/bmi_heat_geo.f90 @@ -58,7 +58,27 @@ function heat_grid_coordinate(this, grid, coordinate, values) result (bmi_status integer, intent(in) :: grid character(len=*), intent(in) :: coordinate double precision, dimension(:), intent(out) :: values - integer :: bmi_status + double precision, allocatable :: origin(:), spacing(:) + integer :: bmi_status, rank, dim, i + + bmi_status = this%bmi_base%get_grid_rank(grid, rank) + + allocate(origin(rank), spacing(rank)) + bmi_status = this%bmi_base%get_grid_origin(grid, origin) + bmi_status = this%bmi_base%get_grid_spacing(grid, spacing) + + select case(coordinate) + case("y") + dim = 1 + case("x") + dim = 2 + end select + + do i = 1, size(values) + values(i) = dble(i - 1) * spacing(dim) + origin(dim) + end do + + deallocate(origin, spacing) bmi_status = BMI_SUCCESS end function heat_grid_coordinate diff --git a/example/bmi_geospatial_ex.f90 b/example/bmi_geospatial_ex.f90 index 55e7e03..fd01117 100644 --- a/example/bmi_geospatial_ex.f90 +++ b/example/bmi_geospatial_ex.f90 @@ -10,7 +10,9 @@ program bmi_geospatial_ex type(bmi_heat_geo) :: g integer :: status, grid_id, grid_rank, i character (len=BMI_MAX_COMPONENT_NAME), pointer :: component_name + integer, allocatable :: grid_shape(:) character (len=BMI_MAX_VAR_NAME), pointer :: names(:), units(:) + double precision, allocatable :: xcoordinate(:), ycoordinate(:) character (len=BMI_MAX_VAR_NAME) :: crs status = h%get_component_name(component_name) @@ -24,6 +26,13 @@ program bmi_geospatial_ex status = h%get_grid_rank(grid_id, grid_rank) write (*,"(a, i3)") "Grid rank:", grid_rank + allocate(grid_shape(grid_rank)) + status = h%get_grid_shape(grid_id, grid_shape) + write (*,"(a)") "Grid shape:" + do i = 1, size(grid_shape) + write (*,"(a, i3)") "-", grid_shape(i) + end do + g = bmi_heat_geo(h) allocate(names(grid_rank)) @@ -40,9 +49,21 @@ program bmi_geospatial_ex write (*,"(a)") "- " // trim(units(i)) end do + allocate(ycoordinate(grid_shape(1))) + status = g%get_grid_coordinate(grid_id, names(1), ycoordinate) + write (*,"(a)") "Y-coordinate:" + write (*,"(*(x, f4.1))") ycoordinate + + allocate(xcoordinate(grid_shape(2))) + status = g%get_grid_coordinate(grid_id, names(2), xcoordinate) + write (*,"(a)") "X-coordinate:" + write (*,"(*(x, f4.1))") xcoordinate + status = g%get_grid_crs(grid_id, crs) write (*,"(a, a30)") "CRS: ", crs + deallocate(grid_shape, names, units, ycoordinate, xcoordinate) + status = h%finalize() end program bmi_geospatial_ex