Statistics
| Revision:

gvsig-raster / org.gvsig.raster.netcdf / trunk / org.gvsig.raster.netcdf / org.gvsig.raster.netcdf.io / src / main / java / org / gvsig / raster / netcdf / io / NetCDFDataParameters.java @ 444

History | View | Annotate | Download (6.2 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.AbstractRasterDataParameters;
38
import org.gvsig.raster.impl.store.AbstractRasterFileDataParameters;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DelegatedDynObject;
41
import org.gvsig.tools.dynobject.DynClass;
42
import org.gvsig.tools.dynobject.DynField;
43
import org.gvsig.tools.dynobject.DynObjectManager;
44

    
45
import ucar.nc2.Dimension;
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 NetCDFDataParameters extends AbstractRasterFileDataParameters {
57
        public static final String            PARAMETERS_DEFINITION_NAME = "NetCDFDataParameters";
58
        public static final String            FIELD_VARIABLE             = "GridVariable";
59
        public static final String            FIELD_TIME                 = "Time";
60
        public static final String            FIELD_LEVEL                = "Level";
61
        private static DynClass               DYNCLASS                   = null;
62
        
63
        private ArrayList<String[]>           varList                    = null;
64
        private HashMap<String, double[]>     rankByVar                  = new HashMap<String, double[]>();
65
        private HashMap<String, Date[]>       dateByVar                  = new HashMap<String, Date[]>();
66
        private String                        xDimVariable               = null;
67
        private String                        yDimVariable               = null;
68
        
69
        
70
        public NetCDFDataParameters() {
71
                super();
72
                initialize();
73
        }
74
        
75
        protected void initialize() {
76
                delegatedDynObject = (DelegatedDynObject) ToolsLocator
77
                                .getDynObjectManager().createDynObject(
78
                                                DYNCLASS);
79
        }
80
        
81
        public static void registerDynClass() {
82
                DynObjectManager dynman = ToolsLocator.getDynObjectManager();
83
                DynClass dynClass;
84
                DynField field;
85
                
86
                if(dynman == null)
87
                        return;
88
                
89
                if (DYNCLASS == null) {
90
                        dynClass = AbstractRasterDataParameters.registerDynClass(PARAMETERS_DEFINITION_NAME);
91
                        //dynClass = dynman.add(AbstractRasterDataParameters.DYNCLASS_NAME);
92
                        
93
                        field = dynClass.addDynFieldString(FIELD_VARIABLE);
94
            field.setDescription("Grid variable");
95
            field.setMandatory(false);
96
            field.setClassOfValue(String.class); 
97
            
98
                        field = dynClass.addDynFieldInt(FIELD_LEVEL);
99
            field.setDescription("Level selected");
100
            field.setMandatory(false);
101
            field.setClassOfValue(Integer.class);
102
            
103
                        field = dynClass.addDynFieldInt(FIELD_TIME);
104
            field.setDescription("Time selected");
105
            field.setMandatory(false);
106
            field.setClassOfValue(Integer.class);
107

    
108
                        DYNCLASS = dynClass;
109
                }
110

    
111
        }
112
        
113
        /**
114
         * Gets the list of grids
115
         * @return A list of pair of values. The first element of one pair is the variable's name 
116
         * and the second its description
117
         * @throws IOException 
118
         */
119
        public ArrayList<String[]> getGridVariables() throws IOException {
120
                if(varList == null) {
121
                        GridDataset connector = GridDataset.open(getFile().getAbsolutePath());
122
                        List<GridDatatype> gridList = connector.getGrids();
123
                        varList = new ArrayList<String[]>();
124
                        Iterator<GridDatatype> it = gridList.iterator();
125
                        while(it.hasNext()) {
126
                                GridDatatype dt = it.next();
127
                                
128
                                //Loads name and description
129
                                String[] pair = new String[2];
130
                                pair[0] = dt.getName();
131
                                pair[1] = dt.getDescription();
132
                                varList.add(pair);
133
                                
134
                                //Loads variable's name
135
                                Dimension dim = dt.getDimensions().get(dt.getXDimensionIndex());
136
                                xDimVariable = dim.getName();
137
                                dim = dt.getDimensions().get(dt.getYDimensionIndex());
138
                                yDimVariable = dim.getName();
139
                                
140
                                if(dt.getShape() != null) {
141
                                        GridCoordSystem gcs = dt.getCoordinateSystem();
142
                                        
143
                                        //Loads levels
144
                                        CoordinateAxis1D zAxis = gcs.getVerticalAxis();
145
                                        if(zAxis != null) {
146
                                                double[] d = zAxis.getCoordEdges();
147
                                                rankByVar.put(pair[0], d);
148
                                        }
149
                                        
150
                                        //Loads time list
151
                                        if (gcs.hasTimeAxis1D()) {
152
                                                CoordinateAxis1DTime tAxis1D = gcs.getTimeAxis1D();
153
                                                Date[] dates = tAxis1D.getTimeDates();
154
                                                dateByVar.put(pair[0], dates);
155
                                        }
156
                                }
157
                        }
158
                        connector.close();
159
                }
160
                return varList;
161
        }
162
        
163
        /**
164
         * Gets the list of dates
165
         * @param var
166
         * @return
167
         */
168
        public Date[] getDates(String var) {
169
                return dateByVar.get(var);
170
        }
171

    
172
        /**
173
         * Gets the number of levels of one variable
174
         * @param var
175
         * @return
176
         */
177
        public double[] getLevelList(String var) {
178
                return rankByVar.get(var);
179
        }
180
        
181
        /**
182
         * Gets the variable's name of the X dimension
183
         * @return
184
         */
185
        public String getXDimVariable() {
186
                return xDimVariable;
187
        }
188
        
189
        /**
190
         * Gets the variable's name of the Y dimension
191
         * @return
192
         */
193
        public String getYDimVariable() {
194
                return yDimVariable;
195
        }
196
        
197
        /*
198
         * (non-Javadoc)
199
         * @see org.gvsig.fmap.dal.DataStoreParameters#getDataStoreName()
200
         */
201
        public String getDataStoreName() {
202
                return NetCDFProvider.NAME;
203
        }
204
        
205
        /*
206
         * (non-Javadoc)
207
         * @see org.gvsig.fmap.dal.DataStoreParameters#getDescription()
208
         */
209
        public String getDescription() {
210
                return NetCDFProvider.DESCRIPTION;
211
        }
212
}