The script below converts the given csv file to a CIF monitor file.
1import pandas as pd
2import numpy as np
3from pycif.utils.datastores.dump import dump_datastore, read_datastore
4from pycif.utils.datastores.empty import init_empty
5
6# Initializes an empty datastore
7ds = init_empty()
8
9# Read original CSV file
10file_csv = "csv_obs.csv"
11column_names = ['stat', 'date', 'tstep', 'i', 'j',
12 'obs', 'sim', 'sim_tl', 'obserror', 'level']
13df = pd.read_csv(file_csv, delimiter=' ', header=None,
14 names=column_names, index_col=False)
15
16# Parse date strings
17df["date"] = pd.to_datetime(df['date'].astype(str), format='%Y%m%d%H')
18
19# Split observation names
20df["station"] = df["stat"].str.split('_', expand=True)[0]
21df["parameter"] = df["stat"].str.split('_', expand=True)[1].str.lower()
22
23# Fill the output datastore
24column2save = ['station', 'parameter', 'date', 'tstep', 'i', 'j',
25 'obs', 'sim', 'sim_tl', 'obserror', 'level']
26ds.loc[:, column2save] = df.loc[:, column2save]
27
28# Dump the datastore
29dump_datastore(ds, "monitor.nc")
30
31# Checking the consistency of the datastore when reading
32ds2 = read_datastore("monitor.nc")
33