Skip to content

.NET API Cheat Sheet

This page contains short code snippets (C#) that demonstrate usage of the .NET API of the Domain Services.

Time Series

NuGet package: DHI.Services.TimeSeries

There are 5 different types of time series services:

  • TimeSeriesService<TId, TValue>: a basic time series service
  • DiscreteTimeSeriesService<TId, TValue>: a discrete time series service with a finite number of time series
  • GroupedDiscreteTimeSeriesService<TId, TValue>: a discrete time series service with a finite number of time series. The time series are organized in hierarchical groups.
  • UpdatableTimeSeriesService<TId, TValue>: a discrete time series service with a finite number of time series. New time series can be added, removed and updated.
  • GroupedUpdatableTimeSeriesService<TId, TValue>: a discrete time series service with a finite number of time series. The time series are organized in hierarchical groups. New time series can be added, removed and updated.

The time series services are all generic types. The first generic type parameter TId is the type of the time series ID (e.g. string) while the second generic type parameter TValue is the type of the time series values (e.g. float).

Initializing a time series service

A specific time series repository instance is injected into a time series service.

// Dfs0
var timeSeriesRepositoryDfs0 = new DHI.Services.Provider.MIKE.Dfs0TimeSeriesRepository(filePath);
var timeSeriesServiceDfs0 = new DiscreteTimeSeriesService<string, double>(timeSeriesRepositoryDfs0);

// DIMS.CORE
var timeSeriesRepositoryDims = new DHI.Services.Provider.DIMS.TimeSeriesRepository(token);
var timeSeriesServiceDims = new DiscreteTimeSeriesService<string, double>(timeSeriesRepositoryDims);

// MIKE OPERATIONS
var timeSeriesRepositoryMc = new DHI.Services.Provider.MC.TimeSeriesRepository(connectionString);
var timeSeriesServiceMc = new DiscreteTimeSeriesService<string, double>(timeSeriesRepositoryMc);

Retrieving time series values

var values = timeSeriesServiceDfs0.GetValues("PWD", from, to);

Retrieving last time series value

var lastValue = timeSeriesServiceDfs0.GetLast("PWD");

Calculate moving average (asynchronously)

Various time series data analysis are implemented - e.g. calculation of moving average:

var timeSeriesData = timeSeriesService.GetValues("MyTimeSeries");
var movingAverage = await Task.Run(() => timeSeriesData.MovingAverage(20));

Time series repository providers

Currently, the following time series repository providers are available:

  • dfs0, dfs2, dfsu (NuGet package DHI.Services.MIKECore)
  • DIMS.CORE (NuGet package DHI.Services.DIMS)
  • CSV (NuGet package DHI.Services)
  • MIKE OPERATIONS (NuGet package DHI.Services.MCLite)
  • ADO.NET (NuGet package DHI.Services.ADONET)
  • WaterForecast (NuGet package DHI.Services.WaterForecast)
  • US Geological Survey (NuGet package DHI.Services.HTTP)

Feature Collections

NuGet package: DHI.Services.GIS

There are 2 types of feature collection services:

  • FeatureCollectionService<TId>: a service for retrieving and querying geometric feature collections.
  • GroupedFeatureCollectionService<TId>: a service for retrieving and querying geometric feature collections. The feature collections are organized in hierarchical groups.

The feature collection services are generic types. The generic type parameter TId is the type of the feature collection ID (e.g. guid).

Initializing a feature collection service

A specific feature collection repository instance is injected into a feature collection service.

// Shapefile
var featureRepository = new DHI.Services.Provider.Shapefile.FeatureRepository(path);
var gisService = new GisService<string>(featureRepository);

Retrieving features meeting a specifc attribute criteria:

var query = new List<QueryCondition> { new QueryCondition("City", QueryOperator.Equal, "Copenhagen") };
var featureCollection = gisService.Get("stations.shp", query);

Feature collection repository providers:

Currently, the following feature collection repository providers are available:

  • Shapefile (NuGet package DHI.Services.Shapefile)
  • DIMS.CORE (NuGet package DHI.Services.DIMS)
  • MIKE OPERATIONS (NuGet package DHI.Services.Provider.MCLite)

Spreadsheets

NuGet package: DHI.Services.Spreadsheets

The spreadsheet service SpreadsheetService<TId> is a generic type. The generic type parameter TId is the type of the spreadsheet ID (e.g. string).

Initializing a spreadsheet service

A specific spreadsheet repository instance is injected into a spreadsheet service.

// Excel
var spreadsheetRepository = new DHI.Services.Provider.OpenXML.SpreadsheetRepository(rootFolder);
var spreadsheetService = new SpreadsheetService<string>(spreadsheetRepository);

Retrieving all data in a particular sheet:

var data = spreadsheetService.GetUsedRange("sample.xlsx", "sheet2");

Retrieving named data in a particular sheet:

var data = spreadsheetService.GetNamedRange("sample.xlsx", "sheet2", "2016");

Saving a data table as a MS Excel spreadsheet

Using the OpenXml provider, data tables can be directly saved as MS Excel files:

var dataSet = new DataSet();
dataSet.Tables.Add(dataTable.Copy());
dataSet.SaveAsXlsx(filePath, false);   

Spreadsheet repository providers:

Currently, the following spreadsheet repository providers are available:

  • Microsoft Excel (NuGet package DHI.Services.OpenXml)
  • MIKE OPERATIONS (NuGet package DHI.Services.MCLite)

Tables

NuGet package: DHI.Services.Tables

Initializing a table service

A specific table repository instance is injected into a table service.

var tableRepository = new DHI.Services.Provider.ODBC.TableRepository(connectionString);
var tableService = new TableService<string>(tableRepository);

Retrieving a list of tables:

var listOfTables = tableService.GetIds().ToList();

Retrieving all data from a particular table as a DataTable object:

var dataTable = tableService.GetData(id).ToDataTable();

Retrieving filtered data:

var filter = new List<QueryCondition> { new QueryCondition("WaterLevel", QueryOperator.GreaterThan, 23.1) };
var data = tableService.GetData(id, filter);

Table repository providers:

Currently, the following table repository providers are available:

  • ODBC (NuGet package DHI.Services.ODBC)

Maps

There are 2 types of map-realated services:

  • MapService: a service for retrieving bitmap images - e.g. to be used as map overlays.
  • MapStyleService: a service for managing map styles (map color palettes).

Initializing a map style service

A specific map style repository instance is injected into a map style service.

var mapStyleRepository = new MapStyleRepository(Path.Combine(binFolder, "styles.json"));
var mapStyleService = new MapStyleService(mapStyleRepository);

Create a map style

var mapStyle = new MapStyle("MyStyle", "My Style");
mapStyle.StyleCode = "0^10:#800080,#5500AB,#2A00D5,#0000FF,#FF5500,#FF0000...";
mapStyleService.Add(mapStyle);

Initializing a map service

A specific map source instance and a map style service instance is injected into a map service:

var mapSource = new Dfs2MapSource("R20141001.dfs2", new Parameters());
var mapService = new MapService(mapSource, mapStyleService);

Retrieving a map image

var image = mapService.GetMap("MyStyle", "EPSG:3857", BoundingBox.Parse("11584184.510675031,78271.51696402066,11623320.26915704,117407.27544603013"), 256, 256, new Parameters());

Map source providers:

Currently, the following map source providers are available:

  • dfs2, dfsu (NuGet package DHI.Services.MIKECore)
  • NetCDF (NuGet package DHI.Services)

Radar Images

NuGet package: DHI.Services.Rasters

The radar image service RadarImageService<TImage> is a generic type. The generic type parameter TImage is the type of radar image (e.g. X00.RadarImage).

Read a DHI LAWR P00 radar image from file and convert the pixel values to rain intensity

var image = RadarImage.CreateNew("AROS1245.p00")).ToIntensity();

Read a EUMETNET ODIM HDF5 CAPPI radar image from file

var image = H5CRadarImage.CreateNew("comp_max201008180310.h5"));

Create a bitmap image of a radar image using a specific color gradient

var bitmap = image.ToBitmap(colorGradient);

Initializing a radar image service

A specific radar image repository instance is injected into a radar image service.

var repository = new RadarImageRepository<RadarImage>(token, timeSeriesName);
var radarImageService = new RadarImageService<RadarImage>(repository);

Initializing a zone service

A specific zone repository instance is injected into a zone service.

var zoneRepository = new ZoneRepository("zones.json"));
var zoneService = new ZoneService(zoneRepository);

Retrieving zones

var zones = zoneService.GetAll();
var zone = zoneService.Get("MyZone");

Retrieving last available image in radar image repository

var lastImage = radarImageService.Last;

Calculate the accumulated depth in the given zone within the last 24 hours

var to = radarImageService.LastDateTime;
var from = to.AddHours(-24);
var depth = radarImageService.GetDepth(zone, from, to);

Radar Image providers:

Currently, the following radar image providers are available:

  • P00, I00 (NuGet package DHI.Services.Rasters)
  • IRIS CAPPI (NuGet package DHI.Services.Rasters)
  • ESRIASCII (NuGet package DHI.Services.Rasters)
  • DELIMITEDASCII (NuGet package DHI.Services.Rasters)
  • EUMETNET ODIM HDF5 CAPPI (NuGet package DHI.Services.HDF5)

Radar Image repository providers:

Currently, the following radar image repository providers are available:

  • DIMS.CORE (NuGet package DHI.Services.DIMS)
  • Generic file based (NuGet package DHI.Services.Rasters)

Units

NuGet package: DHI.Services.Physics

Custom units can be created. Units can be created from multiplication or division of earlier created units. The units can be registered in a unit repository. Unit conversion is available for compatible units (with equal dimensions).

Create some units

var meter = new Unit("m", "meter", Dimension.Length);
var kilometer = new Unit("km", "kilometer", 1000 * meter);
var second = new Unit("s", "second", Dimension.Time);
var hour = new Unit("h", "hour", 60 * 60 * second);

Convert a value from one unit to another

const int speedInMeterPrSecond = 10;
var speedInKilometerPerHour = Unit.Convert(speedInMeterPrSecond, meter / second, kilometer / hour);

Initializing a unit service

A specific unit repository instance is injected into a unit service.

var unitRepository = new UnitRepository("units.json"));
var unitService = new UnitService(unitRepository);

Create and register some units

var meterPerSecond = new Unit("m/s", "meter/second", meter / second);
var kilometerPerHour = new Unit("km/h", "kilometer/hour", kilometer / hour);
unitService.Add(meterPerSecond);
unitService.Add(kilometerPerHour);

Convert a value using some registered units

const int speedInMeterPrSecond = 10;
speedInKilometerPerHour = unitService.Convert(speedInMeterPrSecond, "m/s", "km/h");

Register some custom conversion functions for temperature units

unitService.RegisterConversion("F", "C", d => (d - 32.0) * 5.0 / 9.0);
unitService.RegisterConversion("C", "F", d => d * 9.0 / 5.0 + 32.0);

Convert a temperature value

const double temperatureInFahrenheit = 68;
var temperatureInCelcius = unitService.Convert(temperatureInFahrenheit, "F", "C");