Fit a non-LTE spectrum with multiple fit parameters

With the new fitting module introduced in fit_spectrum() function, and in the example of Tgas fitting using new fitting module, we can see its 1-temperature fitting performance for equilibrium condition.

This example features how new fitting module can fit non-equilibrium spectrum, with multiple fit parameters, such as vibrational/rotational temperatures, or mole fraction, etc.

This is a real fitting case introduced by Mr. Corentin Grimaldi, featuring CO molecule emitting on a wide range of spectrum. This case also features a user-defined trapezoid slit function, which is accounted by the distorion (dispersion) of the slit, as a result of the influences from experimental parameters and spectrometer dispersion parameters during the experiment.

plot newfitting Trot Tvib molfrac
Successfully retrieved the experimental data in 0.0010542869567871094s.


Acquired spectral quantity 'radiance' from the spectrum.
NaN values successfully purged.Number of data points left: 1479 points.
Successfully refined the experimental data in 0.0004978179931640625s.

Commence fitting process for non-LTE spectrum!
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/latest/lib/python3.8/site-packages/radis/misc/warning.py:365: PerformanceWarning:

'gu' was recomputed although 'gp' already in DataFrame. All values are equal

[[Fit Statistics]]
    # fitting method   = L-BFGS-B
    # function evals   = 216
    # data points      = 1
    # variables        = 3
    chi-square         = 1.1112e-21
    reduced chi-square = 1.1112e-21
    Akaike info crit   = -42.2488185
    Bayesian info crit = -48.2488185
##  Warning: uncertainties could not be estimated:
    this fitting method does not natively calculate uncertainties
    and numdifftools is not installed for lmfit to do this. Use
    `pip install numdifftools` for lmfit to estimate uncertainties
    with this fitting method.
[[Variables]]
    Tvib:           4193.18991 (init = 6000)
    Trot:           4037.47940 (init = 4000)
    mole_fraction:  0.07222424 (init = 0.05)

Succesfully finished the fitting process in 116.2964997291565s.
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/latest/lib/python3.8/site-packages/radis/misc/curve.py:241: UserWarning:

Presence of NaN in curve_divide!
Think about interpolation=2


Residual history:

[1.2720385087884685e-10, 1.272038528539869e-10, 1.272038490184736e-10, 1.2720385490153159e-10, 3.897912682975718e-10, 3.8979126763347264e-10, 3.897912687189921e-10, 3.8979126566483376e-10, 9.069022310391143e-11, 9.069022304877782e-11, 9.069022374677857e-11, 9.06902213241106e-11, 8.593012214391381e-11, 8.593012278887774e-11, 8.593012210248072e-11, 8.593012213416363e-11, 8.489655643219627e-11, 8.489655705818043e-11, 8.48965564228012e-11, 8.489655634156959e-11, 8.082374331944376e-11, 8.082374381879629e-11, 8.082374346792738e-11, 8.082374283985169e-11, 7.508394446741617e-11, 7.508394330645004e-11, 7.508394564369405e-11, 7.508394168872801e-11, 6.372342140534266e-11, 6.37234212915983e-11, 6.372342198384493e-11, 6.372341999592582e-11, 5.151020831636673e-11, 5.151020452017883e-11, 5.1510209803810365e-11, 5.1510205890373225e-11, 4.729270388507883e-11, 4.729270359069956e-11, 4.729270439904798e-11, 4.72927027051637e-11, 5.926953199019113e-11, 5.926952751750573e-11, 5.926953340330839e-11, 5.926952986153461e-11, 3.9802322288334486e-11, 3.9802320949066686e-11, 3.980232299741254e-11, 3.9802320809103216e-11, 3.9440162244002776e-11, 3.944016450831957e-11, 3.9440160914222544e-11, 3.944016359807555e-11, 3.6875180349599074e-11, 3.687517923796788e-11, 3.687518036820181e-11, 3.6875179871836864e-11, 3.522928708154004e-11, 3.522928685686471e-11, 3.522928705289622e-11, 3.522928672657755e-11, 3.464852302655068e-11, 3.464852316109725e-11, 3.464852278237672e-11, 3.464852301113526e-11, 3.421201705121377e-11, 3.4212017088753706e-11, 3.4212016843117563e-11, 3.421201705060211e-11, 3.349457700870034e-11, 3.349457694491761e-11, 3.349457694136534e-11, 3.349457700497289e-11, 3.3343779155695174e-11, 3.334377920512691e-11, 3.334377911607555e-11, 3.3343779190955473e-11, 3.3335386772951844e-11, 3.333538677760796e-11, 3.333538676837312e-11, 3.333538677889162e-11, 3.3335158467605014e-11, 3.3335158470333036e-11, 3.333515846643375e-11, 3.3335158469939395e-11, 3.333513457111081e-11, 3.333513457165541e-11, 3.333513457111643e-11, 3.333513457141714e-11, 3.333513261020939e-11, 3.3335132610120884e-11, 3.333513261035629e-11, 3.33351326101259e-11, 3.333513290518049e-11, 3.3335132905085994e-11, 3.3335132905085483e-11, 3.333513290518254e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513268730609e-11, 3.3335132687065716e-11, 3.3335132687333897e-11, 3.3335132687313333e-11, 3.333513259724912e-11, 3.3335132597038396e-11, 3.333513259717508e-11, 3.333513259720297e-11, 3.3335132586005034e-11, 3.333513258585318e-11, 3.333513258612087e-11, 3.333513258585138e-11, 3.333513258415954e-11, 3.3335132584036965e-11, 3.3335132584127883e-11, 3.333513258403707e-11, 3.3335132583993563e-11, 3.3335132583840063e-11, 3.333513258403717e-11, 3.3335132583947196e-11, 3.333513258401848e-11, 3.3335132583870675e-11, 3.33351325839851e-11, 3.333513258395785e-11, 3.333513258398393e-11, 3.333513258392172e-11, 3.3335132584031207e-11, 3.333513258393539e-11, 3.333513258398132e-11, 3.333513258391996e-11, 3.333513258400369e-11, 3.333513258391247e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513258398344e-11, 3.33351325839216e-11, 3.333513258400221e-11, 3.333513258391143e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513258398273e-11, 3.333513258392288e-11, 3.333513258400164e-11, 3.3335132583909656e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.3335132583981447e-11, 3.3335132583922503e-11, 3.3335132584001286e-11, 3.333513258390935e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513258398085e-11, 3.333513258392211e-11, 3.333513258400047e-11, 3.333513258391e-11, 3.3335132583980917e-11, 3.3335132583922355e-11, 3.333513258400024e-11, 3.333513258391025e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513258398069e-11, 3.333513258392234e-11, 3.333513258400075e-11, 3.333513258390951e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.3335132583980497e-11, 3.3335132583922e-11, 3.3335132584000633e-11, 3.3335132583909223e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513258398048e-11, 3.3335132583922e-11, 3.3335132584001105e-11, 3.33351325839092e-11, 3.3335132583980464e-11, 3.333513258392276e-11, 3.333513258400111e-11, 3.333513258390953e-11, 3.333513258398046e-11, 3.333513258392203e-11, 3.3335132584000795e-11, 3.333513258390918e-11, 3.333513258398018e-11, 3.3335132583922e-11, 3.333513258400081e-11, 3.333513258390918e-11, 3.333513258398018e-11, 3.3335132583922e-11, 3.3335132584000995e-11, 3.333513258390918e-11, 3.3335132583980464e-11]

Fitted values history:

[6000.0, 4000.0, 0.05]
[6000.0000199999995, 4000.0, 0.05]
[6000.0, 4000.0000244948974, 0.05]
[6000.0, 4000.0, 0.0500000005]
[5085.615449322544, 4952.6095678519705, 0.013135912222340042]
[5085.615473626976, 4952.6095678519705, 0.013135912222340042]
[5085.615449322544, 4952.6095924388455, 0.013135912222340042]
[5085.615449322544, 4952.6095678519705, 0.01313591256013274]
[5841.963354448817, 4178.526927106568, 0.04220316384380334]
[5841.963375541787, 4178.526927106568, 0.04220316384380334]
[5841.963354448817, 4178.526951899017, 0.04220316384380334]
[5841.963354448817, 4178.526927106568, 0.042203164337686866]
[5854.620648323766, 4134.066895987315, 0.04452870158238221]
[5854.620669335674, 4134.066895987315, 0.04452870158238221]
[5854.620648323766, 4134.066920718051, 0.04452870158238221]
[5854.620648323766, 4134.066895987315, 0.04452870207937968]
[5820.918201452265, 4133.7319211808735, 0.044722921040657775]
[5820.918222677663, 4133.7319211808735, 0.044722921040657775]
[5820.918201452265, 4133.731945911113, 0.044722921040657775]
[5820.918201452265, 4133.7319211808735, 0.044722921537865216]
[5682.831646552064, 4132.392089214818, 0.04550057474549044]
[5682.831668576843, 4132.392089214818, 0.04550057474549044]
[5682.831646552064, 4132.3921139430695, 0.04550057474549044]
[5682.831646552064, 4132.392089214818, 0.04550057524346185]
[5088.210089971502, 4127.033844388251, 0.048620275598397225]
[5088.210114269668, 4127.033844388251, 0.048620275598397225]
[5088.210089971502, 4127.033869108478, 0.048620275598397225]
[5088.210089971502, 4127.033844388251, 0.04862027609820682]
[5117.637143495699, 4082.50833178492, 0.050676262010467155]
[5117.637167720735, 4082.50833178492, 0.050676262010467155]
[5117.637143495699, 4082.5083564338565, 0.050676262010467155]
[5117.637143495699, 4082.50833178492, 0.05067626251042143]
[4125.58205245289, 3854.65270317137, 0.06618540464344512]
[4125.582077170922, 3854.65270317137, 0.06618540464344512]
[4125.58205245289, 3854.6527273240654, 0.06618540464344512]
[4125.58205245289, 3854.65270317137, 0.06618540511652363]
[4698.278153812083, 3984.9633172722165, 0.05737695698575698]
[4698.27817873333, 3984.9633172722165, 0.05737695698575698]
[4698.278153812083, 3984.963341735939, 0.05737695698575698]
[4698.278153812083, 3984.9633172722165, 0.05737695748028509]
[3896.7314449387854, 3689.328780744152, 0.07046122918291087]
[3896.7314692000036, 3689.328780744152, 0.07046122918291087]
[3896.7314449387854, 3689.328804393279, 0.07046122918291087]
[3896.7314449387854, 3689.328780744152, 0.07046122963912771]
[4387.93603102096, 3870.1283142897573, 0.06252758974828306]
[4387.936055995831, 3870.1283142897573, 0.06252758974828306]
[4387.93603102096, 3870.1283384832723, 0.06252758974828306]
[4387.93603102096, 3870.1283142897573, 0.06252759023233466]
[4188.346046062805, 3669.668709690855, 0.06959467204388246]
[4188.346070867787, 3669.668709690855, 0.06959467204388246]
[4188.346046062805, 3669.6687332716733, 0.06959467204388246]
[4188.346046062805, 3669.668709690855, 0.06959467250388776]
[4063.25227647545, 3675.1245574980803, 0.07047908220641283]
[4063.2523010909968, 3675.1245574980803, 0.07047908220641283]
[4063.25227647545, 3675.124581098039, 0.07047908220641283]
[4063.25227647545, 3675.1245574980803, 0.07047908266254956]
[4263.122437713028, 3787.981239402146, 0.0663857623930122]
[4263.122462600553, 3787.981239402146, 0.0663857623930122]
[4263.122437713028, 3787.9812633667643, 0.0663857623930122]
[4263.122437713028, 3787.981239402146, 0.06638576286540046]
[4217.251420657505, 3786.339587703842, 0.06812304149033678]
[4217.251445497097, 3786.339587703842, 0.06812304149033678]
[4217.251420657505, 3786.3396116635768, 0.06812304149033678]
[4217.251420657505, 3786.339587703842, 0.06812304195633628]
[4181.890409562455, 3816.892532412, 0.06958865434391516]
[4181.89043435924, 3816.892532412, 0.06958865434391516]
[4181.890409562455, 3816.892556460626, 0.06958865434391516]
[4181.890409562455, 3816.892532412, 0.06958865480394609]
[4166.775415411606, 3944.4647893889605, 0.07177413396824071]
[4166.775440188534, 3944.4647893889605, 0.07177413396824071]
[4166.775415411606, 3944.464813763908, 0.07177413396824071]
[4166.775415411606, 3944.4647893889605, 0.07177413441833928]
[4188.0883799614, 4018.059159903517, 0.07217601356218245]
[4188.08840476606, 4018.059159903517, 0.07217601356218245]
[4188.0883799614, 4018.0591844345854, 0.07217601356218245]
[4188.0883799614, 4018.059159903517, 0.07217601401031462]
[4191.50063456006, 4036.659116928309, 0.07227493367886229]
[4191.500659368987, 4036.659116928309, 0.07227493367886229]
[4191.50063456006, 4036.659141495187, 0.07227493367886229]
[4191.50063456006, 4036.659116928309, 0.07227493412650358]
[4192.917676642459, 4038.1843603117804, 0.07224593931464357]
[4192.917701453143, 4038.1843603117804, 0.07224593931464357]
[4192.917676642459, 4038.1843848815306, 0.07224593931464357]
[4192.917676642459, 4038.1843603117804, 0.07224593976242903]
[4193.225749127383, 4037.8342233143785, 0.0722284781700246]
[4193.2257739384495, 4037.8342233143785, 0.0722284781700246]
[4193.225749127383, 4037.8342478834707, 0.0722284781700246]
[4193.225749127383, 4037.8342233143785, 0.07222847861789676]
[4193.198938320922, 4037.5316844728295, 0.07222463664134925]
[4193.198963131955, 4037.5316844728295, 0.07222463664134925]
[4193.198938320922, 4037.531709041352, 0.07222463664134925]
[4193.198938320922, 4037.5316844728295, 0.07222463708924047]
[4193.166692887074, 4037.34496864976, 0.0722232028267354]
[4193.166717698066, 4037.34496864976, 0.0722232028267354]
[4193.166692887074, 4037.344993217932, 0.0722232028267354]
[4193.166692887074, 4037.34496864976, 0.07222320327463375]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.164962213132, 4037.486213470752, 0.0722251390611066]
[4193.164987024122, 4037.486213470752, 0.0722251390611066]
[4193.164962213132, 4037.4862380391887, 0.0722251390611066]
[4193.164962213132, 4037.486213470752, 0.07222513950899533]
[4193.184909921244, 4037.4807647590706, 0.07222441627302414]
[4193.184934732259, 4037.4807647590706, 0.07222441627302414]
[4193.184909921244, 4037.4807893274974, 0.07222441627302414]
[4193.184909921244, 4037.4807647590706, 0.07222441672091647]
[4193.189045109815, 4037.479635234268, 0.07222426643738113]
[4193.189069920835, 4037.479635234268, 0.07222426643738113]
[4193.189045109815, 4037.4796598026924, 0.07222426643738113]
[4193.189045109815, 4037.479635234268, 0.0722242668852742]
[4193.189825816029, 4037.4794219847718, 0.07222423814901734]
[4193.18985062705, 4037.4794219847718, 0.07222423814901734]
[4193.189825816029, 4037.479446553196, 0.07222423814901734]
[4193.189825816029, 4037.4794219847718, 0.07222423859691054]
[4193.189900707008, 4037.4794015283405, 0.07222423543539278]
[4193.189925518029, 4037.4794015283405, 0.07222423543539278]
[4193.189900707008, 4037.4794260967647, 0.07222423543539278]
[4193.189900707008, 4037.4794015283405, 0.07222423588328598]
[4193.18990700834, 4037.479399807135, 0.07222423520706821]
[4193.189931819361, 4037.479399807135, 0.07222423520706821]
[4193.18990700834, 4037.4794243755596, 0.07222423520706821]
[4193.18990700834, 4037.479399807135, 0.07222423565496143]
[4193.189908780507, 4037.4793993230687, 0.07222423514285495]
[4193.189933591527, 4037.4793993230687, 0.07222423514285495]
[4193.189908780507, 4037.479423891493, 0.07222423514285495]
[4193.189908780507, 4037.4793993230687, 0.07222423559074817]
[4193.1899088234, 4037.479399311352, 0.07222423514130069]
[4193.189933634421, 4037.479399311352, 0.07222423514130069]
[4193.1899088234, 4037.4794238797763, 0.07222423514130069]
[4193.1899088234, 4037.479399311352, 0.07222423558919391]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.1899088235405, 4037.4793993113144, 0.07222423514129564]
[4193.189933634561, 4037.4793993113144, 0.07222423514129564]
[4193.1899088235405, 4037.4794238797385, 0.07222423514129564]
[4193.1899088235405, 4037.4793993113144, 0.07222423558918886]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.18990882361, 4037.479399311295, 0.0722242351412931]
[4193.1899336346305, 4037.479399311295, 0.0722242351412931]
[4193.18990882361, 4037.4794238797194, 0.0722242351412931]
[4193.18990882361, 4037.479399311295, 0.07222423558918632]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.189908823644, 4037.4793993112853, 0.07222423514129185]
[4193.189933634665, 4037.4793993112853, 0.07222423514129185]
[4193.189908823644, 4037.4794238797094, 0.07222423514129185]
[4193.189908823644, 4037.4793993112853, 0.07222423558918507]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.189908823662, 4037.4793993112808, 0.07222423514129121]
[4193.189933634683, 4037.4793993112808, 0.07222423514129121]
[4193.189908823662, 4037.479423879705, 0.07222423514129121]
[4193.189908823662, 4037.4793993112808, 0.07222423558918444]
[4193.189908823693, 4037.4793993112708, 0.0722242351412909]
[4193.189933634715, 4037.4793993112708, 0.0722242351412909]
[4193.189908823693, 4037.479423879695, 0.0722242351412909]
[4193.189908823693, 4037.4793993112708, 0.07222423558918412]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.189908823687, 4037.4793993112735, 0.07222423514129074]
[4193.189933634708, 4037.4793993112735, 0.07222423514129074]
[4193.189908823687, 4037.4794238796976, 0.07222423514129074]
[4193.189908823687, 4037.4793993112735, 0.07222423558918396]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.189908823683, 4037.4793993112744, 0.07222423514129066]
[4193.189933634704, 4037.4793993112744, 0.07222423514129066]
[4193.189908823683, 4037.479423879699, 0.07222423514129066]
[4193.189908823683, 4037.4793993112744, 0.07222423558918388]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.189908823681, 4037.4793993112753, 0.07222423514129062]
[4193.189933634702, 4037.4793993112753, 0.07222423514129062]
[4193.189908823681, 4037.4794238796994, 0.07222423514129062]
[4193.189908823681, 4037.4793993112753, 0.07222423558918384]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]
[4193.1899336347005, 4037.479399311276, 0.07222423514129059]
[4193.18990882368, 4037.4794238797003, 0.07222423514129059]
[4193.18990882368, 4037.479399311276, 0.07222423558918381]
[4193.1899088236805, 4037.4793993112753, 0.0722242351412906]
[4193.189933634701, 4037.4793993112753, 0.0722242351412906]
[4193.1899088236805, 4037.4794238797, 0.0722242351412906]
[4193.1899088236805, 4037.4793993112753, 0.07222423558918382]
[4193.1899088236805, 4037.4793993112753, 0.07222423514129062]
[4193.189933634701, 4037.4793993112753, 0.07222423514129062]
[4193.1899088236805, 4037.4794238797, 0.07222423514129062]
[4193.1899088236805, 4037.4793993112753, 0.07222423558918382]
[4193.1899088236805, 4037.4793993112753, 0.07222423514129062]
[4193.189933634702, 4037.4793993112753, 0.07222423514129062]
[4193.1899088236805, 4037.4794238796994, 0.07222423514129062]
[4193.1899088236805, 4037.4793993112753, 0.07222423558918382]
[4193.18990882368, 4037.479399311276, 0.07222423514129059]

Total fitting time:
116.2964997291565 s

import numpy as np

from radis import load_spec
from radis.test.utils import getTestFile
from radis.tools.new_fitting import fit_spectrum

# The following script is for Mr. Correnti Grimaldi's cases, used for his own slit settings.
# This is not native to current fitting modules.

# ----------------------- Begin of Mr. Grimaldi's script ----------------------- #


def slit_dispersion(w):
    phi = -6.33
    f = 750
    gr = 300
    m = 1
    phi *= -2 * np.pi / 360
    d = 1e-3 / gr
    disp = (
        w
        / (2 * f)
        * (-np.tan(phi) + np.sqrt((2 * d / m / (w * 1e-9) * np.cos(phi)) ** 2 - 1))
    )
    return disp  # nm/mm


slit = 1500  # µm
pitch = 20  # µm
top_slit_um = slit - pitch  # µm
base_slit_um = slit + pitch  # µm
center_slit = 5090
dispersion = slit_dispersion(center_slit)
top_slit_nm = top_slit_um * 1e-3 * dispersion
base_slit_nm = base_slit_um * 1e-3 * dispersion * 1.33

# ------------------------ End of Mr. Grimaldi's script ------------------------ #


# Load experimental spectrum.
specName = (
    "Corentin_0_100cm_DownSampled_20cm_10pctCO2_1-wc-gw450-gr300-sl1500-acc5000-.spec"
)
s_experimental = load_spec(getTestFile(specName)).offset(-0.6, "nm")

# Experimental conditions which will be used for spectrum modeling. Basically, these are known ground-truths.
experimental_conditions = {
    "molecule": "CO",  # Molecule ID
    "isotope": "1,2,3",  # Isotope ID, can have multiple at once
    "wmin": 2270,  # Starting wavelength/wavenumber to be cropped out from the original experimental spectrum.
    "wmax": 2400,  # Ending wavelength/wavenumber for the cropping range.
    "wunit": "nm",  # Accompanying unit of those 2 wavelengths/wavenumbers above.
    "pressure": 1.01325,  # Partial pressure of gas, in "bar" unit.
    "path_length": 1 / 195,  # Experimental path length, in "cm" unit.
    "slit": {  # Experimental slit. In simple form: "[value] [unit]", i.e. "-0.2 nm". In complex form: a dict with parameters of apply_slit()
        "slit_function": (top_slit_nm, base_slit_nm),
        "unit": "nm",
        "shape": "trapezoidal",
        "center_wavespace": center_slit,
        "slit_dispersion": slit_dispersion,
        "inplace": False,
    },
    "wstep": 0.001,  # Resolution of wavenumber grid, in cm-1.
    "cutoff": 0,  # (RADIS native) Discard linestrengths that are lower that this to reduce calculation time, in cm-1.
    "databank": "hitemp",  # Databank used for calculation. Must be stated.
}

# List of parameters to be fitted, accompanied by its initial value
fit_parameters = {
    "Tvib": 6000,  # Vibrational temperature, in K.
    "Trot": 4000,  # Rotational temperature, in K.
    "mole_fraction": 0.05,  # Species mole fraction, from 0 to 1.
}

# List of bounding ranges applied for those fit parameters above.
# You can skip this step and let it use default bounding ranges, but this is not recommended.
# Bounding range must be at format [<lower bound>, <upper bound>].
bounding_ranges = {
    "Tvib": [2000, 7000],
    "Trot": [2000, 7000],
    "mole_fraction": [0, 0.1],
}

# Fitting pipeline setups.
fit_properties = {
    "method": "lbfgsb",  # Preferred fitting method from the 17 confirmed methods of LMFIT stated in week 4 blog. By default, "leastsq".
    "fit_var": "radiance",  # Spectral quantity to be extracted for fitting process, such as "radiance", "absorbance", etc.
    "normalize": False,  # Either applying normalization on both spectra or not.
    "max_loop": 300,  # Max number of loops allowed. By default, 200.
    "tol": 1e-20,  # Fitting tolerance, only applicable for "lbfgsb" method.
}


# Conduct the fitting process!
s_best, result, log = fit_spectrum(
    s_exp=s_experimental,  # Experimental spectrum.
    fit_params=fit_parameters,  # Fit parameters.
    bounds=bounding_ranges,  # Bounding ranges for those fit parameters.
    model=experimental_conditions,  # Experimental ground-truths conditions.
    pipeline=fit_properties,  # # Fitting pipeline references.
)


# Now investigate the result logs for additional information about what's going on during the fitting process

print("\nResidual history: \n")
print(log["residual"])

print("\nFitted values history: \n")
for fit_val in log["fit_vals"]:
    print(fit_val)

print("\nTotal fitting time: ")
print(log["time_fitting"], end=" s\n")

Total running time of the script: ( 1 minutes 58.540 seconds)