#!/usr/bin/env python3
"""
Prints information about a GEOS-Chem Classic horizontal grid.
Examples
--------
Run the following commands:
.. code-block:: console
$ conda activate gcpy_env
(gcpy_env) $ python -m gcpy.examples.grids/display_gcclassic_grid_info
And select your desired grid from the menu.
"""
from gcpy.grid import make_grid_ll
[docs]
def print_vals(values):
"""
Prints a list of numbers 8 columns wide with 5 decimal places.
Parameters
----------
values : list-like
Values to be displayed
"""
cols = 8
for i in range(0, len(values), cols):
line = "".join(f"{x:11.5f}" for x in values[i:i+cols])
print(line)
[docs]
def create_grid_and_print_info(grid_params):
"""
Creates a grid object and prints its metadata.
Parameters
----------
grid_params : tuple
Resolution, lon range, lat range
"""
# Arguments
res = grid_params[0]
lon_range = grid_params[1]
lat_range = grid_params[2]
grid_type = grid_params[3]
# Make resolution a pretty string
res_display = res.replace("x", "\u00B0 x ") + "\u00B0"
# Create a grid object
out_extent = [lon_range[0], lon_range[1], lat_range[0], lat_range[1]]
grid = make_grid_ll(res, out_extent=out_extent)
# Print metadata
print(f"Name : {res_display} {grid_type}")
print(f"Resolution : {res}")
print(f"Longitude Range : {lon_range}")
print(f"Latitude Range : {lat_range}")
print("Longitude centers")
print_vals(grid["lon"])
print("Latitude centers")
print_vals(grid["lat"])
print("Longitude edges")
print_vals(grid["lon_b"])
print("Latitude edges")
print_vals(grid["lat_b"])
[docs]
def get_grid_parameters(selection):
"""
Returns metadata about a given grid.
Parameters
----------
selection : int
Number of the selected grid
Returns
-------
grid_params : tuple
Resolution, lon range, lat range
"""
# Resolution # Lon range # Lat range
grids = [
("4.0x5.0", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"),
("2.0x2.5", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"),
("0.5x0.625", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"),
("0.5x0.625", [ 60.0, 150.0 ], [-11.0, 55.0 ], "nested AS"),
("0.5x0.625", [ -30.0, 50.0 ], [ 30.0, 70.0 ], "nested EU"),
("0.5x0.625", [-140.0, -40.0 ], [ 10.0, 70.0 ], "nested NA"),
("0.25x0.3125", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"),
("0.25x0.3125", [ -20.0, 52.8125], [-37.0, 40.0 ], "nested AF"),
("0.25x0.3125", [ 70.0, 140.0 ], [ 32.75, 61.25], "nested AS"),
("0.25x0.3125", [ -15.0, 40.0 ], [ 15.0, 55.0 ], "nested EU"),
("0.25x0.3125", [ -20.0, 70.0 ], [ 12.0, 44.0 ], "nested ME"),
("0.25x0.3125", [-130.0, -60.0 ], [ 9.75, 60.0 ], "nested NA"),
("0.25x0.3125", [ 110.0, 180.0 ], [-50.0, 5.0 ], "nested OC"),
("0.25x0.3125", [ -87.8125, -31.25 ], [-59.0, 16.0 ], "nested SA"),
("0.25x0.3125", [ 20.0, 180.0 ], [ 41.0, 83.0 ], "nested RU"),
("0.125x0.15625", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"),
("0.125x0.15625", [ -20.0, 52.8125], [-59.0, 16.0 ], "nested AF"),
("0.125x0.15625", [ 70.0, 140.0 ], [ 15.0, 55.0 ], "nested AS"),
("0.125x0.15625", [ -15.0, 40.0 ], [ 32.75, 61.25], "nested EU"),
("0.125x0.15625", [-130.0, -60.0 ], [ 9.75, 60.0 ], "nested NA"),
("0.125x0.15625", [ -87.8125, -31.25 ], [-59.0, 16.0 ], "nested SA"),
]
return grids[selection]
[docs]
def main():
"""
Main program
"""
# Display a list of grids and get the user's selection
selection = display_menu()
# Get the parameters that define the grid
grid_params = get_grid_parameters(selection)
# Create the grid object and print metadata
create_grid_and_print_info(grid_params)
if __name__ == '__main__':
main()