Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998-2021 University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

Expand Down Expand Up @@ -77,7 +77,7 @@ public static List<Object[]> getTestParameters() {
public static void after(String dataDir, String newModel, FeatureCollectionConfig config, String varName, int orgLen,
int remLen) {
// cleanup index files created during test for a given parameter input
cleanUpIndexFiles(GribCdmIndex.getTopIndexFileFromConfig(config).getParent());
cleanUpIndexFiles(GribCdmIndex.getTopIndexFileFromConfig(config).getPath());
}

///////////////////////////////////////
Expand All @@ -98,7 +98,7 @@ public TestGribCdmIndexUpdating(String dataDir, String newModel, FeatureCollecti
this.orgLen = orgLen;
this.remLen = remLen;
GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
this.indexFile = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
this.indexFile = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

package ucar.nc2.grib;

import static com.google.common.truth.Truth.assertThat;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void testExtraCoordinates() throws IOException {
config.gribConfig.setExcludeZero(true); // no longer the default

boolean changed = GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();

logger.debug("changed = {}", changed);

Expand Down Expand Up @@ -126,7 +127,7 @@ public void testBestReftimeMonotonic() throws IOException {

boolean changed = GribCdmIndex.updateGribCollection(config, updateMode, logger);
logger.debug("changed = {}", changed);
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();
boolean ok = true;

try (NetcdfDataset ds = NetcdfDatasets.openDataset(topLevelIndex)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* Copyright (c) 1998-2022 University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

package ucar.nc2.grib;

import static com.google.common.truth.Truth.assertThat;
Expand Down Expand Up @@ -80,7 +81,7 @@ private void checkVariableNameAndTimeAxis(FeatureCollectionConfig config, String
String units, double[] values) throws IOException {
final boolean changed = GribCdmIndex.updateGribCollection(config, CollectionUpdateType.always, logger);
assertThat(changed).isTrue();
final String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getAbsolutePath();
final String topLevelIndex = GribCdmIndex.getTopIndexFileFromConfig(config).getPath();

try (NetcdfDataset netcdfDataset = NetcdfDatasets.openDataset(topLevelIndex)) {
final Variable variable =
Expand Down
13 changes: 6 additions & 7 deletions grib/src/main/java/ucar/nc2/grib/GribIndex.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

Expand All @@ -12,7 +12,6 @@
import ucar.nc2.grib.grib1.Grib1Index;
import ucar.nc2.grib.grib2.Grib2Index;
import ucar.unidata.io.RandomAccessFile;
import java.io.File;
import java.io.IOException;

/**
Expand All @@ -33,11 +32,11 @@ public boolean hasChangedSince(MFile file, long when) {
String idxPath = file.getPath();
if (!idxPath.endsWith(GBX9_IDX))
idxPath += GBX9_IDX;
File idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
MFile idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
if (idxFile == null)
return true;

long idxLastModified = idxFile.lastModified();
long idxLastModified = idxFile.getLastModified();
if (idxLastModified < file.getLastModified())
return true;
return 0 < when && when < idxLastModified;
Expand All @@ -47,13 +46,13 @@ public boolean hasntChangedSince(MFile file, long when) {
String idxPath = file.getPath();
if (!idxPath.endsWith(GBX9_IDX))
idxPath += GBX9_IDX;
File idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
MFile idxFile = GribIndexCache.getExistingFileOrCache(idxPath);
if (idxFile == null)
return true;

if (idxFile.lastModified() < file.getLastModified())
if (idxFile.getLastModified() < file.getLastModified())
return true;
return 0 < when && idxFile.lastModified() < when;
return 0 < when && idxFile.getLastModified() < when;
}
};
/////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 8 additions & 6 deletions grib/src/main/java/ucar/nc2/grib/GribIndexCache.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

package ucar.nc2.grib;

import ucar.nc2.grib.collection.Grib;
import ucar.nc2.util.DiskCache2;
import thredds.inventory.MFile;
import thredds.inventory.MFiles;
import java.io.File;

/**
Expand Down Expand Up @@ -35,11 +37,11 @@ public static synchronized DiskCache2 getDiskCache2() {
* @param fileLocation full path of original index filename
* @return File, possibly in cache, may or may not exist
*/
public static File getFileOrCache(String fileLocation) {
File result = getExistingFileOrCache(fileLocation);
public static MFile getFileOrCache(String fileLocation) {
MFile result = getExistingFileOrCache(fileLocation);
if (result != null)
return result;
return getDiskCache2().getFile(fileLocation);
return MFiles.create(getDiskCache2().getFile(fileLocation).getPath());
}

/**
Expand All @@ -48,14 +50,14 @@ public static File getFileOrCache(String fileLocation) {
* @param fileLocation full path of original index filename
* @return existing file if you can find it, else null
*/
public static File getExistingFileOrCache(String fileLocation) {
public static MFile getExistingFileOrCache(String fileLocation) {
File result = getDiskCache2().getExistingFileOrCache(fileLocation);
if (result == null && Grib.debugGbxIndexOnly && fileLocation.endsWith(".gbx9.ncx4")) { // might create only from
// gbx9 for debugging
int length = fileLocation.length();
String maybeIndexAlreadyExists = fileLocation.substring(0, length - 10) + ".ncx4";
result = getDiskCache2().getExistingFileOrCache(maybeIndexAlreadyExists);
}
return result;
return result == null ? null : MFiles.create(result.getPath());
}
}
33 changes: 17 additions & 16 deletions grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

package ucar.nc2.grib.collection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import javax.annotation.Nullable;
import thredds.filesystem.MFileOS;
import thredds.inventory.MFile;
import ucar.nc2.util.IO;
import ucar.unidata.io.RandomAccessFile;
import ucar.unidata.util.StringUtil2;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
Expand All @@ -28,7 +25,7 @@
*/
public class GcMFile implements thredds.inventory.MFile {

static List<GcMFile> makeFiles(File directory, List<MFile> files, Set<Integer> allFileSet) {
static List<GcMFile> makeFiles(MFile directory, List<MFile> files, Set<Integer> allFileSet) {
List<GcMFile> result = new ArrayList<>(files.size());
String dirPath = StringUtil2.replace(directory.getPath(), '\\', "/");

Expand All @@ -47,12 +44,12 @@ static List<GcMFile> makeFiles(File directory, List<MFile> files, Set<Integer> a
}

////////////////////////////////////////////////////////////////////////////////////////
public final File directory;
public final MFile directory;
public final String name;
public final long lastModified, length;
public final int index;

GcMFile(File directory, String name, long lastModified, long length, int index) {
GcMFile(MFile directory, String name, long lastModified, long length, int index) {
this.directory = directory;
this.name = name;
this.lastModified = lastModified;
Expand All @@ -77,8 +74,8 @@ public boolean isDirectory() {

@Override
public String getPath() {
String path = new File(directory, name).getPath();
return StringUtil2.replace(path, '\\', "/");
MFile file = directory.getChild(name);
return file == null ? null : file.getPath();
}

@Override
Expand All @@ -87,8 +84,8 @@ public String getName() {
}

@Override
public MFile getParent() {
return new MFileOS(directory);
public MFile getParent() throws IOException {
return directory;
}

@Override
Expand All @@ -105,24 +102,28 @@ public Object getAuxInfo() {
@Override
public void setAuxInfo(Object info) {}

public File getDirectory() {
public MFile getDirectory() {
return directory;
}

@Override
public String toString() {
return "GcMFile{" + "directory=" + directory + ", name='" + name + '\'' + ", lastModified=" + lastModified
return "GcMFile{" + "directory=" + directory.getPath() + ", name='" + name + '\'' + ", lastModified=" + lastModified
+ ", length=" + length + ", index=" + index + '}';
}

@Override
public boolean exists() {
return new File(directory, name).exists();
MFile file = directory.getChild(name);
return file != null && file.exists();
}

@Override
public FileInputStream getInputStream() throws FileNotFoundException {
return new FileInputStream(new File(directory, name));
public java.io.InputStream getInputStream() throws FileNotFoundException {
MFile file = directory.getChild(name);
if (file == null)
throw new FileNotFoundException(name);
return file.getInputStream();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

Expand Down Expand Up @@ -28,7 +28,6 @@
import ucar.nc2.time.CalendarDateRange;
import ucar.nc2.time.CalendarPeriod;
import ucar.nc2.util.CloseableIterator;
import java.io.File;
import java.io.IOException;
import java.util.*;

Expand Down Expand Up @@ -191,7 +190,7 @@ protected boolean writeIndex(String name, String indexFilepath, CoordinateRuntim
List<Grib1CollectionWriter.Group> groups2 = new ArrayList<>();
for (Object g : groups)
groups2.add((Grib1CollectionWriter.Group) g); // why copy ?
File indexFileInCache = GribIndexCache.getFileOrCache(indexFilepath);
MFile indexFileInCache = GribIndexCache.getFileOrCache(indexFilepath);
return writer.writeIndex(name, indexFileInCache, masterRuntime, groups2, files, type, dateRange);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

Expand All @@ -9,6 +9,7 @@
import java.nio.charset.StandardCharsets;
import thredds.inventory.MCollection;
import thredds.inventory.MFile;
import thredds.inventory.MFiles;
import ucar.nc2.grib.coord.Coordinate;
import ucar.nc2.grib.coord.CoordinateEns;
import ucar.nc2.grib.coord.CoordinateRuntime;
Expand Down Expand Up @@ -90,15 +91,18 @@ public Set<Long> getCoordinateRuntimes() {
*/

// indexFile is in the cache
boolean writeIndex(String name, File idxFile, CoordinateRuntime masterRuntime, List<Group> groups, List<MFile> files,
boolean writeIndex(String name, MFile idxFile, CoordinateRuntime masterRuntime, List<Group> groups, List<MFile> files,
GribCollectionImmutable.Type type, CalendarDateRange dateRange) throws IOException {
Grib1Record first = null; // take global metadata from here
boolean deleteOnClose = false;

if (idxFile.exists()) {
RandomAccessFile.eject(idxFile.getPath());
if (!idxFile.delete()) {
logger.warn(" gc1 cant delete index file {}", idxFile.getPath());
if (idxFile instanceof thredds.filesystem.MFileOS) {
File f = new File(idxFile.getPath());
if (!f.delete()) {
logger.warn(" gc1 cant delete index file {}", idxFile.getPath());
}
}
}
logger.debug(" createIndex for {}", idxFile.getPath());
Expand Down Expand Up @@ -186,7 +190,7 @@ boolean writeIndex(String name, File idxFile, CoordinateRuntime masterRuntime, L
indexBuilder.setVersion(currentVersion);

// directory and mfile list
File directory = new File(dcm.getRoot());
MFile directory = MFiles.create(dcm.getRoot());
List<GcMFile> gcmfiles = GcMFile.makeFiles(directory, files, allFileSet);
for (GcMFile gcmfile : gcmfiles) {
GribCollectionProto.MFile.Builder b = GribCollectionProto.MFile.newBuilder();
Expand Down Expand Up @@ -230,8 +234,10 @@ boolean writeIndex(String name, File idxFile, CoordinateRuntime masterRuntime, L
} finally {

// remove it on failure
if (deleteOnClose && !idxFile.delete()) {
logger.error(" gc1 cant deleteOnClose index file {}", idxFile.getPath());
if (deleteOnClose && idxFile instanceof thredds.filesystem.MFileOS) {
File f = new File(idxFile.getPath());
if (!f.delete())
logger.error(" gc1 cant deleteOnClose index file {}", idxFile.getPath());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* Copyright (c) 1998-2026 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/

package ucar.nc2.grib.collection;

import thredds.featurecollection.FeatureCollectionConfig;
import thredds.inventory.MFile;
import thredds.inventory.partition.PartitionManager;
import java.io.File;

/**
* Builds Grib1 PartitionCollections (version 2)
Expand All @@ -18,7 +18,7 @@
class Grib1PartitionBuilder extends GribPartitionBuilder {
public static final String MAGIC_START = "Grib1Partition2Index"; // was Grib1Partition0Index

Grib1PartitionBuilder(String name, File directory, PartitionManager tpc, org.slf4j.Logger logger) {
Grib1PartitionBuilder(String name, MFile directory, PartitionManager tpc, org.slf4j.Logger logger) {
super(name, tpc, logger);

FeatureCollectionConfig config = null;
Expand Down
Loading