ASFC_notebook.ipynb 6.97 KB
Newer Older
sbiri's avatar
sbiri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n",
    "AirSeaFluxCode is developed to provide an easy and accessible way to calculate turbulent surface fluxes (TSFs) from a small number of bulk variables and for a viariety of bulk algorithms. \n",
    "\n",
    "By running AirSeaFluxCode you can compare different bulk algorithms and to also investigate the effect choices within the implementation of each parameterisation have on the TSFs estimates. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Getting started\n",
    "\n",
    "Let's first import the basic python packages we will need for reading in our input data, to perform basic statistics  and plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first import all packages you might need\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import netCDF4 as nc\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from tabulate import tabulate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### AirSeaFluxCode examples\n",
    "\n",
    "AirSeaFluxCode is set up to run in its default setting with a minimum number of input variables, namely wind speed; air temperature; and sea surface temperature. Let's load the code, import some real data composed for testing it (Research vessel data) and run AirSeaFluxCode with default settings (output height 10m, cool skin/warm layer corrections turned off, bulk algorithm Smith 1988, gustiness on, saturation vapour pressure following Buck (2012), tolerance limits set for both flux estimates and height adjusted variables (['all', 0.01, 0.01, 1e-05, 1e-3, 0.1, 0.1]), number of iterations are ten, non converged points are set to missing and Monin-Obukhov stability length is calculated following the ECMWF implementation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from AirSeaFluxCode import AirSeaFluxCode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inDt = pd.read_csv(\"data_all.csv\")\n",
    "date = np.asarray(inDt[\"Date\"])\n",
    "lon = np.asarray(inDt[\"Longitude\"])\n",
    "lat = np.asarray(inDt[\"Latitude\"])\n",
    "spd = np.asarray(inDt[\"Wind speed\"])\n",
    "t = np.asarray(inDt[\"Air temperature\"])\n",
    "sst = np.asarray(inDt[\"SST\"])\n",
    "rh = np.asarray(inDt[\"RH\"])\n",
    "p = np.asarray(inDt[\"P\"])\n",
    "sw = np.asarray(inDt[\"Rs\"])\n",
    "hu = np.asarray(inDt[\"zu\"])\n",
    "ht = np.asarray(inDt[\"zt\"])\n",
    "hin = np.array([hu, ht, ht])\n",
    "del hu, ht, inDt\n",
    "# run AirSeaFluxCode\n",
    "res = AirSeaFluxCode(spd, t, sst, lat=lat, hin=hin, hum=[\"rh\", rh], P=p, cskin=0, Rs=sw,\n",
    "                     tol=['all', 0.01, 0.01, 1e-05, 1e-3, 0.1, 0.1], L=\"Rb\")\n",
    "flg = res[\"flag\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "res is the output of AirSeaFluxCode which is a dataFrame with keys: \"tau\", \"shf\", \"lhf\", \"L\", \"cd\", \"cdn\", \"ct\", \"ctn\", \"cq\", \"cqn\", \"tsrv\", \"tsr\", \"qsr\", \"usr\", \"psim\", \"psit\",\"psiq\", \"u10n\", \"t10n\", \"tv10n\", \"q10n\", \"zo\", \"zot\", \"zoq\", \"uref\", \"tref\", \"qref\", \"iteration\", \"dter\", \"dqer\", \"dtwl\", \"qair\", \"qsea\", \"Rl\", \"Rs\", \"Rnl\", \"ug\", \"Rib\", \"rh\", \"delta\", \"lv\". Let's plot the flux estimates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(3, 1, sharex=True, sharey=False)\n",
    "fig.tight_layout()\n",
    "ax[0].plot(res[\"tau\"], \"-\", color=\"grey\", linewidth=1, alpha = 0.8)\n",
    "ax[1].plot(res[\"shf\"], \"-\", color=\"grey\", linewidth=1, alpha = 0.8)\n",
    "ax[2].plot(res[\"lhf\"], \"-\", color=\"grey\", linewidth=1, alpha = 0.8)\n",
    "ax[0].set_ylabel('tau (Nm$^{-2}$)')\n",
    "ax[1].set_ylabel('shf (Wm$^{-2}$)')\n",
    "ax[2].set_ylabel('lhf (Wm$^{-2}$)')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can save the output in a csv file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res.to_csv(\"test_AirSeaFluxCode.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and generate some statistics which you can save in a txt file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Input summary\", file=open('./stats.txt', 'a'))\n",
    "print('input file name: {}, \\n method: {}, \\n gustiness: {}, \\n cskin: {},'\n",
    "      ' \\n tolerance: {}, \\n qmethod: {}, \\n L: {}'.format(\"data_all.csv\", \"S88\", \"on\",\n",
    "                                                           0, \"all\", \"Buck2\", \"Rb\"),\n",
    "      file=open('./stats.txt', 'a'))\n",
    "ttl = np.asarray([\"tau  \", \"shf  \", \"lhf  \", \"L    \", \"cd   \", \"cdn  \",\n",
    "                  \"ct   \", \"ctn  \", \"cq   \", \"cqn  \", \"tsrv \", \"tsr  \",\n",
    "                  \"qsr  \", \"usr  \", \"psim \", \"psit \", \"psiq \", \"u10n \",\n",
    "                  \"t10n \", \"tv10n\", \"q10n \", \"zo   \", \"zot  \", \"zoq  \",\n",
    "                  \"urefs\", \"trefs\", \"qrefs\", \"itera\", \"dter \", \"dqer \",\n",
    "                  \"dtwl \", \"qair \", \"qsea \", \"Rl   \", \"Rs   \", \"Rnl  \",\n",
    "                  \"ug   \", \"Rib  \", \"rh   \"])\n",
    "header = [\"var\", \"mean\", \"median\", \"min\", \"max\", \"5%\", \"95%\"]\n",
    "n = np.shape(res)\n",
    "stats = np.copy(ttl)\n",
    "a = res.loc[:,\"tau\":\"rh\"].to_numpy(dtype=\"float64\").T\n",
    "stats = np.c_[stats, np.nanmean(a, axis=1)]\n",
    "stats = np.c_[stats, np.nanmedian(a, axis=1)]\n",
    "stats = np.c_[stats, np.nanmin(a, axis=1)]\n",
    "stats = np.c_[stats, np.nanmax(a, axis=1)]\n",
    "stats = np.c_[stats, np.nanpercentile(a, 5, axis=1)]\n",
    "stats = np.c_[stats, np.nanpercentile(a, 95, axis=1)]\n",
    "print(tabulate(stats, headers=header, tablefmt=\"github\", numalign=\"left\",\n",
    "               floatfmt=(\"s\", \"2.2e\", \"2.2e\", \"2.2e\", \"2.2e\", \"2.2e\", \"2.2e\")),\n",
    "      file=open('./stats.txt', 'a'))\n",
    "print('-'*79+'\\n', file=open('./stats.txt', 'a'))\n",
    "del a"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}