Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type errors in calc_rectilinear_lon_edge and calc_rectilinear_lat_edge in grid.py #347

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

yantosca
Copy link
Contributor

@yantosca yantosca commented Jan 8, 2025

Name and Institution (Required)

Name: Bob Yantosca
Institution: Harvard + GCST

Describe the update

This PR fixes type errors in routines to compute the lon & lat edges of rectilinear grids (in gcpy/grid.py). Running this test program:

#!/usr/bin/env python3

import numpy as np
import gcpy

# Longitude parameters
lon_stride = 0.25
center_at_180 = True

# Latitude parameters
lat_stride = 0.3125
half_polar_grid = True

# Grid box lon & lat edges
lon_edge = gcpy.grid.calc_rectilinear_lon_edge(lon_stride, center_at_180)
lat_edge = gcpy.grid.calc_rectilinear_lat_edge(lat_stride, half_polar_grid)

# Grid box area
area = gcpy.grid.calc_rectilinear_grid_area(lon_edge, lat_edge)

print(f"Sum of global surface area [m2]: {np.sum(area)}")

currently results in these errors:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/n/home09/ryantosca/test/python/grid.py", line 15, in <module>
    lon_edge = gcpy.grid.calc_rectilinear_lon_edge(lon_stride, center_at_180)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/n/home09/ryantosca/python/gcpy/gcpy/grid.py", line 981, in calc_rectilinear_lon_edge
    lon_edge = np.linspace(-180.0, 180.0, num=n_lon + 1)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/n/jacob_lab/Users/ryantosca/mamba/envs/test_env/lib/python3.12/site-packages/numpy/core/function_base.py", line 122, in linspace
    num = operator.index(num)


Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/n/home09/ryantosca/test/python/grid.py", line 16, in <module>
    lat_edge = gcpy.grid.calc_rectilinear_lat_edge(lat_stride, half_polar_grid)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/n/home09/ryantosca/python/gcpy/gcpy/grid.py", line 1026, in calc_rectilinear_lat_edge
    lat_edge = np.linspace(-1.0 * start_pt, start_pt,
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/n/jacob_lab/Users/ryantosca/mamba/envs/test_env/lib/python3.12/site-packages/numpy/core/function_base.py", line 122, in linspace
    num = operator.index(num)

This is because the num arguments that are passed to NumPy routine np.linspace within these routines are of type float rather than the expected int. The solution is to cast these to int.

Expected changes

After the fix, the program works as expected and prints

Sum of global surface area [m2]: 510065624779438.94

NOTE: These routines are not used in the benchmarking code but are user utilities.

Related Github Issue

gcpy/grid.py
- In routine calc_rectilinear_lon_edge:
  - Renamed n_lon to n_lon_edge
  - Cast n_lon_edge from float to int (for input to np.linspace)
  - Updated pydoc comments
- In routine calc_rectilinear_lat_edge:
  - Added n_lat_edge variable to hold the length of the output vector
  - Cast n_lat_edge from float to int (for input to np.linspace)
  - Updated pydoc comments
- In routine calc_rectilinear_grid_area:
  - Updated pydoc comments

Signed-off-by: Bob Yantosca <[email protected]>
@yantosca yantosca added category: Bug Fix Fixes a bug that was previously reported topic: Utilities Related to utility functions & convenience routines labels Jan 8, 2025
@yantosca yantosca added this to the 1.6.0 milestone Jan 8, 2025
@yantosca yantosca requested a review from lizziel January 8, 2025 16:31
@yantosca yantosca self-assigned this Jan 8, 2025
Copy link
Contributor

@lizziel lizziel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to merge!

@yantosca yantosca merged commit e4be546 into dev Jan 9, 2025
14 checks passed
@yantosca yantosca deleted the bugfix/grid-box-areas branch January 9, 2025 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: Bug Fix Fixes a bug that was previously reported topic: Utilities Related to utility functions & convenience routines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants