# ---
# 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 variable basics
#
# This is the basic introduction for using the `ugridpy` library.
#
# `ugridpy` can be used for reading variable information from UGrid files. 
#
# At the very beginning, the necessary libraries have to be imported.

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

# Other imports

import numpy as np

# ## Reading topology's attributes names and values

with UGrid("./data_examples/OneMesh2D.nc", "r") as ug:
    # 1. Get the mesh2d name. This is also the name of a variable 
    variable_name = ug.mesh2d_get(0).name
    # 2. Get the mesh2D attribute names
    attribute_names = ug.variable_get_attributes_names(variable_name)
    # 3. Get the mesh2D attribute values
    attribute_values = ug.variable_get_attributes_values(variable_name)

# ### Create a dictionary of topology attributes names and values. This dictionary can be used for reading the file content with other libraries (xarray)

variable_attributes = {name:value for (name,value) in zip(attribute_names,attribute_values) }
print(variable_attributes)

# ## Reading another variable attribute's names and values

# ### In this case the attribute flag_values contains multiple values

with UGrid("./data_examples/OneMesh2D.nc", "r") as ug:
    # 1. Get the mesh2d name. This is also the name of a variable 
    variable_name = "mesh2d_edge_type"
    # 2. Get the mesh2D attribute names
    attribute_names = ug.variable_get_attributes_names(variable_name)
    # 3. Get the mesh2D attribute values
    attribute_values = ug.variable_get_attributes_values(variable_name)

variable_attributes = {name:value for (name,value) in zip(attribute_names,attribute_values) }
print(variable_attributes)

# ## Reading data variable from a NetCDF file

with UGrid("./data_examples/ADH_SanDiego.nc", "r") as ug:
    depth = ug.variable_get_data_double("depth")
    node_x = ug.variable_get_data_double("node_x")
    node_y = ug.variable_get_data_double("node_y")

# +
import numpy as np
import matplotlib.pyplot as plt
  
# Plotting scalar field with tricontour
plt.tricontourf(node_x, node_y, depth[:9140])
plt.title('Scalar Field')
  
# Show plot with gird
plt.grid()
# -



StarcoderdataJupyterScriptsDedupFiltered