1 /**
2 * Copyright 2010, CSIRO Australia.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Represents a range of longitude values for some variables. The intended use is to
26 * store data for all variables in all longitudes for a particular latitude and time.
27 * Note: Speed is chosen over robustness and error checking in this class. Most
28 * problems will be signalled by a runtime exception of some form.
29 *
30 * Copyright 2010, CSIRO Australia
31 * All rights reserved.
32 *
33 * @author Robert Bridle on 12/04/2010
34 * @version $Revision: 78 $ $Date: 2010-07-24 16:23:13 +1000 (Sat, 24 Jul 2010) $
35 */
36 public class LongitudeRange
37 {
38
39 /**
40 * Constant that defines the logger to be used.
41 */
42 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 int counter = 0;
52
53 /**
54 * Create a new longitude range. The values will all be preset to the
55 * supplied default value.
56 *
57 * @param numVariables The number of variables the range should hold.
58 * @param numLongitudes The number of longitudes the range should hold.
59 * @param defaultValue The default values to preset for each variable.
60 */
61 public LongitudeRange(int numVariables, int numLongitudes, String[] defaultValues)
62 {
63 this.numVariables = numVariables;
64 this.numLongitudes = numLongitudes;
65 values = new float[this.numVariables][this.numLongitudes];
66 for (int i = 0; i < numVariables; i++)
67 {
68 Arrays.fill(values[i], Float.valueOf(defaultValues[i]));
69 }
70 }
71
72 /**
73 * Retrieve the values for a particular variable as a string of one value per line. There will be one value for each
74 * longitude.
75 *
76 * @param varIndex
77 * The index of the variable to be returned.
78 * @return the values for a particular variable
79 */
80 public String getValues(int varIndex)
81 {
82 StringBuffer strBuf = new StringBuffer();
83 for (Float f : values[varIndex])
84 {
85 strBuf.append(f).append(System.getProperty("line.separator"));
86 }
87 return strBuf.toString();
88 }
89
90 /**
91 * Retrieve the values for a particular variable as a string of one value per line. There will be one value for each
92 * longitude.
93 *
94 * @param varIndex
95 * The index of the variable to be returned.
96 * @return the values for a particular variable
97 */
98 String getSingleValue(int varIndex, int longIndex)
99 {
100 return String.valueOf(values[varIndex][longIndex]);
101 }
102
103 /**
104 * @return the startLongitudeRange
105 */
106 public float getStartLongitudeRange()
107 {
108 return startLongitudeRange;
109 }
110
111 /**
112 * @param startLongitudeRange
113 * the startLongitudeRange to set
114 */
115 public void setStartLongitudeRange(String startLongitudeRange)
116 {
117 this.startLongitudeRange = Float.valueOf(startLongitudeRange);
118 }
119
120 /**
121 * @return the endLongitudeRange
122 */
123 public String getEndLongitudeRange()
124 {
125 DecimalFormat fmtObj = new DecimalFormat("###0.00");
126 return fmtObj.format(startLongitudeRange + (float) ((numLongitudes - 1) * SPATIAL_INCREMENT_DEGREES));
127 }
128
129 /**
130 * @return the numVariables
131 */
132 public int getNumVariables()
133 {
134 return numVariables;
135 }
136
137 /**
138 * @return the numLongitudes
139 */
140 public int getNumLongitudes()
141 {
142 return numLongitudes;
143 }
144
145 /**
146 * @param rainFall
147 */
148 public void addValues(String[] variableValues)
149 {
150 for (int i = 0; i < variableValues.length; i++)
151 {
152 values[i][counter] = Float.valueOf(variableValues[i]);
153 }
154 counter++;
155 }
156
157 /**
158 * @param rainFall
159 */
160 public void putValues(int longitudeIndex, String[] variableValues)
161 {
162 for (int i = 0; i < variableValues.length; i++)
163 {
164 values[i][longitudeIndex] = Float.valueOf(variableValues[i]);
165 }
166 }
167
168 /**
169 * @param rainFall
170 */
171 public void putValues(String longitude, String[] variableValues)
172 {
173 putValues(convertLongitudeToIndex(longitude), variableValues);
174 }
175
176 private int convertLongitudeToIndex(String longitude)
177 {
178 float longVal = Float.parseFloat(longitude);
179 Float index = (longVal-startLongitudeRange)/SPATIAL_INCREMENT_DEGREES;
180 if (index > 500f)
181 {
182 LOG.error("Got an invalid longitude index from " + longVal + " of " + index);
183 }
184 return index.intValue();
185 }
186
187 public String debugOutput()
188 {
189 StringBuffer result = new StringBuffer();
190 for (int varNum = 0; varNum < numVariables; varNum++)
191 {
192 for (int longIdx = 0; longIdx < numLongitudes; longIdx++)
193 {
194 result.append(values[varNum][longIdx]).append(", ");
195 }
196 result.append("\n");
197 }
198
199 return result.toString();
200 }
201 }