|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "e601719e-1253-4140-aa64-274c5a36f724", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# subsetting a regular grid from an TDS source\n", |
| 9 | + "\n", |
| 10 | + "In this case, the Navy American Seas (AMSEAS) model, as provided by NCEI:\n", |
| 11 | + "\n", |
| 12 | + "https://www.ncei.noaa.gov/thredds-coastal/catalog/ncom_amseas_agg/catalog.html\n", |
| 13 | + "\n" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 1, |
| 19 | + "id": "caa476fd-2375-4a22-8981-5869bbff3eb5", |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "import xarray as xr\n", |
| 24 | + "import xarray_subset_grid\n", |
| 25 | + "from xarray_subset_grid.utils import format_bytes\n" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 2, |
| 31 | + "id": "f01dd841-7c89-47ba-8c16-a154bdee1b80", |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "# EXAMPLE (very small) subset:\n", |
| 36 | + "bbox = (268.0, 29.0, 269, 29.75)" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 3, |
| 42 | + "id": "f22007b0-f540-45a8-b974-e41c321cad0a", |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "# create an xarray dataset from the OpenDAP url\n", |
| 47 | + "ds = xr.open_dataset('https://www.ncei.noaa.gov/thredds-coastal/dodsC/ncom_amseas_agg/AmSeas_Dec_17_2020_to_Current_best.ncd')\n" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 4, |
| 53 | + "id": "4f93c32b-bc4d-4e1e-af4c-dfa890729a20", |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [ |
| 56 | + { |
| 57 | + "name": "stdout", |
| 58 | + "output_type": "stream", |
| 59 | + "text": [ |
| 60 | + "Dataset is: 10.0 TB\n" |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "source": [ |
| 65 | + "print(f\"Dataset is: {format_bytes(ds.nbytes)}\")\n" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 5, |
| 71 | + "id": "5f4fde4a-39bf-491c-a57c-acc2f3216ffa", |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# downscale in time (would be nice to make this smart, but for now:\n", |
| 76 | + "# 3 hour timesteps, last 12 timesteps is 3 days\n", |
| 77 | + "ds = ds.isel(time=slice(-12, None))\n" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 6, |
| 83 | + "id": "ce852bd3-0485-4d59-9310-e35c2d8e0ce5", |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "name": "stdout", |
| 88 | + "output_type": "stream", |
| 89 | + "text": [ |
| 90 | + "Dataset is: 9.9 GB\n" |
| 91 | + ] |
| 92 | + } |
| 93 | + ], |
| 94 | + "source": [ |
| 95 | + "print(f\"Dataset is: {format_bytes(ds.nbytes)}\")" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 7, |
| 101 | + "id": "215d1a33-7bb6-47b5-9343-1e8ff6267103", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "ds_ss = ds.xsg.subset_bbox(bbox)" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 8, |
| 111 | + "id": "ee346b9f-8b5c-4f57-8c63-944c3a11febf", |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stdout", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "Dataset is: 6.4 MB\n" |
| 119 | + ] |
| 120 | + } |
| 121 | + ], |
| 122 | + "source": [ |
| 123 | + "print(f\"Dataset is: {format_bytes(ds_ss.nbytes)}\")\n" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 9, |
| 129 | + "id": "f6487d58-5479-4c86-a927-0accc7f11209", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "# Save out the subset as a netcdf file.\n", |
| 134 | + "\n", |
| 135 | + "ds_ss.to_netcdf(\"AMSEAS-subset.nc\")\n" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": null, |
| 141 | + "id": "2536e8c5-5531-499a-a799-873be65fcd2f", |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "\n" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "id": "549bda29-a996-49d0-be1d-89e8a30b18e2", |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": null, |
| 159 | + "id": "66aa16b1-832e-4882-b974-526553f36bd9", |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "id": "240d4dc1-8a2b-4696-8cd1-eb1ee78c6ef8", |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [], |
| 170 | + "source": [] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": "Python 3 (ipykernel)", |
| 176 | + "language": "python", |
| 177 | + "name": "python3" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "codemirror_mode": { |
| 181 | + "name": "ipython", |
| 182 | + "version": 3 |
| 183 | + }, |
| 184 | + "file_extension": ".py", |
| 185 | + "mimetype": "text/x-python", |
| 186 | + "name": "python", |
| 187 | + "nbconvert_exporter": "python", |
| 188 | + "pygments_lexer": "ipython3", |
| 189 | + "version": "3.12.7" |
| 190 | + } |
| 191 | + }, |
| 192 | + "nbformat": 4, |
| 193 | + "nbformat_minor": 5 |
| 194 | +} |
0 commit comments