Versions Compared

Key

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

...

Register the importer in the application plugin

Wiki Markup
*\[TODO\]* Fixme

Exercise results

Wiki Markup
*\[TODO\]* Description of the exercise results
\\
\\
\\
\\
\\

1. Add a new folder to the project named "Importers"

2. Create a new class named "WaterML2TimeSeriesImporter"

3. Add the following contents to this class:

Register the importer in the application plugin by adding the following code to VolumeModelApplicationPlugin.cs:

Code Block

using DeltaShell.Plugin.DemoApp.Importers;

and

Code Block
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
{
    public class WaterML2TimeSeriesImporter : IFileImporter
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(WaterML2TimeSeriesImporter));

        public string Name
        {
            get { return "WaterML2 time series importer"; }
        }

        public string Category
        {
            get { return "DemoApp importers"; }
        }

        public Bitmap Image
        {
            get { return new Bitmap(16, 16);}
        }

        public IEnumerable<Type> SupportedItemTypes
        {
            get { yield return typeof(TimeSeries); }
        }

        public bool CanImportOnRootLevel
        {
            get { return true; }
        }

        public string FileFilter
        {
            get { return "WaterML2 files|*.XML"; }
        }

        public string TargetDataDirectory { get; set; }

        public bool ShouldCancel { get; set; }

        public ImportProgressChangedDelegate ProgressChanged { get; set; }

        public object ImportItem(string path, object target = null)
        {
            // Check the file path
            if (!File.Exists(path))
            {
                log.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)
            {
                log.Error("Target is of the wrong type (should be time series)");

                return null;
            }

            // Load the 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);
        public override       var value = double.Parse(measurement.Elements().First(e => e.Name.LocalName == "value").Value);

IEnumerable<IFileImporter> GetFileImporters()
        {
         timeSeries[time] = value;
 yield return new WaterML2TimeSeriesImporter();
        }

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

Delta Shell should now be able to find the exporter when it comes to importing data on new or existing time series objects.

Exercise results

Wiki Markup
*\[TODO\]* Description of the exercise results
\\
\\
\\
\\
\\

4. Register the importer in the application plugin class by adding the following code to DemoAppApplicationPlugin:

...