|
| 1 | +# ObservationRecipe |
| 2 | + |
| 3 | +!!! warning |
| 4 | + To enable this extension, use `using ClimaAnalysis` or `import |
| 5 | + ClimaAnalysis`. |
| 6 | + |
| 7 | +When handling weather and climate data, it can be tedious and error-prone when |
| 8 | +setting up the observation for calibration with `EnsembleKalmanProcesses` (or |
| 9 | +`EKP` for short). As such, ClimaCalibrate provides recipes for setting up |
| 10 | +observations consisting of samples, noise covariances, names, and metadata. |
| 11 | + |
| 12 | +## How do I use this to set up observation for calibration with EKP? |
| 13 | + |
| 14 | +All functions assume that any data preprocessing is done with `ClimaAnalysis`. |
| 15 | + |
| 16 | +### Covariance Estimators |
| 17 | + |
| 18 | +There are currently two covariance estimators, `SeasonalDiagonalCovariance` and |
| 19 | +`SVDplusDCovariance`, which are subtypes of `AbstractCovarianceEstimator`. |
| 20 | +`SeasonalDiagonalCovariance` approximates the observation noise covariance as a |
| 21 | +diagonal of variances across all the seasons for each observation, neglecting |
| 22 | +correlations between points. `SVDplusDCovariance` additionally approximates the |
| 23 | +correlations between points from, often limited, time series observations. |
| 24 | + |
| 25 | +### Necessary data preprocessing |
| 26 | + |
| 27 | +The `OutputVar`s should represent **time series data of summary statistics**. |
| 28 | +For example, to compute seasonal averages of a `OutputVar`, one can use |
| 29 | +`ClimaAnalysis.average_season_across_time`, which will produce a `OutputVar` |
| 30 | +that can be used with either `SeasonalDiagonalCovariance` or |
| 31 | +`SVDplusDCovariance`. |
| 32 | + |
| 33 | +```julia |
| 34 | +import ClimaAnalysis |
| 35 | + |
| 36 | +obs_var = ClimaAnalysis.OutputVar( |
| 37 | + "precip.mon.mean.nc", |
| 38 | + "precip", |
| 39 | + new_start_date = start_date, |
| 40 | + shift_by = Dates.firstdayofmonth, |
| 41 | +) |
| 42 | + |
| 43 | +# -- preprocessing for units, times, grid, etc. -- |
| 44 | + |
| 45 | +seasonal_averages = ClimaAnalysis.average_season_across_time(obs_var) |
| 46 | +``` |
| 47 | + |
| 48 | +### Observation |
| 49 | + |
| 50 | +After preprocessing the `OutputVar`s so that they represent time series data of |
| 51 | +summary statistics, one can use set up an `EKP.observation` as shown below. |
| 52 | + |
| 53 | +```julia |
| 54 | +import ClimaAnalysis |
| 55 | +import EnsembleKalmanProcesses as EKP |
| 56 | +import ClimaCalibrate |
| 57 | +import ClimaCalibrate.ObservationRecipe |
| 58 | + |
| 59 | +# Vars are OutputVars preprocessed to ensure consistent units, times, |
| 60 | +# and grid as the diagonstics produced from the model. |
| 61 | +# In this example, we want to calibrate with seasonal averages, so we use |
| 62 | +# ClimaAnalysis.average_season_across_time |
| 63 | +vars = ClimaAnalysis.average_season_across_time.(vars) |
| 64 | + |
| 65 | +# We want the covariance matrix to be Float32, so we change it here. |
| 66 | +vars = ObservationRecipe.change_data_type.(vars, Float32) |
| 67 | + |
| 68 | +# We choose SVDplusDCovariance. We need to supply the start and end dates of |
| 69 | +# the samples with `sample_date_ranges`. To do this, we can use the function |
| 70 | +# below. In this example, the dates in `vars` are all the same. For debugging, |
| 71 | +# it is helpful to use `ClimaAnalysis.dates(var)`. |
| 72 | +sample_date_ranges = |
| 73 | + ObservationRecipe.seasonally_aligned_yearly_sample_date_ranges(first(vars)) |
| 74 | +covar_estimator = SVDplusDCovariance( |
| 75 | + sample_date_ranges, |
| 76 | + model_error_scale = Float32(0.05), |
| 77 | + regularization = Float32(1e-6), |
| 78 | +) |
| 79 | + |
| 80 | +# Finally, we form the observation |
| 81 | +start_date = sample_date_ranges[1][1] |
| 82 | +end_date = sample_date_ranges[1][2] |
| 83 | +obs = ObservationRecipe.observation( |
| 84 | + covar_estimator, |
| 85 | + vars, |
| 86 | + start_date = start_date, |
| 87 | + end_date = end_date, |
| 88 | +) |
| 89 | +``` |
| 90 | + |
| 91 | +## Frequently asked questions |
| 92 | + |
| 93 | +**Q: I need to compute `g_ensemble` and I do not know how the data of the `OutputVar`s is flattened.** |
| 94 | + |
| 95 | +**A:** When forming the sample, the data in a `OutputVar` is flattened using |
| 96 | +`ClimaAnalysis.flatten`. See |
| 97 | +[`ClimaAnalysis.flatten`](https://clima.github.io/ClimaAnalysis.jl/dev/flat/#Flatten) |
| 98 | +in the ClimaAnalysis documentation for more information. The order of the |
| 99 | +variables in the observation is the same as the order of the `OutputVar`s when |
| 100 | +creating the `EKP.Observation` using `ObservationRecipe.observation`. |
| 101 | + |
| 102 | +**Q: How do I handle `NaN`s in the `OutputVar`s so that there are no `NaN`s in the sample and covariance matrix?** |
| 103 | + |
| 104 | +**A:** `NaN`s should be handled when preprocessing the data. In some cases, |
| 105 | +there will be `NaN`s in the data (e.g. calibrating with data that is valid only |
| 106 | +over land). In these cases, the functions for making observations will |
| 107 | +automatically remove `NaN`s from the data. It is important to ensure that across |
| 108 | +the time slices, the `NaN`s appear in the same coordinates of the non-temporal |
| 109 | +dimensions. For example, if the quantity is defined over the dimensions |
| 110 | +longitude, latitude, and time, then any slice of the data at a particular |
| 111 | +longitude and latitude should either only contain `NaN`s or no `NaN`s at all. |
| 112 | + |
| 113 | +**Q: How is the name of the observation determined?** |
| 114 | + |
| 115 | +**A:** The name of the observation is determined by the short name in the |
| 116 | +attributes of the `OutputVar`. If there are multiple `OutputVar`s, then the name |
| 117 | +is all the short names separated by semicolons. If no short name is found, then |
| 118 | +the name will be `nothing`. |
| 119 | + |
| 120 | +**Q: What is `regularization` and `model_error_scale` when making a covariance matrix?** |
| 121 | + |
| 122 | +**A:** The model error scale and regularization terms are used to inflate the |
| 123 | +diagonal of the observation covariance matrix to reflect estimates of |
| 124 | +measurement error. You can add a fixed percentage inflation of the noise due to |
| 125 | +the model error to the covariance matrix with the `model_error_scale` keyword |
| 126 | +argument. Additionally, to prevent very small variance along the diagonal of the |
| 127 | +covariance matrix, you can add a regularization with the `regularization` |
| 128 | +keyword argument. |
0 commit comments