|
| 1 | +import Dates |
| 2 | + |
| 3 | +""" |
| 4 | + struct CalibrateConfig{SPINUP <: Dates.Period, EXTEND <: Dates.Period} |
| 5 | + short_names::Vector{String} |
| 6 | + minibatch_size::Int64 |
| 7 | + n_iterations::Int64 |
| 8 | + sample_date_ranges::Vector{NTuple{2, DATE}} |
| 9 | + extend::EXTEND |
| 10 | + spinup::SPINUP |
| 11 | + nelements::Tuple{Int64, Int64} |
| 12 | + output_dir::String |
| 13 | + rng_seed::Int64 |
| 14 | + end |
| 15 | +
|
| 16 | +A configuration struct for keeping track of multiple fields that are of interest |
| 17 | +to a user running calibration, or that are needed in multiple places (e.g., for |
| 18 | +ensemble members and generating observations). |
| 19 | +""" |
| 20 | +struct CalibrateConfig{SPINUP <: Dates.Period, EXTEND <: Dates.Period} |
| 21 | + "Configuration file to use for ClimaCoupler simulation" |
| 22 | + config_file::String |
| 23 | + |
| 24 | + "The short names of the observations used for calibration. The short names |
| 25 | + should match the same names used for the diagnostics." |
| 26 | + short_names::Vector{String} |
| 27 | + |
| 28 | + "The size of the minibatch for each iteration" |
| 29 | + minibatch_size::Int64 |
| 30 | + |
| 31 | + "The number of iterations to run the calibration for" |
| 32 | + n_iterations::Int64 |
| 33 | + |
| 34 | + "The date ranges of the samples for calibration and used to determine the |
| 35 | + start and end dates of a simulation for each iteration of calibration" |
| 36 | + sample_date_ranges::Vector{NTuple{2, Dates.DateTime}} |
| 37 | + |
| 38 | + "The amount of time to run a simulation after the last date of the |
| 39 | + minibatch" |
| 40 | + extend::EXTEND |
| 41 | + |
| 42 | + "The amount of time to run a simulation before the first date of the |
| 43 | + minibatch" |
| 44 | + spinup::SPINUP |
| 45 | + |
| 46 | + "The directory to store the iterations and members of the calibration." |
| 47 | + output_dir::String |
| 48 | + |
| 49 | + "An integer value for ensuring calibrations are the same between multiple |
| 50 | + calibrations with the same settings" |
| 51 | + rng_seed::Int64 |
| 52 | +end |
| 53 | + |
| 54 | +""" |
| 55 | + CalibrateConfig(; |
| 56 | + config_file, |
| 57 | + short_names, |
| 58 | + sample_date_ranges, |
| 59 | + extend, |
| 60 | + spinup = Dates.Month(3), |
| 61 | + minibatch_size, |
| 62 | + n_iterations, |
| 63 | + output_dir = "calibration/weatherquest",, |
| 64 | + rng_seed = 42, |
| 65 | + ) |
| 66 | +
|
| 67 | +Initializes a CalibrateConfig, which is of interest to a user running |
| 68 | +calibration or contains values needed in multiple places during calibration. |
| 69 | +
|
| 70 | +Keyword arguments |
| 71 | +===================== |
| 72 | +
|
| 73 | +- `config_file`: Configuration file to use for ClimaCoupler simulation. |
| 74 | +
|
| 75 | +- `short_names`: Short names of the observations. The currently supported short |
| 76 | + names are `pr`, `tas`, and `mslp`. |
| 77 | +
|
| 78 | +- `minibatch_size`: The size of the minibatch for each iteration. |
| 79 | +
|
| 80 | +- `n_iterations`: The number of iterations to run the calibration for. |
| 81 | +
|
| 82 | +- `sample_date_ranges`: The date ranges for each sample. The dates should be the |
| 83 | + same as found in the time series data of the observations. |
| 84 | +
|
| 85 | +- `extend`: The amount of time to run the simulation after the end date |
| 86 | + determined by `sample_date_ranges`. For seasonal averages, `extend` should be |
| 87 | + `Dates.Month(3)` and for monthly averages, `extend` should be |
| 88 | + `Dates.Month(1)`. |
| 89 | +
|
| 90 | +- `spinup`: The amount of time to run the simulation before the start date |
| 91 | + determined by `sample_date_ranges`. |
| 92 | +
|
| 93 | +- `nelements`: The resolution of the model. This is also used to determine the |
| 94 | + mask of the observations. |
| 95 | +
|
| 96 | +- `output_dir`: The location to save the calibration at. |
| 97 | +
|
| 98 | +- `rng_seed`: An integer to ensure that calibration runs with the same settings |
| 99 | + are the same. |
| 100 | +""" |
| 101 | +function CalibrateConfig(; |
| 102 | + config_file, |
| 103 | + short_names, |
| 104 | + minibatch_size, |
| 105 | + n_iterations, |
| 106 | + sample_date_ranges, |
| 107 | + extend, |
| 108 | + spinup = Dates.Month(3), |
| 109 | + output_dir = "calibration/weatherquest", |
| 110 | + rng_seed = 42, |
| 111 | +) |
| 112 | + isempty(short_names) && error("Cannot run calibration with no short names") |
| 113 | + isempty(sample_date_ranges) && |
| 114 | + error("Cannot run calibration with no date ranges for the samples") |
| 115 | + |
| 116 | + sample_date_ranges = [ |
| 117 | + (Dates.DateTime(date_pair[1]), Dates.DateTime(date_pair[2])) for |
| 118 | + date_pair in sample_date_ranges |
| 119 | + ] |
| 120 | + |
| 121 | + for (start_date, stop_date) in sample_date_ranges |
| 122 | + start_date <= stop_date || error( |
| 123 | + "The start date ($start_date) should be before the stop date ($stop_date)", |
| 124 | + ) |
| 125 | + end |
| 126 | + issorted(sample_date_ranges) || |
| 127 | + error("The samples in $sample_date_ranges should be sorted") |
| 128 | + |
| 129 | + minibatch_size > 0 || error("The minibatch size ($minibatch_size) should be positive") |
| 130 | + n_iterations > 0 || error("The number of iterations ($n_iterations) should be positive") |
| 131 | + |
| 132 | + num_samples = length(sample_date_ranges) |
| 133 | + minibatch_size > num_samples && error( |
| 134 | + "The minibatch size is $minibatch_size, but the number of samples is $num_samples", |
| 135 | + ) |
| 136 | + |
| 137 | + remaining = num_samples % minibatch_size |
| 138 | + remaining == 0 || @warn( |
| 139 | + "Number of samples is not divisible by the minibatch size; the last $remaining samples may be missing when running the calibration" |
| 140 | + ) |
| 141 | + |
| 142 | + return CalibrateConfig( |
| 143 | + config_file, |
| 144 | + short_names, |
| 145 | + minibatch_size, |
| 146 | + n_iterations, |
| 147 | + sample_date_ranges, |
| 148 | + extend, |
| 149 | + spinup, |
| 150 | + output_dir, |
| 151 | + rng_seed, |
| 152 | + ) |
| 153 | + |
| 154 | +end |
0 commit comments