{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Subsetting\n", "\n", "The subset operation makes use of `clisops.core.subset` to process the datasets and to set the output type and the output file names." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialize the testing data\n", "import clisops.utils.testing as clite\n", "\n", "Stratus = clite.stratus(\n", " repo=clite.XCLIM_TEST_DATA_REPO_URL, branch=clite.XCLIM_TEST_DATA_VERSION, cache_dir=clite.XCLIM_TEST_DATA_CACHE_DIR\n", ")\n", "\n", "# fetch files locally or from GitHub\n", "tas_files = [\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_200512-203011.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_203012-205511.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_205512-208011.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_208012-209912.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_209912-212411.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_212412-214911.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_214912-217411.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_217412-219911.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_219912-222411.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_222412-224911.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_224912-227411.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_227412-229911.nc\",\n", " \"cmip5/tas_Amon_HadGEM2-ES_rcp85_r1i1p1_229912-229912.nc\",\n", "]\n", "for i, name in enumerate(tas_files):\n", " tas_files[i] = Stratus.fetch(name)\n", "\n", "o3_file = Stratus.fetch(\"cmip6/o3_Amon_GFDL-ESM4_historical_r1i1p1f1_gr1_185001-194912.nc\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "\n", "from clisops.ops.subset import subset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `subset` process takes several parameters:\n", "\n", "## Subsetting Parameters\n", "\n", " ds: Union[xr.Dataset, str, Path]\n", " time: Optional[Union[str, TimeParameter]]\n", " area: Optional[\n", " Union[\n", " str,\n", " Tuple[\n", " Union[int, float, str],\n", " Union[int, float, str],\n", " Union[int, float, str],\n", " Union[int, float, str],\n", " ],\n", " AreaParameter,\n", " ]\n", " ]\n", " level: Optional[\n", " Union[\n", " str, LevelParameter\n", " ]\n", " ]\n", " time_components: Optional[Union[str, Dict, TimeComponentsParameter]]\n", " output_dir: Optional[Union[str, Path]]\n", " output_type: {\"netcdf\", \"nc\", \"zarr\", \"xarray\"}\n", " split_method: {\"time:auto\"}\n", " file_namer: {\"standard\"}\n", "\n", "\n", "The output is a list containing the outputs in the format selected." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_mfdataset(tas_files, decode_times=xr.coders.CFDatetimeCoder(use_cftime=True), combine=\"by_coords\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Output to xarray\n", "\n", "There will only be one output for this example." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outputs = subset(\n", " ds=ds,\n", " time=\"2007-01-01T00:00:00/2200-12-30T00:00:00\",\n", " area=(0.0, 10.0, 175.0, 90.0),\n", " output_type=\"xarray\",\n", ")\n", "\n", "print(f\"There is only {len(outputs)} output.\")\n", "outputs[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Output to netCDF with simple namer\n", "\n", "There is only one output as the file size is under the memory limit so does not need to be split.\n", "This example uses the simple namer which numbers output files." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outputs = subset(\n", " ds=ds,\n", " time=\"2007-01-01T00:00:00/2200-12-30T00:00:00\",\n", " area=(0.0, 10.0, 175.0, 90.0),\n", " output_type=\"nc\",\n", " output_dir=\".\",\n", " split_method=\"time:auto\",\n", " file_namer=\"simple\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To open the file\n", "\n", "subset_ds = xr.open_mfdataset(\n", " \"./output_001.nc\", decode_times=xr.coders.CFDatetimeCoder(use_cftime=True), combine=\"by_coords\"\n", ")\n", "subset_ds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Output to netCDF with standard namer\n", "\n", "There is only one output as the file size is under the memory limit so does not need to be split.\n", "This example uses the standard namer which names output filesa ccording the the input file and how it has been subsetted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outputs = subset(\n", " ds=ds,\n", " time=\"2007-01-01T00:00:00/2200-12-30T00:00:00\",\n", " area=(0.0, 10.0, 175.0, 90.0),\n", " output_type=\"nc\",\n", " output_dir=\".\",\n", " split_method=\"time:auto\",\n", " file_namer=\"standard\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Subsetting by level" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataset(o3_file, decode_times=xr.coders.CFDatetimeCoder(use_cftime=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### No subsetting applied" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = subset(ds=ds, output_type=\"xarray\")\n", "\n", "result[0].coords" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Subsetting over level" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# subsetting over pressure level (plev)\n", "\n", "result = subset(ds=ds, level=\"600/100\", output_type=\"xarray\")\n", "\n", "print(result[0].coords)\n", "print(f\"\\nplev has been subsetted and now only has {len(result[0].coords)} values.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use time components" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_mfdataset(tas_files, decode_times=xr.coders.CFDatetimeCoder(use_cftime=True), combine=\"by_coords\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outputs = subset(\n", " ds=ds,\n", " time_components=\"year: 2010, 2020, 2030|month: 12, 1, 2\",\n", " output_type=\"xarray\",\n", ")\n", "\n", "print(f\"There is only {len(outputs)} output.\")\n", "outputs[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using parameter classes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from clisops.parameter import (\n", " time_components,\n", " time_interval,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_mfdataset(tas_files, decode_times=xr.coders.CFDatetimeCoder(use_cftime=True), combine=\"by_coords\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outputs = subset(\n", " ds=ds,\n", " time=time_interval(\"2007-01-01T00:00:00\", \"2200-12-30T00:00:00\"),\n", " time_components=time_components(month=[\"dec\", \"jan\", \"feb\"]),\n", " output_type=\"xarray\",\n", ")\n", "\n", "print(f\"There is only {len(outputs)} output.\")\n", "outputs[0]" ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }