In [1]:
import urllib
import zipfile
import os

fname = "tageswerte_00044_19710301_20131231_hist.zip"
server = "ftp://ftp-cdc.dwd.de/"
url = "pub/CDC/observations_germany/climate/daily/kl/historical/"

if not os.path.exists(fname):
    urllib.urlretrieve(server+url+fname, fname)

datazipfile = zipfile.ZipFile(fname, 'r')

for name in datazipfile.namelist():
    if name.startswith('produkt_klima_Tageswerte'):
        break

mydata = datazipfile.read(name)

mydata_lines = mydata.splitlines()
header = mydata_lines[0].split(';')[:-1]

clean_header = []
for field in header:
    clean_header.append(field.strip())

header = clean_header
data = {}

for field in header:
    data[field] = [] 

for line in mydata_lines[1:]:
    fields = line.split(';')
    count = 0
    for field in fields[:-1]:
        data_string = field
        if data_string.strip() != '-999':
            if header[count] == "Mess_Datum":
                data[header[count]].append(data_string)
            else:
                data_value = float(data_string)
                data[header[count]].append(data_value)
        else:
            data[header[count]].append(None)
        count = count + 1

In [5]:
import pylab
%pylab inline

pylab.plot(data['LUFTTEMPERATUR_MAXIMUM'])
pylab.xticks(range(len(data['Mess_Datum']))[::900],
             data['Mess_Datum'][::900], rotation=45)


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['pylab']
`%matplotlib` prevents importing * from pylab and numpy
Out[5]:
([<matplotlib.axis.XTick at 0x7f4de2564510>,
  <matplotlib.axis.XTick at 0x7f4de24cdc90>,
  <matplotlib.axis.XTick at 0x7f4de258f5d0>,
  <matplotlib.axis.XTick at 0x7f4de26f9d10>,
  <matplotlib.axis.XTick at 0x7f4de272e610>,
  <matplotlib.axis.XTick at 0x7f4de2759f90>,
  <matplotlib.axis.XTick at 0x7f4de274ea90>,
  <matplotlib.axis.XTick at 0x7f4de2744390>,
  <matplotlib.axis.XTick at 0x7f4de273ba90>,
  <matplotlib.axis.XTick at 0x7f4de27dc610>,
  <matplotlib.axis.XTick at 0x7f4de2785210>,
  <matplotlib.axis.XTick at 0x7f4de27d1f50>,
  <matplotlib.axis.XTick at 0x7f4de277b510>,
  <matplotlib.axis.XTick at 0x7f4de27bc690>,
  <matplotlib.axis.XTick at 0x7f4de27fdb10>,
  <matplotlib.axis.XTick at 0x7f4de27a8910>,
  <matplotlib.axis.XTick at 0x7f4de281d890>,
  <matplotlib.axis.XTick at 0x7f4de2812f90>],
 <a list of 18 Text xticklabel objects>)

In [15]:
import numpy

temp_vals = numpy.array(data['LUFTTEMPERATUR_MAXIMUM'], dtype=float)
temp_idx = numpy.arange(len(temp_vals))
temp_labels = numpy.array(data['Mess_Datum'])

print temp_vals
print temp_vals.shape


[-1.6 -1.2 -2.  ...,  6.3  5.9  7.1]
(15646,)

In [16]:
temp_mask = numpy.isfinite(temp_vals)

temp_vals = temp_vals[temp_mask]
temp_idx = temp_idx[temp_mask]
temp_labels = temp_labels[temp_mask]

In [17]:
pylab.figure()

pylab.plot(temp_idx, temp_vals)

label_spacing = numpy.linspace(0, len(temp_vals)-1, 10).astype(int)

pylab.xticks(temp_idx[label_spacing],
             temp_labels[label_spacing],
             rotation=45)


Out[17]:
([<matplotlib.axis.XTick at 0x7f4de2b59890>,
  <matplotlib.axis.XTick at 0x7f4de2785a50>,
  <matplotlib.axis.XTick at 0x7f4de2917950>,
  <matplotlib.axis.XTick at 0x7f4de2a75f10>,
  <matplotlib.axis.XTick at 0x7f4de2a80310>,
  <matplotlib.axis.XTick at 0x7f4de2a95e90>,
  <matplotlib.axis.XTick at 0x7f4de2a89690>,
  <matplotlib.axis.XTick at 0x7f4de29e8950>,
  <matplotlib.axis.XTick at 0x7f4de2985e50>,
  <matplotlib.axis.XTick at 0x7f4de2ad6050>],
 <a list of 10 Text xticklabel objects>)

In [32]:
first_january_idx = []
for idx, date in enumerate(temp_labels):
    if date.endswith("0701"):
        first_january_idx.append(idx)
        
c = numpy.polyfit(temp_idx[first_january_idx], temp_vals[first_january_idx], deg=1)
fit_y = c[1] + temp_idx[first_january_idx] * c[0]

In [21]:
print first_january_idx


[306, 672, 1037, 1402, 1767, 2133, 2498, 2863, 3228, 3594, 3959, 4324, 4689, 5055, 5420, 5785, 6150, 6516, 6881, 7246, 7611, 7977, 8342, 8707, 9072, 9438, 9803, 10168, 10533, 10899, 11264, 11629, 11994, 12360, 12725, 13090, 13455, 13821, 14186, 14551, 14916, 15270]

In [35]:
pylab.plot(temp_idx, temp_vals)
pylab.plot(temp_idx[first_january_idx], temp_vals[first_january_idx], "ro", ms=10)
pylab.plot(temp_idx[first_january_idx], fit_y, "g-", lw=5)
pylab.xticks(temp_idx[label_spacing], temp_labels[label_spacing], rotation=45)


Out[35]:
([<matplotlib.axis.XTick at 0x7f4de3938a50>,
  <matplotlib.axis.XTick at 0x7f4de3a6b6d0>,
  <matplotlib.axis.XTick at 0x7f4de3b3a590>,
  <matplotlib.axis.XTick at 0x7f4de3ba3a50>,
  <matplotlib.axis.XTick at 0x7f4de3bc7790>,
  <matplotlib.axis.XTick at 0x7f4de3b9b410>,
  <matplotlib.axis.XTick at 0x7f4de3be6c10>,
  <matplotlib.axis.XTick at 0x7f4de3b7afd0>,
  <matplotlib.axis.XTick at 0x7f4de3bb1850>,
  <matplotlib.axis.XTick at 0x7f4de3c1d9d0>],
 <a list of 10 Text xticklabel objects>)

In [ ]: