Example usage for org.jdom2 Document Document

List of usage examples for org.jdom2 Document Document

Introduction

In this page you can find the example usage for org.jdom2 Document Document.

Prototype

public Document(List<? extends Content> content) 

Source Link

Document

This will create a new Document, with the supplied list of content, and a DocType declaration only if the content contains a DocType instance.

Usage

From source file:edu.wisc.ssec.mcidasv.data.hydra.SuomiNPPDataSource.java

License:Open Source License

public void setup() throws VisADException {

    // which format, NASA or NOAA?
    isNOAA = false;//from w ww  .  j a  v  a  2  s. co  m

    // store filenames for possible bundle unpersistence
    for (Object o : sources) {
        oldSources.add((String) o);
    }

    // time zone for product labels
    SimpleTimeZone stz = new SimpleTimeZone(0, "GMT");
    sdf.setTimeZone(stz);
    sdfOut.setTimeZone(stz);

    // looking to populate 3 things - path to lat, path to lon, path to relevant products
    String pathToLat = null;
    String pathToLon = null;
    Set<String> pathToProducts = new LinkedHashSet<>();
    Map<String, String> prodToDesc = new HashMap<>();

    // flag to differentiate VIIRS from one of the other Suomi sensors
    boolean isVIIRS = true;

    // check source filenames to see if this is a combined product. everything
    // from last file separator to first underscore should be product info
    int lastSeparator = filename.lastIndexOf(File.separatorChar);
    int firstUnderscore = filename.indexOf("_", lastSeparator + 1);
    String prodStr = filename.substring(lastSeparator + 1, firstUnderscore);
    // only do this check for NOAA data
    if (filename.endsWith(".h5")) {
        isNOAA = true;
        StringTokenizer st = new StringTokenizer(prodStr, "-");
        logger.debug("SNPPDS check for embedded GEO, tokenizing: " + prodStr);
        while (st.hasMoreTokens()) {
            String singleProd = st.nextToken();
            for (int i = 0; i < JPSSUtilities.geoProductIDs.length; i++) {
                if (singleProd.equals(JPSSUtilities.geoProductIDs[i])) {
                    logger.debug("Setting isCombinedProduct true, Found embedded GEO: " + singleProd);
                    isCombinedProduct = true;
                    break;
                }
            }
        }
    }

    // various metatdata we'll need to gather on a per-product basis
    Map<String, String> unsignedFlags = new LinkedHashMap<>();
    Map<String, String> unpackFlags = new LinkedHashMap<>();

    // geo product IDs for each granule
    Set<String> geoProductIDs = new LinkedHashSet<>();

    // aggregations will use sets of NetCDFFile readers
    List<NetCDFFile> ncdfal = new ArrayList<>();

    // we should be able to find an XML Product Profile for each data/product type
    SuomiNPPProductProfile nppPP = null;
    // and also Profile metadata for geolocation variables
    boolean haveGeoMetaData = false;

    // number of source granules which make up the data source
    int granuleCount = 1;

    try {

        nppPP = new SuomiNPPProductProfile();

        // for each source file provided, find the appropriate geolocation,
        // get the nominal time and various other granule-level metadata
        Iterator keyIterator = filenameMap.keySet().iterator();
        while (keyIterator.hasNext()) {
            String keyStr = (String) keyIterator.next();
            List fileNames = (List) filenameMap.get(keyStr);
            granuleCount = fileNames.size();
            setProperty(Constants.PROP_GRANULE_COUNT, granuleCount + " Granule");
            for (int fileCount = 0; fileCount < granuleCount; fileCount++) {
                // need to open the main NetCDF file to determine the geolocation product
                NetcdfFile ncfile = null;
                String fileAbsPath = null;
                try {
                    fileAbsPath = (String) fileNames.get(fileCount);
                    logger.debug("Trying to open file: " + fileAbsPath);
                    ncfile = NetcdfFile.open(fileAbsPath);
                    if (!isCombinedProduct) {
                        if (isNOAA) {
                            Attribute a = ncfile.findGlobalAttribute("N_GEO_Ref");
                            logger.debug("Value of GEO global attribute: " + a.getStringValue());
                            String tmpGeoProductID = a.getStringValue();
                            geoProductIDs.add(tmpGeoProductID);
                        } else {
                            geoProductIDs.add(keyStr.replace("L1B", "GEO"));
                        }
                    }
                    Group rg = ncfile.getRootGroup();

                    List<Group> gl = rg.getGroups();
                    if (gl != null) {
                        for (Group g : gl) {
                            logger.trace("Group name: " + g.getFullName());
                            if (isNOAA) {
                                // when we find the Data_Products group, go down another group level and pull out 
                                // what we will use for nominal day and time (for now anyway).
                                // XXX TJJ fileCount check is so we don't count the GEO file in time array!
                                if (g.getFullName().contains("Data_Products")
                                        && (fileCount != fileNames.size())) {
                                    List<Group> dpg = g.getGroups();

                                    // cycle through once looking for XML Product Profiles
                                    for (Group subG : dpg) {

                                        String subName = subG.getFullName();
                                        // use actual product, not geolocation, to id XML Product Profile
                                        if (!subName.contains("-GEO")) {
                                            // determine the instrument name (VIIRS, ATMS, CrIS, OMPS)
                                            instrumentName = subG.findAttribute("Instrument_Short_Name");

                                            // note any EDR products, will need to check for and remove
                                            // fill scans later
                                            Attribute adtt = subG.findAttribute("N_Dataset_Type_Tag");
                                            if (adtt != null) {
                                                String baseName = adtt.getStringValue();
                                                if ((baseName != null) && (baseName.equals("EDR"))) {
                                                    // have to loop through sub groups variables to determine band
                                                    List<Variable> tmpVar = subG.getVariables();
                                                    for (Variable v : tmpVar) {
                                                        // if Imagery EDR attribute for band is specified, save it
                                                        Attribute mBand = v.findAttribute("Band_ID");
                                                        if (mBand != null) {
                                                            whichEDR = mBand.getStringValue();
                                                        }
                                                    }
                                                }
                                            }

                                            // This is also where we find the attribute which tells us which
                                            // XML Product Profile to use!
                                            Attribute axpp = subG.findAttribute("N_Collection_Short_Name");
                                            if (axpp != null) {
                                                String baseName = axpp.getStringValue();
                                                productName = baseName;

                                                // TJJ Apr 2018
                                                // Hack so we can look at CrIS Full Spectrum, until we can
                                                // track down existence of an official Product Profile for it.
                                                // http://mcidas.ssec.wisc.edu/inquiry-v/?inquiry=2634
                                                // The regular SDR profile lets us visualize it.
                                                if (productName.equals("CrIS-FS-SDR"))
                                                    productName = "CrIS-SDR";

                                                String productProfileFileName = nppPP
                                                        .getProfileFileName(productName);
                                                logger.trace("Found profile: " + productProfileFileName);
                                                if (productProfileFileName == null) {
                                                    throw new Exception(
                                                            "XML Product Profile not found in catalog");
                                                }
                                                try {
                                                    nppPP.addMetaDataFromFile(productProfileFileName);
                                                } catch (Exception nppppe) {
                                                    logger.error("Error parsing XML Product Profile: "
                                                            + productProfileFileName);
                                                    throw new Exception("XML Product Profile Error", nppppe);
                                                }
                                            }
                                        }
                                    }

                                    // 2nd pass through sub-group to extract date/time for aggregation
                                    for (Group subG : dpg) {
                                        List<Variable> vl = subG.getVariables();
                                        for (Variable v : vl) {
                                            Attribute aDate = v.findAttribute("AggregateBeginningDate");
                                            Attribute aTime = v.findAttribute("AggregateBeginningTime");
                                            // did we find the attributes we are looking for?
                                            if ((aDate != null) && (aTime != null)) {
                                                // set time for display to day/time of 1st granule examined
                                                if (!nameHasBeenSet) {
                                                    String sDate = aDate.getStringValue();
                                                    String sTime = aTime.getStringValue();
                                                    logger.debug("For day/time, using: " + sDate
                                                            + sTime.substring(0, sTime.indexOf('Z') - 3));
                                                    Date d = sdf.parse(
                                                            sDate + sTime.substring(0, sTime.indexOf('Z') - 3));
                                                    theDate = d;
                                                    setName(instrumentName.getStringValue() + " "
                                                            + sdfOut.format(d));
                                                    nameHasBeenSet = true;
                                                }
                                                break;
                                            }
                                        }
                                    }
                                    if (!nameHasBeenSet) {
                                        throw new VisADException("No date time found in Suomi NPP granule");
                                    }
                                }
                            } else {
                                // NASA data - date/time from global attribute
                                // set time for display to day/time of 1st granule examined
                                Attribute timeStartNASA = ncfile.findGlobalAttribute("time_coverage_start");
                                Date d = sdfNASA.parse(timeStartNASA.getStringValue());
                                theDate = d;
                                if (!nameHasBeenSet) {
                                    instrumentName = ncfile.findGlobalAttribute("instrument");
                                    setName(instrumentName.getStringValue() + " " + sdfOut.format(d));
                                    nameHasBeenSet = true;
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.warn("Exception during processing of file: " + fileAbsPath);
                    throw (e);
                } finally {
                    ncfile.close();
                }
            }

        }

        // build each union aggregation element
        Iterator<String> iterator = geoProductIDs.iterator();
        for (int elementNum = 0; elementNum < granuleCount; elementNum++) {

            String s = null;

            // build an XML (NCML actually) representation of the union aggregation of these two files
            Namespace ns = Namespace.getNamespace("http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2");
            Element root = new Element("netcdf", ns);
            Document document = new Document(root);

            Element agg = new Element("aggregation", ns);
            agg.setAttribute("type", "union");

            // TJJ - Loop over filename map, could be several products that need to be aggregated
            Set set = filenameMap.keySet();
            Iterator mapIter = set.iterator();
            while (mapIter.hasNext()) {
                String key = (String) mapIter.next();
                List l = (List) filenameMap.get(key);
                Element fData = new Element("netcdf", ns);
                fData.setAttribute("location", (String) l.get(elementNum));
                agg.addContent(fData);
                s = (String) l.get(elementNum);
            }

            String geoFilename = null;
            Element fGeo = new Element("netcdf", ns);
            ;

            if (!isCombinedProduct) {

                if (isNOAA) {
                    geoFilename = s.substring(0, s.lastIndexOf(File.separatorChar) + 1);
                    // check if we have the whole file name or just the prefix
                    String geoProductID = iterator.next();
                    if (geoProductID.endsWith("h5")) {
                        geoFilename += geoProductID;
                    } else {
                        geoFilename += geoProductID;
                        geoFilename += s.substring(s.lastIndexOf(File.separatorChar) + 6);
                    }
                    // Be sure file as specified by N_GEO_Ref global attribute really is there.
                    File tmpGeo = new File(geoFilename);
                    if (!tmpGeo.exists()) {
                        // Ok, the expected file defined (supposedly) exactly by a global att is not there...
                        // We need to check for similar geo files with different creation dates
                        String geoFileRelative = geoFilename
                                .substring(geoFilename.lastIndexOf(File.separatorChar) + 1);
                        // also check for Terrain Corrected version of geo
                        String geoTerrainCorrected = geoFileRelative;
                        geoTerrainCorrected = geoTerrainCorrected.replace("OD", "TC");
                        geoTerrainCorrected = geoTerrainCorrected.replace("MG", "TC");

                        // now we make a file filter, and see if a matching geo file is present
                        File fList = new File(
                                geoFilename.substring(0, geoFilename.lastIndexOf(File.separatorChar) + 1)); // current directory

                        FilenameFilter geoFilter = new FilenameFilter() {
                            public boolean accept(File dir, String name) {
                                if (name.matches(JPSSUtilities.SUOMI_GEO_REGEX_NOAA)) {
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        };

                        File[] files = fList.listFiles(geoFilter);
                        for (File file : files) {
                            if (file.isDirectory()) {
                                continue;
                            }
                            // get the file name for convenience
                            String fName = file.getName();
                            // is it one of the standard Ellipsoid geo types we are looking for?
                            if (fName.substring(0, 5).equals(geoFileRelative.substring(0, 5))) {
                                int geoStartIdx = geoFileRelative.indexOf("_d");
                                int prdStartIdx = fName.indexOf("_d");
                                String s1 = geoFileRelative.substring(geoStartIdx,
                                        geoStartIdx + JPSSUtilities.NOAA_CREATION_DATE_INDEX);
                                String s2 = fName.substring(prdStartIdx,
                                        prdStartIdx + JPSSUtilities.NOAA_CREATION_DATE_INDEX);
                                if (s1.equals(s2)) {
                                    geoFilename = s.substring(0, s.lastIndexOf(File.separatorChar) + 1) + fName;
                                    break;
                                }
                            }
                            // same check, but for Terrain Corrected version
                            if (fName.substring(0, 5).equals(geoTerrainCorrected.substring(0, 5))) {
                                int geoStartIdx = geoTerrainCorrected.indexOf("_d");
                                int prdStartIdx = fName.indexOf("_d");
                                String s1 = geoTerrainCorrected.substring(geoStartIdx,
                                        geoStartIdx + JPSSUtilities.NOAA_CREATION_DATE_INDEX);
                                String s2 = fName.substring(prdStartIdx,
                                        prdStartIdx + JPSSUtilities.NOAA_CREATION_DATE_INDEX);
                                if (s1.equals(s2)) {
                                    geoFilename = s.substring(0, s.lastIndexOf(File.separatorChar) + 1) + fName;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    // NASA format
                    geoFilename = JPSSUtilities.replaceLast(s, "L1B", "GEO");
                    // get list of files in current directory
                    File fList = new File(
                            geoFilename.substring(0, geoFilename.lastIndexOf(File.separatorChar) + 1));
                    // make a NASA style file filter, and see if a matching geo file is present
                    FilenameFilter geoFilter = new FilenameFilter() {
                        public boolean accept(File dir, String name) {
                            if (name.matches(JPSSUtilities.SUOMI_GEO_REGEX_NASA)) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    };
                    File[] files = fList.listFiles(geoFilter);
                    for (File file : files) {
                        if (file.isDirectory()) {
                            continue;
                        }
                        // get the file name for convenience
                        String fName = file.getName();
                        String tmpStr = geoFilename.substring(s.lastIndexOf(File.separatorChar) + 1,
                                s.lastIndexOf(File.separatorChar)
                                        + (JPSSUtilities.NASA_CREATION_DATE_INDEX + 1));
                        if (fName.substring(0, JPSSUtilities.NASA_CREATION_DATE_INDEX)
                                .equals(tmpStr.substring(0, JPSSUtilities.NASA_CREATION_DATE_INDEX))) {
                            geoFilename = s.substring(0, s.lastIndexOf(File.separatorChar) + 1) + fName;
                            break;
                        }
                    }
                }
                logger.debug("Determined GEO file name should be: " + geoFilename);
                fGeo.setAttribute("location", geoFilename);
                // add this to list used if we create a zipped bundle
                geoSources.add(geoFilename);
                agg.addContent(fGeo);
            }

            root.addContent(agg);
            XMLOutputter xmlOut = new XMLOutputter();
            String ncmlStr = xmlOut.outputString(document);
            ByteArrayInputStream is = new ByteArrayInputStream(ncmlStr.getBytes());
            MultiDimensionReader netCDFReader = new NetCDFFile(is);

            // let's try and look through the NetCDF reader and see what we can learn...
            NetcdfFile ncdff = ((NetCDFFile) netCDFReader).getNetCDFFile();

            Group rg = ncdff.getRootGroup();
            // this is a list filled with unpacked qflag products, if any
            ArrayList<VariableDS> qfProds = new ArrayList<VariableDS>();

            // this is a list filled with pseudo Brightness Temp variables converted from Radiance
            ArrayList<VariableDS> btProds = new ArrayList<VariableDS>();

            List<Group> gl = rg.getGroups();
            if (gl != null) {
                int xDimNASA = -1;
                int yDimNASA = -1;
                // Make a first pass to determine the shape of the geolocation data
                for (Group g : gl) {
                    if (g.getFullName().contains("geolocation_data")) {
                        List<Variable> vl = g.getVariables();
                        for (Variable v : vl) {
                            if (v.getShortName().equals("latitude")) {
                                // XXX TJJ Nov 2015
                                // Hack because fill value in attribute does not match
                                // what I am seeing in the data.
                                Attribute fillAtt = new Attribute("_FillValue", -999.0);
                                v.addAttribute(fillAtt);
                                pathToLat = v.getFullName();
                                pathToProducts.add(v.getFullName());
                                prodToDesc.put(v.getFullName(), v.getDescription());
                                xDimNASA = v.getDimension(0).getLength();
                                yDimNASA = v.getDimension(1).getLength();
                            }
                            if (v.getShortName().equals("longitude")) {
                                // XXX TJJ Nov 2015
                                // Hack because fill value in attribute does not match
                                // what I am seeing in the data.
                                Attribute fillAtt = new Attribute("_FillValue", -999.0);
                                v.addAttribute(fillAtt);
                                pathToLon = v.getFullName();
                                pathToProducts.add(v.getFullName());
                                prodToDesc.put(v.getFullName(), v.getDescription());
                            }
                        }
                    }
                }
                for (Group g : gl) {
                    logger.debug("Group name: " + g.getFullName());
                    // NASA only - looking through observation_data and geolocation_data
                    if (g.getFullName().contains("observation_data")) {
                        List<Variable> vl = g.getVariables();
                        for (Variable v : vl) {
                            // keep any data which matches geolocation dimensions
                            if (v.getDimension(0).getLength() == xDimNASA
                                    && v.getDimension(1).getLength() == yDimNASA) {
                                logger.debug("Adding product: " + v.getFullName());
                                pathToProducts.add(v.getFullName());
                                prodToDesc.put(v.getFullName(), v.getDescription());
                                Attribute aUnsigned = v.findAttribute("_Unsigned");
                                if (aUnsigned != null) {
                                    unsignedFlags.put(v.getFullName(), aUnsigned.getStringValue());
                                } else {
                                    unsignedFlags.put(v.getFullName(), "false");
                                }

                                // store units in a map for later
                                Attribute unitAtt = v.findAttribute("units");
                                if (unitAtt != null) {
                                    unitsNASA.put(v.getShortName(), unitAtt.getStringValue());
                                } else {
                                    unitsNASA.put(v.getShortName(), "Unknown");
                                }

                                // TJJ Nov 2018 - SIPS V2+ mods
                                // Regridding with bow-tie interpolation wasn't working since there are
                                // now multiple fill value categories and we need to look specifically
                                // for the bowtie deletion flag

                                Attribute longNameAtt = v.findAttribute("long_name");
                                String longName = "empty";
                                if (longNameAtt != null)
                                    longName = longNameAtt.getStringValue();
                                if (longName.contains("reflectance") || longName.contains("radiance")) {

                                    Attribute flagMeanings = v
                                            .findAttribute(JPSSUtilities.SIPS_FLAG_MEANINGS_ATTRIBUTE);
                                    // If this is not null, we must be v2.0.0 or higher
                                    if (flagMeanings != null) {
                                        String meanings = flagMeanings.getStringValue();
                                        // Tokenize meanings string, multiple flags defined there
                                        StringTokenizer st = new StringTokenizer(meanings);
                                        int bowtieIdx = -1;
                                        boolean foundBowTieAttribute = false;
                                        String tokStr = null;
                                        while (st.hasMoreTokens()) {
                                            tokStr = st.nextToken();
                                            bowtieIdx++;
                                            if (tokStr.equals(JPSSUtilities.SIPS_BOWTIE_DELETED_FLAG)) {
                                                foundBowTieAttribute = true;
                                                break;
                                            }
                                        }

                                        if (foundBowTieAttribute) {
                                            Attribute flagValues = v
                                                    .findAttribute(JPSSUtilities.SIPS_FLAG_VALUES_ATTRIBUTE);
                                            Array flagValsArr = flagValues.getValues();
                                            int bowTieVal = (int) flagValsArr.getInt(bowtieIdx);
                                            Attribute a1 = new Attribute("_FillValue", bowTieVal);
                                            v.addAttribute(a1);
                                        }
                                    }

                                }

                                // TJJ Feb 2016 - Create BT variables where applicable
                                if ((v.getShortName().matches("M12|M13|M14|M15|M16"))
                                        || (v.getShortName().matches("I04|I05"))) {

                                    // Get the LUT variable, load into primitive array
                                    Variable lut = g
                                            .findVariable(v.getShortName() + "_brightness_temperature_lut");
                                    int[] lutShape = lut.getShape();
                                    logger.debug("Handling NASA LUT Variable, LUT size: " + lutShape[0]);

                                    // pull out valid min, max - these will be used for our new VariableDS
                                    Attribute aVMin = lut.findAttribute("valid_min");
                                    Attribute aVMax = lut.findAttribute("valid_max");
                                    Attribute fillAtt = lut.findAttribute("_FillValue");
                                    logger.debug("valid_min from LUT: " + aVMin.getNumericValue());
                                    logger.debug("valid_max from LUT: " + aVMax.getNumericValue());

                                    // A little hacky, but at this point the class is such a mess
                                    // that what's a little more, right? Load M12-M16, I4-I5 LUTS

                                    if (v.getShortName().matches("M12")) {
                                        m12LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            m12LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    if (v.getShortName().matches("M13")) {
                                        m13LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            m13LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    if (v.getShortName().matches("M14")) {
                                        m14LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            m14LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    if (v.getShortName().matches("M15")) {
                                        m15LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            m15LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    if (v.getShortName().matches("M16")) {
                                        m16LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            m16LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    if (v.getShortName().matches("I04")) {
                                        i04LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            i04LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    if (v.getShortName().matches("I05")) {
                                        i05LUT = new float[lutShape[0]];
                                        ArrayFloat.D1 lutArray = (ArrayFloat.D1) lut.read();
                                        for (int lutIdx = 0; lutIdx < lutShape[0]; lutIdx++) {
                                            i05LUT[lutIdx] = lutArray.get(lutIdx);
                                        }
                                    }

                                    // Create a pseudo-variable, fill using LUT
                                    // make a copy of the source variable
                                    // NOTE: by using a VariableDS here, the original
                                    // variable is used for the I/O, this matters!
                                    VariableDS vBT = new VariableDS(g, v, false);

                                    // Name is orig name plus suffix
                                    vBT.setShortName(v.getShortName() + "_BT");

                                    vBT.addAttribute(fillAtt);
                                    vBT.addAttribute(aVMin);
                                    vBT.addAttribute(aVMax);

                                    if (v.getShortName().matches("M12")) {
                                        lutMap.put(vBT.getFullName(), m12LUT);
                                    }
                                    if (v.getShortName().matches("M13")) {
                                        lutMap.put(vBT.getFullName(), m13LUT);
                                    }
                                    if (v.getShortName().matches("M14")) {
                                        lutMap.put(vBT.getFullName(), m14LUT);
                                    }
                                    if (v.getShortName().matches("M15")) {
                                        lutMap.put(vBT.getFullName(), m15LUT);
                                    }
                                    if (v.getShortName().matches("M16")) {
                                        lutMap.put(vBT.getFullName(), m16LUT);
                                    }
                                    if (v.getShortName().matches("I04")) {
                                        lutMap.put(vBT.getFullName(), i04LUT);
                                    }
                                    if (v.getShortName().matches("I05")) {
                                        lutMap.put(vBT.getFullName(), i05LUT);
                                    }
                                    pathToProducts.add(vBT.getFullName());
                                    String newName = vBT.getDescription().replace("radiance",
                                            "brightness temperature");
                                    prodToDesc.put(vBT.getFullName(), newName);
                                    btProds.add(vBT);
                                }
                            }
                        }
                    }
                    if (g.getFullName().contains("geolocation_data")) {
                        List<Variable> vl = g.getVariables();
                        for (Variable v : vl) {
                            // keep any data which matches geolocation dimensions
                            if (v.getDimension(0).getLength() == xDimNASA
                                    && v.getDimension(1).getLength() == yDimNASA) {
                                // except we already found Lat and Lon, skip those 
                                if ((v.getShortName().equals("latitude"))
                                        || (v.getShortName().equals("latitude")))
                                    continue;
                                logger.debug("Adding product: " + v.getFullName());
                                pathToProducts.add(v.getFullName());
                                prodToDesc.put(v.getFullName(), v.getDescription());
                            }
                        }
                    }

                    // NOAA only - we are looking through All_Data, finding displayable data
                    if (g.getFullName().contains("All_Data")) {
                        List<Group> adg = g.getGroups();
                        int xDim = -1;
                        int yDim = -1;

                        // two sub-iterations, first one to find geolocation and product dimensions
                        for (Group subG : adg) {
                            logger.debug("Sub group name: " + subG.getFullName());
                            String subName = subG.getFullName();
                            if (subName.contains("-GEO")) {
                                // this is the geolocation data
                                String geoBaseName = subG.getShortName();
                                geoBaseName = geoBaseName.substring(0, geoBaseName.indexOf('_'));
                                if (!haveGeoMetaData) {
                                    String geoProfileFileName = nppPP.getProfileFileName(geoBaseName);
                                    // also add meta data from geolocation profile
                                    nppPP.addMetaDataFromFile(geoProfileFileName);
                                    haveGeoMetaData = true;
                                }
                                List<Variable> vl = subG.getVariables();
                                for (Variable v : vl) {
                                    if (v.getFullName().endsWith(SEPARATOR_CHAR + "Latitude")) {
                                        pathToLat = v.getFullName();
                                        logger.debug("Ellipsoid Lat/Lon Variable: " + v.getFullName());
                                        // get the dimensions of the lat variable
                                        Dimension dAlongTrack = v.getDimension(0);
                                        yDim = dAlongTrack.getLength();
                                        Dimension dAcrossTrack = v.getDimension(1);
                                        xDim = dAcrossTrack.getLength();
                                        logger.debug("Lat across track dim: " + dAcrossTrack.getLength());
                                    }
                                    if (v.getFullName().endsWith(SEPARATOR_CHAR + "Longitude")) {
                                        // we got dimensions from lat, don't need 'em twice, but need path
                                        pathToLon = v.getFullName();
                                    }
                                }
                                // one more pass in case there is terrain-corrected Lat/Lon
                                for (Variable v : vl) {
                                    if (v.getFullName().endsWith(SEPARATOR_CHAR + "Latitude_TC")) {
                                        pathToLat = v.getFullName();
                                        logger.debug("Switched Lat/Lon Variable to TC: " + v.getFullName());
                                        // get the dimensions of the lat variable
                                        Dimension dAlongTrack = v.getDimension(0);
                                        yDim = dAlongTrack.getLength();
                                        Dimension dAcrossTrack = v.getDimension(1);
                                        xDim = dAcrossTrack.getLength();
                                        logger.debug("Lat across track dim: " + dAcrossTrack.getLength());
                                    }
                                    if (v.getFullName().endsWith(SEPARATOR_CHAR + "Longitude_TC")) {
                                        // we got dimensions from lat, don't need 'em twice, but need path
                                        pathToLon = v.getFullName();
                                    }
                                }
                            }
                        }

                        // second to identify displayable products
                        for (Group subG : adg) {
                            // this is the product data
                            List<Variable> vl = subG.getVariables();
                            for (Variable v : vl) {
                                boolean useThis = false;
                                String vName = v.getFullName();
                                logger.trace("Variable: " + vName);
                                String varShortName = vName.substring(vName.lastIndexOf(SEPARATOR_CHAR) + 1);

                                // Special code to handle quality flags. We throw out anything
                                // that does not match bounds of the geolocation data

                                if (varShortName.startsWith("QF")) {

                                    logger.trace("Handling Quality Flag: " + varShortName);

                                    // this check is done later for ALL variables, but we need
                                    // it early here to weed out those quality flags that are 
                                    // simply a small set of data w/no granule geo nbounds
                                    boolean xScanOk = false;
                                    boolean yScanOk = false;
                                    List<Dimension> dl = v.getDimensions();

                                    // toss out > 2D Quality Flags 
                                    if (dl.size() > 2) {
                                        logger.trace("SKIPPING QF, > 2D: " + varShortName);
                                        continue;
                                    }

                                    for (Dimension d : dl) {
                                        // in order to consider this a displayable product, make sure
                                        // both scan direction dimensions are present and look like a granule
                                        if (d.getLength() == xDim) {
                                            xScanOk = true;
                                        }
                                        if (d.getLength() == yDim) {
                                            yScanOk = true;
                                        }
                                    }

                                    if (!(xScanOk && yScanOk)) {
                                        logger.trace("SKIPPING QF, does not match geo bounds: " + varShortName);
                                        continue;
                                    }

                                    ArrayList<QualityFlag> qfal = nppPP.getQualityFlags(varShortName);
                                    if (qfal != null) {
                                        for (QualityFlag qf : qfal) {
                                            qf.setPackedName(vName);
                                            // make a copy of the qflag variable
                                            // NOTE: by using a VariableDS here, the original
                                            // variable is used for the I/O, this matters!
                                            VariableDS vqf = new VariableDS(subG, v, false);
                                            // prefix with QF num to help guarantee uniqueness across groups
                                            // this will cover most cases, but could still be dupe names
                                            // within a single QF.  This is handled when fetching XMLPP metadata
                                            vqf.setShortName(varShortName.substring(0, 3) + "_" + qf.getName());
                                            logger.debug("New QF var full name: " + vqf.getFullName());
                                            qfProds.add(vqf);
                                            qfMap.put(vqf.getFullName(), qf);
                                        }
                                    }
                                }

                                // for CrIS instrument, first find dimensions of var matching
                                // CrIS filter, then throw out all variables which don't match 
                                // those dimensions

                                if (instrumentName.getStringValue().equals("CrIS")) {
                                    if (!vName.contains("GEO")) {
                                        if (!varShortName.startsWith(crisFilter)) {
                                            logger.trace("Skipping variable: " + varShortName);
                                            continue;
                                        }
                                    } else {
                                        // these variables are all GEO-related
                                        // if they match lat/lon bounds, keep them
                                        List<Dimension> dl = v.getDimensions();
                                        if (dl.size() == 3) {
                                            boolean isDisplayableCrIS = true;
                                            for (Dimension d : dl) {
                                                if ((d.getLength() != xDim) && (d.getLength() != yDim)
                                                        && (d.getLength() != 9)) {
                                                    isDisplayableCrIS = false;
                                                }
                                            }
                                            if (!isDisplayableCrIS) {
                                                continue;
                                            }
                                        }
                                    }
                                }

                                DataType dt = v.getDataType();
                                if ((dt.getSize() != 4) && (dt.getSize() != 2) && (dt.getSize() != 1)) {
                                    continue;
                                }

                                List<Dimension> dl = v.getDimensions();
                                if (dl.size() > 4) {
                                    continue;
                                }

                                // for now, skip any 3D VIIRS data
                                if (instrumentName.getStringValue().equals("VIIRS")) {
                                    if (dl.size() == 3) {
                                        continue;
                                    }
                                }

                                boolean xScanOk = false;
                                boolean yScanOk = false;
                                for (Dimension d : dl) {
                                    // in order to consider this a displayable product, make sure
                                    // both scan direction dimensions are present and look like a granule
                                    if (d.getLength() == xDim) {
                                        xScanOk = true;
                                    }
                                    if (d.getLength() == yDim) {
                                        yScanOk = true;
                                    }
                                }

                                if (xScanOk && yScanOk) {
                                    useThis = true;
                                }

                                // For ATMS, only 3-D variable we pass through is BrightnessTemperature
                                // Dimensions for BT are (lon, lat, channel)
                                if (instrumentName.getStringValue().equals("ATMS")) {
                                    if (dl.size() == 3) {
                                        boolean isDisplayableATMS = false;
                                        for (Dimension d : dl) {
                                            if (d.getLength() == JPSSUtilities.ATMSChannelCenterFrequencies.length) {
                                                isDisplayableATMS = true;
                                                logger.trace(
                                                        "This variable has a dimension matching num ATMS channels");
                                                break;
                                            }
                                        }
                                        if (!isDisplayableATMS)
                                            useThis = false;
                                    }
                                }

                                // sensor data with a channel dimension
                                if (useThis) {
                                    if ((instrumentName.getStringValue().equals("CrIS"))
                                            || (instrumentName.getStringValue().equals("ATMS"))
                                            || (instrumentName.getStringValue().contains("OMPS"))) {
                                        isVIIRS = false;
                                        logger.debug("Handling non-VIIRS data source...");
                                    }
                                }

                                if (useThis) {
                                    // loop through the variable list again, looking for a corresponding "Factors"
                                    float scaleVal = 1f;
                                    float offsetVal = 0f;
                                    boolean unpackFlag = false;

                                    //   if the granule has an entry for this variable name
                                    //     get the data, data1 = scale, data2 = offset
                                    //     create and poke attributes with this data
                                    //   endif

                                    String factorsVarName = nppPP.getScaleFactorName(varShortName);
                                    if (factorsVarName != null) {
                                        logger.debug("Mapping: " + varShortName + " to: " + factorsVarName);
                                        for (Variable fV : vl) {
                                            if (fV.getShortName().equals(factorsVarName)) {
                                                logger.trace("Pulling scale and offset values from variable: "
                                                        + fV.getShortName());
                                                ucar.ma2.Array a = fV.read();
                                                float[] so = (float[]) a.copyTo1DJavaArray();
                                                scaleVal = so[0];
                                                offsetVal = so[1];
                                                logger.trace("Scale value: " + scaleVal + ", Offset value: "
                                                        + offsetVal);
                                                unpackFlag = true;
                                                break;
                                            }
                                        }
                                    }

                                    // poke in scale/offset attributes for now

                                    Attribute a1 = new Attribute("scale_factor", scaleVal);
                                    v.addAttribute(a1);
                                    Attribute a2 = new Attribute("add_offset", offsetVal);
                                    v.addAttribute(a2);

                                    // add valid range and fill value attributes here
                                    // try to fill in valid range
                                    if (nppPP.hasNameAndMetaData(varShortName)) {
                                        String rangeMin = nppPP.getRangeMin(varShortName);
                                        String rangeMax = nppPP.getRangeMax(varShortName);
                                        logger.trace("range min: " + rangeMin + ", range max: " + rangeMax);
                                        // only store range attribute if VALID range found
                                        if ((rangeMin != null) && (rangeMax != null)) {
                                            int[] shapeArr = new int[] { 2 };
                                            ArrayFloat af = new ArrayFloat(shapeArr);
                                            try {
                                                af.setFloat(0, Float.parseFloat(rangeMin));
                                            } catch (NumberFormatException nfe) {
                                                af.setFloat(0, new Float(Integer.MIN_VALUE));
                                            }
                                            try {
                                                af.setFloat(1, Float.parseFloat(rangeMax));
                                            } catch (NumberFormatException nfe) {
                                                af.setFloat(1, new Float(Integer.MAX_VALUE));
                                            }
                                            Attribute rangeAtt = new Attribute("valid_range", af);
                                            v.addAttribute(rangeAtt);
                                        }

                                        // check for and load fill values too...

                                        // we need to check two places, first, the XML product profile
                                        ArrayList<Float> fval = nppPP.getFillValues(varShortName);

                                        // 2nd, does the variable already have one defined?
                                        // if there was already a fill value associated with this variable, make
                                        // sure we bring that along for the ride too...
                                        Attribute aFill = v.findAttribute("_FillValue");

                                        // determine size of our fill value array
                                        int fvArraySize = 0;
                                        if (aFill != null)
                                            fvArraySize++;
                                        if (!fval.isEmpty())
                                            fvArraySize += fval.size();
                                        int[] fillShape = new int[] { fvArraySize };

                                        // allocate the array
                                        ArrayFloat afFill = new ArrayFloat(fillShape);

                                        // and FINALLY, fill it!
                                        if (!fval.isEmpty()) {
                                            for (int fillIdx = 0; fillIdx < fval.size(); fillIdx++) {
                                                afFill.setFloat(fillIdx, fval.get(fillIdx));
                                                logger.trace(
                                                        "Adding fill value (from XML): " + fval.get(fillIdx));
                                            }
                                        }

                                        if (aFill != null) {
                                            Number n = aFill.getNumericValue();
                                            // is the data unsigned?
                                            Attribute aUnsigned = v.findAttribute("_Unsigned");
                                            float fillValAsFloat = Float.NaN;
                                            if (aUnsigned != null) {
                                                if (aUnsigned.getStringValue().equals("true")) {
                                                    DataType fvdt = aFill.getDataType();
                                                    logger.trace("Data String: " + aFill.toString());
                                                    logger.trace("DataType primitive type: "
                                                            + fvdt.getPrimitiveClassType());
                                                    // signed byte that needs conversion?
                                                    if (fvdt.getPrimitiveClassType() == byte.class) {
                                                        fillValAsFloat = (float) Util
                                                                .unsignedByteToInt(n.byteValue());
                                                    } else if (fvdt.getPrimitiveClassType() == short.class) {
                                                        fillValAsFloat = (float) Util
                                                                .unsignedShortToInt(n.shortValue());
                                                    } else {
                                                        fillValAsFloat = n.floatValue();
                                                    }
                                                }
                                            }
                                            afFill.setFloat(fvArraySize - 1, fillValAsFloat);
                                            logger.trace(
                                                    "Adding fill value (from variable): " + fillValAsFloat);
                                        }
                                        Attribute fillAtt = new Attribute("_FillValue", afFill);
                                        v.addAttribute(fillAtt);
                                    }

                                    Attribute aUnsigned = v.findAttribute("_Unsigned");
                                    if (aUnsigned != null) {
                                        unsignedFlags.put(v.getFullName(), aUnsigned.getStringValue());
                                    } else {
                                        unsignedFlags.put(v.getFullName(), "false");
                                    }

                                    if (unpackFlag) {
                                        unpackFlags.put(v.getFullName(), "true");
                                    } else {
                                        unpackFlags.put(v.getFullName(), "false");
                                    }

                                    logger.debug("Adding product: " + v.getFullName());
                                    pathToProducts.add(v.getFullName());
                                    prodToDesc.put(v.getFullName(), v.getDescription());
                                }
                            }
                        }
                    }
                }
            }

            // add in any unpacked qflag products
            for (VariableDS qfV : qfProds) {
                // skip the spares - they are reserved for future use
                if (qfV.getFullName().endsWith("Spare")) {
                    continue;
                }
                // String.endsWith is case sensitive so gotta check both cases
                if (qfV.getFullName().endsWith("spare")) {
                    continue;
                }
                ncdff.addVariable(qfV.getGroup(), qfV);
                logger.trace("Adding QF product: " + qfV.getFullName());
                pathToProducts.add(qfV.getFullName());
                prodToDesc.put(qfV.getFullName(), qfV.getDescription());
                unsignedFlags.put(qfV.getFullName(), "true");
                unpackFlags.put(qfV.getFullName(), "false");
            }

            // add in any pseudo BT products from NASA data
            for (Variable vBT : btProds) {
                logger.trace("Adding BT product: " + vBT.getFullName());
                ncdff.addVariable(vBT.getGroup(), vBT);
                unsignedFlags.put(vBT.getFullName(), "true");
                unpackFlags.put(vBT.getFullName(), "false");
            }

            ncdfal.add((NetCDFFile) netCDFReader);
        }

    } catch (Exception e) {
        logger.error("cannot create NetCDF reader for files selected", e);
        if (e.getMessage() != null && e.getMessage().equals("XML Product Profile Error")) {
            throw new VisADException("Unable to extract metadata from required XML Product Profile", e);
        }
    }

    // TJJ Feb 2018 
    // Doing a reorder of variable names here, as per HP's request from
    // http://mcidas.ssec.wisc.edu/inquiry-v/?inquiry=2613

    if (isVIIRS) {
        // Copy the variable Set to a sortable List
        List<String> sortedList = new ArrayList(pathToProducts);
        Collections.sort(sortedList, new VIIRSSort());

        // Clear the original data structure which retains insert order
        // (it's a LinkedHashSet)
        pathToProducts.clear();

        // Re-add the variables in corrected order
        for (String s : sortedList) {
            pathToProducts.add(s);
        }
    }

    // initialize the aggregation reader object
    try {
        if (isNOAA) {
            nppAggReader = new GranuleAggregation(ncdfal, pathToProducts, "Track", "XTrack", isVIIRS);
            ((GranuleAggregation) nppAggReader).setQfMap(qfMap);
        } else {
            nppAggReader = new GranuleAggregation(ncdfal, pathToProducts, "number_of_lines", "number_of_pixels",
                    isVIIRS);
            ((GranuleAggregation) nppAggReader).setLUTMap(lutMap);
        }
    } catch (Exception e) {
        throw new VisADException("Unable to initialize aggregation reader", e);
    }

    // make sure we found valid data
    if (pathToProducts.size() == 0) {
        throw new VisADException("No data found in files selected");
    }

    logger.debug("Number of adapters needed: " + pathToProducts.size());
    adapters = new MultiDimensionAdapter[pathToProducts.size()];
    Hashtable<String, String[]> properties = new Hashtable<>();

    Iterator<String> iterator = pathToProducts.iterator();
    int pIdx = 0;
    boolean adapterCreated = false;
    while (iterator.hasNext()) {
        String pStr = iterator.next();
        logger.debug("Working on adapter number " + (pIdx + 1) + ": " + pStr);
        Map<String, Object> swathTable = SwathAdapter.getEmptyMetadataTable();
        Map<String, Object> spectTable = SpectrumAdapter.getEmptyMetadataTable();
        swathTable.put("array_name", pStr);
        swathTable.put("lon_array_name", pathToLon);
        swathTable.put("lat_array_name", pathToLat);
        swathTable.put("XTrack", "XTrack");
        swathTable.put("Track", "Track");
        swathTable.put("geo_Track", "Track");
        swathTable.put("geo_XTrack", "XTrack");
        // TJJ is this even needed?  Is product_name used anywhere?
        if (productName == null)
            productName = pStr.substring(pStr.indexOf(SEPARATOR_CHAR) + 1);
        swathTable.put("product_name", productName);
        swathTable.put("_mapping", prodToDesc);
        // array_name common to spectrum table
        spectTable.put("array_name", pStr);
        spectTable.put("product_name", productName);
        spectTable.put("_mapping", prodToDesc);

        if (!isVIIRS) {

            // 3D data is either ATMS, OMPS, or CrIS
            if ((instrumentName.getShortName() != null) && (instrumentName.getStringValue().equals("ATMS"))) {

                spectTable.put(SpectrumAdapter.channelIndex_name, "Channel");
                swathTable.put(SpectrumAdapter.channelIndex_name, "Channel");

                swathTable.put("array_dimension_names", new String[] { "Track", "XTrack", "Channel" });
                swathTable.put("lon_array_dimension_names", new String[] { "Track", "XTrack" });
                swathTable.put("lat_array_dimension_names", new String[] { "Track", "XTrack" });
                spectTable.put("array_dimension_names", new String[] { "Track", "XTrack", "Channel" });
                spectTable.put("lon_array_dimension_names", new String[] { "Track", "XTrack" });
                spectTable.put("lat_array_dimension_names", new String[] { "Track", "XTrack" });

                spectTable.put(SpectrumAdapter.channelType, "wavelength");
                spectTable.put(SpectrumAdapter.channels_name, "Channel");
                spectTable.put(SpectrumAdapter.x_dim_name, "XTrack");
                spectTable.put(SpectrumAdapter.y_dim_name, "Track");

                int numChannels = JPSSUtilities.ATMSChannelCenterFrequencies.length;
                float[] bandArray = new float[numChannels];
                String[] bandNames = new String[numChannels];
                for (int bIdx = 0; bIdx < numChannels; bIdx++) {
                    bandArray[bIdx] = JPSSUtilities.ATMSChannelCenterFrequencies[bIdx];
                    bandNames[bIdx] = "Channel " + (bIdx + 1);
                }
                spectTable.put(SpectrumAdapter.channelValues, bandArray);
                spectTable.put(SpectrumAdapter.bandNames, bandNames);

            } else {
                if (instrumentName.getStringValue().equals("CrIS")) {

                    swathTable.put("XTrack", "dim1");
                    swathTable.put("Track", "dim0");
                    swathTable.put("geo_XTrack", "dim1");
                    swathTable.put("geo_Track", "dim0");
                    swathTable.put("product_name", "CrIS_SDR");
                    swathTable.put(SpectrumAdapter.channelIndex_name, "dim3");
                    swathTable.put(SpectrumAdapter.FOVindex_name, "dim2");

                    spectTable.put(SpectrumAdapter.channelIndex_name, "dim3");
                    spectTable.put(SpectrumAdapter.FOVindex_name, "dim2");
                    spectTable.put(SpectrumAdapter.x_dim_name, "dim1");
                    spectTable.put(SpectrumAdapter.y_dim_name, "dim0");

                } else if (instrumentName.getStringValue().contains("OMPS")) {

                    spectTable.put(SpectrumAdapter.channelIndex_name, "Channel");
                    swathTable.put(SpectrumAdapter.channelIndex_name, "Channel");

                    swathTable.put("array_dimension_names", new String[] { "Track", "XTrack", "Channel" });
                    swathTable.put("lon_array_dimension_names", new String[] { "Track", "XTrack" });
                    swathTable.put("lat_array_dimension_names", new String[] { "Track", "XTrack" });
                    spectTable.put("array_dimension_names", new String[] { "Track", "XTrack", "Channel" });
                    spectTable.put("lon_array_dimension_names", new String[] { "Track", "XTrack" });
                    spectTable.put("lat_array_dimension_names", new String[] { "Track", "XTrack" });

                    spectTable.put(SpectrumAdapter.channelType, "wavelength");
                    spectTable.put(SpectrumAdapter.channels_name, "Channel");
                    spectTable.put(SpectrumAdapter.x_dim_name, "XTrack");
                    spectTable.put(SpectrumAdapter.y_dim_name, "Track");

                    int numChannels = 200;
                    if (instrumentName.getStringValue().equals("OMPS-TC")) {
                        numChannels = 260;
                    }
                    logger.debug("Setting up OMPS adapter, num channels: " + numChannels);
                    float[] bandArray = new float[numChannels];
                    String[] bandNames = new String[numChannels];
                    for (int bIdx = 0; bIdx < numChannels; bIdx++) {
                        bandArray[bIdx] = bIdx;
                        bandNames[bIdx] = "Channel " + (bIdx + 1);
                    }
                    spectTable.put(SpectrumAdapter.channelValues, bandArray);
                    spectTable.put(SpectrumAdapter.bandNames, bandNames);

                } else {
                    // sorry, if we can't id the instrument, we can't display the data!
                    throw new VisADException("Unable to determine instrument name");
                }
            }

        } else {
            swathTable.put("array_dimension_names", new String[] { "Track", "XTrack" });
            swathTable.put("lon_array_dimension_names", new String[] { "Track", "XTrack" });
            swathTable.put("lat_array_dimension_names", new String[] { "Track", "XTrack" });
        }

        swathTable.put("scale_name", "scale_factor");
        swathTable.put("offset_name", "add_offset");
        swathTable.put("fill_value_name", "_FillValue");
        swathTable.put("range_name", pStr.substring(pStr.indexOf(SEPARATOR_CHAR) + 1));
        spectTable.put("range_name", pStr.substring(pStr.indexOf(SEPARATOR_CHAR) + 1));

        // set the valid range hash if data is available
        if (nppPP != null) {
            if (nppPP.getRangeMin(pStr.substring(pStr.lastIndexOf(SEPARATOR_CHAR) + 1)) != null) {
                swathTable.put("valid_range", "valid_range");
            }
        }

        String unsignedAttributeStr = unsignedFlags.get(pStr);
        if ((unsignedAttributeStr != null) && (unsignedAttributeStr.equals("true"))) {
            swathTable.put("unsigned", unsignedAttributeStr);
        }

        String unpackFlagStr = unpackFlags.get(pStr);
        if ((unpackFlagStr != null) && (unpackFlagStr.equals("true"))) {
            swathTable.put("unpack", "true");
        }

        // For Suomi NPP data, do valid range check AFTER applying scale/offset
        swathTable.put("range_check_after_scaling", "true");

        // pass in a GranuleAggregation reader...
        if (!isVIIRS) {
            if (instrumentName.getStringValue().equals("ATMS")) {
                adapters[pIdx] = new SwathAdapter(nppAggReader, swathTable);
                adapterCreated = true;
                SpectrumAdapter sa = new SpectrumAdapter(nppAggReader, spectTable);
                DataCategory.createCategory("MultiSpectral");
                categories = DataCategory.parseCategories("MultiSpectral;MultiSpectral;IMAGE");
                MultiSpectralData msd = new MultiSpectralData((SwathAdapter) adapters[pIdx], sa,
                        "BrightnessTemperature", "BrightnessTemperature", "SuomiNPP", "ATMS");
                msd.setInitialWavenumber(JPSSUtilities.ATMSChannelCenterFrequencies[0]);
                multiSpectralData.add(msd);
            }
            if (instrumentName.getStringValue().equals("CrIS")) {
                if (pStr.contains(crisFilter)) {
                    adapters[pIdx] = new CrIS_SDR_SwathAdapter(nppAggReader, swathTable);
                    adapterCreated = true;
                    CrIS_SDR_Spectrum csa = new CrIS_SDR_Spectrum(nppAggReader, spectTable);
                    DataCategory.createCategory("MultiSpectral");
                    categories = DataCategory.parseCategories("MultiSpectral;MultiSpectral;IMAGE");
                    MultiSpectralData msd = new CrIS_SDR_MultiSpectralData(
                            (CrIS_SDR_SwathAdapter) adapters[pIdx], csa);
                    msd.setInitialWavenumber(csa.getInitialWavenumber());
                    msd_CrIS.add(msd);
                }
            }
            if (instrumentName.getStringValue().contains("OMPS")) {
                adapters[pIdx] = new SwathAdapter(nppAggReader, swathTable);
                adapterCreated = true;
                SpectrumAdapter sa = new SpectrumAdapter(nppAggReader, spectTable);
                DataCategory.createCategory("MultiSpectral");
                categories = DataCategory.parseCategories("MultiSpectral;MultiSpectral;IMAGE");
                MultiSpectralData msd = new MultiSpectralData((SwathAdapter) adapters[pIdx], sa,
                        "RadianceEarth", "RadianceEarth", "SuomiNPP", "OMPS");
                msd.setInitialWavenumber(0);
                multiSpectralData.add(msd);
            }
            if (pIdx == 0) {
                // generate default subset for ATMS and OMPS
                if (!instrumentName.getStringValue().equals("CrIS")) {
                    defaultSubset = multiSpectralData.get(pIdx).getDefaultSubset();
                }
            }

        } else {
            // setting NOAA-format units
            String varName = pStr.substring(pStr.indexOf(SEPARATOR_CHAR) + 1);
            String varShortName = pStr.substring(pStr.lastIndexOf(SEPARATOR_CHAR) + 1);
            String units = nppPP.getUnits(varShortName);

            // setting NASA-format units
            if (!isNOAA) {
                units = unitsNASA.get(varShortName);
                // Need to set _BT variables manually, since they are created on the fly
                if (varShortName.endsWith("_BT"))
                    units = "Kelvin";
            }
            if (units == null)
                units = "Unknown";
            Unit u = null;
            try {
                u = Parser.parse(units);
            } catch (NoSuchUnitException e) {
                u = new DerivedUnit(units);
                logger.debug("Unknown units: " + units);
            } catch (ParseException e) {
                u = new DerivedUnit(units);
                logger.debug("Unparseable units: " + units);
            }
            // associate this variable with these units, if not done already
            RealType.getRealType(varName, u);
            adapters[pIdx] = new SwathAdapter(nppAggReader, swathTable);
            adapterCreated = true;
            if (pIdx == 0) {
                defaultSubset = adapters[pIdx].getDefaultSubset();
            }
            categories = DataCategory.parseCategories("IMAGE");
        }
        // only increment count if we created an adapter, some products are skipped
        if (adapterCreated)
            pIdx++;
        adapterCreated = false;
    }

    if (msd_CrIS.size() > 0) {
        try {
            MultiSpectralAggr aggr = new MultiSpectralAggr(
                    msd_CrIS.toArray(new MultiSpectralData[msd_CrIS.size()]));
            aggr.setInitialWavenumber(902.25f);
            multiSpectralData.add(aggr);
            defaultSubset = ((MultiSpectralData) msd_CrIS.get(0)).getDefaultSubset();
        } catch (Exception e) {
            logger.error("Exception: ", e);
        }
    }

    // Merge with pre-set properties
    Hashtable tmpHt = getProperties();
    tmpHt.putAll(properties);
    setProperties(tmpHt);
}

From source file:Ejercicios.punto5d.CreatorEntity.java

/**
 *
 * @return documento jdom xml con el contenido de la entidad
 * @throws IOException si hay un error al guardar el archivo en disco
 *///from w  ww.j  a v  a2  s  . c o  m
public Document crearXML() throws IOException {
    if ("".equals(nombre) || "".equals(raiz) || 0 == atributos.size()) {
        return null;
    }
    root = new Element(raiz);
    doc = new Document(root);
    for (String s : atributos.keySet()) {
        Element e = new Element(s).addContent(atributos.get(s));
        getDoc().getRootElement().addContent(e);
    }
    try {
        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(getDoc(), new FileWriter(nombre + ".xml"));

        System.out.println("Archivo Guardado!");
        return doc;

    } catch (IOException e) {
        System.out.println(e.getMessage());
        return null;
    }

}

From source file:es.ucm.fdi.ac.Analysis.java

License:Open Source License

/**
 * Saves this analysis to a file/*from   w ww.  j  a v a  2  s .co  m*/
 *
 * @param f the file to write to 
 * @throws java.io.IOException
 */
public void saveToFile(File f) throws IOException {
    FileOutputStream fos = null;
    try {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        fos = new FileOutputStream(f);
        outputter.output(new Document(saveToXML()), fos);
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}

From source file:es.ucm.fdi.clover.gui.CloverSave.java

License:Open Source License

/**
 * Saves the CloverSave to an XML file//from  w ww  . ja  va 2 s  .c o m
 */
public static void save(Collection<ClusterView> views, File f) throws IOException {
    Element root = new Element("clover");
    root.setAttribute("version", saveVersion);
    root.setAttribute("requiresVersion", compatibleWith);
    root.setAttribute("date", new Date().toString());

    Element shared = new Element("shared");
    root.addContent(shared);

    HashMap<Filter, String> filterToId = new HashMap<Filter, String>();
    HashMap<ClusteringEngine, String> engineToId = new HashMap<ClusteringEngine, String>();
    HashMap<ClusterHierarchy, String> hierarchyToId = new HashMap<ClusterHierarchy, String>();

    for (ClusterView v : views) {
        Element view = new Element("view");
        v.save(view);

        Element layoutCache = new Element("layoutCache");
        v.getAnimator().getLayoutCache().save(layoutCache, v.getBase());
        view.addContent(layoutCache);

        Element animatorProps = new Element("animatorProps");
        v.getAnimator().save(animatorProps);
        view.addContent(animatorProps);

        ClusteredGraph cg = (ClusteredGraph) v.getBase();
        ClusterHierarchy h = cg.getHierarchy();
        BaseGraph bg = cg.getBase();

        // create the hierarchy element (if necessary)
        if (!hierarchyToId.containsKey(h)) {
            String hierarchyId = "" + generateId();
            hierarchyToId.put(h, hierarchyId);
            Element hierarchy = new Element("hierarchy");
            hierarchy.setAttribute("id", hierarchyId);
            h.save(hierarchy);

            // save the filtering used in the hierarchy (if necessary)
            if (bg instanceof FilteredGraph) {
                Filter filter = ((FilteredGraph) bg).getFilter();
                if (!filterToId.containsKey(filter)) {
                    String filterId = "" + generateId();
                    filterToId.put(filter, filterId);
                    Element fe = new Element("filter");
                    fe.setAttribute("id", filterId);
                    filter.save(fe);
                    shared.addContent(fe);
                }

                hierarchy.setAttribute("filterId", filterToId.get(filter));
            }

            // save the hierarchy itself: clustering and update-engine
            ClusteringEngine e = h.getEngine();
            Element engine = new Element("engine");
            engine.setAttribute("engineClass", e.getClass().getName());
            e.save(engine);
            hierarchy.addContent(engine);

            shared.addContent(hierarchy);
        }
        view.setAttribute("hierarchyId", hierarchyToId.get(h));

        root.addContent(view);
    }

    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    outputter.output(new Document(root), new FileOutputStream(f));
}

From source file:es.upm.dit.xsdinferencer.generation.generatorimpl.schemageneration.XMLSchemaDocumentGenerator.java

License:Apache License

/**
 * It generates the XSD file of the targetNamespace given at the constructor, taking into account that 
 * the main namespace is the one given at the constructor.
 * //from   ww w  . j  av  a  2s  .c  om
 * @param schema the schema object
 * @param configuration the inference configuration
 * 
 * @return a JDOM2 {@link Document} object containing the XSD contents.
 * 
 * @see SchemaDocumentGenerator#generateSchemaDocument(Schema, XSDInferenceConfiguration)
 */
@Override
public Document generateSchemaDocument(Schema schema, XSDInferenceConfiguration configuration) {
    //      if(!configuration.getElementsGlobal()==false || 
    //            !configuration.getComplexTypesGlobal()==true ||
    //            !configuration.getSimpleTypesGlobal()==true
    //            )
    //         throw new UnsupportedOperationException("Not implemented yet.");
    //
    checkArgument(schema.getNamespacesToPossiblePrefixMappingModifiable().containsKey(mainNamespace),
            "The main namespace must be a known namespace");
    checkArgument(schema.getNamespacesToPossiblePrefixMappingModifiable().containsKey(targetNamespace),
            "The target namespace must be a known namespace");
    //      checkArgument(!schema.getNamespacesToPossiblePrefixMappingModifiable().containsKey(XSD_NAMESPACE_URI),"The XSD namespace must not be a known namespace");
    //      checkArgument(!schema.getNamespacesToPossiblePrefixMappingModifiable().containsKey(XSI_NAMESPACE_URI),"The XSI namespace must not be a known namespace");
    Map<String, String> namespaceURIToPrefixMappings = schema.getSolvedNamespaceMappings();
    if (configuration.getSkipNamespaces().contains(targetNamespace)) {
        throw new IllegalArgumentException("This is an skipped namespace, so its XSD should not be generated");
    }
    if (targetNamespace.equals(XSD_NAMESPACE_URI))
        System.err.println(
                "The XML Schema namespace is being considered as a target namespace in your documents. Independing of the inferred schemas, the only valid XSD for an XSD would be the normative one present at its first RFC");
    Namespace xsdNamespace = Namespace.getNamespace(XSD_NAMESPACE_PREFIX.replace(":", ""), XSD_NAMESPACE_URI);
    List<Namespace> namespaceDeclarations = getNamespaceDeclarations(namespaceURIToPrefixMappings,
            xsdNamespace);
    Element elementSchema = new Element("schema", xsdNamespace);
    for (int i = 0; i < namespaceDeclarations.size(); i++) {
        Namespace currentNamespace = namespaceDeclarations.get(i);
        elementSchema.addNamespaceDeclaration(currentNamespace);
        String currentNamespaceUri = currentNamespace.getURI();
        if (!targetNamespace.equals(mainNamespace) && !currentNamespaceUri.equals(mainNamespace))
            continue;
        if (currentNamespace.equals(Namespace.XML_NAMESPACE)
                && (!schema.getAttributes().containsRow(XSDInferenceConfiguration.XML_NAMESPACE_URI)
                        && !schema.getElements().containsRow(XSDInferenceConfiguration.XML_NAMESPACE_URI))) {
            continue;
        }
        if (currentNamespaceUri.equals(XSD_NAMESPACE_URI)
                && !namespaceURIToPrefixMappings.containsKey(XSD_NAMESPACE_URI))
            continue;
        if (targetNamespace.equals(currentNamespaceUri)
                || (currentNamespaceUri.equals("") && (fileNameGenerator == null)))
            continue;
        if (currentNamespaceUri.equals("") && !currentNamespaceUri.equals(mainNamespace)
                && !schema.getElements().containsRow(""))
            continue;
        Element importElement = new Element("import", xsdNamespace);
        if (!currentNamespaceUri.equals("")) {
            Attribute namespaceAttr = new Attribute("namespace", currentNamespaceUri);
            importElement.setAttribute(namespaceAttr);
        }
        if (fileNameGenerator != null && !configuration.getSkipNamespaces().contains(currentNamespaceUri)) {
            String fileName = fileNameGenerator.getSchemaDocumentFileName(currentNamespaceUri,
                    namespaceURIToPrefixMappings);
            Attribute schemaLocationAttr = new Attribute("schemaLocation", fileName);
            importElement.setAttribute(schemaLocationAttr);
        }
        elementSchema.addContent(importElement);
    }

    if (!targetNamespace.equals("")) {
        Attribute targetNamespaceAttr = new Attribute("targetNamespace", targetNamespace);
        elementSchema.setAttribute(targetNamespaceAttr);
    }
    SortedSet<SimpleType> sortedSimpleTypes = new TreeSet<>(new SimpleTypeComparator());
    sortedSimpleTypes.addAll(schema.getSimpleTypes().values());
    SortedSet<ComplexType> sortedComplexTypes = new TreeSet<>(new ComplexTypeComparator());
    sortedComplexTypes.addAll(schema.getComplexTypes().values());
    //CONTINUE FROM HERE: Generate sorted sets for SchemaElement and SchemaAttribute objects and use them where needed.
    Attribute elementFormDefault = new Attribute("elementFormDefault", "qualified");
    elementSchema.setAttribute(elementFormDefault);
    Document resultingDocument = new Document(elementSchema);
    if (targetNamespace.equals(mainNamespace)) {
        //First, we declare global SimpleTypes.
        //If simpleTypesGlobal is true, any enumeration will be declared as a global simple type.
        //if not, simple types of complex types which have attributes but not children will be declared globally 
        //(due to limitations of XSD, they may not be declared locally together with the attributes info)
        if (configuration.getSimpleTypesGlobal()) {
            for (SimpleType simpleType : sortedSimpleTypes) {
                if (!simpleType.isEnum() || simpleType.isEmpty())
                    continue;
                Element simpleTypeElement = generateSimpleType(simpleType, false, configuration, xsdNamespace);
                elementSchema.addContent(simpleTypeElement);
            }
        } else {
            for (ComplexType complexType : sortedComplexTypes) {
                SimpleType simpleType = complexType.getTextSimpleType();
                if (complexType.getAttributeList().isEmpty() || !(complexType.getAutomaton().nodeCount() == 0)
                        || !simpleType.isEnum() || simpleType.isEmpty())
                    continue;
                Element simpleTypeElement = generateSimpleType(simpleType, false, configuration, xsdNamespace);
                elementSchema.addContent(simpleTypeElement);
            }
        }
        //Global complexType elements are only generated in the main schema (i.e. the one whose targetNamespace is equal to mainNamespace)
        if (configuration.getComplexTypesGlobal()) {
            for (ComplexType complexType : sortedComplexTypes) {
                boolean hasNoChildren = complexType.getRegularExpression().equals(new EmptyRegularExpression());
                boolean hasNoAttributes = complexType.getAttributeList().size() == 0;
                boolean hasNoComments = complexType.getComments().size() == 0;
                //               boolean simpleTypeIsNotEmpty = !complexType.getTextSimpleType().isEmpty();
                boolean simpleTypeIsWhiteSpaceOnlyOrEmpty = !(complexType.getTextSimpleType().isEmpty()
                        || complexType.getTextSimpleType().consistOnlyOfWhitespaceCharacters());
                if (hasNoChildren && hasNoAttributes && simpleTypeIsWhiteSpaceOnlyOrEmpty && hasNoComments)
                    continue; //Because the elements which are linked to this ComplexType at our internal model 
                              //will be linked to an XSD simple type elsewhere, either a builtin or a custom one.
                Element complexTypeElement = generateComplexType(configuration, complexType, false,
                        targetNamespace, namespaceURIToPrefixMappings, mainNamespace, xsdNamespace);
                elementSchema.addContent(complexTypeElement);
            }
        }
    }
    //If there are many namespaces and the workaround is disabled, we must declare global attributes.
    //If the targetNamespace is not the mainNamespace, we must declare all the attributes.
    //if the target namespace is the main namespace, we do not need to declare anything, because the complex types which hold the attributes 
    //are also in the main namespace.
    if ((namespaceURIToPrefixMappings.size() - configuration.getSkipNamespaces().size()) > 1) {

        SortedMap<String, SchemaAttribute> globalAttributeCandidates = new TreeMap<>(
                schema.getAttributes().row(targetNamespace));
        if (!targetNamespace.equals(mainNamespace) && !targetNamespace.equals("")) {
            globalAttributesLoop: for (Map.Entry<String, SchemaAttribute> schemaAttributeEntry : globalAttributeCandidates
                    .entrySet()) {
                SchemaAttribute schemaAttribute = schemaAttributeEntry.getValue();
                //First, we check if the attribute has been already declared when the workaround is disabled. 
                //If so, we update the "use" property.
                //The type should have been already merged.
                if (!configuration.getStrictValidRootDefinitionWorkaround()) {
                    List<Element> alreadyGeneratedAttributeElements = elementSchema.getChildren("attribute",
                            xsdNamespace);
                    for (int i = 0; i < alreadyGeneratedAttributeElements.size(); i++) {
                        Element currentAttributeElement = alreadyGeneratedAttributeElements.get(i);
                        if (currentAttributeElement.getAttributeValue("name")
                                .equals(schemaAttribute.getName())) {
                            continue globalAttributesLoop;
                        }
                    }
                }
                Element attributeOrAttributeGroupElement = generateAttribute(schemaAttribute, true,
                        configuration, namespaceURIToPrefixMappings, targetNamespace, mainNamespace,
                        schemaAttributeEntry.getKey(), xsdNamespace);
                elementSchema.addContent(attributeOrAttributeGroupElement);
            }
        }
    }

    //Now, we declare global elements.
    //An element will be declared globally if and only if: 
    //1-elementsGlobal is true in the configuration
    //2-The element is a valid root
    //3-The element is in a namespace other than the main namespace. Note that the element WILL be surrounded by the corresponding group if the workaround is enabled.
    //Another important remark: Iterating over a set copy implies iterating over DISTINCT SchemaElements, so if two keys pointed to equal SchemaElements, we would generate it only once-
    SortedSet<SchemaElement> schemaElementsAtTargetNamespace = new TreeSet<>(new SchemaElementComparator());
    schemaElementsAtTargetNamespace.addAll(schema.getElements().row(targetNamespace).values());
    globalSchemaElementsLoop: for (SchemaElement schemaElement : schemaElementsAtTargetNamespace) {
        //         if(!configuration.getElementsGlobal()&&
        //               !schemaElement.isValidRoot()&&
        //               (targetNamespace.equals(mainNamespace)||configuration.getStrictValidRootDefinitionWorkaround()))
        if (!configuration.getElementsGlobal() && !schemaElement.isValidRoot()
                && (targetNamespace.equals(mainNamespace)))
            continue;
        //         for(Element currentElement:elementSchema.getContent(Filters.element("element",xsdNamespace))){
        //            if(schemaElement.getName().equals(currentElement.getAttributeValue("name")))
        //               continue globalSchemaElementsLoop;
        //         }
        String possibleGroupName = schemaElement.getName() + configuration.getTypeNamesAncestorsSeparator()
                + schemaElement.getType().getName();
        for (Element currentElement : elementSchema.getContent(Filters.element("group", xsdNamespace))) {
            if (possibleGroupName.equals(currentElement.getAttributeValue("name")))
                continue globalSchemaElementsLoop;
        }
        Element elementOrGroupElement = generateElement(schemaElement, true, configuration, targetNamespace,
                mainNamespace, null, namespaceURIToPrefixMappings, xsdNamespace);
        if (elementOrGroupElement.getName().equals("element")) {
            for (Element currentElement : elementSchema.getChildren("element", xsdNamespace)) {
                if (schemaElement.getName().equals(currentElement.getAttributeValue("name")))
                    continue globalSchemaElementsLoop;
            }
        }
        elementSchema.addContent(elementOrGroupElement);
    }
    return resultingDocument;
}

From source file:es.upm.dit.xsdinferencer.generation.generatorimpl.statisticsgeneration.StatisticResultsDocGeneratorImpl.java

License:Apache License

/**
 * @see StatisticResultsDocGenerator#generateStatisticResultsDoc(Statistics)
 *///from   www . ja  v  a 2 s.c  o  m
@Override
public Document generateStatisticResultsDoc(Statistics statistics) {
    Element statisticsElement = new Element("statistics", STATISTICS_NAMESPACE);

    Element generalStatisticsElement = generateGeneralStatistics(statistics);
    statisticsElement.addContent(generalStatisticsElement);

    Element rootElementsOccurrencesElement = generateRootElementsOccurrencesElement(
            statistics.getRootElementOccurrences());
    statisticsElement.addContent(rootElementsOccurrencesElement);

    Element elementsAtPathElement = generateNodesAtPathElements("elementsAtPathOccurrences", "element",
            statistics.getElementAtPathInfo(), "valuesAtPath", "valueAtPath", statistics.getValuesAtPathInfo(),
            statistics.getStatisticsOfNumericValuesAtPath(), "numericValuesStatistics");
    statisticsElement.addContent(elementsAtPathElement);

    Element attributesAtPathElement = generateNodesAtPathElements("attributesAtPathOccurrences", "attribute",
            statistics.getAttributeAtPathInfo(), "valuesAtPath", "valueAtPath",
            statistics.getValuesAtPathInfo(), statistics.getStatisticsOfNumericValuesAtPath(),
            "numericValuesStatistics");
    statisticsElement.addContent(attributesAtPathElement);

    Element complexTypesInfoElement = generateComplexTypesInfo(statistics.getComplexTypeInfo());
    statisticsElement.addContent(complexTypesInfoElement);

    Document resultingDocument = new Document(statisticsElement);
    return resultingDocument;
}

From source file:eu.optimis.monitoring.amazoncollector.XMLHelper.java

License:Apache License

public static String createDocument(List<Measurement> measurements) {

    Element root = new Element(ROOT);
    Document doc = new Document(root);

    for (Measurement m : measurements) {
        root.addContent(createMonitoringResource(m));
    }/* w  ww .  j  a va2  s  . c  o m*/

    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    try {
        outputter.output(doc, new FileOutputStream("./aws.xml"));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(XMLHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(XMLHelper.class.getName()).log(Level.SEVERE, null, ex);
    }

    String xmlResult = outputter.outputString(doc);
    String result = xmlResult.replace("encoding=\"UTF-8\"", "");
    //System.out.println (result);
    return result;
}

From source file:eus.ixa.ixa.pipe.convert.AbsaSemEval.java

License:Apache License

public static String nafToAbsa2015(String inputNAF) throws IOException {

    Path kafPath = Paths.get(inputNAF);
    KAFDocument kaf = KAFDocument.createFromFile(kafPath.toFile());
    Set<String> reviewIds = getReviewIdsFromXpathAttribute(kaf);

    // root element in ABSA 2015 and 2016 format
    Element reviewsElem = new Element("Reviews");
    Document doc = new Document(reviewsElem);

    // creating Reviews children of Review
    for (String reviewId : reviewIds) {
        Element reviewElem = new Element("Review");
        reviewElem.setAttribute("rid", reviewId);
        Element sentencesElem = new Element("sentences");
        // getting the sentences in the review
        List<List<WF>> sentencesByReview = getSentencesByReview(kaf, reviewId);
        for (List<WF> sent : sentencesByReview) {
            String sentId = sent.get(0).getXpath();
            Integer sentNumber = sent.get(0).getSent();

            // getting text element from word forms in NAF
            String textString = NAFUtils.getSentenceStringFromWFs(sent);
            Element sentenceElem = new Element("sentence");
            sentenceElem.setAttribute("id", sentId);
            Element textElem = new Element("text");
            textElem.setText(textString);
            sentenceElem.addContent(textElem);

            // creating opinions element for sentence
            List<Opinion> opinionsBySentence = getOpinionsBySentence(kaf, sentNumber);
            Element opinionsElem = new Element("Opinions");
            if (!opinionsBySentence.isEmpty()) {
                // getting opinion info from NAF Opinion layer
                for (Opinion opinion : opinionsBySentence) {
                    Element opinionElem = new Element("Opinion");
                    // String polarity = opinion.getOpinionExpression().getPolarity();
                    String category = opinion.getOpinionExpression().getSentimentProductFeature();
                    String targetString = opinion.getStr();
                    int fromOffset = opinion.getOpinionTarget().getTerms().get(0).getWFs().get(0).getOffset();
                    List<WF> targetWFs = opinion.getOpinionTarget().getTerms()
                            .get(opinion.getOpinionTarget().getTerms().size() - 1).getWFs();
                    int toOffset = targetWFs.get(targetWFs.size() - 1).getOffset()
                            + targetWFs.get(targetWFs.size() - 1).getLength();
                    opinionElem.setAttribute("target", targetString);
                    opinionElem.setAttribute("category", category);
                    // TODO we still do not have polarity here
                    opinionElem.setAttribute("polarity", "na");
                    opinionElem.setAttribute("from", Integer.toString(fromOffset));
                    opinionElem.setAttribute("to", Integer.toString(toOffset));
                    opinionsElem.addContent(opinionElem);
                }//from w  w  w  .  j  a  va  2 s .c o m
            }
            sentenceElem.addContent(opinionsElem);
            sentencesElem.addContent(sentenceElem);
        }
        reviewElem.addContent(sentencesElem);
        reviewsElem.addContent(reviewElem);
    } // end of review

    XMLOutputter xmlOutput = new XMLOutputter();
    Format format = Format.getPrettyFormat();
    xmlOutput.setFormat(format);
    return xmlOutput.outputString(doc);
}

From source file:eus.ixa.ixa.pipe.convert.AbsaSemEval.java

License:Apache License

public static String nafToAbsa2014(String kafDocument) {

    KAFDocument kaf = null;// ww  w  .j  a va2  s  .co m
    try {
        Path kafPath = Paths.get(kafDocument);
        kaf = KAFDocument.createFromFile(kafPath.toFile());
    } catch (IOException e) {
        e.printStackTrace();
    }
    Element sentencesElem = new Element("sentences");
    Document doc = new Document(sentencesElem);

    for (List<WF> sent : kaf.getSentences()) {
        String sentId = sent.get(0).getXpath();
        Integer sentNumber = sent.get(0).getSent();

        // getting text element from WFs in NAF
        String textString = NAFUtils.getSentenceStringFromWFs(sent);
        Element sentenceElem = new Element("sentence");
        sentenceElem.setAttribute("id", sentId);
        Element textElem = new Element("text");
        textElem.setText(textString);
        sentenceElem.addContent(textElem);

        // creating opinions element for sentence
        List<Opinion> opinionsBySentence = getOpinionsBySentence(kaf, sentNumber);
        if (!opinionsBySentence.isEmpty()) {
            Element aspectTerms = new Element("aspectTerms");
            // getting opinion info from NAF Opinion layer
            for (Opinion opinion : opinionsBySentence) {
                String polarity = "";
                String targetString = opinion.getStr();
                int fromOffset = opinion.getOpinionTarget().getTerms().get(0).getWFs().get(0).getOffset();
                List<WF> targetWFs = opinion.getOpinionTarget().getTerms()
                        .get(opinion.getOpinionTarget().getTerms().size() - 1).getWFs();
                int toOffset = targetWFs.get(targetWFs.size() - 1).getOffset()
                        + targetWFs.get(targetWFs.size() - 1).getLength();

                Element aspectTerm = new Element("aspectTerm");
                aspectTerm.setAttribute("term", targetString);
                aspectTerm.setAttribute("polarity", polarity);
                aspectTerm.setAttribute("from", Integer.toString(fromOffset));
                aspectTerm.setAttribute("to", Integer.toString(toOffset));
                aspectTerms.addContent(aspectTerm);
            }
            sentenceElem.addContent(aspectTerms);
        }
        sentencesElem.addContent(sentenceElem);
    }
    XMLOutputter xmlOutput = new XMLOutputter();
    Format format = Format.getPrettyFormat();
    xmlOutput.setFormat(format);
    return xmlOutput.outputString(doc);
}

From source file:eus.ixa.ixa.pipe.ml.document.features.DocumentFeatureDescriptor.java

License:Apache License

/**
 * Generate the XML feature descriptor from the docTrainer.properties file.
 * /* ww w.  j  av a  2  s  .  c om*/
 * @param params
 *          the properties file
 * @return the XML feature descriptor
 * @throws IOException
 *           if input output fails
 */
public static String createDocumentFeatureDescriptor(final TrainingParameters params) throws IOException {

    // <generators>
    final Element generators = new Element("generators");
    final Document doc = new Document(generators);

    // <custom bagofwords /.
    if (Flags.isBagOfWordsFeature(params)) {
        final String tokenFeatureRange = Flags.getBagOfWordsFeaturesRange(params);
        final Element tokenFeature = new Element("custom");
        tokenFeature.setAttribute("class", BagOfWordsFeatureGenerator.class.getName());
        tokenFeature.setAttribute("range", tokenFeatureRange);
        generators.addContent(tokenFeature);
        System.err.println("-> BOW features added!");
    }
    if (Flags.isTokenClassFeature(params)) {
        final String tokenClassFeatureRange = Flags.getTokenClassFeaturesRange(params);
        final Element tokenClassFeature = new Element("custom");
        tokenClassFeature.setAttribute("class", DocTokenClassFeatureGenerator.class.getName());
        tokenClassFeature.setAttribute("range", tokenClassFeatureRange);
        generators.addContent(tokenClassFeature);
        System.err.println("-> Token Class Features added!");
    }
    if (Flags.isOutcomePriorFeature(params)) {
        final Element outcomePriorFeature = new Element("custom");
        outcomePriorFeature.setAttribute("class", DocOutcomePriorFeatureGenerator.class.getName());
        generators.addContent(outcomePriorFeature);
        System.err.println("-> Outcome Prior Features added!");
    }
    if (Flags.isSentenceFeature(params)) {
        final String beginSentence = Flags.getSentenceFeaturesBegin(params);
        final String endSentence = Flags.getSentenceFeaturesEnd(params);
        final Element sentenceFeature = new Element("custom");
        sentenceFeature.setAttribute("class", DocSentenceFeatureGenerator.class.getName());
        sentenceFeature.setAttribute("begin", beginSentence);
        sentenceFeature.setAttribute("end", endSentence);
        generators.addContent(sentenceFeature);
        System.err.println("-> Sentence Features added!");
    }
    if (Flags.isPrefixFeature(params)) {
        final String beginPrefix = Flags.getPrefixFeaturesBegin(params);
        final String endPrefix = Flags.getPrefixFeaturesEnd(params);
        final Element prefixFeature = new Element("custom");
        prefixFeature.setAttribute("class", DocPrefixFeatureGenerator.class.getName());
        prefixFeature.setAttribute("begin", beginPrefix);
        prefixFeature.setAttribute("end", endPrefix);
        generators.addContent(prefixFeature);
        System.err.println("-> Prefix Features added!");
    }
    if (Flags.isSuffixFeature(params)) {
        final String beginSuffix = Flags.getSuffixFeaturesBegin(params);
        final String endSuffix = Flags.getSuffixFeaturesEnd(params);
        final Element suffixFeature = new Element("custom");
        suffixFeature.setAttribute("class", DocSuffixFeatureGenerator.class.getName());
        suffixFeature.setAttribute("begin", beginSuffix);
        suffixFeature.setAttribute("end", endSuffix);
        generators.addContent(suffixFeature);
        System.err.println("-> Suffix Features added!");
    }
    if (Flags.isNgramFeature(params)) {
        final String charngramRange = Flags.getNgramFeaturesRange(params);
        final String[] rangeArray = Flags.processNgramRange(charngramRange);
        final Element charngramFeature = new Element("custom");
        charngramFeature.setAttribute("class", NGramFeatureGenerator.class.getName());
        charngramFeature.setAttribute("minLength", rangeArray[0]);
        charngramFeature.setAttribute("maxLength", rangeArray[1]);
        generators.addContent(charngramFeature);
        System.err.println("-> Ngram Features added!");
    }
    if (Flags.isCharNgramClassFeature(params)) {
        final String charngramRange = Flags.getCharNgramFeaturesRange(params);
        final String[] rangeArray = Flags.processNgramRange(charngramRange);
        final Element charngramFeature = new Element("custom");
        charngramFeature.setAttribute("class", DocCharacterNgramFeatureGenerator.class.getName());
        charngramFeature.setAttribute("minLength", rangeArray[0]);
        charngramFeature.setAttribute("maxLength", rangeArray[1]);
        generators.addContent(charngramFeature);
        System.err.println("-> CharNgram Class Features added!");
    }
    // Polarity Dictionary Features
    if (Flags.isDictionaryPolarityFeatures(params)) {
        final String dictPath = Flags.getDictionaryPolarityFeatures(params);
        final List<File> fileList = StringUtils.getFilesInDir(new File(dictPath));
        for (final File dictFile : fileList) {
            final Element dictFeatures = new Element("custom");
            dictFeatures.setAttribute("class", DocPolarityDictionaryFeatureGenerator.class.getName());
            dictFeatures.setAttribute("dict", IOUtils.normalizeLexiconName(dictFile.getName()));
            generators.addContent(dictFeatures);
        }
        System.err.println("-> Dictionary Features added!");
    }
    // Frequent Word Features
    if (Flags.isFrequentWordFeatures(params)) {
        final String dictPath = Flags.getFrequentWordFeatures(params);
        final List<File> fileList = StringUtils.getFilesInDir(new File(dictPath));
        for (final File dictFile : fileList) {
            final Element dictFeatures = new Element("custom");
            dictFeatures.setAttribute("class", FrequentWordFeatureGenerator.class.getName());
            dictFeatures.setAttribute("dict", IOUtils.normalizeLexiconName(dictFile.getName()));
            generators.addContent(dictFeatures);
        }
        System.err.println("-> Frequent Word Features added!");
    }
    //Opinion Target Extraction Features
    if (Flags.isTargetFeatures(params)) {
        final String targetModelPath = Flags.getTargetFeatures(params);
        final String targetModelRange = Flags.getTargetFeaturesRange(params);
        final Element targetClassFeatureElement = new Element("custom");
        targetClassFeatureElement.setAttribute("class", DocTargetFeatureGenerator.class.getName());
        targetClassFeatureElement.setAttribute("model",
                IOUtils.normalizeLexiconName(new File(targetModelPath).getName()));
        targetClassFeatureElement.setAttribute("range", targetModelRange);
        generators.addContent(targetClassFeatureElement);
        System.err.println("-> Target Model Features added!");
    }
    // Dictionary Features
    if (Flags.isDictionaryFeatures(params)) {
        final String dictPath = Flags.getDictionaryFeatures(params);
        final String seqCodec = Flags.getSequenceCodec(params);
        final List<File> fileList = StringUtils.getFilesInDir(new File(dictPath));
        for (final File dictFile : fileList) {
            final Element dictFeatures = new Element("custom");
            dictFeatures.setAttribute("class", DocDictionaryFeatureGenerator.class.getName());
            dictFeatures.setAttribute("dict", IOUtils.normalizeLexiconName(dictFile.getName()));
            dictFeatures.setAttribute("seqCodec", seqCodec);
            generators.addContent(dictFeatures);
        }
        System.err.println("-> Dictionary Features added!");
    }
    // Brown clustering features
    if (Flags.isBrownFeatures(params)) {
        final String brownClusterPath = Flags.getBrownFeatures(params);
        final List<File> brownClusterFiles = Flags.getClusterLexiconFiles(brownClusterPath);
        for (final File brownClusterFile : brownClusterFiles) {
            // brown bigram class features
            final Element brownBigramFeatures = new Element("custom");
            brownBigramFeatures.setAttribute("class", DocBrownBigramFeatureGenerator.class.getName());
            brownBigramFeatures.setAttribute("dict", IOUtils.normalizeLexiconName(brownClusterFile.getName()));
            //generators.addContent(brownBigramFeatures);
            // brown token feature
            final Element brownTokenFeature = new Element("custom");
            brownTokenFeature.setAttribute("class", DocBrownTokenFeatureGenerator.class.getName());
            brownTokenFeature.setAttribute("dict", IOUtils.normalizeLexiconName(brownClusterFile.getName()));
            generators.addContent(brownTokenFeature);
            // brown token class feature
            final Element brownTokenClassFeature = new Element("custom");
            brownTokenClassFeature.setAttribute("class", DocBrownTokenClassFeatureGenerator.class.getName());
            brownTokenClassFeature.setAttribute("dict",
                    IOUtils.normalizeLexiconName(brownClusterFile.getName()));
            //generators.addContent(brownTokenClassFeature);
        }
        System.err.println("-> Brown Cluster Features added!");
    }
    // Clark clustering features
    if (Flags.isClarkFeatures(params)) {
        final String clarkClusterPath = Flags.getClarkFeatures(params);
        final List<File> clarkClusterFiles = Flags.getClusterLexiconFiles(clarkClusterPath);
        for (final File clarkCluster : clarkClusterFiles) {
            final Element clarkFeatures = new Element("custom");
            clarkFeatures.setAttribute("class", DocClarkFeatureGenerator.class.getName());
            clarkFeatures.setAttribute("dict", IOUtils.normalizeLexiconName(clarkCluster.getName()));
            generators.addContent(clarkFeatures);
        }
        System.err.println("-> Clark Cluster Features added!");
    }
    // word2vec clustering features
    if (Flags.isWord2VecClusterFeatures(params)) {
        final String word2vecClusterPath = Flags.getWord2VecClusterFeatures(params);
        final List<File> word2vecClusterFiles = Flags.getClusterLexiconFiles(word2vecClusterPath);
        for (final File word2vecFile : word2vecClusterFiles) {
            final Element word2vecClusterFeatures = new Element("custom");
            word2vecClusterFeatures.setAttribute("class", DocWord2VecClusterFeatureGenerator.class.getName());
            word2vecClusterFeatures.setAttribute("dict", IOUtils.normalizeLexiconName(word2vecFile.getName()));
            generators.addContent(word2vecClusterFeatures);
        }
        System.err.println("-> Word2Vec Clusters Features added!");
    }
    // Morphological features
    if (Flags.isPOSTagModelFeatures(params)) {
        final String posModelPath = Flags.getPOSTagModelFeatures(params);
        final String posModelRange = Flags.getPOSTagModelFeaturesRange(params);
        final Element posTagClassFeatureElement = new Element("custom");
        posTagClassFeatureElement.setAttribute("class", DocPOSTagModelFeatureGenerator.class.getName());
        posTagClassFeatureElement.setAttribute("model",
                IOUtils.normalizeLexiconName(new File(posModelPath).getName()));
        posTagClassFeatureElement.setAttribute("range", posModelRange);
        generators.addContent(posTagClassFeatureElement);
        System.err.println("-> POSTagModel Features added!");
    }
    if (Flags.isLemmaModelFeatures(params)) {
        final String lemmaModelPath = Flags.getLemmaModelFeatures(params);
        final Element lemmaClassFeatureElement = new Element("custom");
        lemmaClassFeatureElement.setAttribute("class", DocLemmaModelFeatureGenerator.class.getName());
        lemmaClassFeatureElement.setAttribute("model",
                IOUtils.normalizeLexiconName(new File(lemmaModelPath).getName()));
        generators.addContent(lemmaClassFeatureElement);
        System.err.println("-> LemmaModel Features added!");
    }
    if (Flags.isLemmaDictionaryFeatures(params)) {
        final String lemmaDictPath = Flags.getLemmaDictionaryFeatures(params);
        final String[] lemmaDictResources = Flags.getLemmaDictionaryResources(lemmaDictPath);
        final Element lemmaClassFeatureElement = new Element("custom");
        lemmaClassFeatureElement.setAttribute("class", DocLemmaDictionaryFeatureGenerator.class.getName());
        lemmaClassFeatureElement.setAttribute("model",
                IOUtils.normalizeLexiconName(new File(lemmaDictResources[0]).getName()));
        lemmaClassFeatureElement.setAttribute("dict",
                IOUtils.normalizeLexiconName(new File(lemmaDictResources[1]).getName()));
        generators.addContent(lemmaClassFeatureElement);
        System.err.println("-> LemmaDictionary Features added!");
    }
    final XMLOutputter xmlOutput = new XMLOutputter();
    final Format format = Format.getPrettyFormat();
    xmlOutput.setFormat(format);
    return xmlOutput.outputString(doc);

}