{ "cells": [ { "cell_type": "markdown", "id": "5f02ac0c", "metadata": {}, "source": [ "# Convert DEPHY forcing to UCLALES-SALSA forcing\n", "\n", "Based on the example by Tim Juliano from NCAR (last updated on 12/1/23)\n" ] }, { "cell_type": "markdown", "id": "8164704b", "metadata": {}, "source": [ "### Import libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "b6e2c74e", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "from netCDF4 import Dataset\n", "import os\n", "import datetime as dt" ] }, { "cell_type": "markdown", "id": "e68367d4-8f12-4f95-aeef-87f036044975", "metadata": { "tags": [] }, "source": [ "## Specify local write directory\n", "\n", "If on the ARM JupyterHub, it is recommended to create and specify a local directory that is outside of the COMBLE-MIP repository to write input files in your model's format\n", "\n", "If on the ARM JupyterHub, these files can be conveniently downloaded to your local machine and will remain when you log out" ] }, { "cell_type": "code", "execution_count": 6, "id": "1d3ba6b7-b104-41ec-83a5-dd0f7a331e3a", "metadata": { "tags": [] }, "outputs": [], "source": [ "my_write_dir = '../../../inputs/'" ] }, { "cell_type": "markdown", "id": "e495d40a-bcec-4a67-812d-88ff33d51f3e", "metadata": { "tags": [] }, "source": [ "## Read the COMBLE-MIP forcing file\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "1190a330-4b1d-4b69-a2db-3cadc1712b26", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:        (time: 21, lev: 136, zw_grid: 160)\n",
       "Coordinates:\n",
       "    t0             datetime64[ns] 2020-03-12T22:00:00\n",
       "    lat            float32 74.5\n",
       "    lon            float32 9.9\n",
       "  * time           (time) datetime64[ns] 2020-03-12T22:00:00 ... 2020-03-13T1...\n",
       "  * lev            (lev) float64 18.17 37.83 59.35 ... 6.851e+04 7.117e+04\n",
       "  * zw_grid        (zw_grid) float64 0.0 20.0 45.0 ... 6.89e+03 6.95e+03 7e+03\n",
       "Data variables: (12/19)\n",
       "    pressure       (lev) float64 ...\n",
       "    u              (lev) float64 ...\n",
       "    v              (lev) float64 ...\n",
       "    temp           (lev) float64 ...\n",
       "    theta          (lev) float64 ...\n",
       "    qv             (lev) float64 ...\n",
       "    ...             ...\n",
       "    theta_nudging  (time, lev) float64 ...\n",
       "    qv_nudging     (time, lev) float64 ...\n",
       "    w_nudging      (time, lev) float64 ...\n",
       "    ts             (time) float64 ...\n",
       "    lat_ref        (time) float32 ...\n",
       "    lon_ref        (time) float32 ...\n",
       "Attributes: (12/30)\n",
       "    title:                          Forcing and initial conditions for 13 Mar...\n",
       "    reference:                      https://arm-development.github.io/comble-...\n",
       "    authors:                        Timothy W. Juliano (NCAR/RAL, tjuliano@uc...\n",
       "    version:                        Created on 2024-01-24\n",
       "    format_version:                 DEPHY SCM format version 2.0\n",
       "    script:                         create_comble_forcing_v2.4.ipynb\n",
       "    ...                             ...\n",
       "    lat:                            74.5 deg N\n",
       "    droplet_activation_diagnostic:  droplet number concentration fixed to 20 ...\n",
       "    ice_nucleation_diagnostic:      total ice number concentration fixed to 2...\n",
       "    droplet_activation_prognostic:  time and space varying aerosol initialize...\n",
       "    aerosol_surface_source:         modal emissions based on Jaegle et al. (2...\n",
       "    ice_nucleation_prognostic:      assume that all sea spray mode and half o...
" ], "text/plain": [ "\n", "Dimensions: (time: 21, lev: 136, zw_grid: 160)\n", "Coordinates:\n", " t0 datetime64[ns] 2020-03-12T22:00:00\n", " lat float32 74.5\n", " lon float32 9.9\n", " * time (time) datetime64[ns] 2020-03-12T22:00:00 ... 2020-03-13T1...\n", " * lev (lev) float64 18.17 37.83 59.35 ... 6.851e+04 7.117e+04\n", " * zw_grid (zw_grid) float64 0.0 20.0 45.0 ... 6.89e+03 6.95e+03 7e+03\n", "Data variables: (12/19)\n", " pressure (lev) float64 ...\n", " u (lev) float64 ...\n", " v (lev) float64 ...\n", " temp (lev) float64 ...\n", " theta (lev) float64 ...\n", " qv (lev) float64 ...\n", " ... ...\n", " theta_nudging (time, lev) float64 ...\n", " qv_nudging (time, lev) float64 ...\n", " w_nudging (time, lev) float64 ...\n", " ts (time) float64 ...\n", " lat_ref (time) float32 ...\n", " lon_ref (time) float32 ...\n", "Attributes: (12/30)\n", " title: Forcing and initial conditions for 13 Mar...\n", " reference: https://arm-development.github.io/comble-...\n", " authors: Timothy W. Juliano (NCAR/RAL, tjuliano@uc...\n", " version: Created on 2024-01-24\n", " format_version: DEPHY SCM format version 2.0\n", " script: create_comble_forcing_v2.4.ipynb\n", " ... ...\n", " lat: 74.5 deg N\n", " droplet_activation_diagnostic: droplet number concentration fixed to 20 ...\n", " ice_nucleation_diagnostic: total ice number concentration fixed to 2...\n", " droplet_activation_prognostic: time and space varying aerosol initialize...\n", " aerosol_surface_source: modal emissions based on Jaegle et al. (2...\n", " ice_nucleation_prognostic: assume that all sea spray mode and half o..." ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dephy_filename = '../forcing/COMBLE_INTERCOMPARISON_FORCING_V2.4.nc'\n", "dephy = xr.open_dataset(dephy_filename)\n", "dephy = dephy.squeeze()\n", "dephy" ] }, { "cell_type": "markdown", "id": "2739f889-df5a-4d01-9829-0c77da22ad73", "metadata": { "tags": [] }, "source": [ "## Create input files for UCLALES-SALSA in ASCII format" ] }, { "cell_type": "markdown", "id": "21479f70", "metadata": {}, "source": [ "### Write input sounding file" ] }, { "cell_type": "code", "execution_count": 8, "id": "0c782307", "metadata": { "tags": [] }, "outputs": [], "source": [ "# use a list to accumulate output ASCII lines\n", "lines_sounding = []\n", "\n", "# number of lines to be read\n", "n_z = dephy.dims['lev']\n", "n_z_les = n_z # Values up to 7000 m\n", "\n", "# arrays to be reported in columns\n", "z_arr = dephy.coords['lev'].values\n", "p_arr = dephy['pressure'].values.squeeze()/100. # hPa = mbar\n", "th_arr = dephy['theta'].values.squeeze() # K\n", "qt_arr = dephy['qv'].values.squeeze()*1000. # g/kg\n", "u_arr = dephy['u'].values.squeeze() # m/s\n", "v_arr = dephy['v'].values.squeeze() # m/s\n", "\n", "ps = dephy['ps'].values.squeeze()/100. # hPa = mbar\n", "#thetas = dephy['thetas'].values.squeeze() # K\n", "\n", "# The first points: surface pressure and then theta, qt, u, and v for the first level \n", "# *below* surface (surface is interpolated). thetas looks a bit low, so set the surface \n", "# to th_arr[0]\n", "#vars_arr = np.array([ps,thetas*2-th_arr[1],qt_arr[0],u_arr[0],v_arr[0]]) # theta is low!\n", "vars_arr = np.array([ps,th_arr[0],qt_arr[0],u_arr[0],v_arr[0]])\n", "str_firstline = np.array2string(vars_arr,formatter={'float_kind':lambda vars_arr:\"%11.5f\" % vars_arr})[1:-1]\n", "lines_sounding.append(str_firstline)\n", "\n", "# do profile\n", "for kk in range(n_z):\n", " vars_arr = np.array([z_arr[kk],th_arr[kk],qt_arr[kk],u_arr[kk],v_arr[kk]])\n", " str_vars = np.array2string(vars_arr,formatter={'float_kind':lambda vars_arr:\"%11.5f\" % vars_arr})[1:-1]\n", " lines_sounding.append(str_vars)\n", " if z_arr[kk]>7000.0:\n", " n_z_les=kk+1\n", " break\n", "\n", "# write list contents to ASCII file\n", "filename_sounding_LES = my_write_dir+'sound_in'\n", "with open(filename_sounding_LES,mode='wt',encoding='utf-8') as sounding_file:\n", " sounding_file.write('\\n'.join(lines_sounding))" ] }, { "cell_type": "markdown", "id": "63382af5", "metadata": {}, "source": [ "### Grid coordinates" ] }, { "cell_type": "code", "execution_count": 9, "id": "b858fb69", "metadata": { "tags": [] }, "outputs": [], "source": [ "# generate vertical grid file (zm_grid_in)\n", "zw_arr = dephy['zw_grid'].values.squeeze() # m\n", "n_zp = dephy.dims['zw_grid']\n", "#\n", "zw_file=open(my_write_dir+'zm_grid_in',mode='wt',encoding='utf-8')\n", "for kk in dephy['zw_grid'].values:\n", " zw_file.write( str(kk) ) \n", " zw_file.write('\\n')\n", "zw_file.close()\n", "\n", "# also zt grid\n", "zw_file=open(my_write_dir+'zt_grid_in',mode='wt',encoding='utf-8')\n", "zw_file.write( str(zw_arr[0]-0.5*(zw_arr[1]-zw_arr[0])) )\n", "for kk in range(n_zp-1):\n", " zw_file.write('\\n')\n", " zw_file.write( str(0.5*(zw_arr[kk+1]+zw_arr[kk])) ) \n", "zw_file.close()" ] }, { "cell_type": "markdown", "id": "b6bbd078", "metadata": {}, "source": [ "### Write background profiles for radiative transfer" ] }, { "cell_type": "code", "execution_count": 10, "id": "30c6ad42", "metadata": { "tags": [] }, "outputs": [], "source": [ "# use a list to accumulate output ASCII lines\n", "lines_pressure = []\n", "\n", "# arrays to be reported in columns\n", "p_arr = dephy['pressure'].values.squeeze() # Pa\n", "p_arr = p_arr[::-1]/100. # hPa\n", "\n", "temp_arr = dephy['temp'].values.squeeze() # K\n", "temp_arr = temp_arr[::-1]\n", "\n", "qt_arr = dephy['qv'].values.squeeze() # kg/kg\n", "qt_arr = qt_arr[::-1]\n", "\n", "o3_arr = dephy['o3'].values.squeeze() # kg kg-1\n", "o3_arr = o3_arr[::-1]\n", "\n", "# add a surface point\n", "str_firstline = (\"%.3f %s\") % (temp_arr[n_z-1],n_z)\n", "lines_pressure.append(str_firstline)\n", "\n", "# do profile\n", "for kk in range(n_z):\n", " str_vars = (\"%11.6f %8.3f %e %e 0\") % (p_arr[kk],temp_arr[kk],qt_arr[kk],o3_arr[kk])\n", " lines_pressure.append(str_vars)\n", "\n", "# write list contents to ASCII file\n", "filename_pressure_LES = my_write_dir+'my.lay'\n", "with open(filename_pressure_LES,mode='wt',encoding='utf-8') as pressure_file:\n", " pressure_file.write('\\n'.join(lines_pressure))" ] }, { "cell_type": "markdown", "id": "ed815e93", "metadata": {}, "source": [ "### Wind forcings" ] }, { "cell_type": "code", "execution_count": 11, "id": "de738f62", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Read data\n", "u_force = dephy['ug'][:,:].values # m/s\n", "v_force = dephy['vg'][:,:].values # m/s\n", "n_t = dephy.dims['time']\n", "\n", "# Open files\n", "forc_file_u=open(my_write_dir+'input_u_forcing.dat',mode='wt',encoding='utf-8')\n", "forc_file_v=open(my_write_dir+'input_v_forcing.dat',mode='wt',encoding='utf-8')\n", "\n", "# The first line: dimensions\n", "forc_file_u.write((\"%i %i \")%(n_z_les,n_t))\n", "forc_file_v.write((\"%i %i \")%(n_z_les,n_t))\n", "\n", "# Note: time is obtained from the sst file\n", "\n", "# Save the data values\n", "for j in range(n_z_les):\n", " forc_file_u.write(\"\\n%10.3f \"%z_arr[j])\n", " forc_file_v.write(\"\\n%10.3f \"%z_arr[j])\n", " for i in range(n_t):\n", " forc_file_u.write(\"%10f \"%u_force[i,j])\n", " forc_file_v.write(\"%10f \"%v_force[i,j])\n", "forc_file_u.close()\n", "forc_file_v.close()" ] }, { "cell_type": "markdown", "id": "45a69b46", "metadata": {}, "source": [ "### Surface temperature forcing" ] }, { "cell_type": "code", "execution_count": 12, "id": "afddafd2", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Read data\n", "ts_arr = dephy['ts'][:].values # K\n", "n_t = dephy.dims['time']\n", "\n", "# Open file\n", "sst_file=open(my_write_dir+'input_sst.dat',mode='wt',encoding='utf-8')\n", "\n", "# The first line: dimensions\n", "sst_file.write(\"%i\"%n_t)\n", "\n", "# Save the data values: elapsed time (s), sst (K)\n", "for kk in range(n_t):\n", " delta = ((dephy['time'][kk]-dephy['time'][0]).dt.seconds).values # time between profiles (s)\n", " sst_file.write((\"\\n%i %10.3f\")%(delta,ts_arr[kk]))\n", "#\n", "sst_file.close()" ] }, { "cell_type": "code", "execution_count": 13, "id": "97f88999", "metadata": { "tags": [] }, "outputs": [], "source": [ "# All done\n", "dephy.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:root] *", "language": "python", "name": "conda-root-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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }