Skip to content

Commit

Permalink
Make NcML reading work with S3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tara Drwenski committed Dec 22, 2023
1 parent d1210b0 commit b4e3cc5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cdm/core/src/main/java/thredds/filesystem/MFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public MFileOS(java.io.File file) {
}

public MFileOS(String filename) {
this.file = new File(filename);
this.file = new File(filename.replaceFirst("^file:", ""));
this.lastModified = file.lastModified();
}

Expand Down
21 changes: 12 additions & 9 deletions cdm/core/src/main/java/ucar/nc2/dataset/DatasetUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import com.google.common.collect.Multimap;
import thredds.client.catalog.ServiceType;
import thredds.inventory.MFile;
import thredds.inventory.MFiles;
import ucar.httpservices.HTTPFactory;
import ucar.httpservices.HTTPMethod;
import ucar.nc2.util.EscapeStrings;
Expand Down Expand Up @@ -59,9 +61,11 @@ public static List<String> getProtocols(String url) {
StringBuilder buf = new StringBuilder(url);
// If there are any leading protocols, then they must stop at the first '/'.
int slashpos = buf.indexOf("/");
// Check special case of file:<path> with no slashes after file:
// Check special cases of file:<path> or cdms3:<path> with no slashes after:
if (url.startsWith("file:") && "/\\".indexOf(url.charAt(5)) < 0) {
allprotocols.add("file");
} else if (url.startsWith("cdms3:") && "/\\".indexOf(url.charAt(6)) < 0) {
allprotocols.add("cdms3");
} else if (slashpos >= 0) {
// Remove everything after the first slash
buf.delete(slashpos + 1, buf.length());
Expand Down Expand Up @@ -128,7 +132,7 @@ public static DatasetUrl findDatasetUrl(String orgLocation) throws IOException {
}
pos = location.lastIndexOf('?');
String query = null;
if (pos >= 0) {
if (pos >= 0 && !leadProtocol.equals("cdms3")) {
query = trueUrl.substring(pos + 1);
trueUrl = trueUrl.substring(0, pos);
}
Expand All @@ -147,9 +151,9 @@ public static DatasetUrl findDatasetUrl(String orgLocation) throws IOException {
// - we have file://<path> or file:<path>; we need to see if
// the extension can help, otherwise, start defaulting.
// - we have a simple url: e.g. http://... ; contact the server
if (leadProtocol.equals("file")) {
if (leadProtocol.equals("file") || leadProtocol.equals("cdms3")) {
serviceType = decodePathExtension(trueUrl); // look at the path extension
if (serviceType == null && checkIfNcml(new File(location))) {
if (serviceType == null && checkIfNcml(MFiles.create(location))) {
serviceType = ServiceType.NCML;
}
} else {
Expand All @@ -163,11 +167,10 @@ public static DatasetUrl findDatasetUrl(String orgLocation) throws IOException {
}
}
}

if (serviceType == ServiceType.NCML) { // ??
// If lead protocol was null, then pretend it was a file
// Note that technically, this should be 'file://'
trueUrl = (allProtocols.isEmpty() ? "file:" + trueUrl : location);
trueUrl = (allProtocols.isEmpty() ? "file:" + trueUrl : trueUrl);
}

// Add back the query and fragment (if any)
Expand Down Expand Up @@ -511,12 +514,12 @@ private static boolean checkIfRemoteNcml(String location) throws IOException {
return false;
}

private static boolean checkIfNcml(File file) throws IOException {
if (!file.exists()) {
private static boolean checkIfNcml(MFile mFile) throws IOException {
if (!mFile.exists()) {
return false;
}

try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file), NUM_BYTES_TO_DETERMINE_NCML)) {
try (BufferedInputStream in = new BufferedInputStream(mFile.getInputStream(), NUM_BYTES_TO_DETERMINE_NCML)) {
byte[] bytes = new byte[NUM_BYTES_TO_DETERMINE_NCML];
int bytesRead = in.read(bytes);

Expand Down
16 changes: 6 additions & 10 deletions cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import java.util.Optional;
import java.util.Set;
import java.util.StringTokenizer;
import java.net.URL;
import javax.annotation.Nullable;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import thredds.inventory.MFile;
import thredds.inventory.MFiles;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.nc2.Attribute;
Expand Down Expand Up @@ -346,26 +347,21 @@ public static NetcdfDataset.Builder readNcml(Reader r, String ncmlLocation, Canc
*/
public static NetcdfDataset.Builder readNcml(String ncmlLocation, String referencedDatasetUri, CancelTask cancelTask)
throws IOException {
URL url = new URL(ncmlLocation);
MFile mFile = MFiles.create(ncmlLocation);

if (debugURL) {
System.out.println(" NcmlReader open " + ncmlLocation);
System.out.println(" URL = " + url);
System.out.println(" external form = " + url.toExternalForm());
System.out.println(" protocol = " + url.getProtocol());
System.out.println(" host = " + url.getHost());
System.out.println(" path = " + url.getPath());
System.out.println(" file = " + url.getFile());
System.out.println(" Path = " + mFile.getPath());
}

org.jdom2.Document doc;
try {
SAXBuilder builder = new SAXBuilder();
builder.setExpandEntities(false);
if (debugURL) {
System.out.println(" NetcdfDataset URL = <" + url + ">");
System.out.println(" NetcdfDataset path = <" + mFile.getPath() + ">");
}
doc = builder.build(url);
doc = builder.build(mFile.getInputStream());
} catch (JDOMException e) {
throw new IOException(e.getMessage());
}
Expand Down
31 changes: 27 additions & 4 deletions cdm/core/src/main/java/ucar/nc2/util/URLnaming.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/
package ucar.nc2.util;

import java.io.IOException;
import thredds.inventory.MFile;
import thredds.inventory.MFiles;
import ucar.unidata.util.StringUtil2;
import java.io.File;
import java.net.URI;
Expand Down Expand Up @@ -86,6 +89,20 @@ public static String resolve(String baseUri, String relativeUri) {
}
}

if (baseUri.startsWith("cdms3:")) {
if (relativeUri.startsWith("cdms3:")) {
return relativeUri;
} else {
MFile absoluteMFile;
try {
absoluteMFile = MFiles.create(baseUri).getParent().getChild(relativeUri);
} catch (IOException e) {
return relativeUri;
}
return absoluteMFile == null ? relativeUri : absoluteMFile.getPath();
}
}

// non-file URLs

// relativeUri = canonicalizeRead(relativeUri);
Expand Down Expand Up @@ -116,12 +133,18 @@ public static String resolveFile(String baseDir, String filepath) {
if (baseDir.startsWith("file:"))
baseDir = baseDir.substring(5);

File base = new File(baseDir);
if (!base.isDirectory())
base = base.getParentFile();
MFile base = MFiles.create(baseDir);
if (!base.isDirectory()) {
try {
base = base.getParent();
} catch (IOException e) {
return filepath;
}
}
if (base == null)
return filepath;
return base.getAbsolutePath() + "/" + filepath;
MFile absoluteFile = base.getChild(filepath);
return absoluteFile == null ? filepath : absoluteFile.getPath();
}

}

0 comments on commit b4e3cc5

Please sign in to comment.