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)
Out[5]:
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
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]:
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
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]:
In [ ]: