Skip to content

Commit

Permalink
Refactor building NetcdfDataset code to helper methog to avoid duplic…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Tara Drwenski committed Nov 14, 2023
1 parent 797d9a8 commit fec51d0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
18 changes: 4 additions & 14 deletions grib/src/main/java/ucar/nc2/grib/collection/Grib1Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package ucar.nc2.grib.collection;

import javax.annotation.Nullable;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.grib.coord.CoordinateTimeAbstract;
import ucar.nc2.*;
import ucar.nc2.constants.DataFormatType;
Expand All @@ -23,7 +22,6 @@
import ucar.nc2.grib.grib1.tables.Grib1Customizer;
import java.io.IOException;
import java.util.Formatter;
import ucar.unidata.io.RandomAccessFile;

/**
* Grib1-specific subclass of GribCollection.
Expand All @@ -43,9 +41,7 @@ public ucar.nc2.dataset.NetcdfDataset getNetcdfDataset(Dataset ds, GroupGC group
FeatureCollectionConfig gribConfig, Formatter errlog, org.slf4j.Logger logger) throws IOException {
if (filename == null) {
Grib1Iosp iosp = new Grib1Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
return buildNetcdfDataset(iosp, getLocation());

} else {
MFile wantFile = findMFileByName(filename);
Expand All @@ -56,9 +52,7 @@ public ucar.nc2.dataset.NetcdfDataset getNetcdfDataset(Dataset ds, GroupGC group
return null;

Grib1Iosp iosp = new Grib1Iosp(gc);
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
return buildNetcdfDataset(iosp, getLocation());
}
return null;
}
Expand All @@ -70,9 +64,7 @@ public ucar.nc2.dt.grid.GridDataset getGridDataset(Dataset ds, GroupGC group, St
FeatureCollectionConfig gribConfig, Formatter errlog, org.slf4j.Logger logger) throws IOException {
if (filename == null) {
Grib1Iosp iosp = new Grib1Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation() + "#" + group.getId(), null);
NetcdfDataset ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
NetcdfDataset ncd = buildNetcdfDataset(iosp, getLocation() + "#" + group.getId());
return new ucar.nc2.dt.grid.GridDataset(ncd); // LOOK - replace with custom GridDataset??

} else {
Expand All @@ -84,9 +76,7 @@ public ucar.nc2.dt.grid.GridDataset getGridDataset(Dataset ds, GroupGC group, St
return null;

Grib1Iosp iosp = new Grib1Iosp(gc);
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
NetcdfDataset ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
NetcdfDataset ncd = buildNetcdfDataset(iosp, getLocation());
return new ucar.nc2.dt.grid.GridDataset(ncd); // LOOK - replace with custom GridDataset??
}
return null;
Expand Down
10 changes: 2 additions & 8 deletions grib/src/main/java/ucar/nc2/grib/collection/Grib1Partition.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import thredds.featurecollection.FeatureCollectionConfig;
import ucar.nc2.constants.CDM;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.ft2.coverage.CoverageCollection;
import ucar.nc2.grib.GribUtils;
import ucar.nc2.grib.coverage.GribCoverageDataset;
import java.io.IOException;
import java.util.Formatter;
import ucar.unidata.io.RandomAccessFile;

/**
* PartitionCollection for Grib1.
Expand All @@ -35,19 +33,15 @@ public ucar.nc2.dataset.NetcdfDataset getNetcdfDataset(Dataset ds, GroupGC group
FeatureCollectionConfig config, Formatter errlog, org.slf4j.Logger logger) throws IOException {

ucar.nc2.grib.collection.Grib1Iosp iosp = new ucar.nc2.grib.collection.Grib1Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
return buildNetcdfDataset(iosp, getLocation());
}

@Override
public ucar.nc2.dt.grid.GridDataset getGridDataset(Dataset ds, GroupGC group, String filename,
FeatureCollectionConfig config, Formatter errlog, org.slf4j.Logger logger) throws IOException {

ucar.nc2.grib.collection.Grib1Iosp iosp = new ucar.nc2.grib.collection.Grib1Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
NetcdfDataset ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
NetcdfDataset ncd = buildNetcdfDataset(iosp, getLocation());
return new ucar.nc2.dt.grid.GridDataset(ncd);
}

Expand Down
18 changes: 4 additions & 14 deletions grib/src/main/java/ucar/nc2/grib/collection/Grib2Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package ucar.nc2.grib.collection;

import javax.annotation.Nullable;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.grib.coord.CoordinateTimeAbstract;
import ucar.ma2.Array;
import ucar.nc2.*;
Expand All @@ -21,7 +20,6 @@
import ucar.nc2.grib.GribTables;
import ucar.nc2.grib.coverage.GribCoverageDataset;
import ucar.nc2.grib.grib2.table.Grib2Tables;
import ucar.unidata.io.RandomAccessFile;
import ucar.unidata.util.StringUtil2;
import java.io.IOException;
import java.util.Formatter;
Expand All @@ -45,9 +43,7 @@ public ucar.nc2.dataset.NetcdfDataset getNetcdfDataset(Dataset ds, GroupGC group

if (filename == null) {
Grib2Iosp iosp = new Grib2Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
return buildNetcdfDataset(iosp, getLocation());

} else {
MFile wantFile = findMFileByName(filename);
Expand All @@ -58,9 +54,7 @@ public ucar.nc2.dataset.NetcdfDataset getNetcdfDataset(Dataset ds, GroupGC group
return null;

Grib2Iosp iosp = new Grib2Iosp(gc);
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
return buildNetcdfDataset(iosp, getLocation());
}
return null;
}
Expand All @@ -73,9 +67,7 @@ public ucar.nc2.dt.grid.GridDataset getGridDataset(Dataset ds, GroupGC group, St

if (filename == null) {
Grib2Iosp iosp = new Grib2Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation() + "#" + group.getId(), null);
NetcdfDataset ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
NetcdfDataset ncd = buildNetcdfDataset(iosp, getLocation() + "#" + group.getId());
return new ucar.nc2.dt.grid.GridDataset(ncd); // LOOK - replace with custom GridDataset??

} else {
Expand All @@ -87,9 +79,7 @@ public ucar.nc2.dt.grid.GridDataset getGridDataset(Dataset ds, GroupGC group, St
return null;

Grib2Iosp iosp = new Grib2Iosp(gc);
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
NetcdfDataset ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
NetcdfDataset ncd = buildNetcdfDataset(iosp, getLocation());
return new ucar.nc2.dt.grid.GridDataset(ncd); // LOOK - replace with custom GridDataset??
}
return null;
Expand Down
12 changes: 2 additions & 10 deletions grib/src/main/java/ucar/nc2/grib/collection/Grib2Partition.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
import thredds.featurecollection.FeatureCollectionConfig;
import ucar.nc2.Attribute;
import ucar.nc2.AttributeContainer;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFiles;
import ucar.nc2.constants.CDM;
import ucar.nc2.constants.DataFormatType;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.ft2.coverage.CoverageCollection;
import ucar.nc2.grib.coverage.GribCoverageDataset;
import ucar.unidata.io.RandomAccessFile;

/**
* PartitionCollection for Grib2.
Expand All @@ -38,19 +34,15 @@ public ucar.nc2.dataset.NetcdfDataset getNetcdfDataset(Dataset ds, GroupGC group
FeatureCollectionConfig config, Formatter errlog, org.slf4j.Logger logger) throws IOException {

ucar.nc2.grib.collection.Grib2Iosp iosp = new ucar.nc2.grib.collection.Grib2Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
return buildNetcdfDataset(iosp, getLocation());
}

@Override
public ucar.nc2.dt.grid.GridDataset getGridDataset(Dataset ds, GroupGC group, String filename,
FeatureCollectionConfig config, Formatter errlog, org.slf4j.Logger logger) throws IOException {

ucar.nc2.grib.collection.Grib2Iosp iosp = new ucar.nc2.grib.collection.Grib2Iosp(group, ds.getType());
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, getLocation(), null);
NetcdfDataset ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
NetcdfDataset ncd = buildNetcdfDataset(iosp, getLocation());
return new ucar.nc2.dt.grid.GridDataset(ncd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
import ucar.nc2.Attribute;
import ucar.nc2.AttributeContainer;
import ucar.nc2.AttributeContainerMutable;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFiles;
import ucar.nc2.constants.CDM;
import ucar.nc2.constants.CF;
import ucar.nc2.constants.FeatureType;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.ft2.coverage.CoverageCollection;
import ucar.nc2.ft2.coverage.SubsetParams;
import ucar.nc2.grib.*;
Expand All @@ -31,6 +35,7 @@
import ucar.nc2.grib.grib1.Grib1Variable;
import ucar.nc2.grib.grib1.tables.Grib1Customizer;
import ucar.nc2.grib.grib2.table.Grib2Tables;
import ucar.nc2.iosp.AbstractIOServiceProvider;
import ucar.nc2.time.CalendarDate;
import ucar.nc2.time.CalendarDateRange;
import ucar.nc2.util.cache.FileCacheIF;
Expand Down Expand Up @@ -1113,6 +1118,13 @@ String getDataRafFilename(int fileno) {
return mfile.getPath();
}

protected static NetcdfDataset buildNetcdfDataset(AbstractIOServiceProvider iosp, String location)
throws IOException {
RandomAccessFile raf = (RandomAccessFile) iosp.sendIospMessage(NetcdfFile.IOSP_MESSAGE_RANDOM_ACCESS_FILE);
NetcdfFile ncfile = NetcdfFiles.build(iosp, raf, location, null);
return NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
}

///////////////////////

// stuff needed by InvDatasetFcGrib
Expand Down

0 comments on commit fec51d0

Please sign in to comment.