|
| 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 | + "The short names of the observations used for calibration. The short names |
| 22 | + should match the same names used for the diagnostics." |
| 23 | + short_names::Vector{String} |
| 24 | + |
| 25 | + "The size of the minibatch for each iteration" |
| 26 | + minibatch_size::Int64 |
| 27 | + |
| 28 | + "The number of iterations to run the calibration for" |
| 29 | + n_iterations::Int64 |
| 30 | + |
| 31 | + "The date ranges of the samples for calibration and used to determine the |
| 32 | + start and end dates of a simulation for each iteration of calibration" |
| 33 | + sample_date_ranges::Vector{NTuple{2, Dates.DateTime}} |
| 34 | + |
| 35 | + "The amount of time to run a simulation after the last date of the |
| 36 | + minibatch" |
| 37 | + extend::EXTEND |
| 38 | + |
| 39 | + "The amount of time to run a simulation before the first date of the |
| 40 | + minibatch" |
| 41 | + spinup::SPINUP |
| 42 | + |
| 43 | + "The number of horizontal and vertical elements of the model. Used for the |
| 44 | + simulation and determining the ocean mask" |
| 45 | + nelements::Tuple{Int64, Int64} |
| 46 | + |
| 47 | + "The directory to store the iterations and members of the calibration." |
| 48 | + output_dir::String |
| 49 | + |
| 50 | + "An integer value for ensuring calibrations are the same between multiple |
| 51 | + calibrations with the same settings" |
| 52 | + rng_seed::Int64 |
| 53 | +end |
| 54 | + |
| 55 | +""" |
| 56 | + CalibrateConfig(; |
| 57 | + short_names, |
| 58 | + sample_date_ranges, |
| 59 | + extend, |
| 60 | + spinup = Dates.Month(3), |
| 61 | + minibatch_size, |
| 62 | + n_iterations, |
| 63 | + nelements = (101, 15), |
| 64 | + output_dir = "experiments/calibration/land_model", |
| 65 | + rng_seed = 42, |
| 66 | + ) |
| 67 | +
|
| 68 | +Initializes a CalibrateConfig, which is of interest to a user running |
| 69 | +calibration or contains values needed in multiple places during calibration. |
| 70 | +
|
| 71 | +Keyword arguments |
| 72 | +===================== |
| 73 | +
|
| 74 | +- `short_names`: Short names of the observations. The currently supported short |
| 75 | + names are `lhf`, `shf`, `lwu`, and `swu`. |
| 76 | +
|
| 77 | +- `minibatch_size`: The size of the minibatch for each iteration. |
| 78 | +
|
| 79 | +- `n_iterations`: The number of iterations to run the calibration for. |
| 80 | +
|
| 81 | +- `sample_date_ranges`: The date ranges for each sample. The dates should be the |
| 82 | + same as found in the time series data of the observations. Since the land |
| 83 | + calibration calibrates using seasonal averages, the times passed must be the |
| 84 | + first day of December, March, June, or September. The seasons are December to |
| 85 | + February (DJF), March to May (MAM), June to August (JJA), and September to |
| 86 | + November (SON). In addition, the start and end dates of the simulation is |
| 87 | + automatically determined from `sample_date_ranges`. |
| 88 | +
|
| 89 | +- `extend`: The amount of time to run the simulation after the end date |
| 90 | + determined by `sample_date_ranges`. For seasonal averages, `extend` should be |
| 91 | + `Dates.Month(3)` and for monthly averages, `extend` should be |
| 92 | + `Dates.Month(1)`. |
| 93 | +
|
| 94 | +- `spinup`: The amount of time to run the simulation before the start date |
| 95 | + determined by `sample_date_ranges`. |
| 96 | +
|
| 97 | +- `nelements`: The resolution of the model. This is also used to determine the |
| 98 | + mask of the observations. |
| 99 | +
|
| 100 | +- `output_dir`: The location to save the calibration at. |
| 101 | +
|
| 102 | +- `rng_seed`: An integer to ensure that calibration runs with the same settings |
| 103 | + are the same. |
| 104 | +""" |
| 105 | +function CalibrateConfig(; |
| 106 | + short_names, |
| 107 | + minibatch_size, |
| 108 | + n_iterations, |
| 109 | + sample_date_ranges, |
| 110 | + extend, |
| 111 | + spinup = Dates.Month(3), |
| 112 | + nelements = (101, 15), |
| 113 | + output_dir = "experiments/calibration/land_model", |
| 114 | + rng_seed = 42, |
| 115 | +) |
| 116 | + isempty(short_names) && error("Cannot run calibration with no short names") |
| 117 | + isempty(sample_date_ranges) && |
| 118 | + error("Cannot run calibration with no date ranges for the samples") |
| 119 | + |
| 120 | + sample_date_ranges = [ |
| 121 | + (Dates.DateTime(date_pair[1]), Dates.DateTime(date_pair[2])) for |
| 122 | + date_pair in sample_date_ranges |
| 123 | + ] |
| 124 | + |
| 125 | + for (start_date, end_date) in sample_date_ranges |
| 126 | + start_date <= end_date || error( |
| 127 | + "The start date ($start_date) should be before the end date ($end_date)", |
| 128 | + ) |
| 129 | + end |
| 130 | + issorted(sample_date_ranges) || |
| 131 | + error("The samples in $sample_date_ranges should be sorted") |
| 132 | + |
| 133 | + minibatch_size > 0 || |
| 134 | + error("The minibatch size ($minibatch_size) should be positive") |
| 135 | + n_iterations > 0 || |
| 136 | + error("The number of iterations ($n_iterations) should be positive") |
| 137 | + |
| 138 | + num_samples = length(sample_date_ranges) |
| 139 | + minibatch_size > num_samples && error( |
| 140 | + "The minibatch size is $minibatch_size, but the number of samples is $num_samples", |
| 141 | + ) |
| 142 | + |
| 143 | + remaining = num_samples % minibatch_size |
| 144 | + remaining == 0 || @warn( |
| 145 | + "Number of samples is not divisible by the minibatch size; the last $remaining samples may be missing when running the calibration" |
| 146 | + ) |
| 147 | + |
| 148 | + return CalibrateConfig( |
| 149 | + short_names, |
| 150 | + minibatch_size, |
| 151 | + n_iterations, |
| 152 | + sample_date_ranges, |
| 153 | + extend, |
| 154 | + spinup, |
| 155 | + nelements, |
| 156 | + output_dir, |
| 157 | + rng_seed, |
| 158 | + ) |
| 159 | + |
| 160 | +end |
0 commit comments