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.soil;
18  
19  import java.io.File;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import fr.inrae.agroclim.indicators.model.TimeScale;
29  import fr.inrae.agroclim.indicators.model.data.FileLoader;
30  import fr.inrae.agroclim.indicators.model.data.Resource;
31  import fr.inrae.agroclim.indicators.model.data.Variable;
32  import lombok.EqualsAndHashCode;
33  
34  /**
35   * Load soil data from file.
36   *
37   * Last changed : $Date$
38   *
39   * @author $Author$
40   * @version $Revision$
41   */
42  @EqualsAndHashCode(callSuper = true, of = {"file", "headers", "separator"})
43  public final class SoilFileLoader extends FileLoader implements SoilLoader {
44      /**
45       * UID for serialization.
46       */
47      private static final long serialVersionUID = -2430155206967092814L;
48      /**
49       * Allowed headers for the CSV file.
50       */
51      private static final List<String> ALLOWED_HEADERS = Arrays.asList("year",
52              "month", "day", "swc", "waterReserve");
53      /**
54       * CSV file.
55       */
56      private final File file;
57  
58      /**
59       * Headers of CSV file.
60       */
61      private final String[] headers;
62  
63      /**
64       * CSV separator.
65       */
66      private String separator = Resource.DEFAULT_SEP;
67  
68      /**
69       * Constructor.
70       *
71       * @param csvFile
72       *            CSV file
73       * @param csvHeaders
74       *            CSV headers
75       * @param csvSeparator
76       *            CSV separator
77       */
78      public SoilFileLoader(final File csvFile, final String[] csvHeaders,
79              final String csvSeparator) {
80          this.file = csvFile;
81          this.headers = csvHeaders;
82          this.separator = csvSeparator;
83      }
84  
85      @Override
86      public SoilFileLoader clone() {
87          return new SoilFileLoader(file, headers, separator);
88      }
89  
90      @Override
91      public Map<String, String> getConfigurationErrors() {
92          final Map<String, String> errors = new HashMap<>();
93          if (file == null) {
94              errors.put("soil.file", "error.evaluation.soil.file.missing");
95          } else if (!file.exists()) {
96              errors.put("soil.file", "error.evaluation.soil.file.doesnotexist");
97          } else if (file.length() == 0) {
98              errors.put("soil.file", "error.evaluation.soil.file.empty");
99          }
100         if (separator == null) {
101             errors.put("soil.separator",
102                     "error.evaluation.soil.separator.missing");
103         } else if (separator.isEmpty()) {
104             errors.put("soil.separator",
105                     "error.evaluation.soil.separator.empty");
106         }
107         if (headers == null) {
108             errors.put("soil.header", "error.evaluation.soil.header.missing");
109         }
110         if (errors.isEmpty()) {
111             return null;
112         }
113         return errors;
114     }
115 
116     @Override
117     public Collection<String> getMissingVariables() {
118         throw new RuntimeException("Not implemented for soil!");
119     }
120 
121     @Override
122     public Set<Variable> getProvidedVariables() {
123         return super.getProvidedVariables(headers);
124     }
125 
126     @Override
127     public Set<Variable> getVariables() {
128         return new HashSet<>();
129     }
130 
131     @Override
132     public List<SoilDailyData> load() {
133         return load(file, separator, headers, ALLOWED_HEADERS,
134                 SoilDailyData.class);
135     }
136 
137     @Override
138     public void setTimeScale(final TimeScale timeScale) {
139         // do nothing
140     }
141 }