Skip to content

Instantly share code, notes, and snippets.

@joAschauer
Last active May 4, 2022 08:03
Show Gist options
  • Save joAschauer/cb61dbf20bd48690fe3341a89be127b4 to your computer and use it in GitHub Desktop.
Save joAschauer/cb61dbf20bd48690fe3341a89be127b4 to your computer and use it in GitHub Desktop.
Read .smet file as pd.Dataframe. quick and dirty solution to read timeseries from a .smet file as pd.Dataframe. Assumes that a 'timestamp' field is present in the .smet file. The function has not been tested on many files. Use on your own responsibility.
import configparser
from io import StringIO
import pandas as pd
def read_smet(file):
"""
Read a .smet file as pd.Dataframe
Parameters
----------
file : str or pathlike
Returns
-------
pd.Dataframe
"""
with open(file, 'r') as f:
filestr = f.read()
headerstr = f"[HEADER]{(filestr.split('[HEADER]'))[1].split('[DATA]')[0]}"
header = configparser.ConfigParser()
header.read_string(headerstr)
header = dict(header['HEADER'])
datastr = filestr.split('[DATA]')[-1]
data = pd.read_csv(
StringIO(datastr),
sep='\s+',
names=[c for c in header['fields'].split(' ')],
parse_dates=['timestamp'],
na_values=float(header['nodata'])
)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment