Skip to content

Commit be15425

Browse files
committed
adding root variables in open function of BufrIosp2 class
1 parent 80f0f9a commit be15425

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

bufr/src/main/java/ucar/nc2/iosp/bufr/BufrIosp2.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,34 @@ public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask)
154154
if (!protoMessage.isTablesComplete())
155155
throw new IllegalStateException("BUFR file has incomplete tables");
156156

157+
// get all prototype messages - contains different message category in a Bufr data file
158+
protoMessages = new ArrayList<>();
159+
protoMessages.add(protoMessage);
160+
int category = protoMessage.ids.getCategory();
161+
while (scanner.hasNext()) {
162+
Message message = scanner.next();
163+
if (message.ids.getCategory() != category) {
164+
protoMessages.add(message);
165+
category = message.ids.getCategory();
166+
}
167+
}
168+
157169
// just get the fields
158170
config = BufrConfig.openFromMessage(raf, protoMessage, iospParam);
159171

160172
// this fills the netcdf object
161-
Construct2 construct = new Construct2(protoMessage, config, ncfile);
162-
Structure obsStructure = construct.getObsStructure();
173+
if (this.protoMessages.size() == 1) {
174+
Construct2 construct = new Construct2(protoMessage, config, ncfile);
175+
} else {
176+
List<BufrConfig> configs = new ArrayList<>();
177+
for (Message message : protoMessages) {
178+
configs.add(BufrConfig.openFromMessage(raf, message, iospParam));
179+
}
180+
Construct2 construct = new Construct2(protoMessage, configs, ncfile);
181+
}
182+
163183
ncfile.finish();
184+
buildFinish(ncfile);
164185
isSingle = false;
165186
}
166187

bufr/src/main/java/ucar/nc2/iosp/bufr/Construct2.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,46 @@ class Construct2 {
8080
ncfile.finish();
8181
}
8282

83+
Construct2(Message proto, List<BufrConfig> bufrConfigs, ucar.nc2.NetcdfFile nc) throws IOException {
84+
this.ncfile = nc;
85+
86+
// global Attributes
87+
ncfile.addAttribute(null, new Attribute(CDM.HISTORY, "Read using CDM BufrIosp2"));
88+
ncfile.addAttribute(null, "location", nc.getLocation());
89+
90+
ncfile.addAttribute(null, "BUFR:categoryName", proto.getLookup().getCategoryName());
91+
ncfile.addAttribute(null, "BUFR:subCategoryName", proto.getLookup().getSubCategoryName());
92+
ncfile.addAttribute(null, "BUFR:centerName", proto.getLookup().getCenterName());
93+
ncfile.addAttribute(null, new Attribute("BUFR:category", proto.ids.getCategory()));
94+
ncfile.addAttribute(null, new Attribute("BUFR:subCategory", proto.ids.getSubCategory()));
95+
ncfile.addAttribute(null, new Attribute("BUFR:localSubCategory", proto.ids.getLocalSubCategory()));
96+
ncfile.addAttribute(null, new Attribute(BufrIosp2.centerId, proto.ids.getCenterId()));
97+
ncfile.addAttribute(null, new Attribute("BUFR:subCenter", proto.ids.getSubCenterId()));
98+
// ncfile.addAttribute(null, "BUFR:tableName", proto.ids.getMasterTableFilename()));
99+
ncfile.addAttribute(null, new Attribute("BUFR:table", proto.ids.getMasterTableId()));
100+
ncfile.addAttribute(null, new Attribute("BUFR:tableVersion", proto.ids.getMasterTableVersion()));
101+
ncfile.addAttribute(null, new Attribute("BUFR:localTableVersion", proto.ids.getLocalTableVersion()));
102+
ncfile.addAttribute(null, "Conventions", "BUFR/CDM");
103+
ncfile.addAttribute(null, new Attribute("BUFR:edition", proto.is.getBufrEdition()));
104+
105+
centerId = proto.ids.getCenterId();
106+
107+
String header = proto.getHeader();
108+
if (header != null && !header.isEmpty())
109+
ncfile.addAttribute(null, new Attribute("WMO Header", header));
110+
111+
for (BufrConfig bufrConfig : bufrConfigs) {
112+
String varName = proto.getLookup().getCategoryName(bufrConfig.getMessage().ids.getCategory());
113+
Sequence rs = new Sequence(ncfile, null, null, varName);
114+
makeObsRecord(bufrConfig, rs);
115+
String coordS = coordinates.toString();
116+
if (!coordS.isEmpty())
117+
rs.addAttribute(new Attribute("coordinates", coordS));
118+
}
119+
120+
ncfile.finish();
121+
}
122+
83123
Sequence getObsStructure() {
84124
return recordStructure;
85125
}
@@ -124,6 +164,45 @@ private void makeObsRecord(BufrConfig bufrConfig) {
124164
}
125165
}
126166

167+
private void makeObsRecord(BufrConfig bufrConfig, Sequence rs) {
168+
ncfile.addVariable(null, rs);
169+
170+
BufrConfig.FieldConverter root = bufrConfig.getRootConverter();
171+
for (BufrConfig.FieldConverter fld : root.flds) {
172+
DataDescriptor dkey = fld.dds;
173+
if (!dkey.isOkForVariable())
174+
continue;
175+
176+
if (dkey.replication == 0) {
177+
addSequence(rs, fld);
178+
179+
} else if (dkey.replication > 1) {
180+
181+
List<BufrConfig.FieldConverter> subFlds = fld.flds;
182+
List<DataDescriptor> subKeys = dkey.subKeys;
183+
if (subKeys.size() == 1) { // only one member
184+
DataDescriptor subDds = dkey.subKeys.get(0);
185+
BufrConfig.FieldConverter subFld = subFlds.get(0);
186+
if (subDds.dpi != null) {
187+
addDpiStructure(rs, fld, subFld);
188+
189+
} else if (subDds.replication == 1) { // one member not a replication
190+
Variable v = addVariable(rs, subFld, dkey.replication);
191+
v.setSPobject(fld); // set the replicating field as SPI object
192+
193+
} else { // one member is a replication (two replications in a row)
194+
addStructure(rs, fld, dkey.replication);
195+
}
196+
} else if (subKeys.size() > 1) {
197+
addStructure(rs, fld, dkey.replication);
198+
}
199+
200+
} else { // replication == 1
201+
addVariable(rs, fld, dkey.replication);
202+
}
203+
}
204+
}
205+
127206
private void addStructure(Structure parent, BufrConfig.FieldConverter fld, int count) {
128207
DataDescriptor dkey = fld.dds;
129208
String uname = findUniqueName(parent, fld.getName(), "struct");

0 commit comments

Comments
 (0)