{ "cells": [ { "cell_type": "markdown", "id": "5f02ac0c", "metadata": {}, "source": [ "# Example: convert DEPHY forcing to WRF-LES forcing\n", "\n", "This code reads the COMBLE-MIP LES/SCM forcing file that is supplied in the DEPHY format and writes input files that are formatted for WRF-LES (ASCII and NetCDF)\n", "\n", "Contributed by Tim Juliano from NCAR, last updated on 12/1/23" ] }, { "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, stringtochar\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": 2, "id": "1d3ba6b7-b104-41ec-83a5-dd0f7a331e3a", "metadata": { "tags": [] }, "outputs": [], "source": [ "my_write_dir = '../../../my_input_files/'" ] }, { "cell_type": "markdown", "id": "e495d40a-bcec-4a67-812d-88ff33d51f3e", "metadata": { "tags": [] }, "source": [ "## Read the COMBLE-MIP forcing file\n", "\n", "NOTE: ERROR 1 message can be ignored" ] }, { "cell_type": "code", "execution_count": 3, "id": "1190a330-4b1d-4b69-a2db-3cadc1712b26", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ERROR 1: PROJ: proj_create_from_database: Open of /opt/conda/share/proj failed\n" ] }, { "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/20)\n",
       "    title:                     Forcing and initial conditions for 13 March 20...\n",
       "    reference:                 https://arm-development.github.io/comble-mip/\n",
       "    authors:                   Timothy W. Juliano (NCAR/RAL, tjuliano@ucar.ed...\n",
       "    version:                   Created on 2023-12-01\n",
       "    format_version:            DEPHY SCM format version 2.0\n",
       "    script:                    create_comble_forcing_v2.3.ipynb\n",
       "    ...                        ...\n",
       "    z0h:                       5.5e-6 m\n",
       "    surface_forcing_moisture:  none\n",
       "    z0q:                       5.5e-6 m\n",
       "    surface_forcing_wind:      z0\n",
       "    z0:                        9.0e-4 m\n",
       "    lat:                       74.5 deg N
" ], "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/20)\n", " title: Forcing and initial conditions for 13 March 20...\n", " reference: https://arm-development.github.io/comble-mip/\n", " authors: Timothy W. Juliano (NCAR/RAL, tjuliano@ucar.ed...\n", " version: Created on 2023-12-01\n", " format_version: DEPHY SCM format version 2.0\n", " script: create_comble_forcing_v2.3.ipynb\n", " ... ...\n", " z0h: 5.5e-6 m\n", " surface_forcing_moisture: none\n", " z0q: 5.5e-6 m\n", " surface_forcing_wind: z0\n", " z0: 9.0e-4 m\n", " lat: 74.5 deg N" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dephy_filename = '../forcing/COMBLE_INTERCOMPARISON_FORCING_V2.3.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 WRF-LES in ASCII or NetCDF format\n", "\n", "Write initial sounding, ozone profile, forcing and vertical grid files" ] }, { "cell_type": "markdown", "id": "21479f70", "metadata": {}, "source": [ "### Write input sounding file for WRF LES in ASCII format" ] }, { "cell_type": "code", "execution_count": 4, "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", "# arrays to be reported in columns\n", "z_arr = dephy.coords['lev'].values\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", "ps = dephy['ps'].values.squeeze()/100. # hPa\n", "thetas = dephy['thetas'].values.squeeze() # K\n", "\n", "# add a surface point (required by WRF code)\n", "vars_arr = np.array([ps,thetas,qt_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", "\n", "# write list contents to ASCII file\n", "filename_sounding_LES = 'input_sounding'\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": "b6bbd078", "metadata": {}, "source": [ "### Write ozone file for RRTMG\n", "#### Pressure file" ] }, { "cell_type": "code", "execution_count": 5, "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", "# do profile\n", "for kk in range(n_z):\n", " vars_arr = np.array([p_arr[kk]])\n", " str_vars = np.array2string(vars_arr,formatter={'float_kind':lambda vars_arr:\"%11.4f\" % vars_arr})[1:-1]\n", " lines_pressure.append(str_vars)\n", "\n", "# write list contents to ASCII file\n", "filename_pressure_LES = 'ozone_plev.formatted'\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": "84b93484", "metadata": {}, "source": [ "#### Ozone file" ] }, { "cell_type": "code", "execution_count": 6, "id": "1669a64c", "metadata": { "tags": [] }, "outputs": [], "source": [ "# use a list to accumulate output ASCII lines\n", "lines_ozone = []\n", "\n", "n_months = 12\n", "n_lat = 64\n", "\n", "# arrays to be reported in columns\n", "o3_arr = dephy['o3'].values.squeeze() # kg kg-1\n", "o3_arr = o3_arr[::-1]\n", "\n", "# do profile\n", "for ii in range(n_months):\n", " for jj in range(n_lat):\n", " for kk in range(n_z):\n", " vars_arr = np.array([o3_arr[kk]])\n", " str_vars = np.array2string(vars_arr,formatter={'float_kind':lambda vars_arr:\"%.4E\" % vars_arr})[1:-1]\n", " lines_ozone.append(str_vars)\n", "\n", "# write list contents to ASCII file\n", "filename_ozone_LES = 'ozone.formatted'\n", "with open(filename_ozone_LES,mode='wt',encoding='utf-8') as ozone_file:\n", " ozone_file.write('\\n'.join(lines_ozone))" ] }, { "cell_type": "markdown", "id": "cb8d2632-b3e5-4136-afea-83b28dfea711", "metadata": {}, "source": [ "### Write forcing file" ] }, { "cell_type": "markdown", "id": "ed815e93", "metadata": {}, "source": [ "#### Read forcing winds from forcing file" ] }, { "cell_type": "code", "execution_count": 7, "id": "a983e193", "metadata": { "tags": [] }, "outputs": [], "source": [ "u_force = dephy['ug'][:,:].values # m/s\n", "v_force = dephy['vg'][:,:].values # m/s" ] }, { "cell_type": "markdown", "id": "0b5c57b2", "metadata": {}, "source": [ "#### Repeat z_ls 1D array to be 2D array for writing" ] }, { "cell_type": "code", "execution_count": 8, "id": "44ebe1b5", "metadata": { "tags": [] }, "outputs": [], "source": [ "nhrs = dephy.dims['time']\n", "z_ls_write = np.tile(z_arr,(np.shape(u_force)[0],1))[0:nhrs,:]" ] }, { "cell_type": "markdown", "id": "079e9382", "metadata": {}, "source": [ "#### Compute wind forcing tendency" ] }, { "cell_type": "code", "execution_count": 9, "id": "54997b1a-f0be-47d3-84b2-60adbd2b3587", "metadata": { "tags": [] }, "outputs": [], "source": [ "delt = ((dephy['time'][1]-dephy['time'][0]).dt.seconds).values # time between profiles (s)\n", "\n", "u_tend = np.zeros([np.shape(u_force)[0],np.shape(u_force)[1]])\n", "v_tend = np.zeros([np.shape(u_force)[0],np.shape(u_force)[1]])\n", "z_tend = np.zeros([np.shape(u_force)[0],np.shape(u_force)[1]])\n", "\n", "u_tend[0:-1,:] = (u_force[1:,:] - u_force[0:-1,:]) / delt\n", "u_tend[-1,:] = u_tend[-2,:]\n", "v_tend[0:-1,:] = (v_force[1:,:] - v_force[0:-1,:]) / delt\n", "v_tend[-1,:] = v_tend[-2,:]\n", "z_tend[0:-1,:] = (z_ls_write[1:,:] - z_ls_write[0:-1,:]) / delt\n", "z_tend[-1,:] = z_tend[-2,:]" ] }, { "cell_type": "markdown", "id": "80f9c481", "metadata": {}, "source": [ " #### Forcing NetCDF name and delete file if already exists" ] }, { "cell_type": "code", "execution_count": 10, "id": "45aba81f", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The file input_ls_forcing.nc has been deleted successfully\n" ] } ], "source": [ "savename = 'input_ls_forcing.nc'\n", "\n", "if os.path.exists(savename):\n", " os.remove(savename)\n", " print('The file ' + savename + ' has been deleted successfully')" ] }, { "cell_type": "markdown", "id": "0a1f723b", "metadata": {}, "source": [ "#### Create new netcdf file" ] }, { "cell_type": "code", "execution_count": 11, "id": "3a6b159c", "metadata": { "tags": [] }, "outputs": [], "source": [ "try: ncfile.close() # just to be safe, make sure dataset is not already open.\n", "except: pass\n", "ncfile = Dataset('./' + savename,mode='w',format='NETCDF3_CLASSIC') \n", "#print(ncfile)" ] }, { "cell_type": "markdown", "id": "8055d4c5", "metadata": {}, "source": [ "#### Create dimensions" ] }, { "cell_type": "code", "execution_count": 12, "id": "c86aede0", "metadata": {}, "outputs": [], "source": [ "levs = np.shape(u_force)[1]\n", "lev_dim = ncfile.createDimension('force_layers', levs) # level axis\n", "time_dim = ncfile.createDimension('Time', None) # unlimited axis (can be appended to)\n", "datestrlen = ncfile.createDimension('DateStrLen', 19)" ] }, { "cell_type": "markdown", "id": "d13d94a3", "metadata": {}, "source": [ "#### Create global attributes" ] }, { "cell_type": "code", "execution_count": 13, "id": "0c591ccd", "metadata": {}, "outputs": [], "source": [ "ncfile.title='AUXILIARY FORCING FOR CRM'" ] }, { "cell_type": "markdown", "id": "b878c0e0", "metadata": {}, "source": [ "#### Create variables" ] }, { "cell_type": "markdown", "id": "bcdf2879", "metadata": {}, "source": [ "##### Dimensions" ] }, { "cell_type": "code", "execution_count": 14, "id": "69e94eb6", "metadata": { "tags": [] }, "outputs": [], "source": [ "time = ncfile.createVariable('Times', 'S1', ('Time','DateStrLen',))" ] }, { "cell_type": "markdown", "id": "5b5f44d6", "metadata": {}, "source": [ "### Add times" ] }, { "cell_type": "code", "execution_count": 15, "id": "3b2cc02d-0106-4d92-8553-08ebf11feb02", "metadata": { "tags": [] }, "outputs": [], "source": [ "for i in np.arange(len(dephy['time'].values)):\n", " yyyy = pd.to_datetime(dephy['time'].values[i]).year\n", " m = pd.to_datetime(dephy['time'].values[i]).month\n", " dd = pd.to_datetime(dephy['time'].values[i]).day\n", " hh = pd.to_datetime(dephy['time'].values[i]).hour\n", " hold = dt.datetime(yyyy,m,dd,hh)\n", " hold2 = hold.strftime('%Y-%m-%d_%H:%M:%S')\n", " \n", " time[i,:] = stringtochar(np.array(hold2, 'S19'))" ] }, { "cell_type": "markdown", "id": "747f3205", "metadata": {}, "source": [ "#### Time-varying wind forcing profiles" ] }, { "cell_type": "code", "execution_count": 16, "id": "a4864508", "metadata": {}, "outputs": [], "source": [ "u_ls = ncfile.createVariable('U_LS', np.float32, ('Time','force_layers',),fill_value=-999.)\n", "u_ls.FieldType = 104\n", "u_ls.MemoryOrder = 'Z '\n", "u_ls.description = 'large scale zonal wind velocity'\n", "u_ls.units = ''\n", "u_ls.stagger = ''\n", "\n", "v_ls = ncfile.createVariable('V_LS', np.float32, ('Time','force_layers',),fill_value=-999.)\n", "v_ls.FieldType = 104\n", "v_ls.MemoryOrder = 'Z '\n", "v_ls.description = 'large scale meridional wind velocity'\n", "v_ls.units = ''\n", "v_ls.stagger = ''\n", "\n", "z_ls = ncfile.createVariable('Z_LS', np.float32, ('Time','force_layers',),fill_value=-999.)\n", "z_ls.FieldType = 104\n", "z_ls.MemoryOrder = 'Z '\n", "z_ls.description = 'height of forcing time series'\n", "z_ls.units = ''\n", "z_ls.stagger = ''\n", "\n", "u_ls_tend = ncfile.createVariable('U_LS_TEND', np.float32, ('Time','force_layers',),fill_value=-999.)\n", "u_ls_tend.FieldType = 104\n", "u_ls_tend.MemoryOrder = 'Z '\n", "u_ls_tend.description = 'tendency of zonal wind'\n", "u_ls_tend.units = ''\n", "u_ls_tend.stagger = ''\n", "\n", "v_ls_tend = ncfile.createVariable('V_LS_TEND', np.float32, ('Time','force_layers',),fill_value=-999.)\n", "v_ls_tend.FieldType = 104\n", "v_ls_tend.MemoryOrder = 'Z '\n", "v_ls_tend.description = 'tendency of meridional wind'\n", "v_ls_tend.units = ''\n", "v_ls_tend.stagger = ''\n", "\n", "z_ls_tend = ncfile.createVariable('Z_LS_TEND', np.float32, ('Time','force_layers',),fill_value=-999.)\n", "z_ls_tend.FieldType = 104\n", "z_ls_tend.MemoryOrder = 'Z '\n", "z_ls_tend.description = 'height of forcing time series'\n", "z_ls_tend.units = ''\n", "z_ls_tend.stagger = ''" ] }, { "cell_type": "markdown", "id": "e9cfe73f", "metadata": {}, "source": [ "### Write data" ] }, { "cell_type": "code", "execution_count": 17, "id": "5de5a3f2", "metadata": {}, "outputs": [], "source": [ "u_ls[:] = u_force\n", "v_ls[:] = v_force\n", "z_ls[:] = z_ls_write\n", "u_ls_tend[:] = u_tend\n", "v_ls_tend[:] = v_tend\n", "z_ls_tend[:] = z_tend" ] }, { "cell_type": "markdown", "id": "85f96413", "metadata": {}, "source": [ "### Close the file" ] }, { "cell_type": "code", "execution_count": 18, "id": "0415a82a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "root group (NETCDF3_CLASSIC data model, file format NETCDF3):\n", " title: AUXILIARY FORCING FOR CRM\n", " dimensions(sizes): force_layers(136), Time(21), DateStrLen(19)\n", " variables(dimensions): |S1 Times(Time, DateStrLen), float32 U_LS(Time, force_layers), float32 V_LS(Time, force_layers), float32 Z_LS(Time, force_layers), float32 U_LS_TEND(Time, force_layers), float32 V_LS_TEND(Time, force_layers), float32 Z_LS_TEND(Time, force_layers)\n", " groups: \n", "Dataset is closed!\n" ] } ], "source": [ "# first print the Dataset object to see what we've got\n", "print(ncfile)\n", "# close the Dataset.\n", "ncfile.close(); print('Dataset is closed!')" ] }, { "cell_type": "markdown", "id": "45a69b46", "metadata": {}, "source": [ "### Write surface forcing file for WRF LES in netCDF format" ] }, { "cell_type": "markdown", "id": "3ce00f4e", "metadata": {}, "source": [ "#### Read skin temperature from forcing file" ] }, { "cell_type": "code", "execution_count": 19, "id": "29c286d4", "metadata": {}, "outputs": [], "source": [ "ts_arr = dephy['ts'][:].values # K" ] }, { "cell_type": "markdown", "id": "47087937", "metadata": {}, "source": [ "#### Compute skin temperature tendency" ] }, { "cell_type": "code", "execution_count": 20, "id": "6afc5f32", "metadata": {}, "outputs": [], "source": [ "ts_tend = np.zeros(nhrs)\n", "\n", "ts_tend[0:-1] = (ts_arr[1:] - ts_arr[0:-1]) / delt\n", "ts_tend[-1] = ts_tend[-2]" ] }, { "cell_type": "markdown", "id": "785f25b5", "metadata": {}, "source": [ " #### Forcing NetCDF name and delete file if already exists" ] }, { "cell_type": "code", "execution_count": 21, "id": "83cf680b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The file input_sfc_forcing.nc has been deleted successfully\n" ] } ], "source": [ "savename = 'input_sfc_forcing.nc'\n", "\n", "if os.path.exists(savename):\n", " os.remove(savename)\n", " print('The file ' + savename + ' has been deleted successfully')" ] }, { "cell_type": "markdown", "id": "379ab4d9", "metadata": {}, "source": [ "#### Create new netcdf file" ] }, { "cell_type": "code", "execution_count": 22, "id": "b03d06bb", "metadata": {}, "outputs": [], "source": [ "try: ncfile.close() # just to be safe, make sure dataset is not already open.\n", "except: pass\n", "ncfile = Dataset('./' + savename,mode='w',format='NETCDF3_CLASSIC') \n", "#print(ncfile)" ] }, { "cell_type": "markdown", "id": "c9a54a88", "metadata": {}, "source": [ "#### Create dimensions" ] }, { "cell_type": "code", "execution_count": 23, "id": "e994f3f5", "metadata": {}, "outputs": [], "source": [ "time_dim = ncfile.createDimension('Time', None) # unlimited axis (can be appended to)\n", "datestrlen = ncfile.createDimension('DateStrLen', 19)" ] }, { "cell_type": "markdown", "id": "ace6f6a8", "metadata": {}, "source": [ "#### Create global attributes" ] }, { "cell_type": "code", "execution_count": 24, "id": "93fadb71", "metadata": {}, "outputs": [], "source": [ "ncfile.title='AUXILIARY FORCING FOR CRM'" ] }, { "cell_type": "markdown", "id": "8cb024e4", "metadata": {}, "source": [ "#### Create variables" ] }, { "cell_type": "markdown", "id": "2235eefe", "metadata": {}, "source": [ "#### Dimensions" ] }, { "cell_type": "code", "execution_count": 25, "id": "6b448fa5", "metadata": {}, "outputs": [], "source": [ "time = ncfile.createVariable('Times', 'S1', ('Time','DateStrLen',))" ] }, { "cell_type": "markdown", "id": "06cad8cd", "metadata": {}, "source": [ "#### Add times" ] }, { "cell_type": "code", "execution_count": 26, "id": "4674da1f", "metadata": {}, "outputs": [], "source": [ "for i in np.arange(len(dephy['time'].values)):\n", " yyyy = pd.to_datetime(dephy['time'].values[i]).year\n", " m = pd.to_datetime(dephy['time'].values[i]).month\n", " dd = pd.to_datetime(dephy['time'].values[i]).day\n", " hh = pd.to_datetime(dephy['time'].values[i]).hour\n", " hold = dt.datetime(yyyy,m,dd,hh)\n", " hold2 = hold.strftime('%Y-%m-%d_%H:%M:%S')\n", " \n", " time[i,:] = stringtochar(np.array(hold2, 'S19'))" ] }, { "cell_type": "markdown", "id": "959e74ef", "metadata": {}, "source": [ "#### Time-varying skin temperature" ] }, { "cell_type": "code", "execution_count": 27, "id": "828da8b1", "metadata": {}, "outputs": [], "source": [ "pre_tsk = ncfile.createVariable('PRE_TSK', np.float32, ('Time',),fill_value=-999.)\n", "pre_tsk.FieldType = 104\n", "pre_tsk.MemoryOrder = 'Z '\n", "pre_tsk.description = 'skin temperature'\n", "pre_tsk.units = ''\n", "pre_tsk.stagger = ''\n", "\n", "pre_tsk_tend = ncfile.createVariable('PRE_TSK_TEND', np.float32, ('Time',),fill_value=-999.)\n", "pre_tsk_tend.FieldType = 104\n", "pre_tsk_tend.MemoryOrder = 'Z '\n", "pre_tsk_tend.description = 'skin temperature'\n", "pre_tsk_tend.units = ''\n", "pre_tsk_tend.stagger = ''" ] }, { "cell_type": "markdown", "id": "b1b5b9a5", "metadata": {}, "source": [ "#### Write data" ] }, { "cell_type": "code", "execution_count": 28, "id": "19798da0", "metadata": {}, "outputs": [], "source": [ "pre_tsk[:] = ts_arr\n", "pre_tsk_tend[:] = ts_tend" ] }, { "cell_type": "markdown", "id": "df1f3f8f", "metadata": {}, "source": [ "#### Close the file" ] }, { "cell_type": "code", "execution_count": 29, "id": "740f49db", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "root group (NETCDF3_CLASSIC data model, file format NETCDF3):\n", " title: AUXILIARY FORCING FOR CRM\n", " dimensions(sizes): Time(21), DateStrLen(19)\n", " variables(dimensions): |S1 Times(Time, DateStrLen), float32 PRE_TSK(Time), float32 PRE_TSK_TEND(Time)\n", " groups: \n", "Dataset is closed!\n" ] } ], "source": [ "# first print the Dataset object to see what we've got\n", "print(ncfile)\n", "# close the Dataset.\n", "ncfile.close(); print('Dataset is closed!')" ] }, { "cell_type": "markdown", "id": "f912dabb", "metadata": {}, "source": [ "### Write vertical grid file for WRF LES in ASCII format" ] }, { "cell_type": "code", "execution_count": 30, "id": "1998478b", "metadata": { "tags": [] }, "outputs": [], "source": [ "# create vertical grid file contents as list\n", "lines_zw = []\n", "\n", "n_zw = n_z = dephy.dims['zw_grid'] # number of heights\n", "\n", "for kk in range(n_zw):\n", " lines_zw.append(str(dephy['zw_grid'][kk].values))\n", "\n", "# write vertical grid to ASCII file\n", "filename_zw_LES = 'input_zw_grid'\n", "with open(filename_zw_LES,mode='wt',encoding='utf-8') as zw_file:\n", " zw_file.write('\\n'.join(lines_zw))" ] }, { "cell_type": "code", "execution_count": null, "id": "a790d6c9", "metadata": {}, "outputs": [], "source": [] } ], "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }