Skip to content

Examples

Basic

Initiate API client and fetch all available campaigns.

1
2
3
4
5
6
7
8
"""
Basic example showing how to initiate the client and list available model test campaigns.
"""
from modeltestsdk import Client


client = Client()
campaigns = client.campaign.get()

Create campaign

Add a campaign to the database, using datetime to properly handle the date. Creating a campaign needs the Admin role; the import pipeline uses client.campaign.upsert instead.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
"""
Simple create example
"""
from datetime import datetime
from modeltestsdk import Client

client = Client()
campaign = client.campaign.create(name="Campaign name",
                                  description="Campaign description",
                                  date=datetime(year=2000, month=1, day=1).isoformat(),
                                  location="Test location",
                                  scale_factor=52,
                                  water_depth=300,
                                  read_only=True)

Get campaign

This example will filter by campaign name and description and sort ascending by date. The filter object enables use of Pythonic syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
"""
Simple example using the sort and filter objects
"""
from modeltestsdk import Client
client = Client()

campaigns = client.campaign.get(filter_by=[
     client.filter.campaign.name == "Campaign name",
     client.filter.campaign.description == "Campaign description"],
     sort_by=[client.sort.campaign.date.asc])

Note

Filtering and sort arguments needs to be contained inside a list. Sorting will be done in the order the arguments are given in the list

Data and statistics

A timeseries' data over its default window or all of it, and its statistics: stored over the default window, or computed on request over a custom window.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
"""
A timeseries' data and statistics
"""
from modeltestsdk import Client

with Client() as client:
    campaign = client.campaign.get_by_name("Bream FPSO").scalar()
    test = client.test.get_by_number("3010").filter(campaign_id=campaign.id).scalar()
    ts = test.timeseries()[0]

    window = ts.get_data()                # the default window
    record = ts.get_data(all_data=True)   # the whole record
    stats = ts.get_statistics()           # stored statistics over the default window
    custom = ts.get_statistics(start=2000.0, end=4000.0)  # computed over a custom window, not stored
    print(ts.sensor.name, stats.max, custom.max)

Documents

A campaign's reports and drawings, listed and downloaded.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
"""
A campaign's documents
"""
from modeltestsdk import Client

with Client() as client:
    campaign = client.campaign.get_by_name("Bream FPSO").scalar()
    for doc in campaign.documents():
        print(doc.document_class, doc.name, f"{doc.size_bytes / 1e6:.1f} MB")
    report = next(d for d in campaign.documents() if d.document_class == "Main report")
    path = campaign.download_document(report.path, ".")  # saved under its own name