48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
import csv
|
||
from rdflib import Graph, URIRef, Literal, Namespace
|
||
from rdflib.namespace import RDF, RDFS, XSD
|
||
|
||
# Define namespaces
|
||
SINCERE = Namespace("http://sincere.org/s1#")
|
||
graph = Graph()
|
||
graph.bind("sincere", SINCERE)
|
||
graph.bind("xsd", XSD)
|
||
|
||
# Create initial triples for the station
|
||
station_uri = URIRef(SINCERE + "davis2")
|
||
graph.add((station_uri, RDF.type, SINCERE.Station))
|
||
graph.add((station_uri, RDFS.label, Literal("davis 2", datatype=XSD.string)))
|
||
graph.add((station_uri, SINCERE.barometerUnit, Literal("hPa", datatype=XSD.string)))
|
||
graph.add((station_uri, SINCERE.temperatureUnit, Literal("ºC", datatype=XSD.string)))
|
||
graph.add((station_uri, SINCERE.humidityUnit, Literal("H%", datatype=XSD.string)))
|
||
graph.add((station_uri, SINCERE.radiationUnit, Literal("Wh/m²", datatype=XSD.string)))
|
||
|
||
# Read the CSV file and generate barometer triples
|
||
csv_file = 'barometer.csv' # Replace with your CSV file path
|
||
with open(csv_file, newline='') as csvfile:
|
||
reader = csv.DictReader(csvfile)
|
||
|
||
# Print the headers to check what's inside
|
||
print("CSV Headers:", reader.fieldnames)
|
||
|
||
for i, row in enumerate(reader, start=1):
|
||
# Normalize column names by stripping spaces and lowercasing them
|
||
row = {key.strip().lower(): value.strip() for key, value in row.items()}
|
||
|
||
print(row)
|
||
|
||
barometer_uri = URIRef(SINCERE + f"davis2/barometer/{i}")
|
||
|
||
# Add the barometer triples
|
||
graph.add((station_uri, SINCERE.hasBarometer, barometer_uri))
|
||
graph.add((barometer_uri, RDF.type, SINCERE.Barometer))
|
||
graph.add((barometer_uri, SINCERE.value, Literal(row["barometer"], datatype=XSD.integer)))
|
||
graph.add((barometer_uri, SINCERE.timestamp, Literal(row["ο»ώtime"], datatype=XSD.string)))
|
||
|
||
|
||
# Save the RDF triples to a Turtle file
|
||
output_file = 'barometer-translated.ttl'
|
||
graph.serialize(destination=output_file, format='turtle')
|
||
|
||
print(f"RDF triples saved to {output_file}")
|