Skip to content

Commit

Permalink
update notebook and function sig
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc2 committed Jan 3, 2025
1 parent 7770e01 commit 6cdbacd
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 81 deletions.
8 changes: 7 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ Grid

Grid.cross_section
Grid.cross_section.constant_latitude
Grid.cross_section.constant_longitude
Grid.cross_section.constant_latitude_interval
Grid.cross_section.constant_longitude_interval


UxDataArray
~~~~~~~~~~~
Expand All @@ -332,7 +336,9 @@ UxDataArray

UxDataArray.cross_section
UxDataArray.cross_section.constant_latitude

UxDataArray.cross_section.constant_longitude
UxDataArray.cross_section.constant_latitude_interval
UxDataArray.cross_section.constant_longitude_interval
Remapping
---------

Expand Down
180 changes: 115 additions & 65 deletions docs/user-guide/cross-sections.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@
"source": [
"# Cross-Sections\n",
"\n",
"This section demonstrates how to extract cross-sections from an unstructured grid using UXarray, which allows the analysis and visualization across slices of grids.\n"
"This section demonstrates how to extract cross-sections from an unstructured grid using UXarray, which allows the analysis and visualization across slices of grids. Cross-sections can be performed directly on a `ux.Grid` object or on a `ux.UxDataArray`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b35ba4a2c30750e4",
"metadata": {
"ExecuteTime": {
"start_time": "2024-10-14T16:39:35.957687Z"
},
"jupyter": {
"is_executing": true
}
},
"outputs": [],
"metadata": {},
"source": [
"import uxarray as ux\n",
"import geoviews.feature as gf\n",
Expand All @@ -31,27 +22,14 @@
"import geoviews as gv\n",
"\n",
"projection = ccrs.Robinson()"
]
},
{
"cell_type": "markdown",
"id": "395a3db7-495c-4cff-b733-06bbe522a604",
"metadata": {},
"source": [
"## Data"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4160275c09fe6b0",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-09T17:50:51.217211Z",
"start_time": "2024-10-09T17:50:50.540946Z"
}
},
"outputs": [],
"metadata": {},
"source": [
"base_path = \"../../test/meshfiles/ugrid/outCSne30/\"\n",
"grid_path = base_path + \"outCSne30.ug\"\n",
Expand All @@ -64,17 +42,9 @@
" projection=projection,\n",
" title=\"Global Plot\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c844a3b5-ef71-41c8-a0ae-02ea937801d6",
"metadata": {},
],
"outputs": [],
"source": [
"uxds.uxgrid.normalize_cartesian_coordinates()"
]
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -83,33 +53,26 @@
"source": [
"## Constant Latitude\n",
"\n",
"Cross-sections along constant latitude lines can be obtained using the ``.cross_section.constant_latitude`` method, available for both ``ux.Grid`` and ``ux.DataArray`` objects. This functionality allows users to extract and analyze slices of data at specified latitudes, providing insights into variations along horizontal sections of the grid.\n"
"Cross-sections along constant latitude lines can be obtained by using the ``.cross_section.constant_latitude(lat)`` method. The sliced grid will be made up of the faces that contain at least one edge that intersects with a line of constant latitude.\n"
]
},
{
"cell_type": "markdown",
"id": "2fbe9f6e5bb59a17",
"metadata": {},
"source": [
"For example, we can obtain a cross-section at 30 degrees latitude by doing the following:"
]
"source": "For example, we can obtain a cross-section at 0 degrees latitude by doing the following:"
},
{
"cell_type": "code",
"execution_count": null,
"id": "3775daa1-2f1d-4738-bab5-2b69ebd689d9",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-09T17:50:53.093314Z",
"start_time": "2024-10-09T17:50:53.077719Z"
}
},
"outputs": [],
"metadata": {},
"source": [
"lat = 0\n",
"\n",
"uxda_constant_lat = uxds[\"psi\"].cross_section.constant_latitude(lat)"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -121,10 +84,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "484b77a6-86da-4395-9e63-f5ac56e37deb",
"metadata": {},
"outputs": [],
"source": [
"(\n",
" uxda_constant_lat.plot(\n",
Expand All @@ -138,7 +99,9 @@
" )\n",
" * gf.grid(projection=projection)\n",
")"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -150,14 +113,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cbee722-34a4-4e67-8e22-f393d7d36c99",
"metadata": {},
"outputs": [],
"source": [
"print(f\"Global Mean: {uxds['psi'].data.mean()}\")\n",
"print(f\"Mean at {lat} degrees lat: {uxda_constant_lat.data.mean()}\")"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -167,28 +130,25 @@
"## Constant Longitude\n",
"\n",
"\n",
"\n",
"Cross-sections along constant longitude lines can be obtained using the ``.cross_section.constant_longitude`` method, available for both ``ux.Grid`` and ``ux.DataArray`` objects. \n"
"Cross-sections along constant longitude lines can be obtained using the ``.cross_section.constant_longitude(lon)`` method. The sliced grid will be made up of the faces that contain at least one edge that intersects with a line of constant longitude.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f10917ce-568c-4e98-9b9c-d7c3c82e9ba3",
"metadata": {},
"outputs": [],
"source": [
"lon = 90\n",
"\n",
"uxda_constant_lon = uxds[\"psi\"].cross_section.constant_longitude(lon)"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "4add3a54-263e-41af-ac97-1e43c9141cb4",
"metadata": {},
"outputs": [],
"source": [
"(\n",
" uxda_constant_lon.plot(\n",
Expand All @@ -203,7 +163,97 @@
" )\n",
" * gf.grid(projection=projection)\n",
")"
]
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Constant Latitude Interval\n",
"\n",
"Cross-sections between two lines of latitudes can be obtained using the ``.cross_section.constant_lats_interval(lats)`` method. The sliced grid will contain faces that are strictly between the latitude interval."
],
"id": "5044b8680d514fdc"
},
{
"metadata": {},
"cell_type": "code",
"source": [
"lats = [-20, 20]\n",
"\n",
"uxda_constant_lat_interval = uxds[\"psi\"].cross_section.constant_latitude_interval(lats)"
],
"id": "fc84e47efe2edf96",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"(\n",
" uxda_constant_lat_interval.plot(\n",
" rasterize=False,\n",
" backend=\"bokeh\",\n",
" cmap=\"inferno\",\n",
" projection=projection,\n",
" global_extent=True,\n",
" coastline=True,\n",
" title=f\"Cross Section between {lats[0]} and {lats[1]} degrees latitude\",\n",
" periodic_elements=\"split\",\n",
" )\n",
" * gf.grid(projection=projection)\n",
")"
],
"id": "60232626ba6c74ad",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Constant Longitude Interval\n",
"\n",
"Cross-sections between two lines of longitude can be obtained using the ``.cross_section.constant_lons_interval(lons)`` method. The sliced grid will contain faces that are strictly between the longitude interval.\n"
],
"id": "4afa3d891a80c597"
},
{
"metadata": {},
"cell_type": "code",
"source": [
"lons = [-25, 25]\n",
"\n",
"uxda_constant_lon_interval = uxds[\"psi\"].cross_section.constant_longitude_interval(lats)"
],
"id": "b183d15838aaf6bb",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"(\n",
" uxda_constant_lon_interval.plot(\n",
" rasterize=False,\n",
" backend=\"bokeh\",\n",
" cmap=\"inferno\",\n",
" projection=projection,\n",
" global_extent=True,\n",
" coastline=True,\n",
" title=f\"Cross Section between {lons[0]} and {lons[1]} degrees longitude\",\n",
" periodic_elements=\"split\",\n",
" )\n",
" * gf.grid(projection=projection)\n",
")"
],
"id": "22793d56701504ce",
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand Down
8 changes: 4 additions & 4 deletions uxarray/cross_sections/dataarray_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __repr__(self):

methods_heading += " * constant_latitude(lat)\n"
methods_heading += " * constant_longitude(lon)\n"
methods_heading += " * latitude_interval(lats)\n"
methods_heading += " * longitude_interval(lons)\n"
methods_heading += " * constant_latitude_interval(lats)\n"
methods_heading += " * constant_longitude_interval(lons)\n"

return prefix + methods_heading

Expand Down Expand Up @@ -106,14 +106,14 @@ def constant_longitude(self, lon: float):

return self.uxda.isel(n_face=faces)

def latitude_interval(self, lats: Tuple[float, float], *args, **kwargs):
def constant_latitude_interval(self, lats: Tuple[float, float], *args, **kwargs):
faces = self.uxda.uxgrid.get_faces_between_latitudes(
lats,
)

return self.uxda.isel(n_face=faces)

def longitude_interval(self, lons: Tuple[float, float], *args, **kwargs):
def constant_longitude_interval(self, lons: Tuple[float, float], *args, **kwargs):
faces = self.uxda.uxgrid.get_faces_between_longitudes(lons)

return self.uxda.isel(n_face=faces)
Expand Down
10 changes: 6 additions & 4 deletions uxarray/cross_sections/grid_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def __repr__(self):

methods_heading += " * constant_latitude(lat, return_face_indices)\n"
methods_heading += " * constant_longitude(lon, return_face_indices)\n"
methods_heading += " * latitude_interval(lats, return_face_indices)\n"
methods_heading += " * longitude_interval(lons, return_face_indices)\n"
methods_heading += " * constant_latitude_interval(lats, return_face_indices)\n"
methods_heading += (
" * constant_longitude_interval(lons, return_face_indices)\n"
)
return prefix + methods_heading

def constant_latitude(
Expand Down Expand Up @@ -129,7 +131,7 @@ def constant_longitude(
def gca(self, *args, **kwargs):
raise NotImplementedError

def latitude_interval(
def constant_latitude_interval(
self,
lats: Tuple[float, float],
return_face_indices: bool = False,
Expand All @@ -145,7 +147,7 @@ def latitude_interval(
else:
return grid_between_lats

def longitude_interval(
def constant_longitude_interval(
self,
lons: Tuple[float, float],
return_face_indices: bool = False,
Expand Down
Loading

0 comments on commit 6cdbacd

Please sign in to comment.