1 | /* |
---|
2 | * $Id: Units.java,v 1.5 2001/02/09 18:42:31 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.util; |
---|
14 | |
---|
15 | import gov.noaa.pmel.sgt.dm.SGTMetaData; |
---|
16 | import gov.noaa.pmel.sgt.dm.SGTData; |
---|
17 | import gov.noaa.pmel.sgt.dm.SGTLine; |
---|
18 | import gov.noaa.pmel.sgt.dm.SGTGrid; |
---|
19 | import gov.noaa.pmel.sgt.dm.SimpleLine; |
---|
20 | import gov.noaa.pmel.sgt.dm.SimpleGrid; |
---|
21 | |
---|
22 | import java.lang.reflect.Constructor; |
---|
23 | |
---|
24 | /** |
---|
25 | * Units is a static class for converting the units of DependentVariables. |
---|
26 | * |
---|
27 | * @author Donald Denbo |
---|
28 | * @version $Revision: 1.5 $, $Date: 2001/02/09 18:42:31 $ |
---|
29 | * @since sgt 1.0 |
---|
30 | **/ |
---|
31 | public class Units implements java.io.Serializable { |
---|
32 | /** No base units selected */ |
---|
33 | public static final int NONE = 0; |
---|
34 | /** base units are temperature */ |
---|
35 | public static final int TEMPERATURE = 1; |
---|
36 | /** base units are velocity */ |
---|
37 | public static final int VELOCITY = 2; |
---|
38 | /** base units are distance */ |
---|
39 | public static final int DISTANCE = 3; |
---|
40 | /** check X MetaData for units */ |
---|
41 | public static final int X_AXIS = 0; |
---|
42 | /** check Y MetaData for units */ |
---|
43 | public static final int Y_AXIS = 1; |
---|
44 | /** check Z MetaData for units */ |
---|
45 | public static final int Z_AXIS = 2; |
---|
46 | /** |
---|
47 | * Return the base unit for the meta data. |
---|
48 | * |
---|
49 | * @return TEMPERATURE, VELOCITY, DISTANCE, or NONE |
---|
50 | */ |
---|
51 | public static int getBaseUnit(SGTMetaData meta) { |
---|
52 | if(Temperature.isBaseUnit(meta)) { |
---|
53 | return TEMPERATURE; |
---|
54 | } else if(Velocity.isBaseUnit(meta)) { |
---|
55 | return VELOCITY; |
---|
56 | } else if(Distance.isBaseUnit(meta)) { |
---|
57 | return DISTANCE; |
---|
58 | } else { |
---|
59 | return NONE; |
---|
60 | } |
---|
61 | } |
---|
62 | /** |
---|
63 | * Convert data to common units. |
---|
64 | * |
---|
65 | * @param grid the data |
---|
66 | * @param base the base units, TEMPERATURE, VELOCITY, DISTANCE, or NONE |
---|
67 | * @param comp grid component, X_AXIS, Y_AXIS, or Z_AXIS |
---|
68 | */ |
---|
69 | public static SGTData convertToBaseUnit(SGTData grid, int base, int comp) { |
---|
70 | switch(base) { |
---|
71 | case TEMPERATURE: return Temperature.convertToBaseUnit(grid, comp); |
---|
72 | case VELOCITY: return Velocity.convertToBaseUnit(grid, comp); |
---|
73 | case DISTANCE: return Distance.convertToBaseUnit(grid, comp); |
---|
74 | default: |
---|
75 | case NONE: return grid; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | /** |
---|
80 | * Supports temperature conversions. |
---|
81 | */ |
---|
82 | class Temperature implements java.io.Serializable { |
---|
83 | // |
---|
84 | // All conversions are to the default units of degC |
---|
85 | // |
---|
86 | // degC = scale*origUnits + offset |
---|
87 | // |
---|
88 | private static final String[] name = |
---|
89 | {"C", "degC", "K", "degK", "F", "degF", "k", "deg_c", "deg_k"}; |
---|
90 | private static final double[] scale = |
---|
91 | {1.0, 1.0, 1.0, 1.0, 5.0/9.0, 5.0/9.0, 1.0, 1.0, 1.0}; |
---|
92 | private static final double[] offset = |
---|
93 | {0.0, 0.0, -273.15, -273.15, 32.0*5.0/9.0, 32.0*5.0/9.0, -273.15, 0.0, -273.15}; |
---|
94 | // |
---|
95 | private static final String timeArrayName = "[Lgov.noaa.pmel.util.GeoDate"; |
---|
96 | private static final String doubleArrayName = "[D"; |
---|
97 | private static final String stringName = "java.lang.String"; |
---|
98 | /** |
---|
99 | * |
---|
100 | */ |
---|
101 | static SGTData convertToBaseUnit(SGTData grid, int comp) { |
---|
102 | int unit, count; |
---|
103 | boolean hand; |
---|
104 | // GeoVariable xDim, yDim, zDim; |
---|
105 | // TimeVariable tDim; |
---|
106 | // DependentVariable variable, new_variable; |
---|
107 | // VarDesc new_vardesc; |
---|
108 | SGTData new_grid = null; |
---|
109 | SGTMetaData meta, newMeta; |
---|
110 | int projection; |
---|
111 | |
---|
112 | String units; |
---|
113 | switch(comp) { |
---|
114 | default: |
---|
115 | case Units.X_AXIS: |
---|
116 | meta = ((SGTLine)grid).getXMetaData(); |
---|
117 | break; |
---|
118 | case Units.Y_AXIS: |
---|
119 | meta = ((SGTLine)grid).getYMetaData(); |
---|
120 | break; |
---|
121 | case Units.Z_AXIS: |
---|
122 | meta = ((SGTGrid)grid).getZMetaData(); |
---|
123 | } |
---|
124 | units = meta.getUnits(); |
---|
125 | // |
---|
126 | // is it one of the changeable units? |
---|
127 | // |
---|
128 | for(unit = 0; unit < name.length; unit++) { |
---|
129 | if(units.equals(name[unit])) break; |
---|
130 | } |
---|
131 | |
---|
132 | if(unit >= name.length) return grid; |
---|
133 | // |
---|
134 | // is it already in the base units? |
---|
135 | // |
---|
136 | if(scale[unit] == 1.0 && offset[unit] == 0.0) return grid; |
---|
137 | // |
---|
138 | // convert the units |
---|
139 | // |
---|
140 | double values[], new_values[]; |
---|
141 | switch(comp) { |
---|
142 | default: |
---|
143 | case Units.X_AXIS: |
---|
144 | values = ((SGTLine)grid).getXArray(); |
---|
145 | break; |
---|
146 | case Units.Y_AXIS: |
---|
147 | values = ((SGTLine)grid).getYArray(); |
---|
148 | break; |
---|
149 | case Units.Z_AXIS: |
---|
150 | values = ((SGTGrid)grid).getZArray(); |
---|
151 | } |
---|
152 | new_values = new double[values.length]; |
---|
153 | |
---|
154 | for(count=0; count < values.length; count++) { |
---|
155 | new_values[count] = scale[unit]*values[count] + offset[unit]; |
---|
156 | } |
---|
157 | // |
---|
158 | // build the new dependent variable |
---|
159 | // |
---|
160 | newMeta = new SGTMetaData(meta.getName(), "degC", meta.isReversed(), meta.isModulo()); |
---|
161 | newMeta.setModuloValue(meta.getModuloValue()); |
---|
162 | newMeta.setModuloTime(meta.getModuloTime()); |
---|
163 | |
---|
164 | if(grid instanceof SGTLine) { |
---|
165 | boolean simpleLine = grid instanceof SimpleLine; |
---|
166 | if(simpleLine) { |
---|
167 | new_grid = (SGTLine)grid.copy(); |
---|
168 | } |
---|
169 | Class[] classArgs = new Class[3]; |
---|
170 | Object[] constructArgs = new Object[3]; |
---|
171 | Class gridClass = grid.getClass(); |
---|
172 | Constructor gridConstruct; |
---|
173 | switch(comp) { |
---|
174 | default: |
---|
175 | case Units.X_AXIS: |
---|
176 | if(simpleLine) { |
---|
177 | ((SimpleLine)new_grid).setXArray(new_values); |
---|
178 | } else { |
---|
179 | if(((SGTLine)grid).isYTime()) { |
---|
180 | try { |
---|
181 | classArgs[0] = Class.forName(doubleArrayName); |
---|
182 | classArgs[1] = Class.forName(timeArrayName); |
---|
183 | classArgs[2] = Class.forName(stringName); |
---|
184 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
185 | constructArgs[0] = new_values; |
---|
186 | constructArgs[1] = ((SGTLine)grid).getTimeArray(); |
---|
187 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
188 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
189 | } catch (java.lang.Exception e) { |
---|
190 | System.out.println("Temperature conversion: " + e); |
---|
191 | } |
---|
192 | } else { |
---|
193 | try { |
---|
194 | classArgs[0] = Class.forName(doubleArrayName); |
---|
195 | classArgs[1] = Class.forName(doubleArrayName); |
---|
196 | classArgs[2] = Class.forName(stringName); |
---|
197 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
198 | constructArgs[0] = new_values; |
---|
199 | constructArgs[1] = ((SGTLine)grid).getYArray(); |
---|
200 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
201 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
202 | } catch (java.lang.Exception e) { |
---|
203 | System.out.println("Temperature conversion: " + e); |
---|
204 | } |
---|
205 | } |
---|
206 | ((SimpleLine)new_grid).setYMetaData(((SGTLine)grid).getYMetaData()); |
---|
207 | } |
---|
208 | ((SimpleLine)new_grid).setXMetaData(newMeta); |
---|
209 | break; |
---|
210 | case Units.Y_AXIS: |
---|
211 | if(simpleLine) { |
---|
212 | ((SimpleLine)new_grid).setYArray(new_values); |
---|
213 | } else { |
---|
214 | if(((SGTLine)grid).isXTime()) { |
---|
215 | try { |
---|
216 | classArgs[0] = Class.forName(timeArrayName); |
---|
217 | classArgs[1] = Class.forName(doubleArrayName); |
---|
218 | classArgs[2] = Class.forName(stringName); |
---|
219 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
220 | constructArgs[0] = ((SGTLine)grid).getTimeArray(); |
---|
221 | constructArgs[1] = new_values; |
---|
222 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
223 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
224 | } catch (java.lang.Exception e) { |
---|
225 | System.out.println("Temperature conversion: " + e); |
---|
226 | } |
---|
227 | } else { |
---|
228 | try { |
---|
229 | classArgs[0] = Class.forName(doubleArrayName); |
---|
230 | classArgs[1] = Class.forName(doubleArrayName); |
---|
231 | classArgs[2] = Class.forName(stringName); |
---|
232 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
233 | constructArgs[0] = ((SGTLine)grid).getXArray(); |
---|
234 | constructArgs[1] = new_values; |
---|
235 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
236 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
237 | } catch (java.lang.Exception e) { |
---|
238 | System.out.println("Temperature conversion: " + e); |
---|
239 | } |
---|
240 | } |
---|
241 | ((SimpleLine)new_grid).setXMetaData(((SGTLine)grid).getXMetaData()); |
---|
242 | } |
---|
243 | ((SimpleLine)new_grid).setYMetaData(newMeta); |
---|
244 | } |
---|
245 | } else if(grid instanceof SGTGrid) { |
---|
246 | boolean simpleGrid = grid instanceof SimpleGrid; |
---|
247 | if(simpleGrid) { |
---|
248 | new_grid = (SGTGrid)grid.copy(); |
---|
249 | } |
---|
250 | Class[] classArgs = new Class[4]; |
---|
251 | Object[] constructArgs = new Object[4]; |
---|
252 | Class gridClass = grid.getClass(); |
---|
253 | Constructor gridConstruct; |
---|
254 | switch(comp) { |
---|
255 | default: |
---|
256 | case Units.X_AXIS: |
---|
257 | if(simpleGrid) { |
---|
258 | ((SimpleGrid)new_grid).setXArray(new_values); |
---|
259 | } else { |
---|
260 | if(((SGTGrid)grid).isYTime()) { |
---|
261 | try { |
---|
262 | classArgs[0] = Class.forName(doubleArrayName); |
---|
263 | classArgs[1] = Class.forName(doubleArrayName); |
---|
264 | classArgs[2] = Class.forName(timeArrayName); |
---|
265 | classArgs[3] = Class.forName(stringName); |
---|
266 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
267 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
268 | constructArgs[1] = new_values; |
---|
269 | constructArgs[2] = ((SGTGrid)grid).getTimeArray(); |
---|
270 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
271 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
272 | } catch (java.lang.Exception e) { |
---|
273 | System.out.println("Temperature conversion: " + e); |
---|
274 | } |
---|
275 | } else { |
---|
276 | try { |
---|
277 | classArgs[0] = Class.forName(doubleArrayName); |
---|
278 | classArgs[1] = Class.forName(doubleArrayName); |
---|
279 | classArgs[2] = Class.forName(doubleArrayName); |
---|
280 | classArgs[3] = Class.forName(stringName); |
---|
281 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
282 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
283 | constructArgs[1] = new_values; |
---|
284 | constructArgs[2] = ((SGTGrid)grid).getYArray(); |
---|
285 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
286 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
287 | } catch (java.lang.Exception e) { |
---|
288 | System.out.println("Temperature conversion: " + e); |
---|
289 | } |
---|
290 | } |
---|
291 | ((SimpleGrid)new_grid).setYMetaData(((SGTGrid)grid).getYMetaData()); |
---|
292 | ((SimpleGrid)new_grid).setZMetaData(((SGTGrid)grid).getZMetaData()); |
---|
293 | } |
---|
294 | ((SimpleGrid)new_grid).setXMetaData(newMeta); |
---|
295 | break; |
---|
296 | case Units.Y_AXIS: |
---|
297 | if(simpleGrid) { |
---|
298 | ((SimpleGrid)new_grid).setYArray(new_values); |
---|
299 | } else { |
---|
300 | if(((SGTGrid)grid).isXTime()) { |
---|
301 | try { |
---|
302 | classArgs[0] = Class.forName(doubleArrayName); |
---|
303 | classArgs[1] = Class.forName(timeArrayName); |
---|
304 | classArgs[2] = Class.forName(doubleArrayName); |
---|
305 | classArgs[3] = Class.forName(stringName); |
---|
306 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
307 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
308 | constructArgs[1] = ((SGTGrid)grid).getTimeArray(); |
---|
309 | constructArgs[2] = new_values; |
---|
310 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
311 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
312 | } catch (java.lang.Exception e) { |
---|
313 | System.out.println("Temperature conversion: " + e); |
---|
314 | } |
---|
315 | } else { |
---|
316 | try { |
---|
317 | classArgs[0] = Class.forName(doubleArrayName); |
---|
318 | classArgs[1] = Class.forName(doubleArrayName); |
---|
319 | classArgs[2] = Class.forName(doubleArrayName); |
---|
320 | classArgs[3] = Class.forName(stringName); |
---|
321 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
322 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
323 | constructArgs[1] = ((SGTGrid)grid).getXArray(); |
---|
324 | constructArgs[2] = new_values; |
---|
325 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
326 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
327 | } catch (java.lang.Exception e) { |
---|
328 | System.out.println("Temperature conversion: " + e); |
---|
329 | } |
---|
330 | } |
---|
331 | ((SimpleGrid)new_grid).setXMetaData(((SGTGrid)grid).getXMetaData()); |
---|
332 | ((SimpleGrid)new_grid).setZMetaData(((SGTGrid)grid).getZMetaData()); |
---|
333 | } |
---|
334 | ((SimpleGrid)new_grid).setYMetaData(newMeta); |
---|
335 | break; |
---|
336 | case Units.Z_AXIS: |
---|
337 | if(simpleGrid) { |
---|
338 | ((SimpleGrid)new_grid).setZArray(new_values); |
---|
339 | } else { |
---|
340 | if(((SGTGrid)grid).isXTime()) { |
---|
341 | try { |
---|
342 | classArgs[0] = Class.forName(doubleArrayName); |
---|
343 | classArgs[1] = Class.forName(timeArrayName); |
---|
344 | classArgs[2] = Class.forName(doubleArrayName); |
---|
345 | classArgs[3] = Class.forName(stringName); |
---|
346 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
347 | constructArgs[0] = new_values; |
---|
348 | constructArgs[1] = ((SGTGrid)grid).getTimeArray(); |
---|
349 | constructArgs[2] = ((SGTGrid)grid).getYArray(); |
---|
350 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
351 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
352 | } catch (java.lang.Exception e) { |
---|
353 | System.out.println("Temperature conversion: " + e); |
---|
354 | } |
---|
355 | } else if(((SGTGrid)grid).isYTime()) { |
---|
356 | try { |
---|
357 | classArgs[0] = Class.forName(doubleArrayName); |
---|
358 | classArgs[1] = Class.forName(doubleArrayName); |
---|
359 | classArgs[2] = Class.forName(timeArrayName); |
---|
360 | classArgs[3] = Class.forName(stringName); |
---|
361 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
362 | constructArgs[0] = new_values; |
---|
363 | constructArgs[1] = ((SGTGrid)grid).getXArray(); |
---|
364 | constructArgs[2] = ((SGTGrid)grid).getTimeArray(); |
---|
365 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
366 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
367 | } catch (java.lang.Exception e) { |
---|
368 | System.out.println("Temperature conversion: " + e); |
---|
369 | } |
---|
370 | } else { |
---|
371 | try { |
---|
372 | classArgs[0] = Class.forName(doubleArrayName); |
---|
373 | classArgs[1] = Class.forName(doubleArrayName); |
---|
374 | classArgs[2] = Class.forName(doubleArrayName); |
---|
375 | classArgs[3] = Class.forName(stringName); |
---|
376 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
377 | constructArgs[0] = new_values; |
---|
378 | constructArgs[1] = ((SGTGrid)grid).getXArray(); |
---|
379 | constructArgs[2] = ((SGTGrid)grid).getYArray(); |
---|
380 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
381 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
382 | } catch (java.lang.Exception e) { |
---|
383 | System.out.println("Temperature conversion: " + e); |
---|
384 | } |
---|
385 | } |
---|
386 | ((SimpleGrid)new_grid).setXMetaData(((SGTGrid)grid).getXMetaData()); |
---|
387 | ((SimpleGrid)new_grid).setYMetaData(((SGTGrid)grid).getYMetaData()); |
---|
388 | } |
---|
389 | ((SimpleGrid)new_grid).setZMetaData(newMeta); |
---|
390 | } |
---|
391 | } |
---|
392 | return new_grid; |
---|
393 | } |
---|
394 | |
---|
395 | static boolean isBaseUnit(SGTMetaData meta) { |
---|
396 | int count; |
---|
397 | String units = meta.getUnits(); |
---|
398 | |
---|
399 | for(count=0; count < name.length; count++) { |
---|
400 | if(units.equals(name[count])) return true; |
---|
401 | } |
---|
402 | return false; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * Supports distance conversions. |
---|
408 | */ |
---|
409 | class Distance implements java.io.Serializable { |
---|
410 | static SGTData convertToBaseUnit(SGTData grid, int comp) { |
---|
411 | return grid; |
---|
412 | } |
---|
413 | static boolean isBaseUnit(SGTMetaData meta) { |
---|
414 | return false; |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | * Supports velocity conversions. |
---|
420 | */ |
---|
421 | class Velocity implements java.io.Serializable { |
---|
422 | // |
---|
423 | // All conversions are to the default units of "m s-1" |
---|
424 | // |
---|
425 | // m s-1 = scale*origUnits + offset |
---|
426 | // |
---|
427 | private static final String[] name = |
---|
428 | {"m s-1", "m/s", "cm s-1", "cm/s"}; |
---|
429 | private static final double[] scale = |
---|
430 | {1.0, 1.0, 0.01, 0.01}; |
---|
431 | private static final double[] offset = |
---|
432 | {0.0, 0.0, 0.0, 0.0}; |
---|
433 | // |
---|
434 | private static final String timeArrayName = "[Lgov.noaa.pmel.util.GeoDate"; |
---|
435 | private static final String doubleArrayName = "[D"; |
---|
436 | private static final String stringName = "java.lang.String"; |
---|
437 | /** |
---|
438 | * |
---|
439 | */ |
---|
440 | static SGTData convertToBaseUnit(SGTData grid, int comp) { |
---|
441 | int unit, count; |
---|
442 | boolean hand; |
---|
443 | // GeoVariable xDim, yDim, zDim; |
---|
444 | // TimeVariable tDim; |
---|
445 | // DependentVariable variable, new_variable; |
---|
446 | // VarDesc new_vardesc; |
---|
447 | SGTData new_grid = null; |
---|
448 | SGTMetaData meta, newMeta; |
---|
449 | int projection; |
---|
450 | |
---|
451 | String units; |
---|
452 | switch(comp) { |
---|
453 | default: |
---|
454 | case Units.X_AXIS: |
---|
455 | meta = ((SGTLine)grid).getXMetaData(); |
---|
456 | break; |
---|
457 | case Units.Y_AXIS: |
---|
458 | meta = ((SGTLine)grid).getYMetaData(); |
---|
459 | break; |
---|
460 | case Units.Z_AXIS: |
---|
461 | meta = ((SGTGrid)grid).getZMetaData(); |
---|
462 | } |
---|
463 | units = meta.getUnits(); |
---|
464 | // |
---|
465 | // is it one of the changeable units? |
---|
466 | // |
---|
467 | for(unit = 0; unit < name.length; unit++) { |
---|
468 | if(units.equals(name[unit])) break; |
---|
469 | } |
---|
470 | |
---|
471 | if(unit >= name.length) return grid; |
---|
472 | // |
---|
473 | // is it already in the base units? |
---|
474 | // |
---|
475 | if(scale[unit] == 1.0 && offset[unit] == 0.0) return grid; |
---|
476 | // |
---|
477 | // convert the units |
---|
478 | // |
---|
479 | double values[], new_values[]; |
---|
480 | switch(comp) { |
---|
481 | default: |
---|
482 | case Units.X_AXIS: |
---|
483 | values = ((SGTLine)grid).getXArray(); |
---|
484 | break; |
---|
485 | case Units.Y_AXIS: |
---|
486 | values = ((SGTLine)grid).getYArray(); |
---|
487 | break; |
---|
488 | case Units.Z_AXIS: |
---|
489 | values = ((SGTGrid)grid).getZArray(); |
---|
490 | } |
---|
491 | new_values = new double[values.length]; |
---|
492 | |
---|
493 | for(count=0; count < values.length; count++) { |
---|
494 | new_values[count] = scale[unit]*values[count] + offset[unit]; |
---|
495 | } |
---|
496 | // |
---|
497 | // build the new dependent variable |
---|
498 | // |
---|
499 | newMeta = new SGTMetaData(meta.getName(), |
---|
500 | "m s-1", |
---|
501 | meta.isReversed(), |
---|
502 | meta.isModulo()); |
---|
503 | newMeta.setModuloValue(meta.getModuloValue()); |
---|
504 | newMeta.setModuloTime(meta.getModuloTime()); |
---|
505 | |
---|
506 | if(grid instanceof SGTLine) { |
---|
507 | boolean simpleLine = grid instanceof SimpleLine; |
---|
508 | if(simpleLine) { |
---|
509 | new_grid = (SGTLine)grid.copy(); |
---|
510 | } |
---|
511 | Class[] classArgs = new Class[3]; |
---|
512 | Object[] constructArgs = new Object[3]; |
---|
513 | Class gridClass = grid.getClass(); |
---|
514 | Constructor gridConstruct; |
---|
515 | switch(comp) { |
---|
516 | default: |
---|
517 | case Units.X_AXIS: |
---|
518 | if(simpleLine) { |
---|
519 | ((SimpleLine)new_grid).setXArray(new_values); |
---|
520 | } else { |
---|
521 | if(((SGTLine)grid).isYTime()) { |
---|
522 | try { |
---|
523 | classArgs[0] = Class.forName(doubleArrayName); |
---|
524 | classArgs[1] = Class.forName(timeArrayName); |
---|
525 | classArgs[2] = Class.forName(stringName); |
---|
526 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
527 | constructArgs[0] = new_values; |
---|
528 | constructArgs[1] = ((SGTLine)grid).getTimeArray(); |
---|
529 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
530 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
531 | } catch (java.lang.Exception e) { |
---|
532 | System.out.println("Velocity conversion: " + e); |
---|
533 | } |
---|
534 | // new_grid = new SimpleLine(new_values, |
---|
535 | // ((SGTLine)grid).getTimeArray(), |
---|
536 | // ((SGTLine)grid).getTitle()); |
---|
537 | } else { |
---|
538 | try { |
---|
539 | classArgs[0] = Class.forName(doubleArrayName); |
---|
540 | classArgs[1] = Class.forName(doubleArrayName); |
---|
541 | classArgs[2] = Class.forName(stringName); |
---|
542 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
543 | constructArgs[0] = new_values; |
---|
544 | constructArgs[1] = ((SGTLine)grid).getYArray(); |
---|
545 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
546 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
547 | } catch (java.lang.Exception e) { |
---|
548 | System.out.println("Velocity conversion: " + e); |
---|
549 | } |
---|
550 | // new_grid = new SimpleLine(new_values, |
---|
551 | // ((SGTLine)grid).getYArray(), |
---|
552 | // ((SGTLine)grid).getTitle()); |
---|
553 | } |
---|
554 | ((SimpleLine)new_grid).setYMetaData(((SGTLine)grid).getYMetaData()); |
---|
555 | } |
---|
556 | ((SimpleLine)new_grid).setXMetaData(newMeta); |
---|
557 | break; |
---|
558 | case Units.Y_AXIS: |
---|
559 | if(simpleLine) { |
---|
560 | ((SimpleLine)new_grid).setYArray(new_values); |
---|
561 | } else { |
---|
562 | if(((SGTLine)grid).isXTime()) { |
---|
563 | try { |
---|
564 | classArgs[0] = Class.forName(timeArrayName); |
---|
565 | classArgs[1] = Class.forName(doubleArrayName); |
---|
566 | classArgs[2] = Class.forName(stringName); |
---|
567 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
568 | constructArgs[0] = ((SGTLine)grid).getTimeArray(); |
---|
569 | constructArgs[1] = new_values; |
---|
570 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
571 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
572 | } catch (java.lang.Exception e) { |
---|
573 | System.out.println("Velocity conversion: " + e); |
---|
574 | } |
---|
575 | // new_grid = new SimpleLine(((SGTLine)grid).getTimeArray(), |
---|
576 | // new_values, |
---|
577 | // ((SGTLine)grid).getTitle()); |
---|
578 | } else { |
---|
579 | try { |
---|
580 | classArgs[0] = Class.forName(doubleArrayName); |
---|
581 | classArgs[1] = Class.forName(doubleArrayName); |
---|
582 | classArgs[2] = Class.forName(stringName); |
---|
583 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
584 | constructArgs[0] = ((SGTLine)grid).getXArray(); |
---|
585 | constructArgs[1] = new_values; |
---|
586 | constructArgs[2] = ((SGTLine)grid).getTitle(); |
---|
587 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
588 | } catch (java.lang.Exception e) { |
---|
589 | System.out.println("Velocity conversion: " + e); |
---|
590 | } |
---|
591 | // new_grid = new SimpleLine(((SGTLine)grid).getXArray(), |
---|
592 | // new_values, |
---|
593 | // ((SGTLine)grid).getTitle()); |
---|
594 | } |
---|
595 | ((SimpleLine)new_grid).setXMetaData(((SGTLine)grid).getXMetaData()); |
---|
596 | } |
---|
597 | ((SimpleLine)new_grid).setYMetaData(newMeta); |
---|
598 | } |
---|
599 | } else if(grid instanceof SGTGrid) { |
---|
600 | boolean simpleGrid = grid instanceof SimpleGrid; |
---|
601 | if(simpleGrid) { |
---|
602 | new_grid = (SGTGrid)grid.copy(); |
---|
603 | } |
---|
604 | Class[] classArgs = new Class[4]; |
---|
605 | Object[] constructArgs = new Object[4]; |
---|
606 | Class gridClass = grid.getClass(); |
---|
607 | Constructor gridConstruct; |
---|
608 | switch(comp) { |
---|
609 | default: |
---|
610 | case Units.X_AXIS: |
---|
611 | if(simpleGrid) { |
---|
612 | ((SimpleGrid)new_grid).setXArray(new_values); |
---|
613 | } else { |
---|
614 | if(((SGTGrid)grid).isYTime()) { |
---|
615 | try { |
---|
616 | classArgs[0] = Class.forName(doubleArrayName); |
---|
617 | classArgs[1] = Class.forName(doubleArrayName); |
---|
618 | classArgs[2] = Class.forName(timeArrayName); |
---|
619 | classArgs[3] = Class.forName(stringName); |
---|
620 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
621 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
622 | constructArgs[1] = new_values; |
---|
623 | constructArgs[2] = ((SGTGrid)grid).getTimeArray(); |
---|
624 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
625 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
626 | } catch (java.lang.Exception e) { |
---|
627 | System.out.println("Velocity conversion: " + e); |
---|
628 | } |
---|
629 | // new_grid = new SimpleGrid(((SGTGrid)grid).getZArray(), |
---|
630 | // new_values, |
---|
631 | // ((SGTGrid)grid).getTimeArray(), |
---|
632 | // ((SGTGrid)grid).getTitle()); |
---|
633 | } else { |
---|
634 | try { |
---|
635 | classArgs[0] = Class.forName(doubleArrayName); |
---|
636 | classArgs[1] = Class.forName(doubleArrayName); |
---|
637 | classArgs[2] = Class.forName(doubleArrayName); |
---|
638 | classArgs[3] = Class.forName(stringName); |
---|
639 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
640 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
641 | constructArgs[1] = new_values; |
---|
642 | constructArgs[2] = ((SGTGrid)grid).getYArray(); |
---|
643 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
644 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
645 | } catch (java.lang.Exception e) { |
---|
646 | System.out.println("Velocity conversion: " + e); |
---|
647 | } |
---|
648 | // new_grid = new SimpleGrid(((SGTGrid)grid).getZArray(), |
---|
649 | // new_values, |
---|
650 | // ((SGTGrid)grid).getYArray(), |
---|
651 | // ((SGTGrid)grid).getTitle()); |
---|
652 | } |
---|
653 | ((SimpleGrid)new_grid).setYMetaData(((SGTGrid)grid).getYMetaData()); |
---|
654 | ((SimpleGrid)new_grid).setZMetaData(((SGTGrid)grid).getZMetaData()); |
---|
655 | } |
---|
656 | ((SimpleGrid)new_grid).setXMetaData(newMeta); |
---|
657 | break; |
---|
658 | case Units.Y_AXIS: |
---|
659 | if(simpleGrid) { |
---|
660 | ((SimpleGrid)new_grid).setYArray(new_values); |
---|
661 | } else { |
---|
662 | if(((SGTGrid)grid).isXTime()) { |
---|
663 | try { |
---|
664 | classArgs[0] = Class.forName(doubleArrayName); |
---|
665 | classArgs[1] = Class.forName(timeArrayName); |
---|
666 | classArgs[2] = Class.forName(doubleArrayName); |
---|
667 | classArgs[3] = Class.forName(stringName); |
---|
668 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
669 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
670 | constructArgs[1] = ((SGTGrid)grid).getTimeArray(); |
---|
671 | constructArgs[2] = new_values; |
---|
672 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
673 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
674 | } catch (java.lang.Exception e) { |
---|
675 | System.out.println("Velocity conversion: " + e); |
---|
676 | } |
---|
677 | // new_grid = new SimpleGrid(((SGTGrid)grid).getZArray(), |
---|
678 | // ((SGTGrid)grid).getTimeArray(), |
---|
679 | // new_values, |
---|
680 | // ((SGTGrid)grid).getTitle()); |
---|
681 | } else { |
---|
682 | try { |
---|
683 | classArgs[0] = Class.forName(doubleArrayName); |
---|
684 | classArgs[1] = Class.forName(doubleArrayName); |
---|
685 | classArgs[2] = Class.forName(doubleArrayName); |
---|
686 | classArgs[3] = Class.forName(stringName); |
---|
687 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
688 | constructArgs[0] = ((SGTGrid)grid).getZArray(); |
---|
689 | constructArgs[1] = ((SGTGrid)grid).getXArray(); |
---|
690 | constructArgs[2] = new_values; |
---|
691 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
692 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
693 | } catch (java.lang.Exception e) { |
---|
694 | System.out.println("Velocity conversion: " + e); |
---|
695 | } |
---|
696 | // new_grid = new SimpleGrid(((SGTGrid)grid).getZArray(), |
---|
697 | // ((SGTGrid)grid).getXArray(), |
---|
698 | // new_values, |
---|
699 | // ((SGTGrid)grid).getTitle()); |
---|
700 | } |
---|
701 | ((SimpleGrid)new_grid).setXMetaData(((SGTGrid)grid).getXMetaData()); |
---|
702 | ((SimpleGrid)new_grid).setZMetaData(((SGTGrid)grid).getZMetaData()); |
---|
703 | } |
---|
704 | ((SimpleGrid)new_grid).setYMetaData(newMeta); |
---|
705 | break; |
---|
706 | case Units.Z_AXIS: |
---|
707 | if(simpleGrid) { |
---|
708 | ((SimpleGrid)new_grid).setZArray(new_values); |
---|
709 | } else { |
---|
710 | if(((SGTGrid)grid).isXTime()) { |
---|
711 | try { |
---|
712 | classArgs[0] = Class.forName(doubleArrayName); |
---|
713 | classArgs[1] = Class.forName(timeArrayName); |
---|
714 | classArgs[2] = Class.forName(doubleArrayName); |
---|
715 | classArgs[3] = Class.forName(stringName); |
---|
716 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
717 | constructArgs[0] = new_values; |
---|
718 | constructArgs[1] = ((SGTGrid)grid).getTimeArray(); |
---|
719 | constructArgs[2] = ((SGTGrid)grid).getYArray(); |
---|
720 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
721 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
722 | } catch (java.lang.Exception e) { |
---|
723 | System.out.println("Velocity conversion: " + e); |
---|
724 | } |
---|
725 | // new_grid = new SimpleGrid(new_values, |
---|
726 | // ((SGTGrid)grid).getTimeArray(), |
---|
727 | // ((SGTGrid)grid).getYArray(), |
---|
728 | // ((SGTGrid)grid).getTitle()); |
---|
729 | } else if(((SGTGrid)grid).isYTime()) { |
---|
730 | try { |
---|
731 | classArgs[0] = Class.forName(doubleArrayName); |
---|
732 | classArgs[1] = Class.forName(doubleArrayName); |
---|
733 | classArgs[2] = Class.forName(timeArrayName); |
---|
734 | classArgs[3] = Class.forName(stringName); |
---|
735 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
736 | constructArgs[0] = new_values; |
---|
737 | constructArgs[1] = ((SGTGrid)grid).getXArray(); |
---|
738 | constructArgs[2] = ((SGTGrid)grid).getTimeArray(); |
---|
739 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
740 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
741 | } catch (java.lang.Exception e) { |
---|
742 | System.out.println("Velocity conversion: " + e); |
---|
743 | } |
---|
744 | // new_grid = new SimpleGrid(new_values, |
---|
745 | // ((SGTGrid)grid).getXArray(), |
---|
746 | // ((SGTGrid)grid).getTimeArray(), |
---|
747 | // ((SGTGrid)grid).getTitle()); |
---|
748 | } else { |
---|
749 | try { |
---|
750 | classArgs[0] = Class.forName(doubleArrayName); |
---|
751 | classArgs[1] = Class.forName(doubleArrayName); |
---|
752 | classArgs[2] = Class.forName(doubleArrayName); |
---|
753 | classArgs[3] = Class.forName(stringName); |
---|
754 | gridConstruct = gridClass.getConstructor(classArgs); |
---|
755 | constructArgs[0] = new_values; |
---|
756 | constructArgs[1] = ((SGTGrid)grid).getXArray(); |
---|
757 | constructArgs[2] = ((SGTGrid)grid).getYArray(); |
---|
758 | constructArgs[2] = ((SGTGrid)grid).getTitle(); |
---|
759 | new_grid = (SGTData)gridConstruct.newInstance(constructArgs); |
---|
760 | } catch (java.lang.Exception e) { |
---|
761 | System.out.println("Velocity conversion: " + e); |
---|
762 | } |
---|
763 | // new_grid = new SimpleGrid(new_values, |
---|
764 | // ((SGTGrid)grid).getXArray(), |
---|
765 | // ((SGTGrid)grid).getYArray(), |
---|
766 | // ((SGTGrid)grid).getTitle()); |
---|
767 | } |
---|
768 | ((SimpleGrid)new_grid).setXMetaData(((SGTGrid)grid).getXMetaData()); |
---|
769 | ((SimpleGrid)new_grid).setYMetaData(((SGTGrid)grid).getYMetaData()); |
---|
770 | } |
---|
771 | ((SimpleGrid)new_grid).setZMetaData(newMeta); |
---|
772 | } |
---|
773 | |
---|
774 | } |
---|
775 | return new_grid; |
---|
776 | } |
---|
777 | static boolean isBaseUnit(SGTMetaData meta) { |
---|
778 | int count; |
---|
779 | String units = meta.getUnits(); |
---|
780 | |
---|
781 | for(count=0; count < name.length; count++) { |
---|
782 | if(units.equals(name[count])) return true; |
---|
783 | } |
---|
784 | return false; |
---|
785 | } |
---|
786 | } |
---|