Skip to content

Commit f02729e

Browse files
committed
add OSCAR->JSON support
1 parent 27b158c commit f02729e

File tree

4 files changed

+295
-88
lines changed

4 files changed

+295
-88
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package net.nullschool.grib2json;
2+
3+
import javax.json.stream.JsonGenerator;
4+
5+
import java.util.Objects;
6+
7+
import static ucar.grib.GribNumbers.UNDEFINED;
8+
9+
10+
/**
11+
* 2014-01-15<p/>
12+
*
13+
* @author Cameron Beccario
14+
*/
15+
abstract class AbstractRecordWriter {
16+
17+
protected final JsonGenerator jg;
18+
protected final Options options;
19+
20+
protected AbstractRecordWriter(JsonGenerator jg, Options options) {
21+
this.jg = Objects.requireNonNull(jg);
22+
this.options = Objects.requireNonNull(options);
23+
}
24+
25+
/**
26+
* Write a "key":int Json pair.
27+
*/
28+
protected void write(String key, int value) {
29+
jg.write(key, value);
30+
}
31+
32+
/**
33+
* Write a "key":int Json pair only if the value is not {@link ucar.grib.GribNumbers#UNDEFINED}.
34+
*/
35+
protected void writeIfSet(String key, int value) {
36+
if (value != UNDEFINED) {
37+
jg.write(key, value);
38+
}
39+
}
40+
41+
/**
42+
* Write a "key":long Json pair.
43+
*/
44+
protected void write(String key, long value) {
45+
jg.write(key, value);
46+
}
47+
48+
/**
49+
* Write a "key":float Json pair.
50+
*/
51+
protected void write(String key, float value) {
52+
jg.write(key, new FloatValue(value));
53+
}
54+
55+
/**
56+
* Write a "key":float Json pair only if the value is not {@link ucar.grib.GribNumbers#UNDEFINED}.
57+
*/
58+
protected void writeIfSet(String key, float value) {
59+
if (value != UNDEFINED) {
60+
jg.write(key, new FloatValue(value));
61+
}
62+
}
63+
64+
/**
65+
* Write a "key":double Json pair.
66+
*/
67+
protected void write(String key, double value) {
68+
jg.write(key, value);
69+
}
70+
71+
/**
72+
* Write a "key":"value" Json pair.
73+
*/
74+
protected void write(String key, String value) {
75+
jg.write(key, value);
76+
}
77+
78+
/**
79+
* Write a "key":"value" Json pair, and a second "keyName":"name" pair if the command line options
80+
* have name printing enabled.
81+
*/
82+
protected void write(String key, int code, String name) {
83+
write(key, code);
84+
if (options.getPrintNames()) {
85+
write(key + "Name", name);
86+
}
87+
}
88+
}

src/main/java/net/nullschool/grib2json/Grib2Json.java

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package net.nullschool.grib2json;
22

3+
import org.joda.time.DateTime;
4+
import org.joda.time.DateTimeZone;
35
import ucar.grib.grib2.*;
6+
import ucar.nc2.NetcdfFile;
47
import ucar.unidata.io.RandomAccessFile;
58

69
import javax.json.Json;
@@ -23,18 +26,18 @@
2326
*/
2427
public final class Grib2Json {
2528

26-
private final File gribFile;
29+
private final File file;
2730
private final List<Options> optionGroups;
2831

29-
public Grib2Json(File gribFile, List<Options> optionGroups) {
30-
if (!gribFile.exists()) {
31-
throw new IllegalArgumentException("Cannot find input file: " + gribFile);
32+
public Grib2Json(File file, List<Options> optionGroups) {
33+
if (!file.exists()) {
34+
throw new IllegalArgumentException("Cannot find input file: " + file);
3235
}
33-
this.gribFile = gribFile;
36+
this.file = file;
3437
this.optionGroups = optionGroups;
3538
}
3639

37-
private void write(RandomAccessFile raf, Grib2Input input, Options options) throws IOException {
40+
private JsonGenerator newJsonGenerator(Options options) throws IOException {
3841
JsonGeneratorFactory jgf =
3942
Json.createGeneratorFactory(
4043
options.isCompactFormat() ?
@@ -45,13 +48,16 @@ private void write(RandomAccessFile raf, Grib2Input input, Options options) thro
4548
new BufferedOutputStream(new FileOutputStream(options.getOutput(), false)) :
4649
System.out;
4750

48-
JsonGenerator jg = jgf.createGenerator(output);
51+
return jgf.createGenerator(output);
52+
}
4953

54+
private void write(RandomAccessFile raf, Grib2Input input, Options options) throws IOException {
55+
JsonGenerator jg = newJsonGenerator(options);
5056
jg.writeStartArray();
5157

5258
List<Grib2Record> records = input.getRecords();
5359
for (Grib2Record record : records) {
54-
RecordWriter rw = new RecordWriter(jg, record, options);
60+
GribRecordWriter rw = new GribRecordWriter(jg, record, options);
5561
if (rw.isSelected()) {
5662
jg.writeStartObject();
5763
rw.writeHeader();
@@ -66,22 +72,44 @@ private void write(RandomAccessFile raf, Grib2Input input, Options options) thro
6672
jg.close();
6773
}
6874

75+
private void write(NetcdfFile netcdfFile, Options options) throws IOException {
76+
JsonGenerator jg = newJsonGenerator(options);
77+
jg.writeStartArray();
78+
79+
int days = netcdfFile.findVariable("time").readScalarInt();
80+
DateTime date = new DateTime(1992, 10, 5, 0, 0, DateTimeZone.UTC).plusDays(days);
81+
double depth = netcdfFile.findVariable("depth").readScalarDouble();
82+
83+
new OscarRecordWriter(jg, netcdfFile.findVariable("u"), date, depth, options).writeRecord();
84+
new OscarRecordWriter(jg, netcdfFile.findVariable("v"), date, depth, options).writeRecord();
85+
86+
jg.writeEnd();
87+
jg.close();
88+
}
89+
6990
/**
70-
* Convert the GRIB2 file to Json as specified by the command line options.
91+
* Convert the input file to Json as specified by the command line options.
7192
*/
7293
public void write() throws IOException {
7394

74-
RandomAccessFile raf = new RandomAccessFile(gribFile.getPath(), "r");
95+
// Try opening the file as GRIB format.
96+
RandomAccessFile raf = new RandomAccessFile(file.getPath(), "r");
7597
raf.order(RandomAccessFile.BIG_ENDIAN);
7698
Grib2Input input = new Grib2Input(raf);
77-
if (!input.scan(false, false)) {
78-
throw new IllegalArgumentException("Failed to successfully scan grib file.");
99+
if (input.scan(false, false)) {
100+
for (Options options : optionGroups) {
101+
write(raf, input, options);
102+
}
103+
raf.close();
79104
}
105+
else {
106+
raf.close();
80107

81-
for (Options options : optionGroups) {
82-
write(raf, input, options);
108+
// Otherwise, process it as NetCDF format.
109+
NetcdfFile netcdfFile = NetcdfFile.open(file.getPath());
110+
for (Options options : optionGroups) {
111+
write(netcdfFile, options);
112+
}
83113
}
84-
85-
raf.close();
86114
}
87115
}

src/main/java/net/nullschool/grib2json/RecordWriter.java renamed to src/main/java/net/nullschool/grib2json/GribRecordWriter.java

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import org.joda.time.DateTime;
44
import org.joda.time.DateTimeZone;
55
import ucar.grib.grib2.*;
6-
import ucar.grib.GribNumbers;
76

87
import javax.json.stream.JsonGenerator;
98
import java.io.IOException;
10-
import java.util.*;
119

1210
import static ucar.grib.grib1.Grib1Tables.*;
1311
import static ucar.grib.grib2.Grib2Tables.*;
@@ -24,24 +22,21 @@
2422
*
2523
* @author Cameron Beccario
2624
*/
27-
final class RecordWriter {
25+
final class GribRecordWriter extends AbstractRecordWriter {
2826

29-
private final JsonGenerator jg;
3027
private final Grib2Record record;
3128
private final Grib2IndicatorSection ins;
3229
private final Grib2IdentificationSection ids;
3330
private final Grib2Pds pds;
3431
private final Grib2GDSVariables gds;
35-
private final Options options;
3632

37-
RecordWriter(JsonGenerator jg, Grib2Record record, Options options) {
38-
this.jg = Objects.requireNonNull(jg);
33+
GribRecordWriter(JsonGenerator jg, Grib2Record record, Options options) {
34+
super(jg, options);
3935
this.record = record;
4036
this.ins = record.getIs();
4137
this.ids = record.getId();
4238
this.pds = record.getPDS().getPdsVars();
4339
this.gds = record.getGDS().getGdsVars();
44-
this.options = options;
4540
}
4641

4742
private boolean isSelected(String filterParameter) {
@@ -68,70 +63,6 @@ boolean isSelected() {
6863
isSelected(options.getFilterParameter());
6964
}
7065

71-
/**
72-
* Write a "key":int Json pair.
73-
*/
74-
private void write(String key, int value) {
75-
jg.write(key, value);
76-
}
77-
78-
/**
79-
* Write a "key":int Json pair only if the value is not {@link GribNumbers#UNDEFINED}.
80-
*/
81-
private void writeIfSet(String key, int value) {
82-
if (value != UNDEFINED) {
83-
jg.write(key, value);
84-
}
85-
}
86-
87-
/**
88-
* Write a "key":long Json pair.
89-
*/
90-
private void write(String key, long value) {
91-
jg.write(key, value);
92-
}
93-
94-
/**
95-
* Write a "key":float Json pair.
96-
*/
97-
private void write(String key, float value) {
98-
jg.write(key, new FloatValue(value));
99-
}
100-
101-
/**
102-
* Write a "key":float Json pair only if the value is not {@link GribNumbers#UNDEFINED}.
103-
*/
104-
private void writeIfSet(String key, float value) {
105-
if (value != UNDEFINED) {
106-
jg.write(key, new FloatValue(value));
107-
}
108-
}
109-
110-
/**
111-
* Write a "key":double Json pair.
112-
*/
113-
private void write(String key, double value) {
114-
jg.write(key, value);
115-
}
116-
117-
/**
118-
* Write a "key":"value" Json pair.
119-
*/
120-
private void write(String key, String value) {
121-
jg.write(key, value);
122-
}
123-
124-
/**
125-
* Write a "key":"value" Json pair, and a second "keyName":"name" pair if the command line options
126-
* have name printing enabled.
127-
*/
128-
private void write(String key, int code, String name) {
129-
write(key, code);
130-
if (options.getPrintNames()) {
131-
write(key + "Name", name);
132-
}
133-
}
134-
13566
/**
13667
* Write contents of the record's indicator section.
13768
*/

0 commit comments

Comments
 (0)