Define maximum layer thicknesses H_max(z)#

from mom6_tools.m6plot import xyplot, myStats
import xarray as xr
import numpy as np
import matplotlib
matplotlib.rcParams.update({'font.size': 16})
import matplotlib.colors as colors
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from datetime import datetime

%matplotlib inline
# The following parameters must be set accordingly
######################################################
# case name - must be changed for each configuration
grid_name = 'tx2_3v2'
# Path to the grid
case = 'baseline'
# add your name and email address below
author = 'Gustavo Marques (gmarques@ucar.edu)'
# MAX_LAYER_THICKNESS in GFDL's OM4, for comparison purposes

max_h = np.array([400.0, 409.63, 410.32, 410.75, 411.07, 411.32, 411.52, 411.7, 
                  411.86, 412.0, 412.13, 412.24, 412.35, 412.45, 412.54, 412.63, 
                  412.71, 412.79, 412.86, 412.93, 413.0, 413.06, 413.12, 413.18,
                  413.24, 413.29, 413.34, 413.39, 413.44, 413.49, 413.54, 413.58,
                  413.62, 413.67, 413.71, 413.75, 413.78, 413.82, 413.86, 413.9, 
                  413.93, 413.97, 414.0, 414.03, 414.06, 414.1, 414.13, 414.16, 
                  414.19, 414.22, 414.24, 414.27, 414.3, 414.33, 414.35, 414.38, 
                  414.41, 414.43, 414.46, 414.48, 414.51, 414.53, 414.55, 414.58, 
                  414.6, 414.62, 414.65, 414.67, 414.69, 414.71, 414.73, 414.75, 
                  414.77, 414.79, 414.83])
path = '/glade/derecho/scratch/gmarques/g.e23_a16g.GJRAv4.TL319_t232_hycom1_N75.2024.hycom1_exploration_N75/run/'
grd = xr.open_dataset(path + case+'/g.e23_a16g.GJRAv4.TL319_t232_hycom1_N75.2024.hycom1_exploration_N75.mom6.h.static.nc')
# load interfaces zstar
dz = xr.open_dataset(path + case+'/Vertical_coordinate.nc')['Layer']
eta = xr.open_dataset(path + case+'/Vertical_coordinate.nc')['Interface']
dz
<xarray.DataArray 'Layer' (Layer: 75)>
array([1.250000e+00, 3.750000e+00, 6.250000e+00, 8.750000e+00, 1.125000e+01,
       1.375000e+01, 1.625000e+01, 1.875000e+01, 2.125000e+01, 2.375000e+01,
       2.625000e+01, 2.875000e+01, 3.125000e+01, 3.375000e+01, 3.625000e+01,
       3.875000e+01, 4.125000e+01, 4.375000e+01, 4.625000e+01, 4.875000e+01,
       5.125000e+01, 5.375000e+01, 5.625000e+01, 5.875000e+01, 6.125000e+01,
       6.375000e+01, 6.625000e+01, 6.875000e+01, 7.125000e+01, 7.375000e+01,
       7.625000e+01, 7.875000e+01, 8.125000e+01, 8.375000e+01, 8.625000e+01,
       8.876372e+01, 9.130488e+01, 9.387348e+01, 9.646951e+01, 9.909299e+01,
       1.017439e+02, 1.044253e+02, 1.071894e+02, 1.101483e+02, 1.134005e+02,
       1.170264e+02, 1.211252e+02, 1.258301e+02, 1.313104e+02, 1.377775e+02,
       1.455000e+02, 1.548061e+02, 1.661100e+02, 1.799286e+02, 1.968920e+02,
       2.177837e+02, 2.435695e+02, 2.754256e+02, 3.147966e+02, 3.634415e+02,
       4.234923e+02, 4.975393e+02, 5.887171e+02, 7.007975e+02, 8.383154e+02,
       1.006720e+03, 1.212538e+03, 1.463554e+03, 1.769054e+03, 2.140079e+03,
       2.589734e+03, 3.133543e+03, 3.789850e+03, 4.580300e+03, 5.505995e+03])
Coordinates:
  * Layer    (Layer) float64 1.25 3.75 6.25 8.75 ... 3.79e+03 4.58e+03 5.506e+03
Attributes:
    long_name:       Layer z-rho
    units:           meter
    cartesian_axis:  Z
    positive:        up
# load CESM hybrid target densities at interfaces
vgrid = 'hybrid_75layer_zstar2.50m-2020-11-23.nc'
sigma2 = xr.open_dataset(path + 'INPUT/'+vgrid)['sigma2']
dz = xr.open_dataset(path + 'INPUT/'+vgrid)['dz']

ICs#

fname = path + case+'/MOM_IC.nc'
ics =  xr.open_dataset(fname).drop('Time')
hist_name = 'g.e23_a16g.GJRAv4.TL319_t232_hycom1_N75.2024.hycom1_exploration_N75.mom6.h.native.0001-01.nc'

fname = path + case+'/' + hist_name
ds =  xr.open_dataset(fname).isel(time=3)
def get_quantile(da, quantile=0.95, dims=['yh','xh']):
    threshold = da.quantile(quantile, dim=dims)
    # Step 2: Filter the array to find values greater than or equal to the threshold
    da_quantile = da.where(da >= threshold, drop=False)  
    #top_5_percent_values = da_quantile.dropna(dim="xh", how="any")

    return da_quantile
matplotlib.rcParams.update({'font.size': 16})

fig, ax = plt.subplots(figsize=(10,8))
for k in range(len(ics.Layer)):
    data = ics.h[0,k,:].values.flatten()
    if k == 0:
      z = 0.5 * ics.h[0,k,:].values
    else:
      z = ics.h[0,0:k-1,:].sum(dim=['Layer'], skipna=False).values + \
          0.5 * ics.h[0,k,:].values
        
    sc = ax.scatter(data, np.ones(len(data))*(k),
                    c=z, s=10, 
                    norm=colors.LogNorm(vmin=10, vmax=6000.), cmap='RdYlBu_r')

ax.plot(dz.values, range(len(dz)), 'k-', label='dz')
ax.plot(max_h, range(len(dz)), 'k--', label='dz_max')
#ax.legend()
ax.invert_yaxis()
ax.set_xlim(-5,450)
plt.colorbar(sc)
plt.xlabel('Layer thickness [m]')
plt.ylabel('Layer index')
plt.grid()
../_images/945daf88c16779febe8ef5518385799404c1f4021dae92b380c1c55acc7cbf94.png

Maximum thicknesses based on the values above the 90% quantile#

fig, ax = plt.subplots(figsize=(10,8))
for k in range(len(ics.Layer)):
    data = get_quantile(ics.h[0,k,:], quantile=0.9, dims=['lath','lonh'])
    data = data.values.flatten()
    if k == 0:
      z = 0.5 * ics.h[0,k,:].values
    else:
      z = ics.h[0,0:k-1,:].sum(dim=['Layer'], skipna=False).values + \
          0.5 * ics.h[0,k,:].values
        
    sc = ax.scatter(data, np.ones(len(data))*(k),
                    c=z, s=10, 
                    norm=colors.LogNorm(vmin=10, vmax=6000.), cmap='RdYlBu_r')

ax.plot(dz.values, range(len(dz)), 'k-', label='dz')
ax.plot(max_h, range(len(dz)), 'k--', label='dz_max')
#ax.legend()
ax.invert_yaxis()
ax.set_xlim(-5,450)
plt.colorbar(sc)
plt.xlabel('Layer thickness [m]')
plt.ylabel('Layer index')
plt.title('Values >= than the 90th quantile')
plt.grid()
../_images/98cbdde46ccbc3498f848f420362319c5fbb82f31ac0083769085760a9be1afb.png

Maps#

bins = np.arange(10, 200, 10)
Mean_90 = np.zeros(len(ics.Layer))
Std_90 = np.zeros(len(ics.Layer))
RMS_90 = np.zeros(len(ics.Layer))

for k in range(len(ics.Layer)):
#for k in range(20):

    data = get_quantile(ics.h[0,k,:], quantile=0.9, dims=['lath','lonh'])
    data1 = np.ma.masked_invalid(data.values)# * grd.wet.values
    Min, Max, Mean_90[k], Std_90[k], RMS_90[k] = myStats(data1, grd.area_t.values)
    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(24,6.5))
    title1 = 'h >= 90th quantile for Layer={}'.format(k)
    xyplot(np.ma.masked_where(grd.wet.values == 0., data1), grd.geolon.values, grd.geolat.values, grd.area_t.values, title=title1, 
           clim=(5., 50.), axis=ax[0], nbins=20, sigma=5)
    title2 = 'h for Layer={}'.format(k)
    #data2 = np.ma.masked_invalid(ics.h[0,k,:].values) #* grd.wet.values
    
    data2 = ics.h[0,k,:].values
    xyplot(np.ma.masked_where(grd.wet.values == 0., data2), grd.geolon.values, grd.geolat.values, grd.area_t.values, title=title2, 
           axis=ax[1], nbins=20, sigma=5)
    plt.subplots_adjust(top = 0.8)

    fig, ax = plt.subplots(figsize=(2.5,2.5))
    ics.h[0,k,:].plot.hist(ax=ax, bins=bins)
    title3 = 'Histogram Layer={}'.format(k)
    ax.set_title(title3)
/glade/derecho/scratch/gmarques/tmp/ipykernel_2588/956470891.py:12: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(24,6.5))
../_images/605d110e78ce7cbab5023ead7e5b91c79538a5fbcb24f1cb9c6611f11b655c44.png ../_images/cf021f700e5e793e799612cba6242afc1e8bb49c208fb10ad1bffcbd60477c5a.png ../_images/4f167191382fa2db01d2099fe32bb0a1234f4162bc70b5c022722642ff89c76d.png ../_images/7bfe455b6aa1abcd278929c910e53e8c3e9e8b540379e5145f87edd910588e95.png ../_images/adf1fb18676e8986c8c8fb772a2af3618497d009c1018c2db5f777c50eff64ec.png ../_images/431d36070e50fecad0150f33a1bc04748305486c0122d9bd81b3b3178f3067c4.png ../_images/14aa43204f4fed860ef15900722963b1d9fb1e2b7c9d2da1227f72e0a399ec0a.png ../_images/0d85f19541d6d2e5fab214650ae89f0fe2c2a31bc25f3043255ab6a1e83be3e7.png ../_images/f709fcfb962e977c1e4b606b716699cfcf271441d3a97897cb8e47d72bc69cca.png ../_images/a4165c203195145d2ecd9b36adb450fc855dd374b78007eadbddf818b3ce6ab7.png ../_images/4ceb3bc45b24fb3056e0d8ac1d1fcd2db84e71f73e2fb80dd550b37e3a828c4b.png ../_images/9744fc60afc6b846df4b687162e4462ddeead7e1de44488b29d69f30c1140063.png ../_images/9d4ad6291db058061be99a5082643cdd507da36456d503d71d4e4cacdae46942.png ../_images/c85a30a1291770c6a610f6a928cd427977661f20429b9e9063a5f7e41d419c30.png ../_images/2d7ef19b52c046f7d5b2d2759fa2137f2c23ec771298e75a9cbb60127bdef7d9.png ../_images/674cb1eefc69e94602e6400dca574c18a13accdac4746321e158e2e38ba219c7.png ../_images/98b0cff43a04f6372961be30ff458bb2b5b2bc2d2ba4aadd4c47d044a7e6ca7b.png ../_images/3893f2756406a0c9f75127acffd31511b09a99b70f7ab6981049998d1a473da6.png ../_images/00a4c3e040a08c4b58d07081fd39641b7ef171d2abff44ebd4b070334bdcda39.png ../_images/0a0be517471ca26425dcb3ef10d4cd08e40a8daf8343f25ff5938c726ecaf0d9.png ../_images/611baf27565b75309bf4d939d16fcd82cd8522dd8896188d3545232feca24cae.png ../_images/252b0e2a238e779e6708eb4990218eb9bd40444a3759fc69ab4761c8c4d4c59c.png ../_images/33fef71030efd391cb8389b9c0418786561d1fba813e1c7e575a7ed452359447.png ../_images/a57af62d2d1635ac8b17233335067afe361b9f00a4135893517031ffc1b894a6.png ../_images/8e654299fe13355446084b993475011baad5d6e2eb7afb3cd6f560ffb4c212bc.png ../_images/bacf61ac269def2a72b019fd4d7bc25c5b01bbb6835b930a5869fe4576fca733.png ../_images/c138fa0f18efe0d781864e59209a06c65553f4aa90cd56691d223ffc4492a398.png ../_images/27e2c92e32764bfc6216d46dfe0c52ca8d446075c41ecf8f58e47a57e1fe7721.png ../_images/9b687d9d729d4b6f9bb20811735067017d25620c0fc817ef618491879c7b13e6.png ../_images/10eb6541704d85bc5f239808972a7862495bdf84d55d690bd7316101e9038898.png ../_images/439c64d737ea1b8d5a0794fda660334f3c566a78395c48d55cf0afcb2db3e11e.png ../_images/a9693e44d4b97d33372d28a612228b4bdf0d01b00c72b02dded769192595acc3.png ../_images/f3b3ffd51aa9ada7ac370a8c8ffde15d115f0628c1afed1acf155ea245edc3af.png ../_images/aae0776caf939fa5a2a0a1d752a8cc329f460e0257e420d40ee2ec69c1e89574.png ../_images/c60ececa96d79524e2a5ebffde61daf00f82e6fd5de90369f53eaec24795860b.png ../_images/8ee7f0b0e0af3a54cd3b2a039f8ba46c5de2783facbb57909e968a572fe70f7e.png ../_images/2a800bf652b87631589cd36ea9b28bb7a824c4c4f24049010267bfcdae52b7c8.png ../_images/bd64b7795ecd0d139e48407d95f2e6ad6d6de775828bd7412fec31547cca6335.png ../_images/5adfef8e994d07647042442dc72e3655bf83d15449cee3cdf7c67e24dfaf6820.png ../_images/d67a61dd4a784946a62c129f9909c4faec9623eff7970ba717ae726e7a2b5484.png ../_images/f3afe8c35bd2a09ca226d2f5bde2eddd6afce57d2d24f03f74226c965de55fb7.png ../_images/dc6a491ed37d05f201055c9fdcd98596cf207f351c986c47147b98df3939264f.png ../_images/dce95ef787b2df4f8f7dcaed60b6888a2f31641f1b8cc0ed1e869d1cf062a691.png ../_images/8e0768cea5885149977df8e5e2aeccd962745bbd7dbd9dd28c60969f06d5684c.png ../_images/a73aff38ca405c6cf39a8a70a4915a342903c0b015c8b60ed257fca85e9c35bd.png ../_images/12edbcf425ab099790b606733336a757a7950e2143ab51b8c515a53dd65c0e73.png ../_images/bfbc62b21bc2b367c0c2b0980ce3507291c5230eeec22cd4e225fb7ceac62e00.png ../_images/b4f3c6f467ef6413cca2cba857bf22fde58915f62f34f4cfcce80afbf2954ebd.png ../_images/90409511a6bd0d43ab12b2201149cf905b5b0a026b5a47a83ab3365a8a3bdde9.png ../_images/e98340297795e7457732e92c249c596b1476325e34308a24a834e30ec146cbdf.png ../_images/834326f3aa06163414cdc43789f4db74d9e901680606c4d6efee9f1d847189bf.png ../_images/3acbdb3b034287780fcf06c2db3d721d0c3647646c09738306ed0339f732e2ff.png ../_images/a2040f1e4caa5b6fc64858966ce44a1f10c01069cd9c504b572b795128a5513a.png ../_images/0018edf4d6145dde12af0c6bc65ec6c2a548d7120b32c6f1e19eb5a0273da958.png ../_images/be9a8eced272ac9967e2c529471181ac44ffdd37f29f8395a38044b2cde7a552.png ../_images/4aea865b6844d66586e1c08a15db1539bf10eff43a3b175a97721d775b92b736.png ../_images/d38e27f913795c763a0a0d22b7933fa884c61a78b41a72e1ebd135fbc2be1220.png ../_images/0ffd8ac1324f572b98ec0f0b0918f219a465d7b05e99dda8dab7a4780f763601.png ../_images/90fe81e2c9d3890b9877244b015338a810aed40cceeda46776cab2e819adb8f7.png ../_images/84b9c5acdbb42d15a0aa27f09fe1b985070b6af3e79dfcc1e0986c5ebefbcdef.png ../_images/c3bb533b879c075923782c5d4f3f26e2cc6061772eb5eb6127fa6a6a991b5e02.png ../_images/6c68b50f0249f63037f5d7c78b25ef773ac782fd14f8bd24085b79947feb8929.png ../_images/fe593874b340405d2c3f9c6908d595ce0050c4fad3a687f59eb950aa2e9cfa41.png ../_images/b7ab7e4e6c6bd28d47325ba29490abd90c04fe45a14f0adbe32eccb5aa78151b.png ../_images/44c9137e86793f0aaf0fbbfd446ca880d093048595d8883dea78713fd41a051b.png ../_images/8be0dbcfcc725998272d9ddf892e50e5d2b73dcbbf724df99003d5620a041afb.png ../_images/45d173e9a2ae0052be043a655be9f5799575de0aef2999fb98b3bc24bde89094.png ../_images/00052dcc5861a07f970f419f9adcffe6e3caaed2f591eff1091a62e38f577427.png ../_images/528121d089174d2e3d6568a22ee80476920e970340186c12d1571a7288565506.png ../_images/601ddcba610f545895da3478f36156f8667b6974de0e0df3ab87a43bff4990c0.png ../_images/232f25c712e35fce6fb5d7d8b1a0f7b34de3e1542490d953d9885ec324a17546.png ../_images/90a8839b6b66c147025c196064d29c29a1e9d07eada579298a9723a884a05db9.png ../_images/1d0869c3ca8a74576a2abbff642d0ac398d351e8f71e657efa51546c6dca2d61.png ../_images/2df1b818b17c79688b76ca421fa889ece906b6969e9dad2eac47e0b63284019f.png ../_images/ef75eb506a66606af78c287e68405ad9b4cfc947129e831f3b38e201c9c2c278.png ../_images/f102c819282f786569784b42b59ebceef0e66724bade13694ce1bc7e184f2b24.png ../_images/bc99897f60e2f4fabd8de9ae7bdcb615699cabb6175f0f6aa6f3b4bc269a137b.png ../_images/7a5222c1362ea4fa57d0d00eb95d862bbeb85fff1d2928c3793eafbae30bc177.png ../_images/085e19f338055c6f1b3273041f65a82956a6479f8945aeaf7024518d8baa4796.png ../_images/9a6f55b39516f383814dd74701e454886373fd79be81efa021ea6fa49a00ef9d.png ../_images/c908a483f21c7439d8d758d93ac8c8e587caab50aec6c31c4373200f8217888f.png ../_images/40f0da394a22af8a811b774279c5863a73a3759ed59d89ba50e19e11c93c75a1.png ../_images/3d1a504b3c0637487298ee21d9e5e873fb4e857e2ed923fa7f2f23320c253cdb.png ../_images/d70fff5988424cd876512b01345172c8bdfa6cc5fc21065b263cdd9443981a5b.png ../_images/bcef1cdec1ecaa07beb34665d990545998d34a9b7c6da7a0990cd06e5544b1f9.png ../_images/ed32ed878506aef38c5a27ca562dc975d2ea33bfbae89dc7dc3bdd70bc50fc69.png ../_images/8191ad9cdf10f56d571f0e16273b2500f2349ecc27966c2116b9244b990f8276.png ../_images/98161dfdcc0a8448b70e0d168a54221ecdb41ff33068d81cc5ffeb692be82afd.png ../_images/9b3405c3c6ac7276c041de74c04cf44bd09d3d4469c74f4b19507a6d9f2952a8.png ../_images/27704719976b079c89221f4a175b11bbb18672c8ec15544b69db7976a9a55460.png ../_images/d544f586bb667d9e813e197aff14e216b0017474ad2c37aced78884ff5e7e525.png ../_images/9112bf033da7109cef076ff01549328cce3c433bd2b990d2a5196aff1a733e72.png ../_images/dafa3a0e378f948adda2733bb467b95ab1f87a72bc10015b0668c5c39f5c2ee9.png ../_images/a198f59e25ec298e2a144b13213f43024721e5bad6ad1967a5215dc2b9367ba2.png ../_images/92ce1574566eb0e134ef63d0bc2bdcafa507ad145861128b7f0c9b246193d4f5.png ../_images/783ca7198530e70002b3591020c06acdbc281c3d2030b692110973214edd374a.png ../_images/9a5fc22a669f6fac09217657ca2e10d4d92d908f4444bfc81e2c790d5588d500.png ../_images/4158a10d7229bbeafe0255551764d95ba62f1d9992928a2bdcb8ead0f2a6eca7.png ../_images/325cf33ea95c7db15f021b8c2c24bba0a1d64e45c5f7ea3005ed236c40bb1370.png ../_images/33d7c178d1248f34bbc50c6d31f604d20acb22ddd089fceee1c52f45f888d4e5.png ../_images/a5095b50f3ca4c37e0cea4d9444a3b67f24ac356a1cb080131f0ad395f530f63.png ../_images/b9f8e3ba9ed596941a4bc357c8add952a05fe49d4bc357e4db899e2d302761d2.png ../_images/5c7cfe92ed59c1faecffac10e912d0fcd523134897bf5f56af63ab2e204df149.png ../_images/1a468fd83085ff36e3d1cef253300dc4b4cadec24974235b974828a8ee7b1bdf.png ../_images/a88dcaf953d22f7cbd30f77d70a97325ea6cd117dbcbb088928f0989bbceebc4.png ../_images/0d07f9572aff52d843f5f14beeb65bfad70386ad88e7d04ae2d79477a5214bf1.png ../_images/e188b984df2bdf8b4c163c9ada164e42dedf5bcb55e112977c4bde24bf84aea8.png ../_images/fba5ad2fcc7d88925781bd47c0b62d78757808a60b64ba776463ea1a437841a5.png ../_images/6624f00abd27939d8a28735a6c1724ebaf84ac923b90aecb42990c8a19cd1e33.png ../_images/95ffae45f92688e3e847deaac8741cbffa709b82b361a114be826000b850a09b.png ../_images/e7023624ae940724d9fb9aa71c50772f65d0806c877ee31d89d9e3a2952c193d.png ../_images/a76ef380fe402cc9e2f0191eae8f88e9d85ae1881a9c53fa4f29c05878ee8e6c.png ../_images/b714e7dd427f5c0b630b8868d250f3bc0986ee9f5236f332ff84b095528e4ca8.png ../_images/4fef5570af8782862deb8b5eda46fd729ae449e2a595dead0239751d0bfd0dcf.png ../_images/4c01a1ed22e06b16ae763da42385bcbd8caa08a08166cd88100a0ddc46e6c3ba.png ../_images/58f35e6e58a6c4cda1b2f314a4f5098cdb12db25601ffce5aae1e769c7bb8043.png ../_images/e064fc5689dabe2bc10eb9a48f1cc4e2af72913d5f23129692cb4eed5f8c6ae0.png ../_images/07e6c956ce86be650dd84cb53a399b7cb8cb6b3a9c2589bac48f4fbbe4023f85.png ../_images/382a9cac9247c264f0090830457bd8f7c9a9712348149b54c5e08d3ec189c5d2.png ../_images/272dcc200b173f29b1210c9a2d942d4349ed717922c010a548032f238e8974fc.png ../_images/456ecc261e971f78b376d77ba047cb2d09081256b14b9dcd5f7baac0d7a7514b.png ../_images/d4f66432eda2ac58560d39e53a6fd53ea5c03abdabef586055e68f3f25905c13.png ../_images/99b5e367bb3c254a7918bb54bfbce81419e59f9fe55bba4c805be32ea1ec6a18.png ../_images/e4d096ddb21bfdd429e288c91352fb0c2154b1bc6db0eee5b7245767acf863d1.png ../_images/9abaad3fac89ccd031cf800611a01b8b5e384d4c0b2388b5245be4844373c051.png ../_images/c4e3df7ecd3dfc4aa9e317f74c8d6332748e269405707d63950ac5473690a5e1.png ../_images/dff8a71faf164004457322bc15c6f264f75328b0228627646ba68a7a7fe72577.png ../_images/b2b1a59a2226dedc585fd7fae5940ba98950d64a56980077d04abc6bd4ccf75a.png ../_images/0b8016ed9904c5d3cdf772316f304be6570f61dece0527ce55dde8e1e7011c9b.png ../_images/70faf7cb3a4a8fc627f08228c77e29301933486fdbaea24b1b9bdb2b02953e0a.png ../_images/11d922d828aba9810913372b7a70b378e6fc492d1c7d5a002ccd25c8e4d4f700.png ../_images/583d2f9fcfc959a02f3378625f1570454cd4b31864874a937dbefc548d921ab4.png ../_images/5431d19ad577fbe6d5333550a9f9184e988e902fd85ad23e35e392ae0dc8d7ec.png ../_images/2c130fb4ff9358c952c4491a8dafe405cf7f7ccccb59737c3b1580d5d2aff8ee.png ../_images/88532111e0962f59b632e2bf0c8c2a22bebb05b011f8aff5cac323448144e5b0.png ../_images/8734203ebc4f68b5ade027bf2b9ff2ec8cef801c80aa0ed710a216dad4b08d68.png ../_images/859012877219fccf27c876fa32edecaf184ecec9d065c3c2f17b07834a990ad5.png ../_images/8461b759d4bb7402167e0cb33120f0b12588fd8444a71d2149c71a9a42f7d90a.png ../_images/c95f5b4c1984ffdb239e787fd5962492059c5ecfa24f101a0ad3f894526386a6.png ../_images/f5a9c0609ae7b4cd593a14652bb4a17150728aff8e9a6394d0f4bfaa18b5119c.png ../_images/a942f1858a2d796341f077c414e92557c94a09522e1bdef4ed39ad566bf13797.png ../_images/68b8de97b2950dcef6eb1b41300540f4f9c69fef1cca55791cb5b11dfcd1ce1f.png ../_images/b7e756cd58ba06df27d916f77f26719f1ea09b80bbcf3cca41527cec612148a6.png ../_images/f02cb4b90d6e5cd8e1e15f4f015e4a6bdf2658e38eb5f73f2431586ce50b7070.png ../_images/945e07edc625d742595baccf51700ec6b51dd30b7a020c8a807e14ed3cd987c4.png ../_images/46445dbea47d0ab57387e9530d33d7e3d8ad07fb05466077c6ea1d701c711dfc.png ../_images/c7bf699bb9cd03a07394570eb2c34fdf412d1e9bd5d5599583638e498be967df.png ../_images/d8d79bd8c089810f0af42672f50d36b22a7342a73f6042e42b9705987e6533f7.png ../_images/6b695edc948d639f90e64263e60a7b29ddcf0d355dfd810c33167630760ef0c1.png ../_images/e0fbabe165077a01c6a59b2259803f1d1f104a127324d8118aab9b4eca831634.png
RMS_90
array([  2.49999987,   2.49999987,   2.64398286,   2.52906127,
         2.528046  ,   3.17002452,   2.82966101,   2.85044182,
         2.7335062 ,   2.78472144,   2.91840888,   3.14788992,
         5.91947574,   4.95183546,   5.549369  ,   6.73346407,
         7.57874192,  15.82926689,  14.50177195,  13.41650331,
        13.4654436 ,  13.72917385,  14.09291042,  15.78575757,
        17.89876773,  21.03573048,  25.23213729,  30.03095485,
        34.55386447,  34.90016546,  36.00712351,  37.10292082,
        44.87783535,  56.20806657,  63.58357165,  72.15017817,
        80.16838769,  94.40601621, 116.13228634, 114.70040409,
       129.6036234 , 131.76797368, 116.2640204 , 109.06462448,
       111.97581924, 117.84792669, 124.64797682, 139.74287665,
       157.62992308, 182.63635534, 218.98859406, 232.00104179,
       215.30296714, 201.19663676, 196.13597868, 212.3738024 ,
       213.79715404, 245.87368957, 245.99288766, 247.73433881,
       264.28649986, 281.17038354, 300.34660854, 345.52134604,
       329.40704216, 356.7702918 , 386.4174176 , 389.15544909,
       392.40018901, 356.05565569, 135.35851589,  73.66796584,
        48.0637156 ,  22.01814795,   8.03510754])

Define the function to fit (tanh)#

def tanh_function(x, a, b, c, d):
    return a * np.tanh(b * x + c) + d

Paste RMS_90 below and manually adjust small values near the bottom#

# note the extra layers at the bottom
y = np.array([  2.49999987,   2.49999987,   2.64398286,   2.52906127,
         2.528046  ,   3.17002452,   2.82966101,   2.85044182,
         2.7335062 ,   2.78472144,   2.91840888,   3.14788992,
         5.91947574,   4.95183546,   5.549369  ,   6.73346407,
         7.57874192,  15.82926689,  14.50177195,  13.41650331,
        13.4654436 ,  13.72917385,  14.09291042,  15.78575757,
        17.89876773,  21.03573048,  25.23213729,  30.03095485,
        34.55386447,  34.90016546,  36.00712351,  37.10292082,
        44.87783535,  56.20806657,  63.58357165,  72.15017817,
        80.16838769,  94.40601621, 116.13228634, 114.70040409,
       129.6036234 , 131.76797368, 116.2640204 , 109.06462448,
       111.97581924, 117.84792669, 124.64797682, 139.74287665,
       157.62992308, 182.63635534, 218.98859406, 232.00104179,
       215.30296714, 201.19663676, 196.13597868, 212.3738024 ,
       213.79715404, 245.87368957, 245.99288766, 247.73433881,
       264.28649986, 281.17038354, 300.34660854, 345.52134604,
       329.40704216, 356.7702918 , 386.4174176 , 389.15544909,
       392.40018901, 356.05565569, 356.0, 356.0,
       356.0, 356.0, 356.0, 356.0, 356.0, 356.0, 356.0, 356.0,
       356.0, 356.0, 356.0, 356.0, 356.0, 356.0, 356.0, 356.0, 356.0, 356.0])

Perform curve fitting#

x = np.arange(len(y))

popt, pcov = curve_fit(tanh_function, x, y)

# Extracting optimized parameters
a, b, c, d = popt

# Generate fitted curve
y_fit = tanh_function(x, a, b, c, d)

# Plotting
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b.', label='Original Data')
plt.plot(x, y_fit, 'r-', label='Fitted Curve')
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Tanh Function Fitting')
plt.legend()
plt.show()

# Printing optimized parameters
print("Optimized Parameters:")
print("a =", a)
print("b =", b)
print("c =", c)
print("d =", d)
../_images/bded0b75537ee0ff2b420532bad962777e2f5d68d02c35fce62f93720c359c12.png
Optimized Parameters:
a = 187.08972100760633
b = 0.056399678180667163
c = -2.80764670977144
d = 189.62178708778868
dz_max_90 = tanh_function(range(len(dz)), a, b, c, d)
dz_max_90
array([  3.88978622,   4.05124971,   4.2318273 ,   4.43375969,
         4.65954471,   4.91196546,   5.19412109,   5.50946059,
         5.86181952,   6.25546   ,   6.69511391,   7.18602944,
         7.73402094,   8.34552182,   9.02764048,   9.78821859,
        10.63589128,  11.58014822,  12.63139452,  13.80100975,
        15.10140314,  16.54606232,  18.14959251,  19.92774221,
        21.89741083,  24.07663286,  26.4845323 ,  29.14124041,
        32.06776905,  35.28583156,  38.81760291,  42.68541118,
        46.91135357,  51.51683185,  56.52200507,  61.94516131,
        67.80201558,  74.1049473 ,  80.86219824,  88.07705989,
        95.74708725, 103.86338297, 112.41000134, 121.36352345,
       130.69285331, 140.35927768, 150.31682062, 160.51290687,
       170.8893278 , 181.38348107, 191.92983316, 202.46153511,
       212.91210815, 223.21710955, 233.31569094, 243.15197092,
       252.67615996, 261.84539616, 270.62427305, 278.98506237,
       286.90765387, 294.37924899, 301.39385482, 307.95162933,
       314.05812899, 319.72350586, 324.96169559, 329.78962955,
       334.22649664, 338.29307219, 342.01112468, 345.40290465,
       348.49071585, 351.29656501, 353.84188435])

Make sure max thickness of first 4 layers is 2.5 m#

dz_max_90[0:4] = 2.5 # 
dz_max_90
array([  2.5       ,   2.5       ,   2.5       ,   2.5       ,
         4.65954471,   4.91196546,   5.19412109,   5.50946059,
         5.86181952,   6.25546   ,   6.69511391,   7.18602944,
         7.73402094,   8.34552182,   9.02764048,   9.78821859,
        10.63589128,  11.58014822,  12.63139452,  13.80100975,
        15.10140314,  16.54606232,  18.14959251,  19.92774221,
        21.89741083,  24.07663286,  26.4845323 ,  29.14124041,
        32.06776905,  35.28583156,  38.81760291,  42.68541118,
        46.91135357,  51.51683185,  56.52200507,  61.94516131,
        67.80201558,  74.1049473 ,  80.86219824,  88.07705989,
        95.74708725, 103.86338297, 112.41000134, 121.36352345,
       130.69285331, 140.35927768, 150.31682062, 160.51290687,
       170.8893278 , 181.38348107, 191.92983316, 202.46153511,
       212.91210815, 223.21710955, 233.31569094, 243.15197092,
       252.67615996, 261.84539616, 270.62427305, 278.98506237,
       286.90765387, 294.37924899, 301.39385482, 307.95162933,
       314.05812899, 319.72350586, 324.96169559, 329.78962955,
       334.22649664, 338.29307219, 342.01112468, 345.40290465,
       348.49071585, 351.29656501, 353.84188435])
# Plotting
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b.', label='Original Data')
plt.plot(range(len(dz)), dz_max_90, 'r-', label='Fitted Curve')
plt.xlabel('Index')
plt.ylabel('Max. thickness (m)')
plt.title('Updated Tanh Function Fitting')
plt.legend()
plt.show()
../_images/826bbed2cfebef47c799a97afa9983d60207482d1ba59ec41ab2bcac8180494d.png

Save to file#

ds = xr.Dataset(
    {"dz": dz_max_90},
    coords={
        'z': np.arange(len(dz_max_90)), 
    },
    attrs = {
        'long_name': 'Maximum thickness using h >= 90th quantile of run 018',
        'units'    : 'm',
        'author'   : author,
        'created'  : datetime.now().strftime("%Y-%d-%m %H:%M:%S")
    }
)
date_fmt = datetime.now().strftime("%Y")[-2:]+datetime.now().strftime("%d")+ \
           datetime.now().strftime("%m")
fname = 'dz_max_{}_{}.nc'.format(grid_name,date_fmt)
ds.to_netcdf(fname)