<commit_before>/*
 * Copyright 1997-2008 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * <EMAIL>.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package ucar.nc2.thredds;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import thredds.catalog.*;
import ucar.nc2.dt.DataIterator;
import ucar.nc2.dt.RadialDatasetSweep;
import ucar.nc2.dt.StationImpl;
import ucar.nc2.dt.radial.StationRadarCollectionImpl;
import ucar.nc2.units.DateUnit;
import ucar.unidata.geoloc.LatLonPointImpl;
import ucar.unidata.geoloc.LatLonRect;
import ucar.unidata.util.DateSelection;
import ucar.unidata.util.DateUtil;
import ucar.unidata.util.DatedThing;
import ucar.unidata.util.Product;

import java.io.IOException;
import java.net.URI;
import java.util.*;

/**
 * Client side for getting data from the TDS radar server.
 * @author yuan
 */
public class TDSRadarDatasetCollection extends StationRadarCollectionImpl {

    //////////////////////////////////////////////////////////////////////////////////

    // private InvDataset ds;


    /** _more_ */
    protected HashMap stationHMap;

    /** _more_ */
    private List radarTimeSpan;

    /** _more_ */
    private LatLonRect radarRegion;

    /** _more_ */
    private String dsc_location;

    /** _more_ */
    private List radarProducts;

    /** _more_ */
    private String summary;

    /** _more_          */
    protected static final Namespace defNS =
        Namespace.getNamespace(
            thredds.catalog.XMLEntityResolver.DQC_NAMESPACE_04);


    /** _more_          */
    private URI docURI;

    /**
     * tds radar dataset collection  factory
     *
     * @param ds _more_
     * @param dsc_location _more_
     * @param errlog _more_
     *
     * @return any foctory
     *
     * @throws java.io.IOException _more_
     *
     * @throws IOException _more_
     * @throws java.net.URISyntaxException _more_
     */

    static public TDSRadarDatasetCollection factory(InvDataset ds,
            String dsc_location,
            StringBuffer errlog) throws IOException,
                                        java.net.URISyntaxException {

        // URI catalogURI = new URI(dsc_location);
        //this.docURI =    catalogURI;
        return factory(ds.getDocumentation("summary"), dsc_location, errlog);
    }

    /**
     *  tds radar dataset collection  factory
     *
     * @param desc _more_
     * @param dsc_location _more_
     * @param errlog _more_
     *
     * @return dataset collection
     *
     * @throws IOException _more_
     */
    static public TDSRadarDatasetCollection factory(String desc,
            String dsc_location, StringBuffer errlog) throws IOException {
        // super();
        SAXBuilder        builder;
        Document          doc  = null;
        XMLEntityResolver jaxp = new XMLEntityResolver(true);
        builder = jaxp.getSAXBuilder();

        try {
            doc = builder.build(dsc_location);
        } catch (JDOMException e) {
            errlog.append(e.toString());
        }

        Element   qcElem = doc.getRootElement();
        Namespace ns     = qcElem.getNamespace();


        return new TDSRadarDatasetCollection(desc, dsc_location, qcElem, ns,
                                             errlog);
    }

    /**
     * initiate a new TDS radar dataset collection object
     *
     * @param desc description
     * @param dsc_location location of dataset
     * @param elem dataset root element
     * @param ns dataset namespace
     * @param errlog error log
     *
     * @throws IOException _more_
     */
    private TDSRadarDatasetCollection(
            String desc, String dsc_location, Element elem, Namespace ns,
            StringBuffer errlog) throws IOException {
        super();
        Element serviceElem = readElements(elem, "service");
        Element dsElem      = readElements(elem, "dataset");

        Element metaElem    = readElements(dsElem, "metadata");
        // HashMap stationHMap = readSelectStations(metaElem, ns);
        String sts = dsc_location.replaceFirst("dataset.xml", "stations.xml");
        HashMap    stationHMap   = readRadarStations(sts);
        LatLonRect radarRegion   = readSelectRegion(metaElem, ns);
        List       radarTimeSpan = readSelectTime(metaElem, ns);
        List       productList   = readSelectVariable(metaElem, ns);
        String     summary       = readSelectDocument(metaElem, ns);

        // gotta have these
        if (stationHMap == null) {
            errlog.append(
                "TDSRadarDatasetCollection must have station selected");
            return;
        }
        if (radarRegion == null) {
            errlog.append(
                "TDSRadarDatasetCollection must have region selected");
            return;
        }
        if (radarTimeSpan == null) {
            errlog.append(
                "TDSRadarDatasetCollection must have time span selected");
            return;
        }

        this.desc          = desc;
        this.dsc_location  = dsc_location;
        this.radarProducts = productList;
        this.summary       = summary;
        this.stationHMap   = stationHMap;
        this.radarRegion   = radarRegion;
        this.radarTimeSpan = radarTimeSpan;
        this.startDate =
            DateUnit.getStandardOrISO((String) radarTimeSpan.get(0));
        this.endDate =
            DateUnit.getStandardOrISO((String) radarTimeSpan.get(0));

        try {
            timeUnit = new DateUnit("hours since 1991-01-01T00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * retrieve all radar stations in this dataset collection
     *
     *<commit_msg>time span end time bug fixed<commit_after>/*
 * Copyright 1997-2008 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * <EMAIL>.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package ucar.nc2.thredds;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import thredds.catalog.*;
import ucar.nc2.dt.DataIterator;
import ucar.nc2.dt.RadialDatasetSweep;
import ucar.nc2.dt.StationImpl;
import ucar.nc2.dt.radial.StationRadarCollectionImpl;
import ucar.nc2.units.DateUnit;
import ucar.unidata.geoloc.LatLonPointImpl;
import ucar.unidata.geoloc.LatLonRect;
import ucar.unidata.util.DateSelection;
import ucar.unidata.util.DateUtil;
import ucar.unidata.util.DatedThing;
import ucar.unidata.util.Product;

import java.io.IOException;
import java.net.URI;
import java.util.*;

/**
 * Client side for getting data from the TDS radar server.
 * @author yuan
 */
public class TDSRadarDatasetCollection extends StationRadarCollectionImpl {

    //////////////////////////////////////////////////////////////////////////////////

    // private InvDataset ds;


    /** _more_ */
    protected HashMap stationHMap;

    /** _more_ */
    private List radarTimeSpan;

    /** _more_ */
    private LatLonRect radarRegion;

    /** _more_ */
    private String dsc_location;

    /** _more_ */
    private List radarProducts;

    /** _more_ */
    private String summary;

    /** _more_          */
    protected static final Namespace defNS =
        Namespace.getNamespace(
            thredds.catalog.XMLEntityResolver.DQC_NAMESPACE_04);


    /** _more_          */
    private URI docURI;

    /**
     * tds radar dataset collection  factory
     *
     * @param ds _more_
     * @param dsc_location _more_
     * @param errlog _more_
     *
     * @return any foctory
     *
     * @throws java.io.IOException _more_
     *
     * @throws IOException _more_
     * @throws java.net.URISyntaxException _more_
     */

    static public TDSRadarDatasetCollection factory(InvDataset ds,
            String dsc_location,
            StringBuffer errlog) throws IOException,
                                        java.net.URISyntaxException {

        // URI catalogURI = new URI(dsc_location);
        //this.docURI =    catalogURI;
        return factory(ds.getDocumentation("summary"), dsc_location, errlog);
    }

    /**
     *  tds radar dataset collection  factory
     *
     * @param desc _more_
     * @param dsc_location _more_
     * @param errlog _more_
     *
     * @return dataset collection
     *
     * @throws IOException _more_
     */
    static public TDSRadarDatasetCollection factory(String desc,
            String dsc_location, StringBuffer errlog) throws IOException {
        // super();
        SAXBuilder        builder;
        Document          doc  = null;
        XMLEntityResolver jaxp = new XMLEntityResolver(true);
        builder = jaxp.getSAXBuilder();

        try {
            doc = builder.build(dsc_location);
        } catch (JDOMException e) {
            errlog.append(e.toString());
        }

        Element   qcElem = doc.getRootElement();
        Namespace ns     = qcElem.getNamespace();


        return new TDSRadarDatasetCollection(desc, dsc_location, qcElem, ns,
                                             errlog);
    }

    /**
     * initiate a new TDS radar dataset collection object
     *
     * @param desc description
     * @param dsc_location location of dataset
     * @param elem dataset root element
     * @param ns dataset namespace
     * @param errlog error log
     *
     * @throws IOException _more_
     */
    private TDSRadarDatasetCollection(
            String desc, String dsc_location, Element elem, Namespace ns,
            StringBuffer errlog) throws IOException {
        super();
        Element serviceElem = readElements(elem, "service");
        Element dsElem      = readElements(elem, "dataset");

        Element metaElem    = readElements(dsElem, "metadata");
        // HashMap stationHMap = readSelectStations(metaElem, ns);
        String sts = dsc_location.replaceFirst("dataset.xml", "stations.xml");
        HashMap    stationHMap   = readRadarStations(sts);
        LatLonRect radarRegion   = readSelectRegion(metaElem, ns);
        List       radarTimeSpan = readSelectTime(metaElem, ns);
        List       productList   = readSelectVariable(metaElem, ns);
        String     summary       = readSelectDocument(metaElem, ns);

        // gotta have these
        if (stationHMap == null) {
            errlog.append(
                "TDSRadarDatasetCollection must have station selected");
            return;
        }
        if (radarRegion == null) {
            errlog.append(
                "TDSRadarDatasetCollection must have region selected");
            return;
        }
        if (radarTimeSpan == null) {
            errlog.append(
                "TDSRadarDatasetCollection must have time span selected");
            return;
        }

        this.desc          = desc;
        this.dsc_location  = dsc_location;
        this.radarProducts = productList;
        this.summary       = summary;
        this.stationHMap   = stationHMap;
        this.radarRegion   = radarRegion;
        this.radarTimeSpan = radarTimeSpan;
        this.startDate =
            DateUnit.getStandardOrISO((String) radarTimeSpan.get(0));
        this.endDate =
            DateUnit.getStandardOrISO((String) radarTimeSpan.get(1));

        try {
            timeUnit = new DateUnit("hours since 1991-01-01T00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * retrieve all radar stations in this dataset collection
     *
     *
{'fraction_non_alphanumeric': 0.09558879938626774, 'fraction_numerical': 0.008438818565400843, 'mean_word_length': 2.2155895411938826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4823482', 'n_tokens_mistral': 3704, 'n_tokens_neox': 3579, 'n_words': 1007}