Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using DelftTools.Functions;
using DelftTools.Functions.Generic;
using DelftTools.Shell.Core;
using log4net;

namespace DeltaShell.Plugin.DemoApp.Importers
{
    /// <summary>
    /// Importer for importing WaterML2 data to time series objects
    /// </summary>
    public class WaterML2TimeSeriesImporter : IFileImporter
    {
        private static readonly ILog logLog = LogManager.GetLogger(typeof(WaterML2TimeSeriesImporter)); // Handle for writing log messages

        /// <summary>
        /// The name of the importer
        /// </summary>
        public /// <remarks>Used in importer selection dialogs</remarks>
        public string Name
        {
            get { return "WaterML2 time series importer"; }
        }

        /// <summary>
        /// The category of the importer
        /// </summary>
        /// <remarks>Used in importer selection dialogs</remarks>
        public string Category
        {
            get { return "DemoAppVolume model importers"; }
        }

        /// <summary>
        /// The image of the importer
        /// </summary>
        public/// Bitmap<remarks>Used Image
in importer selection dialogs</remarks>
     {
   public Bitmap Image
        {
            get { return new Bitmap(16, 16);}
        }

        /// <summary>
        /// The data types supported by the importer
        /// </summary>
        public IEnumerable<Type> SupportedItemTypes
        {
            get { yield return typeof(TimeSeries); }
        }

        /// <summary>
        /// Indicates whetherthat or not the importer can import at root level (folder/project). In other
        /// If truewords, theindicates importerthat willthe always show up in the project->import list. If<see cref="ImportItem"/> method can be called without
        /// false this importer can only be retrieved by supported type, eg, in code.
        /// Use false for partial/composite importers and importers called from map tools
        /// etc. If true, the importer is assumed to support both "new" and "into" modes.
        /// </summary>
        public bool CanImportOnRootLevel
        {
            get { return true; }
        }

        /// <summary>
        /// The file filter of the importer
        /// </summary>
        /// <example>
        /// "My file format1 (*.ext1)|*.ext1|My file format2 (*.ext2)|*.ext2"specifying a time series target... 
        /// </example>summary>
        public stringbool FileFilterCanImportOnRootLevel
        {
            get { return "WaterML2 files|*.XML"true; }
        }

        /// <summary>
        /// PathThe where external data files can be copied intofile filter of the importer
        /// </summary>
        /// <remarks>Used <remarks>
in file selection dialogs</remarks>
     /// Optional, used onlypublic whenstring externalFileFilter
 files need to be copied into the project{
 "as is"
        /// </remarks> 
get { return "WaterML2 files|*.XML"; }
   public string TargetDataDirectory { get; set; }

        /// <summary>
        /// WhetherPath orwhere notexternal andata importfiles taskcan shouldbe becopied cancelledinto
        /// </summary>
        /// <remarks> <remarks>Not relevant in this tutorial</remarks> 
        ///public Thisstring propertyTargetDataDirectory must{ beget; observed by the importer (thread-safe); when it is true the set; }

        /// <summary>
        /// importerWhether mustor stopnot currentan import task should be cancelled
        /// </summary>
        /// <remarks>Not part of this tutorial</remarks> 
        public bool ShouldCancel { get; set; }

        /// <summary>
        /// Fired when progress has been changed
        /// </summary>
        /// </summary> <remarks>Not part in this tutorial</remarks> 
        public ImportProgressChangedDelegate ProgressChanged { get; set; }

        /// <summary>
        /// Imports WaterML2 data from the file with path <paramref name="path"/> to target the 
        /// time series <paramref name="target"/>
        /// </summary>
        /// <remarks>
        /// The target parameter is optional. If a target time series is specified, the
        /// importer should import
        /// new data into the target. If the WaterML2 data to this existing time series. When
        //  no target is specifiedset, the importer should importcreate the
a new time series during    /// data into a new objectthe import.
        /// </remarks>
        public object ImportItem(string path, object target = null)
        {
            // Check the file path
            if (!File.Exists(path))
            {
                logLog.Error("File does not exist");

                return null;
            }

            // Obtain a new time series or check the provided target for being a time series
            var timeSeries = target == null
                ? new TimeSeries { Name = Path.GetFileNameWithoutExtension(path), Components = { new Variable<double>() } }
                : target as TimeSeries;

            if (timeSeries == null)
            {
                logLog.Error("Target is of the wrong type (should be time series)");

                return null;
            }

            // Load the WaterML2 XML document
            var doc = XDocument.Load(path);

            // Obtain the document elements
            var xElements = doc.Descendants();

            // Obtain the measurement TVP tags
            var measurements = xElements.Where(element => element.Name.LocalName == "MeasurementTVP");

            // Get the corresponding time and value for each measurement tag
            foreach (var measurement in measurements)
            {
                var time = DateTime.Parse(measurement.Elements().First(e => e.Name.LocalName == "time").Value);
                var value = double.Parse(measurement.Elements().First(e => e.Name.LocalName == "value").Value);

                timeSeries[time] = value;
            }

            // Return the time series
            return timeSeries;
        }
    }
}
Info

The derives...
Verder geldt; the comments should complain the different parts of the importer code.

Fixme: Info on the code above.

...