From 12c45b42c847123017cab16de1d764e2194ed21b Mon Sep 17 00:00:00 2001 From: Daniel Kennedy Date: Thu, 7 Oct 2021 10:25:11 -0600 Subject: [PATCH] example notebook to edit paramfiles --- params/modify_params.ipynb | 3001 ++++++++++++++++++++++++++++++++++++ 1 file changed, 3001 insertions(+) create mode 100644 params/modify_params.ipynb diff --git a/params/modify_params.ipynb b/params/modify_params.ipynb new file mode 100644 index 0000000..f5a8bbf --- /dev/null +++ b/params/modify_params.ipynb @@ -0,0 +1,3001 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d007fef7-e788-45c9-a1bd-e37eff9c7e91", + "metadata": {}, + "source": [ + "# Editing the CTSM paramfiles\n", + "- some typical examples" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "81c94223-a9cd-4683-a298-a4d188f86d03", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import xarray as xr\n", + "import os\n", + "import netCDF4" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "ab067e34-b931-443d-9b00-82c85e17afc5", + "metadata": {}, + "outputs": [], + "source": [ + "#this is the base paramfile that you will copy and edit\n", + "basefile = '/glade/p/cesm/cseg/inputdata/lnd/clm2/paramdata/ctsm51_params.c210507.nc'\n", + "\n", + "#important that your paramfile match the version of CLM that you are using\n", + "# consider examining bld/namelist_files/namelist_defaults_ctsm.xml" + ] + }, + { + "cell_type": "markdown", + "id": "37b717ba-cb80-4ace-befc-2cdbd4b1dd4f", + "metadata": {}, + "source": [ + "### examine a pft file" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3ac988fa-de04-44d7-af83-54b312b171d7", + "metadata": {}, + "outputs": [], + "source": [ + "ds = xr.open_dataset(basefile)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "bc331c0b-0c9b-44f2-94f0-82e92aab4161", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'kmax' (segment: 4, pft: 79)>\n",
+       "array([[0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n",
+       "        2.000000e-08, 2.000000e-08],\n",
+       "       [0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n",
+       "        2.000000e-08, 2.000000e-08],\n",
+       "       [0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n",
+       "        2.000000e-08, 2.000000e-08],\n",
+       "       [0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n",
+       "        2.000000e-08, 2.000000e-08]])\n",
+       "Coordinates:\n",
+       "    pftname  (pft) |S40 b'not_vegetated                           ' ... b'irr...\n",
+       "  * segment  (segment) |S40 b'sunlit                                  ' ... b...\n",
+       "Dimensions without coordinates: pft\n",
+       "Attributes:\n",
+       "    units:      mm h2o (transpired)/mm h2o (water potential gradient)/sec\n",
+       "    long_name:  plant segment max conductance
" + ], + "text/plain": [ + "\n", + "array([[0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n", + " 2.000000e-08, 2.000000e-08],\n", + " [0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n", + " 2.000000e-08, 2.000000e-08],\n", + " [0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n", + " 2.000000e-08, 2.000000e-08],\n", + " [0.000000e+00, 1.602140e-08, 2.234574e-08, ..., 2.000000e-08,\n", + " 2.000000e-08, 2.000000e-08]])\n", + "Coordinates:\n", + " pftname (pft) |S40 ...\n", + " * segment (segment) |S40 b'sunlit ' ... b...\n", + "Dimensions without coordinates: pft\n", + "Attributes:\n", + " units: mm h2o (transpired)/mm h2o (water potential gradient)/sec\n", + " long_name: plant segment max conductance" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds.kmax" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5c41f87c-d99b-4bf6-9ae6-04351571c2a7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'medlynslope' (pft: 79)>\n",
+       "array([0.  , 2.35, 2.35, 2.35, 4.12, 4.12, 4.45, 4.45, 4.45, 4.7 , 4.7 , 4.7 ,\n",
+       "       2.22, 5.25, 1.62, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 1.79,\n",
+       "       1.79, 1.79, 1.79, 1.79, 1.79, 5.79, 5.79])\n",
+       "Coordinates:\n",
+       "    pftname  (pft) |S40 b'not_vegetated                           ' ... b'irr...\n",
+       "Dimensions without coordinates: pft\n",
+       "Attributes:\n",
+       "    long_name:  Medlyn slope of conductance-photosynthesis relationship\n",
+       "    units:      umol H2O/umol CO2\n",
+       "    comment:    Values come from the values used in the CABLE model
" + ], + "text/plain": [ + "\n", + "array([0. , 2.35, 2.35, 2.35, 4.12, 4.12, 4.45, 4.45, 4.45, 4.7 , 4.7 , 4.7 ,\n", + " 2.22, 5.25, 1.62, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 1.79,\n", + " 1.79, 1.79, 1.79, 1.79, 1.79, 5.79, 5.79])\n", + "Coordinates:\n", + " pftname (pft) |S40 b'not_vegetated ' ... b'irr...\n", + "Dimensions without coordinates: pft\n", + "Attributes:\n", + " long_name: Medlyn slope of conductance-photosynthesis relationship\n", + " units: umol H2O/umol CO2\n", + " comment: Values come from the values used in the CABLE model" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds.medlynslope" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "27f831de-8631-4f30-800f-fd1c8735ee92", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'jmaxb0' ()>\n",
+       "array(0.0311)\n",
+       "Attributes:\n",
+       "    long_name:  Baseline proportion of nitrogen allocated for electron transport\n",
+       "    units:      J
" + ], + "text/plain": [ + "\n", + "array(0.0311)\n", + "Attributes:\n", + " long_name: Baseline proportion of nitrogen allocated for electron transport\n", + " units: J" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ds.jmaxb0" + ] + }, + { + "cell_type": "markdown", + "id": "580e8d55-fbde-418c-a364-9800c0ebd6e9", + "metadata": {}, + "source": [ + "## edit a paramfile\n", + " - avoiding xarray for writing the new files, for some reason paramfiles written with xarray tend to crash the model" + ] + }, + { + "cell_type": "markdown", + "id": "f3d8f8b4-b282-4509-b99d-b5c266d9d61a", + "metadata": {}, + "source": [ + "### jmaxb0, increase to 0.05" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "027b148d-3f2f-48c1-b4cc-e79044f27a0b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## CREATE the new paramfile\n", + "newfile = 'jmaxb0_5e-2.nc'\n", + "cmd = 'cp '+basefile+' '+newfile\n", + "os.system(cmd)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "7772f5b6-99ba-4f9c-aae9-aaadfcf2d77a", + "metadata": {}, + "outputs": [], + "source": [ + "param = 'jmaxb0'\n", + "dset = netCDF4.Dataset(newfile,'r+')\n", + "dset[param][:] = 0.05\n", + "dset.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "6d39c684-ae26-482b-be5a-1fd9d2160e84", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'jmaxb0' ()>\n",
+       "array(0.05)\n",
+       "Attributes:\n",
+       "    long_name:  Baseline proportion of nitrogen allocated for electron transport\n",
+       "    units:      J
" + ], + "text/plain": [ + "\n", + "array(0.05)\n", + "Attributes:\n", + " long_name: Baseline proportion of nitrogen allocated for electron transport\n", + " units: J" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "xr.open_dataset(newfile)['jmaxb0']" + ] + }, + { + "cell_type": "markdown", + "id": "5dba3b01-589e-4829-8a8f-448933904552", + "metadata": {}, + "source": [ + "### kmax, double all values\n", + " - note that the shapes of the various parameters vary\n", + " - some are scalar, some have a pft-dimension, and some have pft and plant-segment dimensions\n", + " - easist to read in the example from basefile, and edit accordingly " + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "93ef7e76-0041-425e-8ad6-3f4396e314fe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## CREATE the new paramfile\n", + "newfile = 'kmax_2x.nc'\n", + "cmd = 'cp '+basefile+' '+newfile\n", + "os.system(cmd)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "bcf2c053-b2ce-4406-a1ba-41d9684a23f2", + "metadata": {}, + "outputs": [], + "source": [ + "param = 'kmax'\n", + "kmax = xr.open_dataset(basefile)[param].values\n", + "newkmax = kmax*2" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "1baf2f03-4da4-40b9-887e-f8afeb686b3d", + "metadata": {}, + "outputs": [], + "source": [ + "dset = netCDF4.Dataset(newfile,'r+')\n", + "dset[param][:] = newkmax\n", + "dset.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "62f01983-27c6-4c32-949b-a95580638685", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'kmax' (segment: 4, pft: 79)>\n",
+       "array([[0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n",
+       "        4.000000e-08, 4.000000e-08],\n",
+       "       [0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n",
+       "        4.000000e-08, 4.000000e-08],\n",
+       "       [0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n",
+       "        4.000000e-08, 4.000000e-08],\n",
+       "       [0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n",
+       "        4.000000e-08, 4.000000e-08]])\n",
+       "Coordinates:\n",
+       "    pftname  (pft) |S40 b'not_vegetated                           ' ... b'irr...\n",
+       "  * segment  (segment) |S40 b'sunlit                                  ' ... b...\n",
+       "Dimensions without coordinates: pft\n",
+       "Attributes:\n",
+       "    units:      mm h2o (transpired)/mm h2o (water potential gradient)/sec\n",
+       "    long_name:  plant segment max conductance
" + ], + "text/plain": [ + "\n", + "array([[0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n", + " 4.000000e-08, 4.000000e-08],\n", + " [0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n", + " 4.000000e-08, 4.000000e-08],\n", + " [0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n", + " 4.000000e-08, 4.000000e-08],\n", + " [0.000000e+00, 3.204280e-08, 4.469149e-08, ..., 4.000000e-08,\n", + " 4.000000e-08, 4.000000e-08]])\n", + "Coordinates:\n", + " pftname (pft) |S40 ...\n", + " * segment (segment) |S40 b'sunlit ' ... b...\n", + "Dimensions without coordinates: pft\n", + "Attributes:\n", + " units: mm h2o (transpired)/mm h2o (water potential gradient)/sec\n", + " long_name: plant segment max conductance" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "xr.open_dataset(newfile)['kmax']" + ] + }, + { + "cell_type": "markdown", + "id": "84ba4bae-1b4e-4b5f-af7f-2411971fccd2", + "metadata": {}, + "source": [ + "### medlynslope\n", + "- substituting a value for just one pft" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "6b184d47-246d-461f-9f11-09d599a2b689", + "metadata": {}, + "outputs": [], + "source": [ + "p = xr.open_dataset(basefile)\n", + "mypft = 'broadleaf_deciduous_temperate_tree'\n", + "ix = np.array([mypft in str(n) for n in p.pftname.values]) #index vector for mypft" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "006f879a-085e-4bea-904e-c0f1b4eaeb15", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0. , 2.3499999 , 2.3499999 , 2.3499999 , 4.11999989,\n", + " 4.11999989, 4.44999981, 6. , 4.44999981, 4.69999981,\n", + " 4.69999981, 4.69999981, 2.22000003, 5.25 , 1.62 ,\n", + " 5.78999996, 5.78999996, 1.78999996, 1.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 5.78999996, 5.78999996, 5.78999996,\n", + " 5.78999996, 5.78999996, 1.78999996, 1.78999996, 5.78999996,\n", + " 5.78999996, 1.78999996, 1.78999996, 1.78999996, 1.78999996,\n", + " 1.78999996, 1.78999996, 5.78999996, 5.78999996])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "param = 'medlynslope'\n", + "m = p[param].values\n", + "m[ix] = 6\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "7b0a1d7d-1b76-48a1-bb8d-5b7ae496bc22", + "metadata": {}, + "outputs": [], + "source": [ + "newfile = 'medlynslope_BDT_6.nc'\n", + "cmd = 'cp '+basefile+' '+newfile\n", + "os.system(cmd)\n", + "dset = netCDF4.Dataset(newfile,'r+')\n", + "dset[param][:] = m\n", + "dset.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "300f8235-0c9f-4cc3-92a9-26ae32f4a31c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'medlynslope' (pft: 79)>\n",
+       "array([0.  , 2.35, 2.35, 2.35, 4.12, 4.12, 4.45, 6.  , 4.45, 4.7 , 4.7 , 4.7 ,\n",
+       "       2.22, 5.25, 1.62, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n",
+       "       5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 1.79,\n",
+       "       1.79, 1.79, 1.79, 1.79, 1.79, 5.79, 5.79])\n",
+       "Coordinates:\n",
+       "    pftname  (pft) |S40 b'not_vegetated                           ' ... b'irr...\n",
+       "Dimensions without coordinates: pft\n",
+       "Attributes:\n",
+       "    long_name:  Medlyn slope of conductance-photosynthesis relationship\n",
+       "    units:      umol H2O/umol CO2\n",
+       "    comment:    Values come from the values used in the CABLE model
" + ], + "text/plain": [ + "\n", + "array([0. , 2.35, 2.35, 2.35, 4.12, 4.12, 4.45, 6. , 4.45, 4.7 , 4.7 , 4.7 ,\n", + " 2.22, 5.25, 1.62, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79,\n", + " 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 5.79, 1.79, 1.79, 5.79, 5.79, 1.79,\n", + " 1.79, 1.79, 1.79, 1.79, 1.79, 5.79, 5.79])\n", + "Coordinates:\n", + " pftname (pft) |S40 ...\n", + "Dimensions without coordinates: pft\n", + "Attributes:\n", + " long_name: Medlyn slope of conductance-photosynthesis relationship\n", + " units: umol H2O/umol CO2\n", + " comment: Values come from the values used in the CABLE model" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "xr.open_dataset(newfile)[param]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "db275759-d218-4a83-af53-2fedb86563ad", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:miniconda3-lens-py]", + "language": "python", + "name": "conda-env-miniconda3-lens-py-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.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}