Skip to content

Commit

Permalink
add reflectance
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Aug 9, 2023
1 parent c3d516f commit 753ab9c
Showing 1 changed file with 229 additions and 0 deletions.
229 changes: 229 additions & 0 deletions notebooks/wp5/WIP_ocean_color_reflectance.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d2078cb1",
"metadata": {},
"source": [
"# Ocean color reflectance"
]
},
{
"cell_type": "markdown",
"id": "25f9797c",
"metadata": {},
"source": [
"## Import packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b0ba8de-66d3-4280-b486-ea3bafa4ae28",
"metadata": {},
"outputs": [],
"source": [
"import tempfile\n",
"\n",
"import cartopy.crs as ccrs\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import xarray as xr\n",
"from c3s_eqc_automatic_quality_control import diagnostics, download, plot, utils\n",
"\n",
"plt.style.use(\"seaborn-v0_8-notebook\")"
]
},
{
"cell_type": "markdown",
"id": "20d3c6af",
"metadata": {},
"source": [
"## Define Parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0061522-8772-42d4-8f1a-f6bbdb03ad25",
"metadata": {},
"outputs": [],
"source": [
"# Time period\n",
"year_start = 1998\n",
"year_stop = 1999\n",
"\n",
"# Variable to analyse\n",
"wavelength = 443\n",
"assert wavelength in (412, 443, 490, 510, 560, 665)"
]
},
{
"cell_type": "markdown",
"id": "42726c21",
"metadata": {},
"source": [
"## Define request"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "382c1767-9a60-4db1-8791-fc5f65887d10",
"metadata": {},
"outputs": [],
"source": [
"collection_id = \"satellite-ocean-colour\"\n",
"\n",
"request = {\n",
" \"variable\": \"remote_sensing_reflectance\",\n",
" \"projection\": \"regular_latitude_longitude_grid\",\n",
" \"version\": \"6_0\",\n",
" \"format\": \"zip\",\n",
"}\n",
"\n",
"# Parameters to speed up I/O\n",
"open_mfdataset_kwargs = {\n",
" \"concat_dim\": \"time\",\n",
" \"combine\": \"nested\",\n",
" \"data_vars\": \"minimal\",\n",
" \"coords\": \"minimal\",\n",
" \"compat\": \"override\",\n",
" \"parallel\": True,\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1dce537a-7786-4472-9e99-11c25236f5b3",
"metadata": {},
"source": [
"## Functions to cache"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e9c43b1-48ef-4a00-90cf-d062cb59c9af",
"metadata": {},
"outputs": [],
"source": [
"def rechunk(obj):\n",
" chunks = {\"time\": -1, \"year\": 1, \"longitude\": 270, \"latitude\": 270}\n",
" return obj.chunk(**{k: v for k, v in chunks.items() if k in obj.dims})\n",
"\n",
"\n",
"def rrs_annual_weighted_log_mean(ds, wavelength):\n",
" name = f\"Rrs_{wavelength}\"\n",
" da = rechunk(ds[name])\n",
" with tempfile.TemporaryDirectory() as tmpdir:\n",
" da.to_zarr(tmpdir)\n",
" da = xr.open_dataarray(tmpdir, engine=\"zarr\", chunks=dict(da.chunksizes))\n",
" weights = np.abs(np.cos(np.deg2rad(da[\"latitude\"])))\n",
" with xr.set_options(keep_attrs=True):\n",
" da = 10 ** diagnostics.annual_weighted_mean(\n",
" np.log10(da * weights), weights=False\n",
" )\n",
" da = rechunk(da.compute())\n",
" da.encoding[\"chunksizes\"] = tuple(map(max, da.chunks))\n",
" return da.to_dataset(name=name)"
]
},
{
"cell_type": "markdown",
"id": "3d8a7b28",
"metadata": {},
"source": [
"## Download and transform data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8cb75234-f402-4ce7-9585-4192ea1b2fb5",
"metadata": {},
"outputs": [],
"source": [
"datasets = []\n",
"for year in range(year_start, year_stop + 1):\n",
" print(f\"{year=}\")\n",
" requests = download.update_request_date(\n",
" request, start=f\"{year}-01\", stop=f\"{year}-12\", stringify_dates=True\n",
" )\n",
" ds = download.download_and_transform(\n",
" collection_id,\n",
" requests,\n",
" transform_chunks=False,\n",
" transform_func=rrs_annual_weighted_log_mean,\n",
" transform_func_kwargs={\"wavelength\": wavelength},\n",
" chunks={\"year\": 1, \"month\": 1},\n",
" **open_mfdataset_kwargs,\n",
" )\n",
" datasets.append(rechunk(ds))\n",
"ds = xr.concat(datasets, \"year\")"
]
},
{
"cell_type": "markdown",
"id": "94b4fe5c-2eae-4847-bb17-66fbd531b522",
"metadata": {},
"source": [
"## Plot global maps"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afd7484e-f23a-4558-83ad-779504e68c8d",
"metadata": {},
"outputs": [],
"source": [
"da = ds[f\"Rrs_{wavelength}\"]\n",
"plot_kwargs = {\"col\": \"year\", \"col_wrap\": 2, \"robust\": True}\n",
"\n",
"da = da.coarsen(latitude=5, longitude=5).mean()\n",
"facet = plot.projected_map(da, projection=ccrs.Robinson(), **plot_kwargs)"
]
},
{
"cell_type": "markdown",
"id": "1a4a6b69-cca8-4b7f-84ba-a724909db848",
"metadata": {},
"source": [
"## Plot regional maps"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a030fe6-6cf4-4f65-914a-fdb70ee1d943",
"metadata": {},
"outputs": [],
"source": [
"da_region = utils.regionalise(da, lon_slice=slice(-55, -40), lat_slice=slice(30, 15))\n",
"facet = plot.projected_map(da_region, projection=ccrs.PlateCarree(), **plot_kwargs)"
]
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 753ab9c

Please sign in to comment.