-
Notifications
You must be signed in to change notification settings - Fork 68
/
Nexrad2IOServiceProvider.java
588 lines (486 loc) · 24.1 KB
/
Nexrad2IOServiceProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.nc2.iosp.nexrad2;
import static ucar.nc2.iosp.nexrad2.Level2Record.BELOW_THRESHOLD;
import static ucar.nc2.iosp.nexrad2.Level2Record.DIFF_PHASE;
import static ucar.nc2.iosp.nexrad2.Level2Record.HORIZONTAL_BEAM_WIDTH;
import static ucar.nc2.iosp.nexrad2.Level2Record.MISSING_DATA;
import static ucar.nc2.iosp.nexrad2.Level2Record.REFLECTIVITY_HIGH;
import static ucar.nc2.iosp.nexrad2.Level2Record.VELOCITY_HIGH;
import static ucar.nc2.iosp.nexrad2.Level2Record.getDatatypeUnits;
import static ucar.nc2.iosp.nexrad2.Level2Record.getDate;
import static ucar.nc2.iosp.nexrad2.Level2Record.getVolumeCoveragePatternName;
import ucar.ma2.*;
import ucar.nc2.*;
import ucar.nc2.constants.*;
import ucar.nc2.iosp.AbstractIOServiceProvider;
import ucar.nc2.units.DateFormatter;
import ucar.nc2.util.CancelTask;
import ucar.unidata.io.RandomAccessFile;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
/**
* An IOServiceProvider for NEXRAD level II files.
*
* @author caron
*/
public class Nexrad2IOServiceProvider extends AbstractIOServiceProvider {
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Nexrad2IOServiceProvider.class);
private static final int MISSING_INT = -9999;
private static final float MISSING_FLOAT = Float.NaN;
static private boolean isNEXRAD2Format(String format) {
// somewhat duplicated in Nexrad2RadialAdapter - so if changing something here, also
// look in cdm-core module's Nexrad2RadialAdapter.isNEXRAD2Format
if (format != null && (format.equals("ARCHIVE2") || format.startsWith("AR2V"))) {
if (Integer.parseInt(format.substring(4)) > 8)
logger.warn("Trying to handle unknown but valid-looking format: " + format);
return true;
}
return false;
}
public boolean isValidFile(RandomAccessFile raf) {
try {
raf.seek(0);
return isNEXRAD2Format(raf.readString(8));
} catch (IOException ioe) {
return false;
}
}
// private Dimension radialDim;
private double radarRadius;
private Variable v0, v1;
private DateFormatter formatter = new DateFormatter();
private boolean overMidNight;
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
super.open(raf, ncfile, cancelTask);
NexradStationDB.init();
Level2VolumeScan volScan = new Level2VolumeScan(raf, cancelTask); // note raf may change when compressed
this.raf = volScan.raf;
this.location = volScan.raf.getLocation();
if (volScan.hasDifferentDopplarResolutions())
throw new IllegalStateException("volScan.hasDifferentDopplarResolutions");
if (volScan.hasHighResolutions(0)) {
if (volScan.getHighResReflectivityGroups() != null)
makeVariable2(ncfile, REFLECTIVITY_HIGH, "Reflectivity", "Reflectivity", "R", volScan);
if (volScan.getHighResVelocityGroups() != null)
makeVariable2(ncfile, VELOCITY_HIGH, "RadialVelocity", "Radial Velocity", "V", volScan);
if (volScan.getHighResSpectrumGroups() != null) {
List<List<Level2Record>> gps = volScan.getHighResSpectrumGroups();
List<Level2Record> gp = gps.get(0);
Level2Record record = gp.get(0);
if (v1 != null)
makeVariableNoCoords(ncfile, Level2Record.SPECTRUM_WIDTH_HIGH, "SpectrumWidth_HI", "Radial Spectrum_HI", v1,
record);
if (v0 != null)
makeVariableNoCoords(ncfile, Level2Record.SPECTRUM_WIDTH_HIGH, "SpectrumWidth", "Radial Spectrum", v0,
record);
}
}
List<List<Level2Record>> gps = volScan.getHighResDiffReflectGroups();
if (gps != null) {
makeVariable2(ncfile, Level2Record.DIFF_REFLECTIVITY_HIGH, "DifferentialReflectivity",
"Differential Reflectivity", "D", volScan);
}
gps = volScan.getHighResCoeffocientGroups();
if (gps != null) {
makeVariable2(ncfile, Level2Record.CORRELATION_COEFFICIENT, "CorrelationCoefficient", "Correlation Coefficient",
"C", volScan);
}
gps = volScan.getHighResDiffPhaseGroups();
if (gps != null) {
makeVariable2(ncfile, DIFF_PHASE, "DifferentialPhase", "Differential Phase", "P", volScan);
}
gps = volScan.getReflectivityGroups();
if (gps != null) {
makeVariable(ncfile, Level2Record.REFLECTIVITY, "Reflectivity", "Reflectivity", "R",
volScan.getReflectivityGroups(), 0, volScan);
int velocity_type =
(volScan.getDopplarResolution() == Level2Record.DOPPLER_RESOLUTION_HIGH_CODE) ? Level2Record.VELOCITY_HI
: Level2Record.VELOCITY_LOW;
Variable v = makeVariable(ncfile, velocity_type, "RadialVelocity", "Radial Velocity", "V",
volScan.getVelocityGroups(), 0, volScan);
gps = volScan.getVelocityGroups();
List<Level2Record> gp = gps.get(0);
Level2Record record = gp.get(0);
makeVariableNoCoords(ncfile, Level2Record.SPECTRUM_WIDTH, "SpectrumWidth", "Spectrum Width", v, record);
}
if (volScan.getStationId() != null) {
ncfile.addAttribute(null, new Attribute("Station", volScan.getStationId()));
ncfile.addAttribute(null, new Attribute("StationName", volScan.getStationName()));
ncfile.addAttribute(null, new Attribute("StationLatitude", volScan.getStationLatitude()));
ncfile.addAttribute(null, new Attribute("StationLongitude", volScan.getStationLongitude()));
ncfile.addAttribute(null, new Attribute("StationElevationInMeters", volScan.getStationElevation()));
double latRadiusDegrees = Math.toDegrees(radarRadius / ucar.unidata.geoloc.Earth.getRadius());
ncfile.addAttribute(null, new Attribute("geospatial_lat_min", volScan.getStationLatitude() - latRadiusDegrees));
ncfile.addAttribute(null, new Attribute("geospatial_lat_max", volScan.getStationLatitude() + latRadiusDegrees));
double cosLat = Math.cos(Math.toRadians(volScan.getStationLatitude()));
double lonRadiusDegrees = Math.toDegrees(radarRadius / cosLat / ucar.unidata.geoloc.Earth.getRadius());
ncfile.addAttribute(null, new Attribute("geospatial_lon_min", volScan.getStationLongitude() - lonRadiusDegrees));
ncfile.addAttribute(null, new Attribute("geospatial_lon_max", volScan.getStationLongitude() + lonRadiusDegrees));
// add a radial coordinate transform (experimental)
/*
* Variable ct = new Variable(ncfile, null, null, "radialCoordinateTransform");
* ct.setDataType(DataType.CHAR);
* ct.setDimensions(""); // scalar
* ct.addAttribute( new Attribute("transform_name", "Radial"));
* ct.addAttribute( new Attribute("center_latitude", volScan.getStationLatitude()));
* ct.addAttribute( new Attribute("center_longitude", volScan.getStationLongitude()));
* ct.addAttribute( new Attribute("center_elevation", volScan.getStationElevation()));
* ct.addAttribute( new Attribute(_Coordinate.TransformType, "Radial"));
* ct.addAttribute( new Attribute(_Coordinate.AxisTypes, "RadialElevation RadialAzimuth RadialDistance"));
*
* Array data = Array.factory(DataType.CHAR, new int[0], new char[] {' '});
* ct.setCachedData(data, true);
* ncfile.addVariable(null, ct);
*/
}
DateFormatter formatter = new DateFormatter();
ncfile.addAttribute(null, new Attribute(CDM.CONVENTIONS, _Coordinate.Convention));
ncfile.addAttribute(null, new Attribute("format", volScan.getDataFormat()));
ncfile.addAttribute(null, new Attribute("cdm_data_type", FeatureType.RADIAL.toString()));
Date d = getDate(volScan.getTitleJulianDays(), volScan.getTitleMsecs());
ncfile.addAttribute(null, new Attribute("base_date", formatter.toDateOnlyString(d)));
ncfile.addAttribute(null, new Attribute("time_coverage_start", formatter.toDateTimeStringISO(d)));
ncfile.addAttribute(null, new Attribute("time_coverage_end", formatter.toDateTimeStringISO(volScan.getEndDate())));
ncfile.addAttribute(null, new Attribute(CDM.HISTORY, "Direct read of Nexrad Level 2 file into CDM"));
ncfile.addAttribute(null, new Attribute("DataType", "Radial"));
ncfile.addAttribute(null,
new Attribute("Title",
"Nexrad Level 2 Station " + volScan.getStationId() + " from "
+ formatter.toDateTimeStringISO(volScan.getStartDate()) + " to "
+ formatter.toDateTimeStringISO(volScan.getEndDate())));
ncfile.addAttribute(null, new Attribute("Summary", "Weather Surveillance Radar-1988 Doppler (WSR-88D) "
+ "Level II data are the three meteorological base data quantities: reflectivity, mean radial velocity, and "
+ "spectrum width."));
ncfile.addAttribute(null, new Attribute("keywords",
"WSR-88D; NEXRAD; Radar Level II; reflectivity; mean radial velocity; spectrum width"));
ncfile.addAttribute(null,
new Attribute("VolumeCoveragePatternName", getVolumeCoveragePatternName(volScan.getVCP())));
ncfile.addAttribute(null, new Attribute("VolumeCoveragePattern", volScan.getVCP()));
ncfile.addAttribute(null, new Attribute("HorizontalBeamWidthInDegrees", (double) HORIZONTAL_BEAM_WIDTH));
ncfile.finish();
}
public void makeVariable2(NetcdfFile ncfile, int datatype, String shortName, String longName, String abbrev,
Level2VolumeScan vScan) {
List<List<Level2Record>> groups;
if (shortName.startsWith("Reflectivity"))
groups = vScan.getHighResReflectivityGroups();
else if (shortName.startsWith("RadialVelocity"))
groups = vScan.getHighResVelocityGroups();
else if (shortName.startsWith("DifferentialReflectivity"))
groups = vScan.getHighResDiffReflectGroups();
else if (shortName.startsWith("CorrelationCoefficient"))
groups = vScan.getHighResCoeffocientGroups();
else if (shortName.startsWith("DifferentialPhase"))
groups = vScan.getHighResDiffPhaseGroups();
else
throw new IllegalStateException("Bad group: " + shortName);
int nscans = groups.size();
if (nscans == 0) {
throw new IllegalStateException("No data for " + shortName);
}
List<List<Level2Record>> firstGroup = new ArrayList<>(groups.size());
List<List<Level2Record>> secondGroup = new ArrayList<>(groups.size());
for (List<Level2Record> o : groups) {
Level2Record firstRecord = o.get(0);
int ol = o.size();
if (ol >= 720) {
firstGroup.add(o);
} else if (ol <= 360) {
secondGroup.add(o);
} else if (firstRecord.getGateCount(REFLECTIVITY_HIGH) > 500 || firstRecord.getGateCount(VELOCITY_HIGH) > 1000) {
firstGroup.add(o);
} else {
secondGroup.add(o);
}
}
if (firstGroup != null && !firstGroup.isEmpty())
v1 = makeVariable(ncfile, datatype, shortName + "_HI", longName + "_HI", abbrev + "_HI", firstGroup, 1, vScan);
if (secondGroup != null && !secondGroup.isEmpty())
v0 = makeVariable(ncfile, datatype, shortName, longName, abbrev, secondGroup, 0, vScan);
}
public int getMaxRadials(List groups) {
int maxRadials = 0;
for (Object o : groups) {
ArrayList group = (ArrayList) o;
maxRadials = Math.max(maxRadials, group.size());
}
return maxRadials;
}
public Variable makeVariable(NetcdfFile ncfile, int datatype, String shortName, String longName, String abbrev,
List<List<Level2Record>> groups, int rd) {
return makeVariable(ncfile, datatype, shortName, longName, abbrev, groups, rd, null);
}
public Variable makeVariable(NetcdfFile ncfile, int datatype, String shortName, String longName, String abbrev,
List<List<Level2Record>> groups, int rd, Level2VolumeScan volScan) {
int nscans = groups.size();
if (nscans == 0) {
throw new IllegalStateException("No data for " + shortName + " file= " + ncfile.getLocation());
}
// get representative record
List<Level2Record> firstGroup = groups.get(0);
Level2Record firstRecord = firstGroup.get(0);
int ngates = firstRecord.getGateCount(datatype);
String scanDimName = "scan" + abbrev;
String gateDimName = "gate" + abbrev;
String radialDimName = "radial" + abbrev;
Dimension scanDim = new Dimension(scanDimName, nscans);
Dimension gateDim = new Dimension(gateDimName, ngates);
Dimension radialDim = new Dimension(radialDimName, volScan.getMaxRadials(rd), true);
ncfile.addDimension(null, scanDim);
ncfile.addDimension(null, gateDim);
ncfile.addDimension(null, radialDim);
List<Dimension> dims = new ArrayList<>();
dims.add(scanDim);
dims.add(radialDim);
dims.add(gateDim);
Variable v = new Variable(ncfile, null, null, shortName);
if (datatype == DIFF_PHASE) {
v.setDataType(DataType.USHORT);
} else {
v.setDataType(DataType.UBYTE);
}
v.setDimensions(dims);
ncfile.addVariable(null, v);
v.addAttribute(new Attribute(CDM.UNITS, getDatatypeUnits(datatype)));
v.addAttribute(new Attribute(CDM.LONG_NAME, longName));
byte[] b = new byte[2];
b[0] = MISSING_DATA;
b[1] = BELOW_THRESHOLD;
Array missingArray = Array.factory(DataType.BYTE, new int[] {2}, b);
v.addAttribute(new Attribute(CDM.MISSING_VALUE, missingArray));
v.addAttribute(new Attribute("signal_below_threshold", BELOW_THRESHOLD));
v.addAttribute(new Attribute(CDM.SCALE_FACTOR, firstRecord.getDatatypeScaleFactor(datatype)));
v.addAttribute(new Attribute(CDM.ADD_OFFSET, firstRecord.getDatatypeAddOffset(datatype)));
// v.addAttribute( new Attribute(CDM.UNSIGNED, "true"));
if (rd == 1) {
v.addAttribute(new Attribute("SNR_threshold", firstRecord.getDatatypeSNRThreshhold(datatype)));
}
v.addAttribute(new Attribute("range_folding_threshold", firstRecord.getDatatypeRangeFoldingThreshhold(datatype)));
List<Dimension> dim2 = new ArrayList<>();
dim2.add(scanDim);
dim2.add(radialDim);
// add time coordinate variable
String timeCoordName = "time" + abbrev;
Variable timeVar = new Variable(ncfile, null, null, timeCoordName);
timeVar.setDataType(DataType.INT);
timeVar.setDimensions(dim2);
ncfile.addVariable(null, timeVar);
// int julianDays = volScan.getTitleJulianDays();
// Date d = Level2Record.getDate( julianDays, 0);
// Date d = getDate(volScan.getTitleJulianDays(), volScan.getTitleMsecs());
Date d = getDate(volScan.getTitleJulianDays(), 0); // times are msecs from midnight
String units = "msecs since " + formatter.toDateTimeStringISO(d);
timeVar.addAttribute(new Attribute(CDM.LONG_NAME, "time of each ray"));
timeVar.addAttribute(new Attribute(CDM.UNITS, units));
timeVar.addAttribute(new Attribute(CDM.MISSING_VALUE, MISSING_INT));
timeVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
// add elevation coordinate variable
String elevCoordName = "elevation" + abbrev;
Variable elevVar = new Variable(ncfile, null, null, elevCoordName);
elevVar.setDataType(DataType.FLOAT);
elevVar.setDimensions(dim2);
ncfile.addVariable(null, elevVar);
elevVar.addAttribute(new Attribute(CDM.UNITS, "degrees"));
elevVar.addAttribute(
new Attribute(CDM.LONG_NAME, "elevation angle in degres: 0 = parallel to pedestal base, 90 = perpendicular"));
elevVar.addAttribute(new Attribute(CDM.MISSING_VALUE, MISSING_FLOAT));
elevVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.RadialElevation.toString()));
// add azimuth coordinate variable
String aziCoordName = "azimuth" + abbrev;
Variable aziVar = new Variable(ncfile, null, null, aziCoordName);
aziVar.setDataType(DataType.FLOAT);
aziVar.setDimensions(dim2);
ncfile.addVariable(null, aziVar);
aziVar.addAttribute(new Attribute(CDM.UNITS, "degrees"));
aziVar.addAttribute(new Attribute(CDM.LONG_NAME, "azimuth angle in degrees: 0 = true north, 90 = east"));
aziVar.addAttribute(new Attribute(CDM.MISSING_VALUE, MISSING_FLOAT));
aziVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.RadialAzimuth.toString()));
// add gate coordinate variable
String gateCoordName = "distance" + abbrev;
Variable gateVar = new Variable(ncfile, null, null, gateCoordName);
gateVar.setDataType(DataType.FLOAT);
gateVar.setDimensions(gateDimName);
Array data = Array.makeArray(DataType.FLOAT, ngates, (double) firstRecord.getGateStart(datatype),
(double) firstRecord.getGateSize(datatype));
gateVar.setCachedData(data, false);
ncfile.addVariable(null, gateVar);
radarRadius = firstRecord.getGateStart(datatype) + ngates * firstRecord.getGateSize(datatype);
gateVar.addAttribute(new Attribute(CDM.UNITS, "m"));
gateVar.addAttribute(new Attribute(CDM.LONG_NAME, "radial distance to start of gate"));
gateVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.RadialDistance.toString()));
// add number of radials variable
String nradialsName = "numRadials" + abbrev;
Variable nradialsVar = new Variable(ncfile, null, null, nradialsName);
nradialsVar.setDataType(DataType.INT);
nradialsVar.setDimensions(scanDim.getShortName());
nradialsVar.addAttribute(new Attribute(CDM.LONG_NAME, "number of valid radials in this scan"));
ncfile.addVariable(null, nradialsVar);
// add number of gates variable
String ngateName = "numGates" + abbrev;
Variable ngateVar = new Variable(ncfile, null, null, ngateName);
ngateVar.setDataType(DataType.INT);
ngateVar.setDimensions(scanDim.getShortName());
ngateVar.addAttribute(new Attribute(CDM.LONG_NAME, "number of valid gates in this scan"));
ncfile.addVariable(null, ngateVar);
makeCoordinateDataWithMissing(datatype, timeVar, elevVar, aziVar, nradialsVar, ngateVar, groups);
// back to the data variable
String coordinates = timeCoordName + " " + elevCoordName + " " + aziCoordName + " " + gateCoordName;
v.addAttribute(new Attribute(_Coordinate.Axes, coordinates));
// make the record map
int nradials = radialDim.getLength();
Level2Record[][] map = new Level2Record[nscans][nradials];
for (int i = 0; i < groups.size(); i++) {
Level2Record[] mapScan = map[i];
List<Level2Record> group = groups.get(i);
for (Level2Record r : group) {
int radial = r.radial_num - 1;
if (radial >= nradials) {
radial %= nradials;
}
mapScan[radial] = r;
}
}
Vgroup vg = new Vgroup(datatype, map);
v.setSPobject(vg);
return v;
}
private void makeVariableNoCoords(NetcdfFile ncfile, int datatype, String shortName, String longName, Variable from,
Level2Record record) {
// get representative record
Variable v = new Variable(ncfile, null, null, shortName);
v.setDataType(DataType.UBYTE);
v.setDimensions(from.getDimensions());
ncfile.addVariable(null, v);
v.addAttribute(new Attribute(CDM.UNITS, getDatatypeUnits(datatype)));
v.addAttribute(new Attribute(CDM.LONG_NAME, longName));
byte[] b = new byte[2];
b[0] = MISSING_DATA;
b[1] = BELOW_THRESHOLD;
Array missingArray = Array.factory(DataType.BYTE, new int[] {2}, b);
v.addAttribute(new Attribute(CDM.MISSING_VALUE, missingArray));
v.addAttribute(new Attribute("signal_below_threshold", BELOW_THRESHOLD));
v.addAttribute(new Attribute(CDM.SCALE_FACTOR, record.getDatatypeScaleFactor(datatype)));
v.addAttribute(new Attribute(CDM.ADD_OFFSET, record.getDatatypeAddOffset(datatype)));
// v.addAttribute( new Attribute(CDM.UNSIGNED, "true"));
if (datatype == Level2Record.SPECTRUM_WIDTH_HIGH) {
v.addAttribute(new Attribute("SNR_threshold", record.getDatatypeSNRThreshhold(datatype)));
}
v.addAttribute(new Attribute("range_folding_threshold", record.getDatatypeRangeFoldingThreshhold(datatype)));
Attribute fromAtt = from.findAttribute(_Coordinate.Axes);
v.addAttribute(new Attribute(_Coordinate.Axes, fromAtt));
Vgroup vgFrom = (Vgroup) from.getSPobject();
Vgroup vg = new Vgroup(datatype, vgFrom.map);
v.setSPobject(vg);
}
private void makeCoordinateDataWithMissing(int datatype, Variable time, Variable elev, Variable azi,
Variable nradialsVar, Variable ngatesVar, List groups) {
Array timeData = Array.factory(time.getDataType(), time.getShape());
Index timeIndex = timeData.getIndex();
Array elevData = Array.factory(elev.getDataType(), elev.getShape());
Index elevIndex = elevData.getIndex();
Array aziData = Array.factory(azi.getDataType(), azi.getShape());
Index aziIndex = aziData.getIndex();
Array nradialsData = Array.factory(nradialsVar.getDataType(), nradialsVar.getShape());
IndexIterator nradialsIter = nradialsData.getIndexIterator();
Array ngatesData = Array.factory(ngatesVar.getDataType(), ngatesVar.getShape());
IndexIterator ngatesIter = ngatesData.getIndexIterator();
// first fill with missing data
IndexIterator ii = timeData.getIndexIterator();
while (ii.hasNext())
ii.setIntNext(MISSING_INT);
ii = elevData.getIndexIterator();
while (ii.hasNext())
ii.setFloatNext(MISSING_FLOAT);
ii = aziData.getIndexIterator();
while (ii.hasNext())
ii.setFloatNext(MISSING_FLOAT);
// now set the coordinate variables from the Level2Record radial
int last_msecs = Integer.MIN_VALUE;
int nscans = groups.size();
for (int scan = 0; scan < nscans; scan++) {
List scanGroup = (List) groups.get(scan);
int nradials = scanGroup.size();
Level2Record first = null;
for (Object o : scanGroup) {
Level2Record r = (Level2Record) o;
if (first == null) {
first = r;
}
int radial = r.radial_num - 1;
if (radial >= nradials) {
radial %= nradials;
}
if (last_msecs != Integer.MIN_VALUE && (last_msecs - r.data_msecs) > 80000000) {
overMidNight = true;
}
if (overMidNight) {
timeData.setInt(timeIndex.set(scan, radial), r.data_msecs + 24 * 3600 * 1000);
} else {
timeData.setInt(timeIndex.set(scan, radial), r.data_msecs);
}
elevData.setFloat(elevIndex.set(scan, radial), r.getElevation());
aziData.setFloat(aziIndex.set(scan, radial), r.getAzimuth());
if (r.data_msecs < last_msecs && !overMidNight) {
logger.warn("makeCoordinateData time out of order: " + r.data_msecs + " before " + last_msecs);
}
last_msecs = r.data_msecs;
}
nradialsIter.setIntNext(nradials);
if (first != null)
ngatesIter.setIntNext(first.getGateCount(datatype));
}
time.setCachedData(timeData, false);
elev.setCachedData(elevData, false);
azi.setCachedData(aziData, false);
nradialsVar.setCachedData(nradialsData, false);
ngatesVar.setCachedData(ngatesData, false);
}
public Array readData(Variable v2, Section section) throws IOException {
Vgroup vgroup = (Vgroup) v2.getSPobject();
Range scanRange = section.getRange(0);
Range radialRange = section.getRange(1);
Range gateRange = section.getRange(2);
Array data = Array.factory(v2.getDataType(), section.getShape());
IndexIterator ii = data.getIndexIterator();
for (int scanIdx : scanRange) {
Level2Record[] mapScan = vgroup.map[scanIdx];
readOneScan(mapScan, radialRange, gateRange, vgroup.datatype, ii);
}
return data;
}
private void readOneScan(Level2Record[] mapScan, Range radialRange, Range gateRange, int datatype, IndexIterator ii)
throws IOException {
for (int radialIdx : radialRange) {
Level2Record r = mapScan[radialIdx];
readOneRadial(r, datatype, gateRange, ii);
}
}
private void readOneRadial(Level2Record r, int datatype, Range gateRange, IndexIterator ii) throws IOException {
if (r == null) {
for (int i = 0; i < gateRange.length(); i++)
ii.setByteNext(MISSING_DATA);
return;
}
r.readData(raf, datatype, gateRange, ii);
}
private static class Vgroup {
Level2Record[][] map;
int datatype;
Vgroup(int datatype, Level2Record[][] map) {
this.datatype = datatype;
this.map = map;
}
}
/////////////////////////////////////////////////////////////////////
public String getFileTypeId() {
return DataFormatType.NEXRAD2.getDescription();
}
public String getFileTypeDescription() {
return "NEXRAD Level-II Base Data";
}
}