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 public class LongitudeRange
37 {
38
39
40
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
55
56
57
58
59
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
74
75
76
77
78
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
92
93
94
95
96
97
98 String getSingleValue(int varIndex, int longIndex)
99 {
100 return String.valueOf(values[varIndex][longIndex]);
101 }
102
103
104
105
106 public float getStartLongitudeRange()
107 {
108 return startLongitudeRange;
109 }
110
111
112
113
114
115 public void setStartLongitudeRange(String startLongitudeRange)
116 {
117 this.startLongitudeRange = Float.valueOf(startLongitudeRange);
118 }
119
120
121
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
131
132 public int getNumVariables()
133 {
134 return numVariables;
135 }
136
137
138
139
140 public int getNumLongitudes()
141 {
142 return numLongitudes;
143 }
144
145
146
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
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
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 }