All Downloads are FREE. Search and download functionalities are using the official Maven repository.

thredds.util.UnidataTdsDataPathRemapper Maven / Gradle / Ivy

Go to download

The NetCDF-Java Library is a Java interface to NetCDF files, as well as to many other types of scientific data formats.

The newest version!
package thredds.util;

import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Remap the dataset UrlPath's from TDS 4.2 to
 * TDS 4.3 for the Unidata TDS server. This is only
 * valid for the initial move from 4.3 to 4.3.
 *
 * This code is heavily based on
 * ucar.nc2.grib.GribVariableRenamer.
 *
 * @author arms
 * @since 12/21/2012
 */
public class UnidataTdsDataPathRemapper {

    private static InputStream getInputStream(String resourceName, Class originClass) {

        InputStream s;
        while (originClass != null) {
            s = originClass.getResourceAsStream(resourceName);
            if (s != null) {
                break;
            }
            originClass = originClass.getSuperclass();
        }

        // Try class loader to get resource
        ClassLoader cl = UnidataTdsDataPathRemapper.class.getClassLoader();
        s = cl.getResourceAsStream(resourceName);
        if (s != null) {
            return s;
        }

        //Try the file system
        File f = new File(resourceName);
        if (f.exists()) {
            try {
                s = new FileInputStream(f);
            } catch (Exception e) {
            }
        }
        if (s != null) {
            return s;
        }

        return s;
    }

    private static HashMap map;

    private void initMap() {
        List beans = readUrlRemapFile("resources/thredds/4p2to4p3Remap.xml");
        map = makeMapBeans(beans);
    }

    /**
     *
     * Get the new datasetUrlPath given the old dataset UrlPath
     *
     * @param oldUrlPath Old dataset UrlPath
     * @return result List containing possible remaps
     */
    public List getMappedUrlPaths(String oldUrlPath) {
        List result = getMappedUrlPaths(oldUrlPath,null);
        return result;
    }

    /**
     *
     * Get the new datasetUrlPath given the old dataset UrlPath
     * and the dataset "type" (a "file" or "best time series")
     *
     * A "file" urlPath looks something like:
     *   fmrc/NCEP/GFS/Alaska_191km/files/
     *
     * while a "best" urlPath looks like:
     *   fmrc/NCEP/GFS/Alaska_191km/NCEP-GFS-Alaska_191km_best.ncd
     *
     * @param oldUrlPath Old dataset UrlPath
     * @param urlType  either file (which includes latest) or best (for best time series)
     * @return result List containing possible remaps
     */
    public List getMappedUrlPaths(String oldUrlPath,String urlType) {
        List result = new ArrayList();
        // look in our Url Remapper map
        if (map == null) {
            initMap();
        }

        Remapper mbean = map.get(oldUrlPath);
        if (mbean != null && mbean.newUrls != null)  {
            result.add(mbean.newUrl); // if its unique, then we are done
            return result;
        }

        // not unique - match against urlType (best, latest, or files)
        if (urlType != null) {
            if (mbean != null) {
                for (UrlRemapperBean r : mbean.newUrls) {
                    if (r.getUrlType().equals(urlType)) result.add(r.newUrlPath);
                }
            }
        }
        return result;
    }

    private List readUrlRemapFile(String path) {
        java.util.List beans = new ArrayList(1000);
        InputStream is = null;

        try {
            is = getInputStream(path, null);

            if (is == null) {
                System.out.println("Cant read file " + path);
                return null;
            }

            SAXBuilder builder = new SAXBuilder();
            org.jdom2.Document doc = builder.build(is);
            org.jdom2.Element root = doc.getRootElement();
            List dsElems = root.getChildren("urlMap");
            for (org.jdom2.Element dsElem : dsElems) {
                String dsType = dsElem.getAttributeValue("type");
                List params = dsElem.getChildren("urlPath");
                for (org.jdom2.Element elem : params) {
                    String oldUrlPath = elem.getAttributeValue("oldUrlPath");
                    String newUrlPath = elem.getAttributeValue("newUrlPath");
                    beans.add(new UrlRemapperBean(dsType,oldUrlPath,newUrlPath));
                }
            }
            return beans;

        } catch (IOException ioe) {
            ioe.printStackTrace();
            return null;

        } catch (JDOMException e) {
            e.printStackTrace();
            return null;

        } finally {
            if (is != null) try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    private String getNewName(HashMap map, String urlType, String oldUrlPath) {
        Remapper mbean = map.get(oldUrlPath);
        if (mbean == null) return null; // ??
        if (mbean.newUrl != null) return mbean.newUrl; // if its unique, then we are done

        for (UrlRemapperBean r : mbean.newUrls) {
            if (r.getUrlType().equals(urlType)) return r.getNewUrlPath();
        }

        return null;
    }

    public static class UrlRemapperBean implements Comparable {
        String urlType, oldUrlPath, newUrlPath;

        // no-arg constructor
        public UrlRemapperBean() {
        }

        public UrlRemapperBean(String dsType, String oldUrlPath, String newUrlPath) {
            this.urlType = dsType;
            this.oldUrlPath = oldUrlPath;
            this.newUrlPath = newUrlPath;
        }

        public String getUrlType() {
            return urlType;
        }

        public String getOldUrlPath() {
            return oldUrlPath;
        }

        public String getNewUrlPath() {
            return newUrlPath;
        }

        @Override
        public int compareTo(UrlRemapperBean o) {
            return newUrlPath.compareTo(o.getNewUrlPath());
        }
    }

    private HashMap makeMapBeans(List vbeans) {
        HashMap map = new HashMap(200);
        for (UrlRemapperBean vbean : vbeans) {

            // construct the old -> new mapping
            Remapper mbean = map.get(vbean.getOldUrlPath());
            if (mbean == null) {
                mbean = new Remapper(vbean.getOldUrlPath());
                map.put(vbean.getOldUrlPath(), mbean);
            }
            mbean.add(vbean);
        }

        for (Remapper rmap : map.values()) {
            rmap.finish();
        }

        return map;
    }

    private class Remapper {
        String oldUrl, newUrl; // newName exists when theres only one
        List newUrls = new ArrayList();
        HashMap newUrlMap = new HashMap();

        // no-arg constructor
        public Remapper() {
        }

        public Remapper(String oldUrl) {
            this.oldUrl = oldUrl;
        }

        void add(UrlRemapperBean vbean) {
            newUrlMap.put(vbean.getNewUrlPath(), vbean);
            newUrls.add(vbean);
        }

        void finish() {
            if (newUrlMap.values().size() == 1) {
                newUrl = newUrls.get(0).getNewUrlPath();
            }
        }

        public int getCount() {
            return newUrls.size();
        }

        public String getOldUrl() {
            return oldUrl;
        }
    }

    public static void main(String[] args) throws IOException {
        UnidataTdsDataPathRemapper u = new UnidataTdsDataPathRemapper();

        List result1 = u.getMappedUrlPaths("fmrc/NCEP/GFS/Alaska_191km/files/");

        assert result1.size() == 1;
        assert result1.get(0) == "grib/NCEP/GFS/Alaska_191km/files/";

        System.out.println(result1.toString());

        List result2 = u.getMappedUrlPaths("fmrc/NCEP/GFS/Alaska_191km/files/", "files");
        assert result1 == result2;

        System.out.println(result2.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy