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;
18  
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import fr.inrae.agroclim.indicators.model.TimeScale;
23  import jakarta.xml.bind.annotation.XmlAccessType;
24  import jakarta.xml.bind.annotation.XmlAccessorType;
25  import jakarta.xml.bind.annotation.XmlEnum;
26  import jakarta.xml.bind.annotation.XmlEnumValue;
27  import lombok.Getter;
28  import lombok.NonNull;
29  
30  /**
31   * Climatic or soil variables usable to compute indicators.
32   *
33   * Last changed : $Date$
34   *
35   * @author $Author$
36   * @version $Revision$
37   */
38  @XmlAccessorType(XmlAccessType.FIELD)
39  @XmlEnum
40  public enum Variable {
41      /**
42       * Instantaneous hourly air temperature [°C].
43       */
44      @XmlEnumValue("th")
45      TH(Type.CLIMATIC, TimeScale.HOURLY),
46      /**
47       * Minimal air temperature [°C].
48       */
49      @XmlEnumValue("tmin")
50      TMIN(Type.CLIMATIC, TimeScale.DAILY),
51      /**
52       * Maximal air temperature [°C].
53       */
54      @XmlEnumValue("tmax")
55      TMAX(Type.CLIMATIC, TimeScale.DAILY),
56      /**
57       * Average air temperature [°C].
58       */
59      @XmlEnumValue("tmean")
60      TMEAN(Type.CLIMATIC, TimeScale.DAILY),
61      /**
62       * Global radiation [W/m²].
63       */
64      @XmlEnumValue("radiation")
65      RADIATION(Type.CLIMATIC, TimeScale.DAILY),
66      /**
67       * Rain precipitation during the period [mm].
68       */
69      @XmlEnumValue("rain")
70      RAIN(Type.CLIMATIC, TimeScale.DAILY, TimeScale.HOURLY),
71      /**
72       * Wind speed [m/s].
73       */
74      @XmlEnumValue("wind")
75      WIND(Type.CLIMATIC, TimeScale.DAILY, TimeScale.HOURLY),
76      /**
77       * Evapotranspiration [mm/d].
78       */
79      @XmlEnumValue("etp")
80      ETP(Type.CLIMATIC, TimeScale.DAILY),
81      /**
82       * Relative humidity [%].
83       *
84       * Range: 0-100.
85       */
86      @XmlEnumValue("rh")
87      RH(Type.CLIMATIC, TimeScale.DAILY, TimeScale.HOURLY),
88      /**
89       * Soil water reserve [mm].
90       */
91      @XmlEnumValue("waterreserve")
92      WATER_RESERVE(Type.SOIL, TimeScale.DAILY),
93      /**
94       * Soil water content [% mass].
95       */
96      @XmlEnumValue("soilwatercontent")
97      SOILWATERCONTENT(Type.SOIL, TimeScale.DAILY);
98  
99      /**
100      * Type of variable.
101      */
102     public enum Type {
103         /**
104          * Climatic variable.
105          */
106         CLIMATIC,
107         /**
108          * Soil variable.
109          */
110         SOIL;
111     }
112 
113     /**
114      * @param variableName name of variable to find.
115      * @return found variable or null
116      */
117     public static Variable getByName(final String variableName) {
118         if (variableName == null) {
119             throw new NullPointerException("Variable name is null!");
120         }
121         return valueOf(variableName.toUpperCase());
122     }
123 
124     /**
125      * @param timescale time scale of variable
126      * @param type type of variable
127      * @return filtered set of variable according to TimeScale and Type.
128      */
129     public static Set<Variable> getByTimeScaleAndType(@NonNull final TimeScale timescale, final Type type) {
130         final Set<Variable> variables = new HashSet<>();
131         for (final Variable variable : values()) {
132             if (variable.getType().equals(type)) {
133                 for (final TimeScale ts : variable.getTimescales()) {
134                     if (ts.equals(timescale)) {
135                         variables.add(variable);
136                     }
137                 }
138             }
139         }
140         return variables;
141     }
142 
143     /**
144      * @param type type of variable
145      * @return filtered set of variable according to Type.
146      */
147     public static Set<Variable> getByType(final Type type) {
148         final Set<Variable> variables = new HashSet<>();
149         for (final Variable var : values()) {
150             if (var.getType().equals(type)) {
151                 variables.add(var);
152             }
153         }
154         return variables;
155     }
156 
157     /**
158      * Related time scales.
159      */
160     @Getter
161     private final TimeScale[] timescales;
162 
163     /**
164      * Variable type.
165      */
166     @Getter
167     private final Type type;
168 
169     /**
170      * Constructor.
171      *
172      * @param variableType variable type
173      * @param variableTimescales related time scales
174      */
175     Variable(final Type variableType, final TimeScale... variableTimescales) {
176         type = variableType;
177         if (variableTimescales == null) {
178             timescales = new TimeScale[]{TimeScale.DAILY};
179         } else {
180             timescales = variableTimescales;
181         }
182     }
183 
184     /**
185      * @return variable name
186      */
187     public String getName() {
188         return name().toLowerCase();
189     }
190 }