1010import os
1111
1212import numpy as np
13+ import pytest
1314
1415from earthkit .meteo import thermo
1516
@@ -320,7 +321,7 @@ def test_saturation_specific_humidity_slope():
320321 np .testing .assert_allclose (svp , v_ref [i ])
321322
322323
323- def test_temperature_from_saturation_vapour_pressure ():
324+ def test_temperature_from_saturation_vapour_pressure_1 ():
324325 ref_file = "sat_vp.csv"
325326 d = np .genfromtxt (
326327 data_file (ref_file ),
@@ -329,7 +330,32 @@ def test_temperature_from_saturation_vapour_pressure():
329330 )
330331
331332 t = thermo .array .temperature_from_saturation_vapour_pressure (d ["water" ])
332- np .testing .assert_allclose (t , d ["t" ])
333+ assert np .allclose (t , d ["t" ], equal_nan = True )
334+
335+
336+ @pytest .mark .parametrize (
337+ "es,kwargs,expected_values" ,
338+ [
339+ (4.2 , {}, 219.7796336743947 ),
340+ ([4.2 , 0 , 0.001 , np .nan ], {"eps" : 1e-2 , "out" : 100 }, [219.7796336743947 , 100 , 100 , np .nan ]),
341+ ([4.2 , 0 , 0.001 , np .nan ], {"eps" : 1e-2 , "out" : np .nan }, [219.7796336743947 , np .nan , np .nan , np .nan ]),
342+ (0 , {}, np .nan ),
343+ (0.001 , {"eps" : 1e-2 , "out" : 100 }, 100.0 ),
344+ (0.001 , {"eps" : 1e-2 , "out" : np .nan }, np .nan ),
345+ ],
346+ )
347+ def test_temperature_from_saturation_vapour_pressure_2 (es , kwargs , expected_values ):
348+
349+ multi = isinstance (es , list )
350+ if multi :
351+ es = np .array (es )
352+ expected_values = np .array (expected_values )
353+
354+ t = thermo .array .temperature_from_saturation_vapour_pressure (es , ** kwargs )
355+ if multi :
356+ np .testing .assert_allclose (t , expected_values , equal_nan = True )
357+ else :
358+ assert np .isclose (t , expected_values , equal_nan = True )
333359
334360
335361def test_relative_humidity_from_dewpoint ():
@@ -421,43 +447,112 @@ def test_specific_humidity_from_relative_humidity():
421447 np .testing .assert_allclose (q , v_ref )
422448
423449
424- def test_dewpoint_from_relative_humidity ():
425- t = thermo .array .celsius_to_kelvin (np .array ([20.0 , 20 , 0 , 35 , 5 , - 15 , 25 ]))
426- r = np .array (
427- [
428- 100.0000000000 ,
429- 52.5224541378 ,
430- 46.8714823296 ,
431- 84.5391163313 ,
432- 21.9244774232 ,
433- 46.1081101229 ,
434- 15.4779832381 ,
435- ]
436- )
437- v_ref = thermo .array .celsius_to_kelvin (np .array ([20.0 , 10 , - 10 , 32 , - 15 , - 24 , - 3 ]))
450+ @pytest .mark .parametrize (
451+ "t,r,kwargs,expected_values" ,
452+ [
453+ (
454+ [20.0 , 20 , 0 , 35 , 5 , - 15 , 25 , 25 ],
455+ [
456+ 100.0000000000 ,
457+ 52.5224541378 ,
458+ 46.8714823296 ,
459+ 84.5391163313 ,
460+ 21.9244774232 ,
461+ 46.1081101229 ,
462+ 15.4779832381 ,
463+ 0 ,
464+ ],
465+ {},
466+ [20.0 , 10 , - 10 , 32 , - 15 , - 24 , - 3 , np .nan ],
467+ ),
468+ (
469+ [20.0 , 20.0 , 20.0 ],
470+ [
471+ 52.5224541378 ,
472+ 0.0 ,
473+ 0.000001 ,
474+ ],
475+ {"eps" : 1e-3 , "out" : thermo .array .celsius_to_kelvin (100 )},
476+ [10 , 100 , 100 ],
477+ ),
478+ (
479+ [20.0 , 20.0 , 20.0 ],
480+ [
481+ 52.5224541378 ,
482+ 0.0 ,
483+ 0.000001 ,
484+ ],
485+ {"eps" : 1e-3 , "out" : np .nan },
486+ [10 , np .nan , np .nan ],
487+ ),
488+ ],
489+ )
490+ def test_dewpoint_from_relative_humidity (t , r , kwargs , expected_values ):
438491 # reference was tested with an online relhum calculator at:
439492 # https://bmcnoldy.rsmas.miami.edu/Humidity.html
440- td = thermo .array .dewpoint_from_relative_humidity (t , r )
441- np .testing .assert_allclose (td , v_ref )
442-
443493
444- def test_dewpoint_from_specific_humidity ():
445- p = np .array ([967.5085 , 936.3775 , 872.248 , 756.1647 , 649.157 , 422.4207 ]) * 100
446- q = np .array (
447- [
448- 0.0169461501 ,
449- 0.0155840075 ,
450- 0.0134912382 ,
451- 0.0083409720 ,
452- 0.0057268584 ,
453- 0.0025150791 ,
454- ]
455- )
456- v_ref = thermo .array .celsius_to_kelvin (
457- np .array ([21.78907 , 19.90885 , 16.50236 , 7.104064 , - 0.3548709 , - 16.37916 ])
458- )
459- td = thermo .array .dewpoint_from_specific_humidity (q , p )
460- np .testing .assert_allclose (td , v_ref )
494+ multi = isinstance (t , list )
495+ if multi :
496+ t = np .array (t )
497+ r = np .array (r )
498+ expected_values = np .array (expected_values )
499+
500+ t = thermo .array .celsius_to_kelvin (t )
501+ v_ref = thermo .array .celsius_to_kelvin (expected_values )
502+
503+ td = thermo .array .dewpoint_from_relative_humidity (t , r , ** kwargs )
504+ if multi :
505+ assert np .allclose (td , v_ref , equal_nan = True )
506+ else :
507+ assert np .isclose (td , v_ref , equal_nan = True )
508+
509+
510+ @pytest .mark .parametrize (
511+ "q,p,kwargs,expected_values" ,
512+ [
513+ (
514+ [0.0169461501 , 0.0155840075 , 0.0134912382 , 0.0083409720 , 0.0057268584 , 0.0025150791 , 0 ],
515+ [967.5085 , 936.3775 , 872.248 , 756.1647 , 649.157 , 422.4207 , 422.4207 ],
516+ {},
517+ [21.78907 , 19.90885 , 16.50236 , 7.104064 , - 0.3548709 , - 16.37916 , np .nan ],
518+ ),
519+ (
520+ [
521+ 0.0169461501 ,
522+ 0.0 ,
523+ 0.000001 ,
524+ ],
525+ [967.5085 , 967.5085 , 967.5085 ],
526+ {"eps" : 1e-3 , "out" : thermo .array .celsius_to_kelvin (100 )},
527+ [21.78907 , 100 , 100 ],
528+ ),
529+ (
530+ [
531+ 0.0169461501 ,
532+ 0.0 ,
533+ 0.000001 ,
534+ ],
535+ [967.5085 , 967.5085 , 967.5085 ],
536+ {"eps" : 1e-3 , "out" : np .nan },
537+ [21.78907 , np .nan , np .nan ],
538+ ),
539+ ],
540+ )
541+ def test_dewpoint_from_specific_humidity (q , p , kwargs , expected_values ):
542+ multi = isinstance (q , list )
543+ if multi :
544+ q = np .array (q )
545+ p = np .array (p )
546+ expected_values = np .array (expected_values )
547+
548+ p = p * 100.0
549+ v_ref = thermo .array .celsius_to_kelvin (expected_values )
550+
551+ td = thermo .array .dewpoint_from_specific_humidity (q , p , ** kwargs )
552+ if multi :
553+ assert np .allclose (td , v_ref , equal_nan = True )
554+ else :
555+ assert np .isclose (td , v_ref , equal_nan = True )
461556
462557
463558def test_virtual_temperature ():
0 commit comments