Skip to main content

A library for reading and writing XML files to interact with Delft-FEWS.

Project description

fewsxml

A library to read and write XML files to interact with Delft-FEWS.

Installation

pip install fewsxml

Overview

fewsxml provides two data structures:

  • FXTimeseries
  • FXData

To read and write data from/to XML files with the FEWS PI standard, one needs to be familiar with these two data structures.

Reading Procedure

Reading data from an XML file consists of two steps: first, create an instance of FXData, and second, call the function read_xml. Here is an example:

import fewsxml as fx

data = fx.FXData({
    "inputFilePath": "timeseries_export.xml"
})
data_in_xml = fx.read_xml(data)

The FXData structure contains many fields, but for reading data, only the inputFilePath field needs to be filled. As a result of a successful read operation, the FXData instance is populated with relevant information. Most importantly, FXData contains a list of FXTimeseries, called timeseries, where each element represents one timeseries. For example, a list of timeseries with the parameterId of paramId1 can be retrieved by:

tss = [timeserie for timeserie in data["timeseries"] if timeserie["parameterId"] == "paramId1"]

Writing Procedure

The function write_xml is used for writing an XML file with the FEWS PI standard. Similar to the reading procedure, an instance of FXData must be created. However, the required fields in this case are timeseries and outputFilePath. The outputFilePath should indicate the location where the XML file will be created. The timeseries field is a list of FXTimeseries instances, where each instance represents a timeseries to be written into the XML file. The required fields in each FXTimeseries instance are:

  • locationId: The location ID of the timeseries.
  • parameterId: The parameter ID of the timeseries.
  • timesteps: A list of datetime objects, where each element represents a timestep in the timeseries.
  • values: A list of values for each timestep in the timeseries.
  • timeStepSize: The constant interval between consecutive sample times in timesteps.
  • startDateTime: The start date and time of the timeseries.
  • endDateTime: The end date and time of the timeseries.
  • flags (optional): A list of flags for each element of the timeseries.

Here is an example of how to write a timeseries:

import os.path
import fewsxml as fx
from datetime import datetime, timedelta

def _create_datetime_list(sDateTime, hDuration, sInterval):
    total_duration_seconds = hDuration * 3600
    num_steps = total_duration_seconds // sInterval
    # Create the list of datetimes
    datetime_list = [sDateTime + timedelta(seconds=i * sInterval) for i in range(int(num_steps) + 1)]
    return datetime_list

sDateTime = datetime(2025, 4, 10, 14, 0, 0)  # Start datetime
hDuration = 2  # Duration in hours
sInterval = 600  # Interval in seconds (e.g., 600 seconds = 10 minutes)

datetime_list = _create_datetime_list(sDateTime, hDuration, sInterval)

timeseries1 = fx.FXTimeseries({
    "locationId": "testLoc1",
    "parameterId": "paramId1",
    "timesteps": datetime_list,
    "values": [0] * len(datetime_list),
    "timeStepSize": sInterval,
    "startDateTime": datetime_list[0],
    "endDateTime": datetime_list[-1]
})
timeseries2 = fx.FXTimeseries({
    "locationId": "testLoc2",
    "parameterId": "paramId2",
    "timesteps": datetime_list,
    "values": [1.1] * len(datetime_list),
    "timeStepSize": sInterval,
    "startDateTime": datetime_list[0],
    "endDateTime": datetime_list[-1]
})

data = fx.FXData({
    "timeseries": [timeseries1, timeseries2],
    "outputFilePath": os.path.join("timeseries_export.xml")
})
fx.write_xml(data)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fewsxml-0.1.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fewsxml-0.1.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file fewsxml-0.1.2.tar.gz.

File metadata

  • Download URL: fewsxml-0.1.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for fewsxml-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9f758db14908d6fa2b0b74af316d729fdd83c473057dd5ad79c433338f35322a
MD5 b9c4fdd5a1366bf1dd7e87b64144313c
BLAKE2b-256 d72ac25d083b600e033758e1b906c6815126ae66d1d83ed533100003640df501

See more details on using hashes here.

File details

Details for the file fewsxml-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fewsxml-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for fewsxml-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bac2a9e222acbc39913438bbc40b3fc5c5f0a5b86a50cb8441213aaff774a8d4
MD5 6e58b0838fe03b4b146a7cc9174b9404
BLAKE2b-256 ac3cb54dcfc9ad344078217680ab3702a87d4ab8cbcd54d76e2b37ba76c87e0f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page