1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package au.csiro.netcdf.wron; |
18 | |
|
19 | |
import java.text.DecimalFormat; |
20 | |
import java.util.Arrays; |
21 | |
|
22 | |
import org.apache.log4j.Logger; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 2 | public class LongitudeRange |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 2 | private static final Logger LOG = Logger.getLogger(LongitudeRange.class.getName()); |
43 | |
|
44 | |
private static final float SPATIAL_INCREMENT_DEGREES = 0.05f; |
45 | |
|
46 | |
private int numVariables; |
47 | |
private int numLongitudes; |
48 | |
private float startLongitudeRange; |
49 | |
private float values[][]; |
50 | |
|
51 | 103808 | int counter = 0; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 103808 | public LongitudeRange(int numVariables, int numLongitudes, String[] defaultValues) |
62 | |
{ |
63 | 103808 | this.numVariables = numVariables; |
64 | 103808 | this.numLongitudes = numLongitudes; |
65 | 103808 | values = new float[this.numVariables][this.numLongitudes]; |
66 | 1037940 | for (int i = 0; i < numVariables; i++) |
67 | |
{ |
68 | 934132 | Arrays.fill(values[i], Float.valueOf(defaultValues[i])); |
69 | |
} |
70 | 103808 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public String getValues(int varIndex) |
81 | |
{ |
82 | 86196 | StringBuffer strBuf = new StringBuffer(); |
83 | 1379136 | for (Float f : values[varIndex]) |
84 | |
{ |
85 | 1292940 | strBuf.append(f).append(System.getProperty("line.separator")); |
86 | |
} |
87 | 86196 | return strBuf.toString(); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
String getSingleValue(int varIndex, int longIndex) |
99 | |
{ |
100 | 79284 | return String.valueOf(values[varIndex][longIndex]); |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public float getStartLongitudeRange() |
107 | |
{ |
108 | 0 | return startLongitudeRange; |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setStartLongitudeRange(String startLongitudeRange) |
116 | |
{ |
117 | 103808 | this.startLongitudeRange = Float.valueOf(startLongitudeRange); |
118 | 103808 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public String getEndLongitudeRange() |
124 | |
{ |
125 | 0 | DecimalFormat fmtObj = new DecimalFormat("###0.00"); |
126 | 0 | return fmtObj.format(startLongitudeRange + (float) ((numLongitudes - 1) * SPATIAL_INCREMENT_DEGREES)); |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public int getNumVariables() |
133 | |
{ |
134 | 86196 | return numVariables; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public int getNumLongitudes() |
141 | |
{ |
142 | 0 | return numLongitudes; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public void addValues(String[] variableValues) |
149 | |
{ |
150 | 0 | for (int i = 0; i < variableValues.length; i++) |
151 | |
{ |
152 | 0 | values[i][counter] = Float.valueOf(variableValues[i]); |
153 | |
} |
154 | 0 | counter++; |
155 | 0 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void putValues(int longitudeIndex, String[] variableValues) |
161 | |
{ |
162 | 88020 | for (int i = 0; i < variableValues.length; i++) |
163 | |
{ |
164 | 79204 | values[i][longitudeIndex] = Float.valueOf(variableValues[i]); |
165 | |
} |
166 | 8816 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public void putValues(String longitude, String[] variableValues) |
172 | |
{ |
173 | 8816 | putValues(convertLongitudeToIndex(longitude), variableValues); |
174 | 8816 | } |
175 | |
|
176 | |
private int convertLongitudeToIndex(String longitude) |
177 | |
{ |
178 | 8816 | float longVal = Float.parseFloat(longitude); |
179 | 8816 | Float index = (longVal-startLongitudeRange)/SPATIAL_INCREMENT_DEGREES; |
180 | 8816 | if (index > 500f) |
181 | |
{ |
182 | 0 | LOG.error("Got an invalid longitude index from " + longVal + " of " + index); |
183 | |
} |
184 | 8816 | return index.intValue(); |
185 | |
} |
186 | |
|
187 | |
public String debugOutput() |
188 | |
{ |
189 | 0 | StringBuffer result = new StringBuffer(); |
190 | 0 | for (int varNum = 0; varNum < numVariables; varNum++) |
191 | |
{ |
192 | 0 | for (int longIdx = 0; longIdx < numLongitudes; longIdx++) |
193 | |
{ |
194 | 0 | result.append(values[varNum][longIdx]).append(", "); |
195 | |
} |
196 | 0 | result.append("\n"); |
197 | |
} |
198 | |
|
199 | 0 | return result.toString(); |
200 | |
} |
201 | |
} |