YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_core_grids.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (c) 2024 The YAC Authors
4#
5# SPDX-License-Identifier: BSD-3-Clause
6
7
11
12try:
13 # Initialize MPI if available; ignore errors if not.
14 from mpi4py import MPI
15except Exception:
16 pass
17
18# Core YAC grid utilities
19from yac.core import (
20 BasicGrid,
21 yac_location,
22 lonlat2xyz,
23)
24
25import numpy as np
26import sys
27
28# Optional UXArray support
29try:
30 import uxarray as ux
31 ux_found = True
32except ModuleNotFoundError:
33 ux_found = False
34 print("UXArray not found! Skipping `from_uxgrid`.")
35
36# Path to directory containing ICON grid files (first command‑line argument)
37grid_dir = sys.argv[1]
38
39# ----------------------------------------------------------------------
40# 1. Create a simple regular latitude‑longitude grid for testing
41# ----------------------------------------------------------------------
42reg_grid = BasicGrid.reg_2d_new(
43 "reg_grid",
44 np.linspace(-2, 2, 20), # longitude coordinates
45 np.linspace(-1.5, 1.5, 20) # latitude coordinates
46)
47
48# ----------------------------------------------------------------------
49# 2. Load the ICON grid from NetCDF files
50# ----------------------------------------------------------------------
51icon_grid, icon_cell_field = BasicGrid.read_icon(
52 grid_dir + "icon_grid_R02B01.nc",
53 "icon"
54)
55
56# ----------------------------------------------------------------------
57# 3. If UXArray is available, create a YAC grid from the UXArray grid
58# ----------------------------------------------------------------------
59if ux_found:
60 # Open the same NetCDF file with UXArray
61 ux_grid = ux.open_grid(grid_dir + "icon_grid_R02B01.nc")
62
63 # Convert to a YAC BasicGrid (edges are not defined here)
64 icon_uxgrid = BasicGrid.from_uxgrid("icon_uxgrid", ux_grid, def_edges=False)
65
66 # Attach geographic coordinates (radians) to each cell
67 icon_uxgrid.add_coordinates(
68 yac_location.YAC_LOC_CELL,
69 np.deg2rad(ux_grid.face_lon),
70 np.deg2rad(ux_grid.face_lat)
71 )
72
73# ----------------------------------------------------------------------
74# 4. Generate a dummy SCRIP grid, read it back, then clean up files
75# ----------------------------------------------------------------------
76try:
77 from write_dummy_scrip_grid import write_dummy_scrip_grid_file
78 import os
79
80 # Create temporary SCRIP files (grid, mask)
81 write_dummy_scrip_grid_file(
82 "dummy_scrip", # grid name prefix
83 "dummy_scrip.nc", # output grid file
84 "dummy_scrip_mask.nc", # output mask file
85 True, # generate a mask
86 180, # number of longitudes
87 90, # number of latitudes
88 (0, 360), # longitude bounds
89 (-90, 90) # latitude bounds
90 )
91
92 # Read the created SCRIP grid into a YAC BasicGrid
93 scrip_grid = BasicGrid.read_scrip(
94 "dummy_scrip.nc",
95 "dummy_scrip_mask.nc",
96 "dummy_scrip",
97 "dummy_scrip"
98 )
99
100 # Remove the temporary files
101 os.remove("dummy_scrip.nc")
102 os.remove("dummy_scrip_mask.nc")
103except ImportError:
104 print("Cannot write dummy scrip! Skipping `read_scrip`.")