In [38]:
import os.path
import sys
import logging

import pandas
import matplotlib.pyplot as plt
import matplotlib.style
%matplotlib inline

In [39]:
# use this stylesheet
matplotlib.style.use('ggplot')

# get the filename from the first command line argument (0=script, 1=filename)
filename = sys.argv[1]
try:
    assert os.path.isfile(filename), 'First command line argument should be a filename, %s is not a file' % (filename, )
except AssertionError:
    logging.exception("Command line error")
    filename = '/Users/baart_f/data/sobek/Totalarea_h_x1800m.fix'
    #filename = '/Users/baart_f/data/sobek/Discharge_B2.fix'


ERROR:root:Command line error
Traceback (most recent call last):
  File "<ipython-input-39-f2652db55d7a>", line 7, in <module>
    assert os.path.isfile(filename), 'First command line argument should be a filename, %s is not a file' % (filename, )
AssertionError: First command line argument should be a filename, -f is not a file

In [40]:
# read file, assuming multiple space separted columns (no spaces in parameters or locations)
widths = [25, 25, 11, 14, 18, 18, 14]
df = pandas.read_fwf(filename, widths=widths,  skiprows=2, parse_dates=[['Date', 'Time']])

# generate 1 row, 2 columns
fig, axes = plt.subplots(1, 2, figsize=(13, 5.5))

# assuming only 1 parameter, location per file
parameters = set(df['Parameter'])
locations = set(df['Location'])
assert len(locations) == 1, 'Expected 1 location'
assert len(parameters) == 1, 'Expected exactly 1 parameter'
location, parameter = list(locations)[0], list(parameters)[0]

# plot in the two axes
df.plot('Date_Time', ['Diff'], ax=axes[0])
axes[0].set_title(location)
axes[0].set_ylabel(parameter)
axes[0].set_xlabel('time')
df.plot('Date_Time', ['SOBEK2', 'SOBEK3'], ax=axes[1])
axes[1].set_title(location)
axes[1].set_ylabel(parameter)
axes[1].set_xlabel('time')
fig.tight_layout()

# save the figure
outfilename = '{parameter} {location}.png'.format(location=location, parameter=parameter)
fig.savefig(outfilename)



In [41]:
df


Out[41]:
Date_Time Parameter Location SOBEK2 SOBEK3 Diff
0 2015-01-01 00:00:00 Total area h_x1800m 967.1875 967.1875 0.000000
1 2015-01-01 06:00:00 Total area h_x1800m 967.5520 967.5413 0.010681
2 2015-01-01 12:00:00 Total area h_x1800m 1218.2710 1284.5570 -66.285770
3 2015-01-01 18:00:00 Total area h_x1800m 1334.8800 1535.3900 -200.509600
4 2015-01-02 00:00:00 Total area h_x1800m 1665.9720 1820.5570 -154.584700
5 2015-01-02 06:00:00 Total area h_x1800m 1975.2270 1988.8500 -13.623170
6 2015-01-02 12:00:00 Total area h_x1800m 2092.5520 2092.5520 0.000000
7 2015-01-02 18:00:00 Total area h_x1800m 2797.5490 2797.5490 0.000000
8 2015-01-03 00:00:00 Total area h_x1800m 2146.0630 2146.0630 0.000000
9 2015-01-03 06:00:00 Total area h_x1800m 1960.9100 1963.7890 -2.878418
10 2015-01-03 12:00:00 Total area h_x1800m 1881.4780 1890.9520 -9.473755
11 2015-01-03 18:00:00 Total area h_x1800m 1555.1230 1607.7480 -52.625000
12 2015-01-04 00:00:00 Total area h_x1800m 1415.0610 1472.0280 -56.966800
13 2015-01-04 06:00:00 Total area h_x1800m 1213.2470 1279.8680 -66.621580
14 2015-01-04 12:00:00 Total area h_x1800m 728.7654 779.7911 -51.025630
15 2015-01-04 18:00:00 Total area h_x1800m 594.2535 630.2842 -36.030640
16 2015-01-05 00:00:00 Total area h_x1800m 417.9831 431.5209 -13.537810
17 2015-01-05 06:00:00 Total area h_x1800m 243.9587 243.9587 0.000031
18 2015-01-05 12:00:00 Total area h_x1800m 366.0204 366.0202 0.000214
19 2015-01-05 18:00:00 Total area h_x1800m 506.9926 506.9922 0.000488
20 2015-01-06 00:00:00 Total area h_x1800m 1133.0710 1141.0360 -7.964966
21 2015-01-06 06:00:00 Total area h_x1800m 1239.9940 1330.3490 -90.355220
22 2015-01-06 12:00:00 Total area h_x1800m 1435.1160 1664.5030 -229.387600
23 2015-01-06 18:00:00 Total area h_x1800m 1133.0710 1141.0360 -7.964966
24 2015-01-07 00:00:00 Total area h_x1800m 847.3503 847.3491 0.001160
25 2015-01-07 06:00:00 Total area h_x1800m 1154.1830 1171.1590 -16.976680
26 2015-01-07 12:00:00 Total area h_x1800m 1261.9050 1379.2230 -117.318100
27 2015-01-07 18:00:00 Total area h_x1800m 1975.2270 1988.8500 -13.623170
28 2015-01-08 00:00:00 Total area h_x1800m 1133.0710 1389.0640 -255.993500
29 2015-01-08 06:00:00 Total area h_x1800m 3125.7190 3125.7190 0.000000
30 2015-01-08 12:00:00 Total area h_x1800m 2066.1720 2066.1720 0.000000
31 2015-01-08 18:00:00 Total area h_x1800m 1934.5090 1939.1210 -4.612915
32 2015-01-09 00:00:00 Total area h_x1800m 1527.3020 1581.4750 -54.173580
33 2015-01-09 06:00:00 Total area h_x1800m 1386.7600 1444.2880 -57.528560
34 2015-01-09 12:00:00 Total area h_x1800m 705.4024 753.9289 -48.526490
35 2015-01-09 18:00:00 Total area h_x1800m 400.2839 411.3221 -11.038240
36 2015-01-10 00:00:00 Total area h_x1800m 243.9587 243.9587 0.000031
37 2015-01-10 06:00:00 Total area h_x1800m 769.0907 769.0897 0.000977

In [ ]: