From 1fdea7f99eebb2c08a686f15a368112374d78e43 Mon Sep 17 00:00:00 2001
From: Aiko Voigt <aiko.voigt@univie.ac.at>
Date: Sun, 12 May 2024 01:02:50 +0200
Subject: [PATCH] Fixes nan values in MAC-SP input aerosol file that cause
 crash in year 2017 and beyond

---
 fix-mac-sp-aerosols.ipynb | 171 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100644 fix-mac-sp-aerosols.ipynb

diff --git a/fix-mac-sp-aerosols.ipynb b/fix-mac-sp-aerosols.ipynb
new file mode 100644
index 0000000..3e2c862
--- /dev/null
+++ b/fix-mac-sp-aerosols.ipynb
@@ -0,0 +1,171 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "id": "ddd5af91-af0b-4379-88d4-6edc6ef337b2",
+   "metadata": {},
+   "source": [
+    "# Fix MAC-SP input data to use beyond year 2016.\n",
+    "\n",
+    "Reason: all runs crash on Jan 5, 2017. This indicates that the crash is caused by some issue in the input data, instead of a model instability.\n",
+    "\n",
+    "Indeed, it turns out to be caused by the fact that the MAC-SP aerosol data is nan beyond year 2016. This is fixed here by using the 2016 year values to extend the dataset to year 2100."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "id": "3b52083f-c1f5-406e-917c-97b385ccd6fd",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "import numpy as np\n",
+    "import xarray as xr\n",
+    "import matplotlib.pyplot as plt"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "id": "6ca6b837-288f-4d4f-8d21-521fe4b4ed57",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Load MAC SP\n",
+    "path=\"/home/fs72044/avoigt_teach/climlab_s2024/icon-esm-univie/data/\"\n",
+    "macsp = xr.open_dataset(path+\"MACv2.0-SP_v1.nc\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "82cc7e00-3d3c-42cf-bea7-e60c6faa7129",
+   "metadata": {
+    "tags": []
+   },
+   "source": [
+    "The problem is caused by macsp.years_weight=nan beyond year 2016."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "id": "56b85c79-01c0-4feb-939a-2984e3b08fa5",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "array([[0.71, 0.71,  nan,  nan,  nan,  nan],\n",
+       "       [0.53, 0.53,  nan,  nan,  nan,  nan],\n",
+       "       [0.99, 0.99,  nan,  nan,  nan,  nan],\n",
+       "       [1.28, 1.28,  nan,  nan,  nan,  nan],\n",
+       "       [1.23, 1.23,  nan,  nan,  nan,  nan],\n",
+       "       [1.09, 1.09,  nan,  nan,  nan,  nan],\n",
+       "       [1.1 , 1.1 ,  nan,  nan,  nan,  nan],\n",
+       "       [1.29, 1.29,  nan,  nan,  nan,  nan],\n",
+       "       [0.94, 0.94,  nan,  nan,  nan,  nan]], dtype=float32)"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "macsp.year_weight.sel(years=slice(2015,2020)).values"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "id": "a1d9f144-1a3e-421f-9028-c6f318b8b043",
+   "metadata": {},
+   "source": [
+    "Create new dataset with year 2016 values used to fill the values from 2017 to 2100."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "id": "cc3b433e-835c-4d3b-8f29-df2f7149e684",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "macsp_beyond2016 = macsp.copy(deep=True)\n",
+    "for ind in np.arange(167,251):\n",
+    "    macsp_beyond2016.year_weight[:,ind] = macsp.year_weight[:,166]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "id": "027f417c-9e45-44d4-b0e5-b4584326c54a",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "array([[0.71, 0.71, 0.71, 0.71, 0.71],\n",
+       "       [0.53, 0.53, 0.53, 0.53, 0.53],\n",
+       "       [0.99, 0.99, 0.99, 0.99, 0.99],\n",
+       "       [1.28, 1.28, 1.28, 1.28, 1.28],\n",
+       "       [1.23, 1.23, 1.23, 1.23, 1.23],\n",
+       "       [1.09, 1.09, 1.09, 1.09, 1.09],\n",
+       "       [1.1 , 1.1 , 1.1 , 1.1 , 1.1 ],\n",
+       "       [1.29, 1.29, 1.29, 1.29, 1.29],\n",
+       "       [0.94, 0.94, 0.94, 0.94, 0.94]], dtype=float32)"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# check\n",
+    "macsp_beyond2016[\"year_weight\"].sel(years=slice(2016,2020)).values"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "id": "be3ea71b-1a0d-4176-8367-f502c8949dd0",
+   "metadata": {
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "# save to netcdf file\n",
+    "macsp_beyond2016.to_netcdf(path+\"MACv2.0-SP_v1.fixed-for-use-beyond2016.nc\")"
+   ]
+  }
+ ],
+ "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.9"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
-- 
GitLab