1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package fr.inrae.agroclim.indicators.model.data.climate;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import fr.inrae.agroclim.indicators.model.TimeScale;
28 import fr.inrae.agroclim.indicators.model.data.DataLoadingListenerHandler;
29 import fr.inrae.agroclim.indicators.model.data.Variable;
30 import jakarta.xml.bind.annotation.XmlAccessType;
31 import jakarta.xml.bind.annotation.XmlAccessorType;
32 import jakarta.xml.bind.annotation.XmlElement;
33 import jakarta.xml.bind.annotation.XmlTransient;
34 import lombok.EqualsAndHashCode;
35 import lombok.Getter;
36 import lombok.Setter;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 @XmlAccessorType(XmlAccessType.FIELD)
51 @EqualsAndHashCode(
52 callSuper = false,
53 of = {"etpCalculator", "file", "loader", "windAt10"}
54 )
55 public final class ClimateLoaderProxy extends DataLoadingListenerHandler implements ClimateLoader {
56
57
58
59 private static final long serialVersionUID = 455956144995603377L;
60
61
62
63
64 @XmlTransient
65 private EtpCalculator etpCalculator;
66
67
68
69
70 @Getter
71 @Setter
72 @XmlElement(name = "file")
73 private ClimateFileLoader file;
74
75
76
77
78 @Setter
79 @XmlTransient
80 private ClimateLoader loader;
81
82
83
84
85 @Setter
86 @XmlTransient
87 private TimeScale timeScale = TimeScale.DAILY;
88
89
90
91
92 @XmlElement(name = "windspeedat10m")
93 private Boolean windAt10;
94
95 @Override
96 public ClimateLoaderProxy clone() throws CloneNotSupportedException {
97 final ClimateLoaderProxy clone = new ClimateLoaderProxy();
98 if (etpCalculator != null) {
99 clone.etpCalculator = etpCalculator.clone();
100 }
101 if (file != null) {
102 clone.file = file.clone();
103 }
104 if (loader != null) {
105 clone.loader = loader.clone();
106 }
107 clone.windAt10 = windAt10;
108 return clone;
109 }
110
111 @Override
112 public Map<String, String> getConfigurationErrors() {
113 if (getLoader() == null) {
114 final Map<String, String> errors = new HashMap<>();
115 errors.put("climate", "error.evaluation.climate.loader.missing");
116 return errors;
117 }
118 return getLoader().getConfigurationErrors();
119 }
120
121
122
123
124 public EtpCalculator getEtpCalculator() {
125 if (etpCalculator == null) {
126 etpCalculator = new EtpPenmanMonteithFAO(windAt10 == null
127 || windAt10);
128 }
129 return etpCalculator;
130 }
131
132
133
134
135 private ClimateLoader getLoader() {
136 if (file != null) {
137 file.addDataLoadingListeners(getDataLoadingListeners());
138 file.setEtpCalculator(getEtpCalculator());
139 file.setTimeScale(timeScale);
140 return file;
141 } else if (loader != null) {
142 loader.addDataLoadingListeners(getDataLoadingListeners());
143 return loader;
144 }
145 return null;
146 }
147
148
149
150
151 @Override
152 public Collection<String> getMissingVariables() {
153 if (getLoader() == null) {
154 return new HashSet<>();
155 }
156 return getLoader().getMissingVariables();
157 }
158
159
160
161
162 @Override
163 public Set<Variable> getProvidedVariables() {
164 if (getLoader() == null) {
165 return new HashSet<>();
166 }
167 return getLoader().getProvidedVariables();
168 }
169
170 @Override
171 public Set<Variable> getVariables() {
172 if (getLoader() == null) {
173 return new HashSet<>();
174 }
175 return getLoader().getVariables();
176 }
177
178 @Override
179 public List<ClimaticDailyData> load() {
180 if (getLoader() == null) {
181 return new ArrayList<>();
182 }
183 getLoader().setTimeScale(timeScale);
184 return getLoader().load();
185 }
186 }