# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # UGrid Mesh2D basics
#
# This is the basic introduction for using the `ugridpy` library.
#
# `ugridpy` can be used for reading and writing mesh2D. 
#
# At the very beginning, the necessary libraries have to be imported.

from ugrid import UGrid, UGridMesh2D
from ugrid.version import __version__
__version__

# Other imports

import numpy as np

# ## Mesh2D reading

with UGrid("./data_examples/AllUGridEntities.nc", "r") as ug:
    # 1. Count the number of mesh2d topologies
    num_mesh2d_topologies = ug.mesh2d_get_num_topologies()
    # 2. Get the data of the last mesh2d in the list
    mesh2d = ug.mesh2d_get(num_mesh2d_topologies - 1)

# ### Print some data

print(f"Mesh 2D name: {mesh2d.name}")
print(f"Mesh 2D first 10 elements of node_x: {mesh2d.node_x[:10]}")
print(f"Mesh 2D first 10 elements of node_y: {mesh2d.node_y[:10]}")
print(f"Mesh 2D edge_node: {mesh2d.edge_node}")
print(f"Mesh 2D face_node: {mesh2d.face_node}")

# ## Mesh2D writing

# ### Define Mesh2D arrays

# +
name = "mesh2d"
node_x = np.array([0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 3, 3, 3, 3], dtype=np.double)
node_y = np.array([0, 0, 1, 1, 2, 2, 3, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=np.double)
edge_node = np.array(
    [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        2,
        9,
        4,
        10,
        6,
        11,
        8,
        12,
        9,
        13,
        10,
        14,
        11,
        15,
        12,
        16,
        1,
        3,
        3,
        5,
        5,
        7,
        2,
        4,
        4,
        6,
        6,
        8,
        9,
        10,
        10,
        11,
        11,
        12,
        13,
        14,
        14,
        15,
        15,
        16,
    ],
    dtype=np.int32,
)

face_x = np.array([0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5], dtype=np.double)
face_y = np.array([0.5, 1.5, 2.5, 0.5, 1.5, 2.5, 0.5, 1.5, 2.5], dtype=np.double)
face_node = np.array(
    [
        1,
        2,
        4,
        3,
        3,
        4,
        6,
        5,
        5,
        6,
        8,
        7,
        2,
        9,
        10,
        4,
        4,
        10,
        11,
        6,
        6,
        11,
        12,
        8,
        9,
        13,
        14,
        10,
        10,
        14,
        15,
        11,
        11,
        15,
        16,
        12,
    ],
    dtype=np.int32,
)
# -

# ### Instantiate UGridMesh2D class

mesh2d = UGridMesh2D(
    name=name,
    node_x=node_x,
    node_y=node_y,
    edge_node=edge_node,
    face_x=face_x,
    face_y=face_y,
    face_node=face_node,
)

# ### Write UGridMesh2D instance to file

with UGrid("Mesh2DWrite.nc", "w+") as ug:
    # 1. Define a new network1d
    topology_id = ug.mesh2d_define(mesh2d)
    # 2. Write a new network1d
    ug.mesh2d_put(topology_id, mesh2d)
    # 3. Add crs to file
    attribute_dict = {
        "name": "Unknown projected",
        "epsg": np.array([0], dtype=int),
        "grid_mapping_name": "Unknown projected",
        "longitude_of_prime_meridian": np.array([0.0], dtype=float),
        "semi_major_axis": np.array([6378137.0], dtype=float),
        "semi_minor_axis": np.array([6356752.314245], dtype=float),
        "inverse_flattening": np.array([6356752.314245], dtype=float),
        "EPSG_code": "EPSG:0",
        "value": "value is equal to EPSG code"}
    ug.variable_int_with_attributes_define("projected_coordinate_system", attribute_dict)
    # 4. Add conventions (global attributes)
    conventions = {
        "institution": "Deltares",
        "references": "Unknown",
        "source": "Unknown Unknown. Model: Unknown",
        "history": "Created on 2017-11-27T18:05:09+0100, Unknown",
        "Conventions": "CF-1.6 UGRID-1.0/Deltares-0.8"}
    ug.attribute_global_define(conventions)



StarcoderdataJupyterScriptsDedupFiltered