|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Prints information about a GEOS-Chem Classic horizontal grid |
| 4 | +""" |
| 5 | +from gcpy.grid import make_grid_ll |
| 6 | + |
| 7 | + |
| 8 | +def print_vals(values): |
| 9 | + """ |
| 10 | + Prints a list of numbers 8 columns wide with 5 decimal places. |
| 11 | +
|
| 12 | + Args |
| 13 | + values : list-like : Values to be displayed |
| 14 | + """ |
| 15 | + cols = 8 |
| 16 | + for i in range(0, len(values), cols): |
| 17 | + line = "".join(f"{x:11.5f}" for x in values[i:i+cols]) |
| 18 | + print(line) |
| 19 | + |
| 20 | + |
| 21 | +def create_grid_and_print_info(grid_params): |
| 22 | + """ |
| 23 | + Creates a grid object and prints its metadata. |
| 24 | +
|
| 25 | + Args |
| 26 | + grid_params : tuple : Resolution, lon range, lat range |
| 27 | + """ |
| 28 | + |
| 29 | + # Arguments |
| 30 | + res = grid_params[0] |
| 31 | + lon_range = grid_params[1] |
| 32 | + lat_range = grid_params[2] |
| 33 | + grid_type = grid_params[3] |
| 34 | + |
| 35 | + # Make resolution a pretty string |
| 36 | + res_display = res.replace("x", "\u00B0 x ") + "\u00B0" |
| 37 | + |
| 38 | + # Create a grid object |
| 39 | + out_extent = [lon_range[0], lon_range[1], lat_range[0], lat_range[1]] |
| 40 | + grid = make_grid_ll(res, out_extent=out_extent) |
| 41 | + |
| 42 | + # Print metadata |
| 43 | + print(f"Name : {res_display} {grid_type}") |
| 44 | + print(f"Resolution : {res}") |
| 45 | + print(f"Longitude Range : {lon_range}") |
| 46 | + print(f"Latitude Range : {lat_range}") |
| 47 | + print("Longitude centers") |
| 48 | + print_vals(grid["lon"]) |
| 49 | + print("Latitude centers") |
| 50 | + print_vals(grid["lat"]) |
| 51 | + print("Longitude edges") |
| 52 | + print_vals(grid["lon_b"]) |
| 53 | + print("Latitude edges") |
| 54 | + print_vals(grid["lat_b"]) |
| 55 | + |
| 56 | + |
| 57 | +def get_grid_parameters(selection): |
| 58 | + """ |
| 59 | + Returns metadata about a given grid |
| 60 | +
|
| 61 | + Args |
| 62 | + selection : int : Number of the selected grid |
| 63 | +
|
| 64 | + Returns |
| 65 | + grid_params : tuple : Resolution, lon range, lat range |
| 66 | +
|
| 67 | + """ |
| 68 | + # Resolution # Lon range # Lat range |
| 69 | + grids = [ |
| 70 | + ("4.0x5.0", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"), |
| 71 | + ("2.0x2.5", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"), |
| 72 | + ("0.5x0.625", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"), |
| 73 | + ("0.5x0.625", [ 60.0, 150.0 ], [-11.0, 55.0 ], "nested AS"), |
| 74 | + ("0.5x0.625", [ -30.0, 50.0 ], [ 30.0, 70.0 ], "nested EU"), |
| 75 | + ("0.5x0.625", [-140.0, -40.0 ], [ 10.0, 70.0 ], "nested NA"), |
| 76 | + ("0.25x0.3125", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"), |
| 77 | + ("0.25x0.3125", [ -20.0, 52.8125], [-37.0, 40.0 ], "nested AF"), |
| 78 | + ("0.25x0.3125", [ 70.0, 140.0 ], [ 32.75, 61.25], "nested AS"), |
| 79 | + ("0.25x0.3125", [ -15.0, 40.0 ], [ 15.0, 55.0 ], "nested EU"), |
| 80 | + ("0.25x0.3125", [ -20.0, 70.0 ], [ 12.0, 44.0 ], "nested ME"), |
| 81 | + ("0.25x0.3125", [-130.0, -60.0 ], [ 9.75, 60.0 ], "nested NA"), |
| 82 | + ("0.25x0.3125", [ 110.0, 180.0 ], [-50.0, 5.0 ], "nested OC"), |
| 83 | + ("0.25x0.3125", [ -87.8125, -31.25 ], [-59.0, 16.0 ], "nested SA"), |
| 84 | + ("0.25x0.3125", [ 20.0, 180.0 ], [ 41.0, 83.0 ], "nested RU"), |
| 85 | + ("0.125x0.15625", [-180.0, 180.0 ], [-90.0, 90.0 ], "global"), |
| 86 | + ("0.125x0.15625", [ -20.0, 52.8125], [-59.0, 16.0 ], "nested AF"), |
| 87 | + ("0.125x0.15625", [ 70.0, 140.0 ], [ 15.0, 55.0 ], "nested AS"), |
| 88 | + ("0.125x0.15625", [ -15.0, 40.0 ], [ 32.75, 61.25], "nested EU"), |
| 89 | + ("0.125x0.15625", [-130.0, -60.0 ], [ 9.75, 60.0 ], "nested NA"), |
| 90 | + ("0.125x0.15625", [ -87.8125, -31.25 ], [-59.0, 16.0 ], "nested SA"), |
| 91 | + ] |
| 92 | + |
| 93 | + return grids[selection] |
| 94 | + |
| 95 | + |
| 96 | +def display_menu(): |
| 97 | + """ |
| 98 | + Displays a menu of grids to choose from. |
| 99 | +
|
| 100 | + Returns |
| 101 | + selection : int : Number of the grid selected by the user |
| 102 | + """ |
| 103 | + msg = """\ |
| 104 | +Please select a GEOS-Chem Classic horizontal grid: |
| 105 | +
|
| 106 | +1. 4.0\u00B0 x 5.0\u00B0 global |
| 107 | +
|
| 108 | +2. 2.0\u00B0 x 2.5\u00B0 global |
| 109 | +
|
| 110 | +3. 0.5\u00B0 x 0.625\u00B0 global |
| 111 | +4. 0.5\u00B0 x 0.625\u00B0 nested AS |
| 112 | +5. 0.5\u00B0 x 0.625\u00B0 nested EU |
| 113 | +6. 0.5\u00B0 x 0.625\u00B0 nested NA |
| 114 | +
|
| 115 | +7 0.25\u00B0 x 0.3125\u00B0 global |
| 116 | +8. 0.25\u00B0 x 0.3125\u00B0 nested AF |
| 117 | +9. 0.25\u00B0 x 0.3125\u00B0 nested AS |
| 118 | +10. 0.25\u00B0 x 0.3125\u00B0 nested EU |
| 119 | +11. 0.25\u00B0 x 0.3125\u00B0 nested ME |
| 120 | +12. 0.25\u00B0 x 0.3125\u00B0 nested NA |
| 121 | +13 0.25\u00B0 x 0.3125\u00B0 nested OC |
| 122 | +14. 0.25\u00B0 x 0.3125\u00B0 nested SA |
| 123 | +15. 0.25\u00B0 x 0.3125\u00B0 nested RU |
| 124 | +
|
| 125 | +16. 0.125\u00B0 x 0.15625\u00B0 global |
| 126 | +17. 0.125\u00B0 x 0.15625\u00B0 nested AF |
| 127 | +18. 0.125\u00B0 x 0.15625\u00B0 nested AS |
| 128 | +19. 0.125\u00B0 x 0.15625\u00B0 nested EU |
| 129 | +20. 0.125\u00B0 x 0.15625\u00B0 nested NA |
| 130 | +21. 0.125\u00B0 x 0.15625\u00B0 nested SA |
| 131 | + """ |
| 132 | + print(msg) |
| 133 | + |
| 134 | + while True: |
| 135 | + try: |
| 136 | + selection = int(input("Enter a selection >>> ")) |
| 137 | + if 1 <= selection <= 21: |
| 138 | + break # valid, exit loop |
| 139 | + print("❌ Entry out of range. Try again.") |
| 140 | + except ValueError: |
| 141 | + print("❌ Not a number. Try again.") |
| 142 | + |
| 143 | + return selection - 1 |
| 144 | + |
| 145 | + |
| 146 | +def main(): |
| 147 | + """ |
| 148 | + Main program |
| 149 | + """ |
| 150 | + |
| 151 | + # Display a list of grids and get the user's selection |
| 152 | + selection = display_menu() |
| 153 | + |
| 154 | + # Get the parameters that define the grid |
| 155 | + grid_params = get_grid_parameters(selection) |
| 156 | + |
| 157 | + # Create the grid object and print metadata |
| 158 | + create_grid_and_print_info(grid_params) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + main() |
0 commit comments