List<String> allprotocols = DatasetUrl.getProtocols(location); svctype = searchFragment(fragment); svctype = decodeLeadProtocol(leadprotocol); svctype = searchPath(trueurl); svctype = decodePathExtension(trueurl); // look at the path extension if(svctype == null && checkIfNcml(new File(location))) { svctype = ServiceType.NCML; svctype = disambiguateHttp(trueurl); if (checkIfRemoteNcml(trueurl)) { svctype = ServiceType.NCML; return new DatasetUrl(svctype, trueurl);
/** * Factory method for opening a NetcdfFile through the netCDF API. * * @param location location of dataset. * @param cancelTask use to allow task to be cancelled; may be null. * @return NetcdfFile object * @throws java.io.IOException on read error */ public static NetcdfFile openFile(String location, ucar.nc2.util.CancelTask cancelTask) throws IOException { DatasetUrl durl = DatasetUrl.findDatasetUrl(location); return openOrAcquireFile(null, null, null, durl, -1, cancelTask, null); }
ServiceType result = checkIfCdmr(location); if (result != null) return result; checkCdmr = true; ServiceType result = checkIfDods(location); if (result != null) return result; checkDap2 = true; ServiceType result = checkIfDap4(location); if (result != null) return result; checkDap4 = true; ServiceType result = checkIfDods(location); if (result != null) return result; ServiceType result = checkIfDap4(location); if (result != null) return result; ServiceType result = checkIfCdmr(location); if (result != null) return result;
static private boolean checkIfRemoteNcml(String location) throws IOException { if (decodePathExtension(location)==ServiceType.NCML) { // just because location ends with ncml does not mean it's ncml // if the ncml file is being served up via http by a remote server, // we should be able to read the first bit of it and see if it even // looks like an ncml file. try (HTTPMethod method = HTTPFactory.Get(location)) { method.setRange(0, NUM_BYTES_TO_DETERMINE_NCML); method.setRequestHeader("accept-encoding", "identity"); int statusCode = method.execute(); if (statusCode >= 300) { if (statusCode == 401) { throw new IOException("Unauthorized to open dataset " + location); } else if (statusCode == 406) { String msg = location + " - this server does not support returning content without any encoding."; msg = msg + " Please download the file locally. Return status=" + statusCode; throw new IOException(msg); } else { throw new IOException(location + " is not a valid URL, return status=" + statusCode); } } return checkIfNcml(method.getResponseAsString()); } } return false; }
public TestDatasetWrapP(String filename) { durl = new DatasetUrl(null, filename); }
protected void protocheck(String path, String expected) { if (expected == null) expected = ""; List<String> protocols = DatasetUrl.getProtocols(path); StringBuilder buf = new StringBuilder(); for (String s : protocols) { buf.append(s); buf.append(":"); } String result = buf.toString(); boolean ok = expected.equals(result); if (show || !ok) System.out.printf(" path=%s; result=%s; pass=%s\n", path, result, ok); Assert.assertEquals(path, expected, result); }
static private boolean checkIfNcml(File file) throws IOException { if (!file.exists()) { return false; } try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file), NUM_BYTES_TO_DETERMINE_NCML)) { byte[] bytes = new byte[NUM_BYTES_TO_DETERMINE_NCML]; int bytesRead = in.read(bytes); if (bytesRead <= 0) { return false; } else { return checkIfNcml(new String(bytes, 0, bytesRead)); } } }
public GribCollectionImmutable getGribCollection() throws IOException { String path = getIndexFilenameInCache(); if (path == null) { if (Grib.debugIndexOnly) { // we are running in debug mode where we only have the indices, not the data files // tricky: substitute the current root File orgParentDir = new File(directory); File currentFile = new File(PartitionCollectionMutable.this.indexFilename); File currentParent = currentFile.getParentFile(); File currentParentWithDir = new File(currentParent, orgParentDir.getName()); File nestedIndex = isPartitionOfPartitions ? new File(currentParentWithDir, filename) : new File(currentParent, filename); // JMJ path = nestedIndex.getPath(); } else { throw new FileNotFoundException("No index filename for partition= "+this.toString()); } } // LOOK not cached return (GribCollectionImmutable) PartitionCollectionImmutable.partitionCollectionFactory.open(new DatasetUrl(null, path), -1, null, this); }
protected boolean protocheck(String path, String expected) { if(expected == null) expected = ""; List<String> protocols = DatasetUrl.getProtocols(path); StringBuilder buf = new StringBuilder(); for(String s : protocols) { buf.append(s); buf.append(":"); } String result = buf.toString(); boolean ok = expected.equals(result); System.err.printf("path=|%s| result=|%s| pass=%s\n", path, result, (ok ? "true" : "false")); System.err.flush(); return ok; }
public TestNcmlWriteAndCompareShared(String location, boolean compareData) throws IOException { this.durl = DatasetUrl.findDatasetUrl(location); this.compareData = compareData; }
static public GribCollectionImmutable acquireGribCollection(FileFactory factory, Object hashKey, String location, int buffer_size, CancelTask cancelTask, Object spiObject) throws IOException { FileCacheable result; DatasetUrl durl = new DatasetUrl(null, location); if (gribCollectionCache != null) { // FileFactory factory, Object hashKey, String location, int buffer_size, CancelTask cancelTask, Object spiObject result = GribCdmIndex.gribCollectionCache.acquire(factory, hashKey, durl, buffer_size, cancelTask, spiObject); } else { // String location, int buffer_size, ucar.nc2.util.CancelTask cancelTask, Object iospMessage result = factory.open(durl, buffer_size, cancelTask, spiObject); } return (GribCollectionImmutable) result; }
/** * Factory method for opening a dataset through the netCDF API, and identifying its coordinate variables. * * @param location location of file * @param enhance if true, use defaultEnhanceMode, else no enhancements * @param buffer_size RandomAccessFile buffer size, if <= 0, use default size * @param cancelTask allow task to be cancelled; may be null. * @param spiObject sent to iosp.setSpecial() if not null * @return NetcdfDataset object * @throws java.io.IOException on read error */ static public NetcdfDataset openDataset(String location, boolean enhance, int buffer_size, ucar.nc2.util.CancelTask cancelTask, Object spiObject) throws IOException { DatasetUrl durl = DatasetUrl.findDatasetUrl(location); return openDataset(durl, enhance ? defaultEnhanceMode : null, buffer_size, cancelTask, spiObject); }
static public RandomAccessFile acquire(String location) throws IOException { if (cache == null) return new RandomAccessFile(location, "r"); else return (RandomAccessFile) cache.acquire(factory, new DatasetUrl(null, location)); }
/** * Open a netcdf dataset, using NetcdfDataset.defaultEnhanceMode plus CoordSystems * and turn into a DtCoverageDataset. * * @param location netcdf dataset to open, using NetcdfDataset.acquireDataset(). * @return GridDataset * @throws java.io.IOException on read error * @see ucar.nc2.dataset.NetcdfDataset#acquireDataset */ static public DtCoverageDataset open(String location) throws java.io.IOException { DatasetUrl durl = DatasetUrl.findDatasetUrl(location); return open(durl, NetcdfDataset.getDefaultEnhanceMode()); }
static public RandomAccessFile acquire(String location, int buffer_size) throws IOException { if (cache == null) return new RandomAccessFile(location, "r", buffer_size); else return (RandomAccessFile) cache.acquire(factory, location, new DatasetUrl(null, location), buffer_size, null, null); }
/** * Open a netcdf dataset, using NetcdfDataset.defaultEnhanceMode plus CoordSystems * and turn into a GridDataset. * * @param location netcdf dataset to open, using NetcdfDataset.acquireDataset(). * @param enhanceMode open netcdf dataset with this enhanceMode * @return GridDataset * @throws java.io.IOException on read error * @see ucar.nc2.dataset.NetcdfDataset#acquireDataset */ static public GridDataset open(String location, Set<NetcdfDataset.Enhance> enhanceMode) throws java.io.IOException { NetcdfDataset ds = ucar.nc2.dataset.NetcdfDataset.acquireDataset(null, DatasetUrl.findDatasetUrl(location), enhanceMode, -1, null, null); return new GridDataset(ds, null); }
/** * Open a file, keep track of open files * @param location open this location * @param openFiles keep track of open files * @return file or null if not found */ private NetcdfDataset open(String location, Map<String, NetcdfDataset> openFiles) throws IOException { NetcdfDataset ncd; if (openFiles != null) { ncd = openFiles.get(location); if (ncd != null) return ncd; } if (config.innerNcml == null) { ncd = NetcdfDataset.acquireDataset(new DatasetUrl(null, location), true, null); // default enhance } else { NetcdfFile nc = NetcdfDataset.acquireFile(new DatasetUrl(null, location), null); ncd = NcMLReader.mergeNcML(nc, config.innerNcml); // create new dataset ncd.enhance(); // now that the ncml is added, enhance "in place", ie modify the NetcdfDataset } if (openFiles != null && ncd != null) { openFiles.put(location, ncd); } return ncd; }
/** * Open a dataset as a TypedDataset. * * @param datatype open this kind of Typed Dataset; may be null, which means search all factories. * If datatype is not null, only return correct TypedDataset (eg PointObsDataset for DataType.POINT). * @param location URL or file location of the dataset * @param task user may cancel * @param errlog place errors here, may not be null * @return a subclass of TypedDataset * @throws java.io.IOException on io error */ static public TypedDataset open( FeatureType datatype, String location, ucar.nc2.util.CancelTask task, StringBuilder errlog) throws IOException { DatasetUrl durl = DatasetUrl.findDatasetUrl(location); NetcdfDataset ncd = NetcdfDataset.acquireDataset(durl, true, task); return open( datatype, ncd, task, errlog); }
static private NetcdfFile acquireDODS(FileCache cache, FileFactory factory, Object hashKey, String location, int buffer_size, ucar.nc2.util.CancelTask cancelTask, Object spiObject) throws IOException { if (cache == null) { return openDodsByReflection(location, cancelTask); } if (factory == null) factory = new DodsFactory(); return (NetcdfFile) cache.acquire(factory, hashKey, new DatasetUrl(ServiceType.OPENDAP, location), buffer_size, cancelTask, spiObject); }
public NetcdfFile acquireFile(CancelTask cancelTask) throws IOException { if (debugOpenFile) System.out.println(" try to acquire " + cacheLocation); long start = System.currentTimeMillis(); if (durl == null) durl = DatasetUrl.findDatasetUrl(cacheLocation); // cache the ServiceType so we dont have to keep figuring it out NetcdfFile ncfile = NetcdfDataset.acquireFile(reader, null, durl, -1, cancelTask, spiObject); // must merge NcML before enhancing if (mergeNcml != null) ncfile = NcMLReader.mergeNcML(ncfile, mergeNcml); // create new dataset if (enhance == null || enhance.isEmpty()) { if (debugOpenFile) System.out.println(" acquire (no enhance) " + cacheLocation + " took " + (System.currentTimeMillis() - start)); return ncfile; } // must enhance NetcdfDataset ds; if (ncfile instanceof NetcdfDataset) { ds = (NetcdfDataset) ncfile; ds.enhance(enhance); // enhance "in place", ie modify the NetcdfDataset } else { ds = new NetcdfDataset(ncfile, enhance); // enhance when wrapping } if (debugOpenFile) System.out.println(" acquire (enhance) " + cacheLocation + " took " + (System.currentTimeMillis() - start)); return ds; }