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.Date;
20 import java.util.List;
21 import java.util.Set;
22
23 import fr.inrae.agroclim.indicators.model.data.Resource;
24 import fr.inrae.agroclim.indicators.model.data.Variable;
25 import fr.inrae.agroclim.indicators.util.DateUtils;
26
27 /**
28 * Storage of climatic daily data.
29 *
30 * Last change $Date$
31 *
32 * @author $Author$
33 * @version $Revision$
34 */
35 public class ClimaticResource extends Resource<ClimaticDailyData> {
36 /**
37 * UUID for Serializable.
38 */
39 private static final long serialVersionUID = 4872847709393688049L;
40
41 /**
42 * Constructor.
43 */
44 public ClimaticResource() {
45 super();
46 }
47
48 /**
49 * @param dailyData
50 * a climatic daily data to add
51 */
52 private void addData(final ClimaticDailyData dailyData) {
53 getData().add(dailyData);
54 addYear(dailyData.getYear());
55 fireDataLoadingAddEvent(dailyData);
56 }
57
58 @Override
59 public final ClimaticResource clone() throws CloneNotSupportedException {
60 final ClimaticResource clone = (ClimaticResource) super.clone();
61 clone.getData().addAll(getData());
62 return clone;
63 }
64
65 /**
66 * Create a new resource for the period.
67 *
68 * @param startDate period start included
69 * @param endDate period end excluded
70 * @return new resource with data
71 */
72 public final ClimaticResource getClimaticDataByPhaseAndYear(
73 final Date startDate, final Date endDate) {
74 final ClimaticResource newResource = new ClimaticResource();
75 newResource.setMissingVariables(getMissingVariables());
76
77 getData().stream().filter(dailyData -> {
78 final Date date = dailyData.getDate();
79 if (date != null) {
80 return date.equals(startDate)
81 || dailyData.getDate().after(startDate)
82 && dailyData.getDate().before(endDate);
83 } else {
84 return false;
85 }
86
87 }
88 ).forEach(newResource::addData);
89 return newResource;
90 }
91
92 /**
93 * Create a new resource for the period.
94 *
95 * @param start
96 * period start (day of year) included
97 * @param end
98 * period end (day of year) excluded
99 * @param year
100 * period year
101 * @return new resource with data
102 */
103 public final ClimaticResource getClimaticDataByPhaseAndYear(
104 final int start, final int end, final int year) {
105 final Date startDate = DateUtils.getDate(year, start);
106 final Date endDate = DateUtils.getDate(year, end);
107 return getClimaticDataByPhaseAndYear(startDate, endDate);
108 }
109
110 /**
111 * @param list all climatic daily data for the resource
112 */
113 public final void setData(final List<ClimaticDailyData> list) {
114 getData().clear();
115 getYears().clear();
116 list.forEach(this::addData);
117 fireDataLoadingEndEvent(list.size() + " climatic data added!");
118 }
119 /**
120 * Check if current year has complete data.
121 * If at least one variable in the dataset has at least a zero value, then the year is considered incomplete.
122 * @param providedVariables List of variables present in the climate file
123 * @return {@code true} if the data for the year are all present or {@code false} otherwise
124 */
125 public boolean hasCompleteDataInYear(final Set<Variable> providedVariables) {
126 boolean state = true;
127 for (final ClimaticDailyData cdd : this.getData()) { // Pour chaque ligne remontée pour la phase / année
128 for (final Variable v : providedVariables) { // Pour chaque variable du fichier climatique
129 final Double value = cdd.getValue(v);
130
131 state = !(value == null) && state;
132 }
133 }
134 return state;
135 }
136 }