66
77import warnings
88import logging
9- import dask
109import numpy as np
1110import xarray as xr
1211from xclim import sdba , set_options
@@ -637,71 +636,14 @@ def apply_precip_ceiling(ds, ceiling):
637636 return ds_corrected
638637
639638
640- def validate_dataset (ds , var , data_type , time_period = "future" ):
641- """
642- Validate a Dataset. Valid for CMIP6, bias corrected and downscaled.
643-
644- Raises AssertionError when validation fails.
645-
646- Parameters
647- ----------
648- ds : xr.Dataset
649- var : {"tasmax", "tasmin", "dtr", "pr"}
650- Variable in Dataset to validate.
651- data_type : {"cmip6", "bias_corrected", "downscaled"}
652- Type of data output to validate.
653- time_period : {"historical", "future"}
654- Time period of data that will be validated.
655- """
656- # This is pretty rough but works to communicate the idea.
657- # Consider having failed tests raise something like ValidationError rather
658- # than AssertionErrors.
659-
660- # These only read in Zarr Store metadata -- not memory intensive.
661- _test_variable_names (ds , var )
662- _test_timesteps (ds , data_type , time_period )
663-
664- # Other test are done on annual selections with dask.delayed to
665- # avoid large memory errors. xr.map_blocks had trouble with this.
666- @dask .delayed
667- def memory_intensive_tests (ds , v , t ):
668- d = ds .sel (time = str (t ))
669-
670- _test_for_nans (d , v )
671-
672- if v == "tasmin" :
673- _test_temp_range (d , v )
674- elif v == "tasmax" :
675- _test_temp_range (d , v )
676- elif v == "dtr" :
677- _test_dtr_range (d , v , data_type )
678- _test_negative_values (d , v )
679- elif v == "pr" :
680- _test_negative_values (d , v )
681- _test_maximum_precip (d , v )
682- else :
683- raise ValueError (f"Argument { v = } not recognized" )
684-
685- # Assumes error thrown if had problem before this.
686- return True
687-
688- results = []
689- for t in np .unique (ds ["time" ].dt .year .data ):
690- logger .debug (f"Validating year { t } " )
691- results .append (memory_intensive_tests (ds , var , t ))
692- results = dask .compute (* results )
693- assert all (results ) # Likely don't need this
694- return True
695-
696-
697- def _test_for_nans (ds , var ):
639+ def test_for_nans (ds , var ):
698640 """
699641 Tests for presence of NaNs
700642 """
701643 assert ds [var ].isnull ().sum () == 0 , "there are nans!"
702644
703645
704- def _test_timesteps (ds , data_type , time_period ):
646+ def test_timesteps (ds , data_type , time_period ):
705647 """
706648 Tests that Dataset contains the correct number of timesteps (number of days on a noleap calendar)
707649 for the data_type/time_period combination.
@@ -763,14 +705,14 @@ def _test_timesteps(ds, data_type, time_period):
763705 )
764706
765707
766- def _test_variable_names (ds , var ):
708+ def test_variable_names (ds , var ):
767709 """
768710 Test that the correct variable name exists in the file
769711 """
770712 assert var in ds .var (), "{} not in Dataset" .format (var )
771713
772714
773- def _test_temp_range (ds , var ):
715+ def test_temp_range (ds , var ):
774716 """
775717 Ensure temperature values are in a valid range
776718 """
@@ -781,7 +723,7 @@ def _test_temp_range(ds, var):
781723 ), "{} values are invalid" .format (var )
782724
783725
784- def _test_dtr_range (ds , var , data_type ):
726+ def test_dtr_range (ds , var , data_type ):
785727 """
786728 Ensure DTR values are in a valid range
787729 Test polar values separately since some polar values can be much higher post-bias correction.
@@ -830,7 +772,7 @@ def _test_dtr_range(ds, var, data_type):
830772 ), "diurnal temperature range max is {} for non-polar regions" .format (non_polar_max )
831773
832774
833- def _test_negative_values (ds , var ):
775+ def test_negative_values (ds , var ):
834776 """
835777 Tests for presence of negative values
836778 """
@@ -839,7 +781,7 @@ def _test_negative_values(ds, var):
839781 assert neg_values == 0 , "there are {} negative values!" .format (neg_values )
840782
841783
842- def _test_maximum_precip (ds , var ):
784+ def test_maximum_precip (ds , var ):
843785 """
844786 Tests that max precip is reasonable
845787 """
0 commit comments