# -*- coding: utf-8 -*-

import statsmodels.api as sm
import statsmodels.formula.api as smf
import csv
import numpy as N
import io
import string
from mpl_toolkits.basemap import Basemap
import datetime

from pandas import DataFrame, read_csv
import matplotlib.pyplot as P
import pandas as pd 

gi=N.genfromtxt('gitter04k.txt',usecols=(0),skip_header=0,dtype='i')
lo=N.genfromtxt('gitter04k.txt',usecols=(1),skip_header=0,dtype='f')
la=N.genfromtxt('gitter04k.txt',usecols=(2),skip_header=0,dtype='f')

wi=N.genfromtxt('weinberglagen.csv', delimiter=',',skip_header=1,usecols=(0),dtype='S')
yo=N.genfromtxt('weinberglagen.csv', delimiter=',',skip_header=1,usecols=(1),dtype='f')
xo=N.genfromtxt('weinberglagen.csv', delimiter=',',skip_header=1,usecols=(2),dtype='f')

nw = len(wi)

P.figure(figsize=(7,5))

ax = P.subplot(111)

m = Basemap(projection='merc', lon_0=10,lat_0=52, lat_ts=58, llcrnrlat=47.,urcrnrlat=55.0,llcrnrlon=5.0,urcrnrlon=16,resolution='h')
m.drawcoastlines()
m.drawlsmask(land_color = "lightgray",ocean_color="lightblue",resolution = 'h')
m.drawcountries(linewidth=1.0)
m.readshapefile('./shp/vg2500_bld', 'vg2500_bld', drawbounds=True)


for i in range(nw):

    print wi[i],yo[i],xo[i]

    xx,yy = m(xo[i],yo[i])
    m.scatter(xx,yy,s=3,c='m')
    
    distance = (lo-xo[i])**2 + (la-yo[i])**2
    id = N.where(distance==distance.min())

    file = str(gi[id][0])
                    
    dd=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(0),skip_header=1,dtype='i')
    mm=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(1),skip_header=1,dtype='i')
    jj=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(2),skip_header=1,dtype='i')
                                                            
    tn=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(3),skip_header=1,dtype='f')
    tg=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(4),skip_header=1,dtype='f')
    tx=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(5),skip_header=1,dtype='f')
    ws=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(7),skip_header=1,dtype='f')
    so=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(8),skip_header=1,dtype='f')
    sn=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(9),skip_header=1,dtype='f')
    da=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(10),skip_header=1,dtype='f')                                                        
    lu=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(11),skip_header=1,dtype='f')
    rf=N.genfromtxt('../output_grid/'+file+'.dat',usecols=(12),skip_header=1,dtype='f') 
    pr=N.genfromtxt('../../nied/output_grid/'+file+'.dat',usecols=(3),skip_header=1,dtype='f')
                                          
    it = N.where((jj>=2013)&(jj<=2017))[0]
    
    f = open(wi[i]+'.dat','w')
    
    f.write('%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s\n'%('ta','mo','jahr','tmin','tmit','tmax','wmit','sonn','snow','dadr','ludr','relf','nied'))
    
    for i in it:
    
        f.write('%7i%7i%7i%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n'%(dd[i],mm[i],jj[i],tn[i],tg[i],tx[i],ws[i],so[i],sn[i],da[i],lu[i],rf[i],pr[i]))
    
    f.close()                                      
                                                            
P.savefig('weinberglagen.png',dpi=240,bbox_inches='tight')

#############################################

nd = 365
jo = [2013,2014,2015,2016,2017]
do = N.arange(1,nd+1,1)

P.figure(figsize=(12,6))

for i in range(nw):

    #P.figure(figsize=(12,6))
    
    jj=N.genfromtxt(wi[i]+'.dat',usecols=(2),skip_header=1,dtype='i')
    tg=N.genfromtxt(wi[i]+'.dat',usecols=(4),skip_header=1,dtype='f')
    pr=N.genfromtxt(wi[i]+'.dat',usecols=(12),skip_header=1,dtype='f')

    P.subplot(121)

    for j in jo: 
      
        id = N.where(jj==j)[0]  
        
        dum = N.cumsum(tg[id])
        
        if(j==2013): P.plot(do,dum[0:365],'k',label=str(j),lw=0.5)
        if(j==2014): P.plot(do,dum[0:365],'b',label=str(j),lw=0.5)
        if(j==2015): P.plot(do,dum[0:365],'g',label=str(j),lw=0.5)
        if(j==2016): P.plot(do,dum[0:365],'y',label=str(j),lw=0.5)
        if(j==2017): P.plot(do,dum[0:365],'r',label=str(j),lw=0.5)
        
    if(i==0): 
     
        P.legend(loc=0)
        P.grid()
        P.ylim(-500,4500)
        P.xlim(0,366)
        P.xticks([30,60,90,120,150,180,210,240,270,300,330,360])
    
        P.xlabel('Tag des Jahre',fontsize=20)
        P.ylabel('Temperatursumme',fontsize=20)   
        P.title('Alle Weinberge',fontsize=20)
        
    P.subplot(122)    
        
    for j in jo:
    
        id = N.where(jj==j)[0]
            
        dum = N.cumsum(pr[id])
                    
        if(j==2013): P.plot(do,dum[0:365],'k',label=str(j),lw=0.5)
        if(j==2014): P.plot(do,dum[0:365],'b',label=str(j),lw=0.5)
        if(j==2015): P.plot(do,dum[0:365],'g',label=str(j),lw=0.5)
        if(j==2016): P.plot(do,dum[0:365],'y',label=str(j),lw=0.5)
        if(j==2017): P.plot(do,dum[0:365],'r',label=str(j),lw=0.5)
       
    if(i==0): 
                                                            
        P.legend(loc=0)
        P.grid()
        P.ylim(0,800)
        P.xlim(0,366)
        P.xticks([30,60,90,120,150,180,210,240,270,300,330,360])
    
        P.xlabel('Tag des Jahre',fontsize=20)  
        P.ylabel('Niederschlagssumme',fontsize=20)        
        P.title('Alle Weinberge',fontsize=20)
                
P.savefig('summen.png',dpi=240,bbox_inches='tight')

#############################################

for i in range(nw):

    P.figure(figsize=(12,6))
    
    jj=N.genfromtxt(wi[i]+'.dat',usecols=(2),skip_header=1,dtype='i')
    tg=N.genfromtxt(wi[i]+'.dat',usecols=(4),skip_header=1,dtype='f')
    pr=N.genfromtxt(wi[i]+'.dat',usecols=(12),skip_header=1,dtype='f')

    P.subplot(121)

    for j in jo: 
      
        id = N.where(jj==j)[0]  
        
        dum = N.cumsum(tg[id])
        
        if(j==2013): P.plot(do,dum[0:365],'k',label=str(j),lw=0.5)
        if(j==2014): P.plot(do,dum[0:365],'b',label=str(j),lw=0.5)
        if(j==2015): P.plot(do,dum[0:365],'g',label=str(j),lw=0.5)
        if(j==2016): P.plot(do,dum[0:365],'y',label=str(j),lw=0.5)
        if(j==2017): P.plot(do,dum[0:365],'r',label=str(j),lw=0.5)
        
    P.legend(loc=0)
    P.grid()
    P.ylim(-500,4500)
    P.xlim(0,366)
    P.xticks([30,60,90,120,150,180,210,240,270,300,330,360])
    
    P.xlabel('Tag des Jahre',fontsize=20)
    P.ylabel('Temperatursumme',fontsize=20)
    P.title(wi[i],fontsize=20)    
        
    P.subplot(122)    
        
    for j in jo:
    
        id = N.where(jj==j)[0]
            
        dum = N.cumsum(pr[id])
                    
        if(j==2013): P.plot(do,dum[0:365],'k',label=str(j),lw=0.5)
        if(j==2014): P.plot(do,dum[0:365],'b',label=str(j),lw=0.5)
        if(j==2015): P.plot(do,dum[0:365],'g',label=str(j),lw=0.5)
        if(j==2016): P.plot(do,dum[0:365],'y',label=str(j),lw=0.5)
        if(j==2017): P.plot(do,dum[0:365],'r',label=str(j),lw=0.5)
                                                   
    P.legend(loc=0)
    P.grid()
    P.ylim(0,800)
    P.xlim(0,366)
    P.xticks([30,60,90,120,150,180,210,240,270,300,330,360])
    
    P.xlabel('Tag des Jahre',fontsize=20)  
    P.ylabel('Niederschlagssumme',fontsize=20)        
    P.title(wi[i],fontsize=20)

    P.savefig(wi[i]+'.png',dpi=120,bbox_inches='tight')
                
