View Javadoc
1   /**
2    * This file is part of Indicators.
3    *
4    * Indicators is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * Indicators is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with Indicators. If not, see <https://www.gnu.org/licenses/>.
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   * Proxy to load climate data.
40   *
41   * Loader is chosen according to properties (XML tag).
42   *
43   * The proxy is initialized from evaluation XML file.
44   *
45   * Last changed : $Date$
46   *
47   * @author $Author$
48   * @version $Revision$
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       * UUID for Serializable.
58       */
59      private static final long serialVersionUID = 455956144995603377L;
60  
61      /**
62       * Calculator to compute ETP from climatic daily data.
63       */
64      @XmlTransient
65      private EtpCalculator etpCalculator;
66  
67      /**
68       * CSV file.
69       */
70      @Getter
71      @Setter
72      @XmlElement(name = "file")
73      private ClimateFileLoader file;
74  
75      /**
76       * Generic loader.
77       */
78      @Setter
79      @XmlTransient
80      private ClimateLoader loader;
81  
82      /**
83       * Related time scales.
84       */
85      @Setter
86      @XmlTransient
87      private TimeScale timeScale = TimeScale.DAILY;
88  
89      /**
90       * Wind speed is measure at 10m. If not set, true.
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      * @return Calculator to compute ETP from climatic daily data.
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      * @return file/database loader according to configuration.
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      * @return Missing climatic variables, to check in aggregation indicators.
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      * @return variables provided by the loader
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 }