Skip to content

Commit

Permalink
adding root variables in open function of BufrIosp2 class
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaqiang committed Dec 16, 2024
1 parent 80f0f9a commit be15425
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
25 changes: 23 additions & 2 deletions bufr/src/main/java/ucar/nc2/iosp/bufr/BufrIosp2.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,34 @@ public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask)
if (!protoMessage.isTablesComplete())
throw new IllegalStateException("BUFR file has incomplete tables");

// get all prototype messages - contains different message category in a Bufr data file
protoMessages = new ArrayList<>();
protoMessages.add(protoMessage);
int category = protoMessage.ids.getCategory();
while (scanner.hasNext()) {
Message message = scanner.next();
if (message.ids.getCategory() != category) {
protoMessages.add(message);
category = message.ids.getCategory();
}
}

// just get the fields
config = BufrConfig.openFromMessage(raf, protoMessage, iospParam);

// this fills the netcdf object
Construct2 construct = new Construct2(protoMessage, config, ncfile);
Structure obsStructure = construct.getObsStructure();
if (this.protoMessages.size() == 1) {
Construct2 construct = new Construct2(protoMessage, config, ncfile);
} else {
List<BufrConfig> configs = new ArrayList<>();
for (Message message : protoMessages) {
configs.add(BufrConfig.openFromMessage(raf, message, iospParam));
}
Construct2 construct = new Construct2(protoMessage, configs, ncfile);
}

ncfile.finish();
buildFinish(ncfile);
isSingle = false;
}

Expand Down
79 changes: 79 additions & 0 deletions bufr/src/main/java/ucar/nc2/iosp/bufr/Construct2.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,46 @@ class Construct2 {
ncfile.finish();
}

Construct2(Message proto, List<BufrConfig> bufrConfigs, ucar.nc2.NetcdfFile nc) throws IOException {
this.ncfile = nc;

// global Attributes
ncfile.addAttribute(null, new Attribute(CDM.HISTORY, "Read using CDM BufrIosp2"));
ncfile.addAttribute(null, "location", nc.getLocation());

ncfile.addAttribute(null, "BUFR:categoryName", proto.getLookup().getCategoryName());
ncfile.addAttribute(null, "BUFR:subCategoryName", proto.getLookup().getSubCategoryName());
ncfile.addAttribute(null, "BUFR:centerName", proto.getLookup().getCenterName());
ncfile.addAttribute(null, new Attribute("BUFR:category", proto.ids.getCategory()));
ncfile.addAttribute(null, new Attribute("BUFR:subCategory", proto.ids.getSubCategory()));
ncfile.addAttribute(null, new Attribute("BUFR:localSubCategory", proto.ids.getLocalSubCategory()));
ncfile.addAttribute(null, new Attribute(BufrIosp2.centerId, proto.ids.getCenterId()));
ncfile.addAttribute(null, new Attribute("BUFR:subCenter", proto.ids.getSubCenterId()));
// ncfile.addAttribute(null, "BUFR:tableName", proto.ids.getMasterTableFilename()));
ncfile.addAttribute(null, new Attribute("BUFR:table", proto.ids.getMasterTableId()));
ncfile.addAttribute(null, new Attribute("BUFR:tableVersion", proto.ids.getMasterTableVersion()));
ncfile.addAttribute(null, new Attribute("BUFR:localTableVersion", proto.ids.getLocalTableVersion()));
ncfile.addAttribute(null, "Conventions", "BUFR/CDM");
ncfile.addAttribute(null, new Attribute("BUFR:edition", proto.is.getBufrEdition()));

centerId = proto.ids.getCenterId();

String header = proto.getHeader();
if (header != null && !header.isEmpty())
ncfile.addAttribute(null, new Attribute("WMO Header", header));

for (BufrConfig bufrConfig : bufrConfigs) {
String varName = proto.getLookup().getCategoryName(bufrConfig.getMessage().ids.getCategory());
Sequence rs = new Sequence(ncfile, null, null, varName);
makeObsRecord(bufrConfig, rs);
String coordS = coordinates.toString();
if (!coordS.isEmpty())
rs.addAttribute(new Attribute("coordinates", coordS));
}

ncfile.finish();
}

Sequence getObsStructure() {
return recordStructure;
}
Expand Down Expand Up @@ -124,6 +164,45 @@ private void makeObsRecord(BufrConfig bufrConfig) {
}
}

private void makeObsRecord(BufrConfig bufrConfig, Sequence rs) {
ncfile.addVariable(null, rs);

BufrConfig.FieldConverter root = bufrConfig.getRootConverter();
for (BufrConfig.FieldConverter fld : root.flds) {
DataDescriptor dkey = fld.dds;
if (!dkey.isOkForVariable())
continue;

if (dkey.replication == 0) {
addSequence(rs, fld);

} else if (dkey.replication > 1) {

List<BufrConfig.FieldConverter> subFlds = fld.flds;
List<DataDescriptor> subKeys = dkey.subKeys;
if (subKeys.size() == 1) { // only one member
DataDescriptor subDds = dkey.subKeys.get(0);
BufrConfig.FieldConverter subFld = subFlds.get(0);
if (subDds.dpi != null) {
addDpiStructure(rs, fld, subFld);

} else if (subDds.replication == 1) { // one member not a replication
Variable v = addVariable(rs, subFld, dkey.replication);
v.setSPobject(fld); // set the replicating field as SPI object

} else { // one member is a replication (two replications in a row)
addStructure(rs, fld, dkey.replication);
}
} else if (subKeys.size() > 1) {
addStructure(rs, fld, dkey.replication);
}

} else { // replication == 1
addVariable(rs, fld, dkey.replication);
}
}
}

private void addStructure(Structure parent, BufrConfig.FieldConverter fld, int count) {
DataDescriptor dkey = fld.dds;
String uname = findUniqueName(parent, fld.getName(), "struct");
Expand Down

0 comments on commit be15425

Please sign in to comment.