{ "cells": [ { "cell_type": "markdown", "id": "b5177062", "metadata": {}, "source": [ "## How to create a subgrid info file for CESM2's CLM processing?\n", "\n", "Why we need this script to save the a \"`CESM2_subgrid_info.nc`\" for CESM CLM processing? \n", "Because the CLM files uploaded to AWS only contain: \n", "- dimension: `(member_id, time, landunit)` \n", "- coordinates: `member_id`, and `time` \n", " \n", "In general, CESM's CLM varibales are saved as `(time, landunit)`. \n", "But usually we want to analyze the datasets with the format `(time, lat, lon)`. \n", "So the workflow for CESM's CLM variables would be\n", "- `(:, landunit)` \\[1D\\] -> `(:, landtype, lat, lon)` \\[3D\\] -> `(:, lat, lon)` \\[2D\\] \n", "\n", "As a result, we at least need to know: \n", "- `land1d_ixy` (landunit): for mapping from 1D to 3D \n", "- `land1d_jxy` (landunit): for mapping from 1D to 3D \n", "- `land1d_ityplunit` (landunit): for mapping from 1D to 3D \n", "- `lat`: for setting up the list of lat \n", "- `lon`: for setting up the list of lon \n", "\n", "reference: https://github.com/zzheng93/CLM-1D-to-2D" ] }, { "cell_type": "code", "execution_count": 1, "id": "widespread-gasoline", "metadata": {}, "outputs": [], "source": [ "#https://github.com/NCAR/ctsm_python_gallery/blob/master/notebooks/PFT-Gridding.ipynb\n", "import numpy as np\n", "import xarray as xr\n", "\n", "class load_clm:\n", " def __init__(self, args):\n", " self.ds = xr.open_dataset(args)\n", " self.lat = self.ds.lat\n", " self.lon = self.ds.lon\n", " self.time = self.ds.time\n", " self.ixy = self.ds.land1d_ixy\n", " self.jxy = self.ds.land1d_jxy\n", " self.ltype = self.ds.land1d_ityplunit\n", " self.ltype_dict = {value:key for key, value in self.ds.attrs.items() if 'ltype_' in key.lower()} \n", " def get2D(self, var_str):\n", " var = self.ds[var_str]\n", " nlat = len(self.lat.values)\n", " nlon = len(self.lon.values)\n", " ntim = len(self.time.values)\n", " nltype = len(self.ltype_dict)\n", " # create an empty array\n", " gridded = np.full([ntim,nltype,nlat,nlon],np.nan)\n", " # assign the values\n", " gridded[:,\n", " self.ltype.values.astype(int) - 1, # Fortran arrays start at 1\n", " self.jxy.values.astype(int) - 1,\n", " self.ixy.values.astype(int) - 1] = var.values\n", " grid_dims = xr.DataArray(gridded, dims=(\"time\",\"ltype\",\"lat\",\"lon\"))\n", " grid_dims = grid_dims.assign_coords(time=self.time,\n", " ltype=[i for i in range(self.ltype.values.min(), \n", " self.ltype.values.max()+1)],\n", " lat=self.lat.values,\n", " lon=self.lon.values)\n", " grid_dims.name = var_str\n", " return grid_dims" ] }, { "cell_type": "code", "execution_count": 2, "id": "sorted-ukraine", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{1: 'ltype_vegetated_or_bare_soil',\n", " 2: 'ltype_crop',\n", " 3: 'ltype_UNUSED',\n", " 4: 'ltype_landice_multiple_elevation_classes',\n", " 5: 'ltype_deep_lake',\n", " 6: 'ltype_wetland',\n", " 7: 'ltype_urban_tbd',\n", " 8: 'ltype_urban_hd',\n", " 9: 'ltype_urban_md'}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp = \"/glade/campaign/cgd/cesm/CESM2-LE/timeseries/lnd/proc/tseries/day_1/TREFMXAV/\"\n", "fn = \"b.e21.BSSP370smbb.f09_g17.LE2-1281.019.clm2.h6.TREFMXAV.20950101-21001231.nc\"\n", "clm = load_clm(fp+fn)\n", "clm.ltype_dict" ] }, { "cell_type": "code", "execution_count": 3, "id": "monetary-bundle", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:             (levgrnd: 25, levlak: 10, levdcmp: 25, lon: 288,\n",
       "                         lat: 192, gridcell: 21013, landunit: 62125,\n",
       "                         column: 554298, pft: 848480, time: 2191,\n",
       "                         hist_interval: 2)\n",
       "Coordinates:\n",
       "  * levgrnd             (levgrnd) float32 0.01 0.04 0.09 ... 19.48 28.87 42.0\n",
       "  * levlak              (levlak) float32 0.05 0.6 2.1 4.6 ... 25.6 34.33 44.78\n",
       "  * levdcmp             (levdcmp) float32 0.01 0.04 0.09 ... 19.48 28.87 42.0\n",
       "  * lon                 (lon) float32 0.0 1.25 2.5 3.75 ... 356.2 357.5 358.8\n",
       "  * lat                 (lat) float32 -90.0 -89.06 -88.12 ... 88.12 89.06 90.0\n",
       "  * time                (time) object 2095-01-01 00:00:00 ... 2101-01-01 00:0...\n",
       "Dimensions without coordinates: gridcell, landunit, column, pft, hist_interval\n",
       "Data variables: (12/45)\n",
       "    area                (lat, lon) float32 29.95 29.95 29.95 ... nan nan nan\n",
       "    landfrac            (lat, lon) float32 1.0 1.0 1.0 1.0 ... nan nan nan nan\n",
       "    landmask            (lat, lon) float64 1.0 1.0 1.0 1.0 ... nan nan nan nan\n",
       "    pftmask             (lat, lon) float64 1.0 1.0 1.0 1.0 ... nan nan nan nan\n",
       "    nbedrock            (lat, lon) float64 20.0 20.0 20.0 20.0 ... nan nan nan\n",
       "    grid1d_lon          (gridcell) float64 0.0 1.25 2.5 ... 335.0 328.8 330.0\n",
       "    ...                  ...\n",
       "    mscur               (time) int32 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0\n",
       "    nstep               (time) int32 1401600 1401648 1401696 ... 1506672 1506720\n",
       "    time_bounds         (time, hist_interval) object 2094-12-31 00:00:00 ... ...\n",
       "    date_written        (time) |S16 b'02/01/21' b'02/01/21' ... b'02/02/21'\n",
       "    time_written        (time) |S16 b'23:11:01' b'23:25:32' ... b'11:34:39'\n",
       "    TREFMXAV            (time, landunit) float32 ...\n",
       "Attributes: (12/102)\n",
       "    title:                                     CLM History file information\n",
       "    comment:                                   NOTE: None of the variables ar...\n",
       "    Conventions:                               CF-1.0\n",
       "    history:                                   created on 02/01/21 23:11:01\n",
       "    source:                                    Community Land Model CLM4.0\n",
       "    hostname:                                  aleph\n",
       "    ...                                        ...\n",
       "    cft_irrigated_tropical_corn:               62\n",
       "    cft_tropical_soybean:                      63\n",
       "    cft_irrigated_tropical_soybean:            64\n",
       "    time_period_freq:                          day_1\n",
       "    Time_constant_3Dvars_filename:             ./b.e21.BSSP370smbb.f09_g17.LE...\n",
       "    Time_constant_3Dvars:                      ZSOI:DZSOI:WATSAT:SUCSAT:BSW:H...
" ], "text/plain": [ "\n", "Dimensions: (levgrnd: 25, levlak: 10, levdcmp: 25, lon: 288,\n", " lat: 192, gridcell: 21013, landunit: 62125,\n", " column: 554298, pft: 848480, time: 2191,\n", " hist_interval: 2)\n", "Coordinates:\n", " * levgrnd (levgrnd) float32 0.01 0.04 0.09 ... 19.48 28.87 42.0\n", " * levlak (levlak) float32 0.05 0.6 2.1 4.6 ... 25.6 34.33 44.78\n", " * levdcmp (levdcmp) float32 0.01 0.04 0.09 ... 19.48 28.87 42.0\n", " * lon (lon) float32 0.0 1.25 2.5 3.75 ... 356.2 357.5 358.8\n", " * lat (lat) float32 -90.0 -89.06 -88.12 ... 88.12 89.06 90.0\n", " * time (time) object 2095-01-01 00:00:00 ... 2101-01-01 00:0...\n", "Dimensions without coordinates: gridcell, landunit, column, pft, hist_interval\n", "Data variables: (12/45)\n", " area (lat, lon) float32 ...\n", " landfrac (lat, lon) float32 ...\n", " landmask (lat, lon) float64 ...\n", " pftmask (lat, lon) float64 ...\n", " nbedrock (lat, lon) float64 ...\n", " grid1d_lon (gridcell) float64 ...\n", " ... ...\n", " mscur (time) int32 ...\n", " nstep (time) int32 ...\n", " time_bounds (time, hist_interval) object ...\n", " date_written (time) |S16 ...\n", " time_written (time) |S16 ...\n", " TREFMXAV (time, landunit) float32 ...\n", "Attributes: (12/102)\n", " title: CLM History file information\n", " comment: NOTE: None of the variables ar...\n", " Conventions: CF-1.0\n", " history: created on 02/01/21 23:11:01\n", " source: Community Land Model CLM4.0\n", " hostname: aleph\n", " ... ...\n", " cft_irrigated_tropical_corn: 62\n", " cft_tropical_soybean: 63\n", " cft_irrigated_tropical_soybean: 64\n", " time_period_freq: day_1\n", " Time_constant_3Dvars_filename: ./b.e21.BSSP370smbb.f09_g17.LE...\n", " Time_constant_3Dvars: ZSOI:DZSOI:WATSAT:SUCSAT:BSW:H..." ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clm.ds[[\"lat\",\"lon\",\n", " \"land1d_ixy\",\"land1d_jxy\",\"land1d_ityplunit\",\n", " \"land1d_lon\",\"land1d_lat\",\n", " \"landfrac\",\"landmask\",\"land1d_wtgcell\",\"land1d_active\"]].to_netcdf(\"./CESM2_subgrid_info.nc\")\n", "clm.ds" ] }, { "cell_type": "code", "execution_count": 4, "id": "3c275515", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'landunit' (landunit: 62125)>\n",
       "array([    0,     1,     2, ..., 62122, 62123, 62124])\n",
       "Dimensions without coordinates: landunit
" ], "text/plain": [ "\n", "array([ 0, 1, 2, ..., 62122, 62123, 62124])\n", "Dimensions without coordinates: landunit" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clm.ds.landunit" ] }, { "cell_type": "code", "execution_count": 5, "id": "39a703b1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'land1d_ixy' (landunit: 62125)>\n",
       "array([  1,   1,   1, ..., 265, 265, 265], dtype=int32)\n",
       "Dimensions without coordinates: landunit\n",
       "Attributes:\n",
       "    long_name:  2d longitude index of corresponding landunit
" ], "text/plain": [ "\n", "array([ 1, 1, 1, ..., 265, 265, 265], dtype=int32)\n", "Dimensions without coordinates: landunit\n", "Attributes:\n", " long_name: 2d longitude index of corresponding landunit" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clm.ds.land1d_ixy" ] }, { "cell_type": "code", "execution_count": 6, "id": "9887da01", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'land1d_jxy' (landunit: 62125)>\n",
       "array([  1,   1,   1, ..., 186, 186, 186], dtype=int32)\n",
       "Dimensions without coordinates: landunit\n",
       "Attributes:\n",
       "    long_name:  2d latitude index of corresponding landunit
" ], "text/plain": [ "\n", "array([ 1, 1, 1, ..., 186, 186, 186], dtype=int32)\n", "Dimensions without coordinates: landunit\n", "Attributes:\n", " long_name: 2d latitude index of corresponding landunit" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clm.ds.land1d_jxy" ] } ], "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.8.0" } }, "nbformat": 4, "nbformat_minor": 5 }