gvsig-raster / org.gvsig.raster.netcdf / trunk / org.gvsig.raster.netcdf / org.gvsig.raster.netcdf.io / src / main / java / org / gvsig / raster / netcdf / io / NetCDFDataParametersImpl.java @ 2460
History | View | Annotate | Download (9.48 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.raster.netcdf.io; |
29 |
|
30 |
import java.io.IOException; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Date; |
33 |
import java.util.HashMap; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
|
37 |
import org.gvsig.raster.impl.store.AbstractRasterFileDataParameters; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
40 |
import org.gvsig.tools.dynobject.DynStruct; |
41 |
import org.gvsig.tools.persistence.PersistenceManager; |
42 |
|
43 |
import ucar.nc2.Dimension; |
44 |
import ucar.nc2.NetcdfFile; |
45 |
import ucar.nc2.Variable; |
46 |
import ucar.nc2.dataset.CoordinateAxis1D; |
47 |
import ucar.nc2.dataset.CoordinateAxis1DTime; |
48 |
import ucar.nc2.dt.GridCoordSystem; |
49 |
import ucar.nc2.dt.GridDatatype; |
50 |
import ucar.nc2.dt.grid.GridDataset; |
51 |
|
52 |
/**
|
53 |
* Parameters for the NetCDF provider
|
54 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
55 |
*/
|
56 |
public class NetCDFDataParametersImpl extends AbstractRasterFileDataParameters implements NetCDFDataParameters { |
57 |
private ArrayList<String[]> gridVarList = null; |
58 |
private ArrayList<Variable> varList = null; |
59 |
private HashMap<String, double[]> rankByVar = new HashMap<String, double[]>(); |
60 |
private HashMap<String, Date[]> dateByVar = new HashMap<String, Date[]>(); |
61 |
private String xDimVariable = null; |
62 |
private String yDimVariable = null; |
63 |
|
64 |
|
65 |
public NetCDFDataParametersImpl() {
|
66 |
super();
|
67 |
initialize(); |
68 |
} |
69 |
|
70 |
protected void initialize() { |
71 |
delegatedDynObject = (DelegatedDynObject) ToolsLocator |
72 |
.getDynObjectManager().createDynObject( |
73 |
registerDynClass()); |
74 |
} |
75 |
|
76 |
public static DynStruct registerDynClass() { |
77 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
78 |
DynStruct definition = manager.getDefinition("NetCDFDataParametersImpl_Persistent");
|
79 |
if( definition == null ) { |
80 |
definition = manager.addDefinition( |
81 |
NetCDFDataParametersImpl.class, |
82 |
"NetCDFDataParametersImpl_Persistent",
|
83 |
"NetCDFDataParametersImpl Persistent",
|
84 |
null,
|
85 |
null
|
86 |
); |
87 |
AbstractRasterFileDataParameters.registerDynClass(definition); |
88 |
} |
89 |
|
90 |
definition.addDynFieldString(FIELD_VARIABLE) |
91 |
.setDescription("Grid variable")
|
92 |
.setMandatory(false);
|
93 |
|
94 |
definition.addDynFieldInt(FIELD_LEVEL) |
95 |
.setDescription("Level selected")
|
96 |
.setMandatory(false);
|
97 |
|
98 |
definition.addDynFieldInt(FIELD_TIME) |
99 |
.setDescription("Time selected")
|
100 |
.setMandatory(false);
|
101 |
|
102 |
definition.addDynFieldString(FIELD_XDIM). |
103 |
setMandatory(false);
|
104 |
|
105 |
definition.addDynFieldString(FIELD_YDIM). |
106 |
setMandatory(false);
|
107 |
|
108 |
return definition;
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Gets the list of grids
|
113 |
* @return A list of pair of values. The first element of one pair is the variable's name
|
114 |
* and the second its description
|
115 |
* @throws IOException
|
116 |
*/
|
117 |
public ArrayList<Variable> getVariables() throws IOException { |
118 |
if(varList == null) { |
119 |
NetcdfFile netCDFFile = NetcdfFile.open(getFile().getAbsolutePath()); |
120 |
varList = new ArrayList<Variable>(); |
121 |
|
122 |
List<Variable> variableList = netCDFFile.getVariables();
|
123 |
Iterator<Variable> it = variableList.iterator();
|
124 |
while(it.hasNext()) {
|
125 |
Variable var = it.next(); |
126 |
varList.add(var); |
127 |
} |
128 |
netCDFFile.close(); |
129 |
} |
130 |
return varList;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Gets the list of grids
|
135 |
* @return A list of pair of values. The first element of one pair is the variable's name
|
136 |
* and the second its description
|
137 |
* @throws IOException
|
138 |
*/
|
139 |
public ArrayList<String[]> getGridVariables() throws IOException { |
140 |
if(gridVarList == null) { |
141 |
GridDataset connector = GridDataset.open(getFile().getAbsolutePath()); |
142 |
List<GridDatatype> gridList = connector.getGrids();
|
143 |
gridVarList = new ArrayList<String[]>(); |
144 |
Iterator<GridDatatype> it = gridList.iterator();
|
145 |
while(it.hasNext()) {
|
146 |
GridDatatype dt = it.next(); |
147 |
|
148 |
//Loads name and description
|
149 |
String[] pair = new String[2]; |
150 |
pair[0] = dt.getName();
|
151 |
pair[1] = dt.getDescription();
|
152 |
gridVarList.add(pair); |
153 |
|
154 |
//Loads variable's name
|
155 |
Dimension dim = dt.getDimensions().get(dt.getXDimensionIndex());
|
156 |
xDimVariable = dim.getName(); |
157 |
dim = dt.getDimensions().get(dt.getYDimensionIndex()); |
158 |
yDimVariable = dim.getName(); |
159 |
|
160 |
if(dt.getShape() != null) { |
161 |
GridCoordSystem gcs = dt.getCoordinateSystem(); |
162 |
//Loads levels
|
163 |
CoordinateAxis1D zAxis = gcs.getVerticalAxis(); |
164 |
if(zAxis != null) { |
165 |
double[] d = calcSliceValue(zAxis.getCoordEdges()); |
166 |
rankByVar.put(pair[0], d);
|
167 |
} |
168 |
|
169 |
//Loads time list
|
170 |
if (gcs.hasTimeAxis1D()) {
|
171 |
CoordinateAxis1DTime tAxis1D = gcs.getTimeAxis1D(); |
172 |
Date[] dates = tAxis1D.getTimeDates(); |
173 |
dateByVar.put(pair[0], dates);
|
174 |
} |
175 |
} |
176 |
} |
177 |
connector.close(); |
178 |
} |
179 |
return gridVarList;
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Calculates the array of levels
|
184 |
* @param coordsEdges
|
185 |
* @return
|
186 |
*/
|
187 |
private double[] calcSliceValue(double[] coordsEdges) { |
188 |
if(coordsEdges == null) |
189 |
return null; |
190 |
double[] values = new double[coordsEdges.length - 1]; |
191 |
for (int i = 0; i < coordsEdges.length - 1; i++) { |
192 |
values[i] = ((coordsEdges[i] + coordsEdges[i + 1]) / 2); |
193 |
} |
194 |
return values;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Gets the list of dates
|
199 |
* @param var
|
200 |
* @return
|
201 |
*/
|
202 |
public Date[] getTimeList(String var) { |
203 |
return dateByVar.get(var);
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* Gets the number of levels of one variable
|
208 |
* @param var
|
209 |
* @return
|
210 |
*/
|
211 |
public double[] getLevelList(String var) { |
212 |
return rankByVar.get(var);
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Gets the variable's name of the X dimension
|
217 |
* @return
|
218 |
*/
|
219 |
public String getXDimVariable() { |
220 |
return xDimVariable;
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* Gets the variable's name of the Y dimension
|
225 |
* @return
|
226 |
*/
|
227 |
public String getYDimVariable() { |
228 |
return yDimVariable;
|
229 |
} |
230 |
|
231 |
public String getDataStoreName() { |
232 |
return NetCDFProvider.NAME;
|
233 |
} |
234 |
|
235 |
public String getDescription() { |
236 |
return NetCDFProvider.DESCRIPTION;
|
237 |
} |
238 |
|
239 |
public void selectNextInstant() { |
240 |
int i = getFieldTime() + 1; |
241 |
if(i < getTimeListLength()) {
|
242 |
setDynValue(NetCDFDataParameters.FIELD_TIME, i); |
243 |
} |
244 |
} |
245 |
|
246 |
public void selectPrevInstant() { |
247 |
int i = getFieldTime() - 1; |
248 |
if(i >= 0) { |
249 |
setDynValue(NetCDFDataParameters.FIELD_TIME, i); |
250 |
} |
251 |
} |
252 |
|
253 |
public void goTo(int position) { |
254 |
if(position < getTimeListLength() && position >= 0) { |
255 |
setDynValue(NetCDFDataParameters.FIELD_TIME, position); |
256 |
} |
257 |
} |
258 |
|
259 |
public int getFieldTime() { |
260 |
if(hasDynValue(NetCDFDataParameters.FIELD_TIME) && getDynValue(NetCDFDataParameters.FIELD_TIME) != null) { |
261 |
return ((Integer)getDynValue(NetCDFDataParameters.FIELD_TIME)).intValue(); |
262 |
} |
263 |
return 0; |
264 |
} |
265 |
|
266 |
public int getFieldLevel() { |
267 |
if(hasDynValue(NetCDFDataParameters.FIELD_LEVEL) && getDynValue(NetCDFDataParameters.FIELD_LEVEL) != null) { |
268 |
return ((Integer)getDynValue(NetCDFDataParameters.FIELD_LEVEL)).intValue(); |
269 |
} |
270 |
return 0; |
271 |
} |
272 |
|
273 |
public String getStringVariable() { |
274 |
if(hasDynValue(NetCDFDataParameters.FIELD_VARIABLE)) {
|
275 |
return (String)getDynValue(NetCDFDataParameters.FIELD_VARIABLE); |
276 |
} |
277 |
|
278 |
//We will use the first variable in the list
|
279 |
|
280 |
try {
|
281 |
ArrayList<String[]> vars = getGridVariables(); |
282 |
if(vars.size() > 0) { |
283 |
String[] var = vars.get(0); |
284 |
if(var.length > 0) { |
285 |
setFieldVariable(var[0]);
|
286 |
return var[0]; |
287 |
} |
288 |
} |
289 |
} catch (IOException e) { |
290 |
e.printStackTrace(); |
291 |
} |
292 |
return null; |
293 |
} |
294 |
|
295 |
public String getStringLevel() { |
296 |
if(!hasDynValue(NetCDFDataParameters.FIELD_LEVEL)) {
|
297 |
//We will use the first level in the list
|
298 |
setFieldLevel(0);
|
299 |
} |
300 |
|
301 |
int index = ((Integer)getDynValue(NetCDFDataParameters.FIELD_LEVEL)).intValue(); |
302 |
double[] levelList = getLevelList(getStringVariable()); |
303 |
if(levelList == null) |
304 |
return "-1"; |
305 |
return levelList[index] + ""; |
306 |
} |
307 |
|
308 |
public String getStringTime() { |
309 |
if(!hasDynValue(NetCDFDataParameters.FIELD_TIME)) {
|
310 |
setFieldTime(0);
|
311 |
} |
312 |
|
313 |
int index = ((Integer)getDynValue(NetCDFDataParameters.FIELD_TIME)).intValue(); |
314 |
Date[] timeList = getTimeList(getStringVariable()); |
315 |
if(timeList == null) |
316 |
return "-1"; |
317 |
return timeList[index].toString();
|
318 |
} |
319 |
|
320 |
/**
|
321 |
* Gets the length of the time list
|
322 |
* @return
|
323 |
*/
|
324 |
private int getTimeListLength() { |
325 |
String var = (String)getDynValue(NetCDFDataParameters.FIELD_VARIABLE); |
326 |
if(var != null) { |
327 |
Date[] dateList = getTimeList(var); |
328 |
if(dateList != null) |
329 |
return dateList.length;
|
330 |
} |
331 |
return 0; |
332 |
} |
333 |
|
334 |
public void setFieldVariable(String variable) { |
335 |
setDynValue(NetCDFDataParameters.FIELD_VARIABLE, variable); |
336 |
} |
337 |
|
338 |
public void setFieldTime(int timePosInList) { |
339 |
setDynValue(NetCDFDataParameters.FIELD_TIME, timePosInList); |
340 |
} |
341 |
|
342 |
public void setFieldLevel(int levelPosInList) { |
343 |
setDynValue(NetCDFDataParameters.FIELD_LEVEL, levelPosInList); |
344 |
} |
345 |
} |