{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ec26be4e-73db-4a0a-9d7c-b68bc707fabf",
   "metadata": {},
   "source": [
    "Convert the MCB variables (saved in complete duration files) in degrees C and mm/day to compare to ARISE-SSP245 simulations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f18f1d96-7ec3-4635-9172-e4a02c68d579",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/glade/work/maritye/opt/miniconda/envs/npl_mrt2023a/lib/python3.9/site-packages/pandas/core/arrays/masked.py:59: UserWarning: Pandas requires version '1.3.2' or newer of 'bottleneck' (version '1.2.1' currently installed).\n",
      "  from pandas.core import (\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import xarray as xr\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fac0f77c-9484-4045-8c31-fb40a100b0fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "fpI2 = '/atm/proc/tseries/day_1/'\n",
    "fpmcb = '/glade/campaign/cgd/projects/cchen/MCB/b.e21.BSSP245smbb.f09_g17.MCB-050PCT.'\n",
    "dirO = \"/glade/work/maritye/Data/ARISE-MCB/Dailies/\"\n",
    "var = ['TREFHTMN', 'TREFHTMX'] # do precipitation separately\n",
    "membsmcb = [f\"{i:03}\" for i in range(1,11)]\n",
    "del membsmcb[7] # there isn't run 008"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b25c0051-7797-40ef-a767-a6577e042bfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['001', '002', '003', '004', '005', '006', '007', '009', '010']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "membsmcb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4947eeb-be20-408e-abf6-b290763bbccc",
   "metadata": {},
   "source": [
    "### MCB Runs with 5% Seeding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "aeed2001-e082-41fe-8f48-87345b6af153",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TREFHTMN\n",
      "001\n",
      "002\n",
      "003\n",
      "004\n",
      "005\n",
      "006\n",
      "007\n",
      "009\n",
      "010\n",
      "TREFHTMX\n",
      "001\n",
      "002\n",
      "003\n",
      "004\n",
      "005\n",
      "006\n",
      "007\n",
      "009\n",
      "010\n"
     ]
    }
   ],
   "source": [
    "for vee in var:\n",
    "    print(vee)\n",
    "    \n",
    "    for em in membsmcb:\n",
    "        print(em)\n",
    "        fp = fpmcb + em + fpI2\n",
    "        file = fp + 'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.'+ em +'.cam.h1.'+ vee + '.20350101-206912313131.nc'    #open each member convert to Celsius, drop variables close and save to netcdf\n",
    "        dsT = xr.open_dataset(file) [vee] - 273.15\n",
    "        dsT.to_netcdf(dirO + 'TREFHT/b.e21.BSSP245smbb.f09_g17.MCB-050PCT.'+ em +'.cam.h1.'+ vee +'.20350101-20691231.nc') #   ds.close()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ab44079-40f9-4705-81da-35b39d61efc5",
   "metadata": {},
   "source": [
    "One file has a slightly different name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "76a3778a-99ff-4e93-a736-3e7d16d153c0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TREFHTMN\n",
      "TREFHTMX\n"
     ]
    }
   ],
   "source": [
    "for vee in var:\n",
    "    print(vee)\n",
    "    \n",
    "    fp = '/glade/campaign/cgd/projects/cchen/MCB/b.e21.BSSP245cmip6.f09_g17.CMIP6-MCB-050PCT.000/atm/proc/tseries/day_1/'\n",
    "    file = fp + 'b.e21.BSSP245cmip6.f09_g17.CMIP6-MCB-050PCT.000.cam.h1.'+ vee + '.20350101-206912313131.nc' \n",
    "    dsT = xr.open_dataset(file) [vee] - 273.15\n",
    "    dsT.to_netcdf(dirO + 'TREFHT/b.e21.BSSP245cmip6.f09_g17.CMIP6-MCB-050PCT.000.cam.h1.'+ vee +'.20350101-20691231.nc') #   ds.close()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e5dc4d3a-16b6-4c08-93c3-de13e33987aa",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "file = fp + 'b.e21.BSSP245cmip6.f09_g17.CMIP6-MCB-050PCT.000.cam.h1.PRECT.20350101-206912313131.nc' \n",
    "dsP = xr.open_dataset(file) ['PRECT'] * 86400 * 1000\n",
    "dsP.to_netcdf(dirO + 'PRECT/b.e21.BSSP245cmip6.f09_g17.CMIP6-MCB-050PCT.000.cam.h1.PRECT.20350101-20691231.nc') #   ds.close()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "81644abc-ba31-4a14-a9a5-17f63bdfba12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "001\n",
      "002\n",
      "003\n",
      "004\n",
      "005\n",
      "006\n",
      "007\n",
      "009\n",
      "010\n"
     ]
    }
   ],
   "source": [
    "for em in membsmcb:\n",
    "    print(em)\n",
    "    fp = '/glade/campaign/cgd/projects/cchen/MCB/b.e21.BSSP245smbb.f09_g17.MCB-050PCT.'+ em + fpI2\n",
    "    file = fp + 'b.e21.BSSP245smbb.f09_g17.MCB-050PCT.'+ em +'.cam.h1.PRECT.20350101-206912313131.nc'    \n",
    "    ds = xr.open_dataset(file) ['PRECT'] * 86400 * 1000\n",
    "    ds.to_netcdf(dirO + 'PRECT/b.e21.BSSP245smbb.f09_g17.MCB-050PCT.'+ em +'.cam.h1.PRECT.20350101-20691231.nc') #   ds.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "860526b9-6bd1-4e47-9ad5-7ae3779b0ab8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:miniconda-npl_mrt2023a]",
   "language": "python",
   "name": "conda-env-miniconda-npl_mrt2023a-py"
  },
  "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.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
