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



from builtins import zip

from builtins import str
from builtins import range
from PyQt5.QtCore import*

from PyQt5.QtGui import*

from PyQt5.QtWidgets import *

from qgis.PyQt import QtCore, QtGui

from qgis.core import *

from qgis.gui import *

from .Ui_ECOAL import Ui_ECOAL

import csv

import re

import glob

from . import ftools_utils

from . import GdalTools_utils

import subprocess

from qgis.PyQt import QtXml

import csv

import re

import os, glob, sys 

from matplotlib import *

import matplotlib.pyplot as plt

import matplotlib.dates as md

import dateutil

import datetime as dt

import numpy as np



# Prepare processing framework

from processing.core.Processing import Processing





class ECOAL(QDialog, Ui_ECOAL):

    

    def __init__(self, iface):

        QDialog.__init__(self)

        self.iface = iface

        self.setupUi(self)

        

        self.selectButton.clicked.connect(self.fillinputdirectory)

        self.buttonBox.accepted.connect(self.txttemperat)

        self.selectmaskButton.clicked.connect(self.fillinputmask)

        self.selectoutButton.clicked.connect(self.filloutputdirectory)

        self.selecttxtButton.clicked.connect(self.fillinputtxt)

        self.selectshpButton.clicked.connect(self.fillinputmask)

        self.selectshp.clicked.connect(self.fillinputpoint)

        #self.play_pause.clicked.connect(self.playClicked)

        self.plot.clicked.connect(self.plot_create)

        #self.buttonBox.button.clicked.connect(self.help)

        

    

    # def help(self):
    #
    #     QMessageBox.about(self, "ECOAL", """<p><b>ECOAL</b></p>
    #
    #     <p>ECOAL GIS is an application to produce dynamic maps which allows the monitoring of temperature variations in a self burning coal
    #
    #     waste pile, under a GIS open source environment.</p>
    #
    #     <p> The GIS-ECOAL application is a single button that could be added to QGIS software, composed by eight fields divided in four sections.
    #
    #     Five of them incorporate the input files and the other three incorporate the output files. The input files defined in the first zone, named
    #
    #     <b>Input temperature data and study zone information</b>, are the temperature measurement data in text format and the vector file (polygon) defining
    #
    #     the study area extension. The zone with the name <b>Input coordinates information</b> is composed by two different methods to introduce the coordinates
    #
    #     information. According to the available data, the user can choose between a text file with the coordinates disposed in two columns, x and y, or a
    #
    #     vector file with the position of the points. The next zone corresponds to the folder where the output tif raster and the composed images are saved
    #
    #     and finally the part where the video area is displayed (black window). </p>
    #
    #     <p> The input files with temperature values can be in any text format.</p>
    #
    #     <p> Please make sure that you have <b>ffmpeg program</b> installed in your computer. This program will be used to create the video.</p>
    #
    #
    #
    #           """)

        

    # play video

    # def playClicked(self):
    #
    #     #self.video()
    #
    #     images_out = self.outputCombo.currentText()
    #
    #     if os.path.exists(os.path.join(str(images_out), 'movie.webm'))==True:
    #
    #         #self.player = Phonon.VideoPlayer()
    #
    #         media_source = Phonon.MediaSource(os.path.join(str(images_out), 'movie.webm'))
    #
    #         #QMessageBox.about(self,'test', str(media_source))
    #
    #         self.player.show()
    #
    #         self.player.load(media_source)
    #
    #         a=self.player.play()
    #
    #         #QMessageBox.about(self,'test', str(a))
    #
    #     else:
    #
    #         QMessageBox.warning( self, self.tr( "No files found" ), self.tr( "There is no video in this directory. Please create a video first." ) )
    #
    #

    def fillinputdirectory(self):

        settings = QSettings()

        lastDir = settings.value( "/fTools/lastRasterDir", "." )

        inDir = QFileDialog.getExistingDirectory( self, self.tr( "Select directory with text files (temperature data)" ), lastDir )

    

        if not inDir:

            return

    

        self.workDir = QDir( inDir )

        self.workDir.setFilter( QDir.Files)

        self.inputFiles = self.workDir.entryList()

        if len( self.inputFiles ) == 0:

            QMessageBox.warning( self, self.tr( "No files found" ), self.tr( "There are no files in this directory. Please select another one." ) )

            self.inputFiles = None

            return

    

        settings.setValue( "/fTools/lastRasterDir", inDir )

    

        self.inputLayerCombo.addItem( inDir )       

        

    # OUTPUT IMAGES

    def filloutputdirectory(self):

        settings = QSettings()

        lastDir = settings.value( "/fTools/lastRasterDir", "." )

        inDir = QFileDialog.getExistingDirectory( self, self.tr( "Select directory to save images" ), lastDir )

    

        if not inDir:

            return

    

        self.workDir = QDir( inDir )

        self.workDir.setFilter( QDir.Files | QDir.NoSymLinks | QDir.NoDotAndDotDot )

    

        settings.setValue( "/fTools/lastRasterDir", inDir )

    

        self.outputCombo.addItem( inDir )    

        

        

    # INPUT MASK SHAPEFILE

    def fillinputmask(self):

        lastUsedFilter = Utils.FileFilter.lastUsedVectorFilter()

        inputFile, encoding = Utils.FileDialog.getOpenFileName(self, self.tr( "Select the input file" ), Utils.FileFilter.allVectorsFilter(), lastUsedFilter, True)

        self.lastEncoding = encoding

        self.inputmaskCombo.addItem(inputFile)

        check = QFile(inputFile) 

        

    def fillinputpoint(self):

        lastUsedFilter = Utils.FileFilter.lastUsedVectorFilter()

        inputFile, encoding = Utils.FileDialog.getOpenFileName(self, self.tr( "Select the input file" ), Utils.FileFilter.allVectorsFilter(), lastUsedFilter, True)

        self.lastEncoding = encoding

        self.inputshppointCombo.addItem(inputFile)

        check = QFile(inputFile)    

        

    def fillinputtxt(self):

        inputFile = Utils.FileDialog.getOpenFileName()

        self.inputtxtCombo.addItem(inputFile)

        check = QFile(inputFile)           

        

        

    def txttemperat(self):

        

        import os

        diretory_home = os.path.dirname(os.path.realpath(__file__))

        inf = self.inputLayerCombo.currentText()

        images_out = self.outputCombo.currentText()

        input_txt = self.inputtxtCombo.currentText()

        inputMask = QgsVectorLayer(self.inputmaskCombo.currentText(),'limite','ogr')

             

        extent_rect = inputMask.extent()

            

        xmin = extent_rect.xMinimum()

        xmax = extent_rect.xMaximum()

        ymin = extent_rect.yMinimum()

        ymax = extent_rect.yMaximum()

        extent = str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)          

        

        import os

        from datetime import datetime

        for filename in os.listdir(inf):

            if os.path.splitext(os.path.basename(filename))[1] != '.txt':

                os.rename(os.path.join(inf,filename), os.path.join(inf, filename[4:6] + '-' + filename[7:9] + '-' + filename[10:14] + ' ' + filename[15:17] + 'h' + filename[18:20] + '.'+'txt'))    

            if os.path.splitext(os.path.basename(filename))[1] == '.txt':

                os.rename(os.path.join(inf,filename), os.path.join(inf, filename[4:6] + '-' + filename[7:9] + '-' + filename[10:14] + ' ' + filename[15:17] + 'h' + filename[18:20] + '.'+'txt'))

            

        for file_name in glob.glob(str(inf) + '\*.txt'): 

            date_object = datetime.strptime(os.path.splitext(os.path.basename(file_name))[0], '%d-%m-%Y %Hh%M') 

            dd =date_object.strftime('%Y-%m-%d %Hh%M')

            tt = os.rename(os.path.join(inf,file_name), os.path.join(inf,str(dd) + '.' + 'txt'))

            

        for file in glob.glob(str(inf) + '\*.txt'):   

            import os

            input_file = open(file, 'r')

            data = []

            input_file.readline() # first line with titles

            for line in input_file: # lines with data

                data.append([float(x.replace(',', '.')) for x in line.split()])

            input_file.close()

            result_file = open(os.path.join(str(inf),os.path.basename(file)), 'w')

            result_file.writelines('\n'.join('\t'.join(map(str,row)) for row in data))

            result_file.close()                    

  

        

        listt =[]

        for file in glob.glob(str(inf) + '\*.txt'):

            input_file = open(file, "r") 

            output_file = open(os.path.join(str(inf), 'newline.txt'), "w")            

            for lin in input_file: 

                line = lin.strip()

                sline = lin.split()

                #QMessageBox.about(self, "plano", str(sline))

                # list with the values for points

                if float(sline[0])>=68.000 and float(sline[0])<=734.000:

                    #sline.insert(2,sline[1])  

                    sline.insert(2,sline[1])  

                    #QMessageBox.about(self, "plano", str(sline))

                    output_file.write("\t".join(sline)+"\n")

            output_file.close()      

            input_file.close()

            try:

                os.remove(file)  

                os.rename(os.path.join(str(inf), 'newline.txt'), file)   

            except IndexError:

                pass          

        #exit()

        # create txt file with coordinates and median temperature values to introduce as points in QGIS

        for file in glob.glob(str(inf) + '\*.txt'):

            input_file = open(file, "r") 

            lista =  []

            for lin in input_file:  

                line = lin.strip()

                sline = lin.split()        

                #QMessageBox.about(self, "plano", str(sline))

                

                lista.append(sline[1])

                #QMessageBox.about(self, "plano", str(lista))

            values = [float(x) for x in lista]

            #QMessageBox.about(self, "plano", str(values))

            subList = [values[n:n+11] for n in range(0, len(values), 10)]

            #QMessageBox.about(self, "plano", str(subList))

            list_median = []

            list_median1 = []

            for l in subList:

                ##calculate median of each 10 values

                median = np.median(l)

                list_median1.append(median)

            list_median = list_median1[1:]

            #QMessageBox.about(self, "plano", str(list_median))

            #quit()

            #exit()

            # if txt doesn't exist and shp exist

            if self.inputtxtCombo.currentText()=='':

                shp = self.inputshpCombo.currentText()

                layer_points = QgsVectorLayer(str(shp).encode('utf8'), shp , "ogr")

                iter = layer_points.getFeatures()

                list_tuple_coord = []

                for feature in iter:

                    geom = feature.geometry()

                    if geom.type() == QGis.Point:

                        x = geom.asPoint()

                        list_tuple_coord.append(x)

                #Extract the points coordinates and write in txt file        

                with open(os.path.join(diretory_home, 'shp_points_coord.txt').replace('\\', '/'), 'w') as f:

                    for line in list_tuple_coord:

                        strs=str(line[0]) + '\t' + str(line[1])

                        f.write(strs+"\n")  

                f.close   

                coord_file = open(os.path.join(diretory_home, 'shp_points_coord.txt').replace('\\', '/'), 'r')

            else:

                # if txt exist

                coord_file = open(str(input_txt), 'r')

            

            rows = [line.split('\t') for line in coord_file]

            new_list = ['{:.3f}'.format(x) for x in list_median]

            cols = list(zip(*rows))

            cols.insert(0, new_list)

            rows = list(zip(*cols))

            input_file.close()

            output_file = open(os.path.join(str(inf), os.path.basename(file)), "w") 

            output_file.writelines(['\t'.join(row) for row in rows])



            output_file.close()      

            coord_file.close()



        for file in glob.glob(str(inf) + '\*.txt'): 

            input_file = open(file, "r") 

            output_file = open(os.path.join(str(inf), 'newline.txt'), "w")   

            lista =  []

            for lin in input_file:  

                line = lin.strip()

                sline = lin.split() 

                #QMessageBox.about(self, "plano", str(sline[0]))

                if sline[0]!='nan':

                    #QMessageBox.about(self, "plano", str(sline[0]))

                    output_file.write("\t".join(sline)+"\n")

            output_file.close()

            input_file.close()

            try:

                os.remove(file)  

                os.rename(os.path.join(str(inf), 'newline.txt'), file)   

            except IndexError:

                pass                         

                    

        

        # insert headers in files with MEDIAN X and Y fields

        for file in glob.glob(str(inf) + '\*.txt'): 

            import os

            headers = ['MEDIAN', 'X', 'Y']

            tmp = open('TMP', 'w')

            orig = open(file, 'r')

            tmp.write('\t'.join(headers) + '\n')

            for line in orig.readlines():

                #QMessageBox.about(self, "plano", str(line))

                tmp.write(line)

            orig.close()

            tmp.close()

            os.remove(file)

            os.rename('TMP', file)    

                    

        

        # create shapefiles with the coordinates and median values

        indir = inf

        outdir = inf

        for root, dirs, files in os.walk(indir):

            for file in files:

                if file.endswith('.txt'):

                    fullname = os.path.join(root, file).replace('\\', '/')

                    filename = os.path.splitext(os.path.basename(fullname))[0]

                    uri = 'file:///%s?crs=%s&delimiter=%s&xField=%s&yField=%s&decimal=%s' % (fullname, 'EPSG:3763', '\t', 'x', 'y',',')

                    layer = QgsVectorLayer(uri, 'my_layer', 'delimitedtext')

                    QgsVectorFileWriter.writeAsVectorFormat(layer, outdir + '/' + filename + '.shp', 'CP1250', None, 'ESRI Shapefile')  

                    

        

        i = 1

        for file_in in glob.glob(str(inf) + '\*.shp'): 

            txt_file = str(diretory_home) + '\color.txt'

            out = os.path.join(str(inf), os.path.splitext(os.path.basename(file_in))[0] + '_' + str(i) + '.sdat')

            outwithmask = os.path.join(str(inf), 'mask'+ os.path.splitext(os.path.basename(file_in))[0] + '_' + str(i) + '.sdat')

            colorraster = os.path.join(str(images_out), os.path.splitext(os.path.basename(file_in))[0] + '_' + str('%03d'%i) + '.tif')

            Processing.runAlgorithm("saga:ordinarykriging",None,file_in,"MEDIAN",0,False,False,100,-1,2,1,"a + b * x",0,1000,0,2,5,0,True,extent,1,0,out,None)

            try:

                Processing.runAlgorithm("saga:clipgridwithpolygon", None, out, inputMask,outwithmask)

            except:

                Processing.runAlgorithm("saga:clipgridwithpolygon", None, out, inputMask,outwithmask)

    

            

            # second attempt to color raster file (test)

            from PyQt import QtGui

            

            #Next we load the raster layer:

            #lyr = QgsRasterLayer(outwithmask, 'Temperature (Celsius)')

            lyr = QgsRasterLayer(outwithmask, 'Temperature (Celsius)')

            

            #Now we create a generic raster shader object:

            s = QgsRasterShader()

            

            #Then we instantiate the specialized ramp shader object:

            c = QgsColorRampShader()

            

            #We must name a type for the ramp shader. In this case we use an interpolatedshader:

            c.setColorRampType(QgsColorRampShader.INTERPOLATED)

            

            #Now we ll create a list hold our color ramp definition:

            iii = []

            

            #Then we populate the list with color ramp color values corresponding to elevation value ranges:

            iii.append(QgsColorRampShader.ColorRampItem(41.97, QtGui.QColor('#1a9641'), '0-41.97'))

            

            iii.append(QgsColorRampShader.ColorRampItem(55.95, QtGui.QColor('#a6d96a'), '41-97-55.95'))

            

            iii.append(QgsColorRampShader.ColorRampItem(61.64, QtGui.QColor('#ffff74'), '55.95-61.64'))

            

            iii.append(QgsColorRampShader.ColorRampItem(75.63, QtGui.QColor('#fdae61'), '61.64-75.63'))

            

            iii.append(QgsColorRampShader.ColorRampItem(110, QtGui.QColor('#d7191c'), '75.63-110'))

            

            #Now we assign the color ramp to our shader:

            c.setColorRampItemList(iii)

            

            #Now we tell the generic raster shader to use the color ramp:

            s.setRasterShaderFunction(c)

            

            #Next we create a raster renderer object with the shader:

            ps = QgsSingleBandPseudoColorRenderer(lyr.dataProvider(), 1, s)

            

            #We assign the renderer to the raster layer:

            lyr.setRenderer(ps)

            

            #Finally we add the layer to the canvas to view it:

            QgsMapLayerRegistry.instance().addMapLayer(lyr)   

      

 #___Create composition___      

            # composition

            layerset = []

            #QgsMapLayerRegistry.instance().addMapLayer(RUSLE_color_raster)

            layerset.append(lyr.id())

            

            myMap = QgsMapSettings()

        

            actual_crs = lyr.crs()

            #map_proj = QgsCoordinateReferenceSystem(3763, QgsCoordinateReferenceSystem.PostgisCrsId)

            myMap.setDestinationCrs(actual_crs)

            myMap.setLayers(layerset)

            mapRectangle = myMap.fullExtent()

            #mapRectangle.scale(0.5)

            myMap.setExtent(mapRectangle)

            myMap.setOutputSize(QSize(800,400))

          

            

            myComposition = QgsComposition(myMap)

            myComposition.setPlotStyle(QgsComposition.Print)

           

            myFile = str(diretory_home) + '/template2.qpt'

        

            

            myTemplateFile = open(myFile, 'rt')

            myTemplateContent = myTemplateFile.read()

            ###QMessageBox.about(self, "plano", myTemplateContent)

            myTemplateFile.close()    

            myDocument = QtXml.QDomDocument()

            myDocument.setContent(myTemplateContent)

            myComposition.loadFromTemplate(myDocument)  

           

            x,y = 20,20

            # paper A4

            w,h = myComposition.paperWidth(), myComposition.paperHeight()

            composerMap = QgsComposerMap(myComposition,x,y,w-30,h-70)

            composerMap.gridEnabled()

            myComposition.addComposerMap(composerMap)

            composerMap.setLayerSet(myMap.layers())

          

            

            #legend

            legend = QgsComposerLegend(myComposition)

            legend.setComposerMap(composerMap)

            #legend.model().setLayerSet(myMap.layers())

            #legend.model().setLayerSet

            myComposition.addComposerLegend(legend)

            legend.setItemPosition(252.2,160.1)

            legend.update()                

            #legend.setItemPosition (100,122)

            #legend.setFrameEnabled(True)

            #legend.setScale(.7)   

            

            # title

            title = QgsComposerLabel(myComposition)

            title.setText(str(os.path.splitext(os.path.basename(file_in))[0]))

            title.setFont(QFont("Cambria",40, QFont.Bold))

            title.setItemPosition(93,1)

            title.adjustSizeToText()

            #title.setFrameEnabled(True)

            myComposition.addItem(title)                

    

            # arrow picture

            pic_path = str(diretory_home) + '/north.png'

            pic = QgsComposerPicture(myComposition)

            pic.setPictureFile(pic_path)

            pic.setRotationMap(0)

            pic.setSceneRect(QRectF(0,0,20,20))

            myComposition.addComposerPicture(pic)

            pic.setItemPosition(2.2,2.6)

            #pic.setScale(2)

            #pic.setFrameEnabled(True)

            #pic.refreshPicture()

            

            # add scale bar

            item = QgsComposerScaleBar(myComposition)

            item.setComposerMap(composerMap)

            myComposition.addComposerScaleBar(item)

            item.setStyle('Double Box') # optionally modify the style

            #item.SegmentSizeMode('SegmentSizeFitWidth')

            item.adjustBoxSize()

            #item.setNumSegments(4)

            item.setUnitLabeling("km")

            #item.setNumMapUnitsPerScaleBarUnit(1)

            #item.setHeight(8)

            ##item.correctXPositionAlignment(10,10)

            item.applyDefaultSize()

            item.setItemPosition(0,190)

            item.update()

            

           

            output_jpeg = os.path.join(str(images_out), 'Temperature (Celsius)' + str('%03d'%i) + '.tif')

            myImage = myComposition.printPageAsRaster(0)

            myImage.save(output_jpeg)

#___End composition___            

            

           

       

  

            i = i+1

        times = i



    



        nFrames = times-1

        os.chdir('C:/ffmpeg/bin')

    

    

    #def video(self):

        #os.chdir('C:/ffmpeg/bin')

        t = os.path.join(os.path.dirname(os.path.realpath(__file__)), '\\ffmpeg/bin')

        os.chdir(str(t))

        #QMessageBox.about(self,'ecoal',str(t))

        import subprocess

        images_out = self.outputCombo.currentText()

        #subprocess.call(['ffmpeg.exe', '-f', 'image2', '-r','1/2','-i', os.path.join(str(images_out), 'Temperature (Celsius)%03d.tif'), '-vcodec', 'libx264',os.path.join(str(images_out), 'movie.mp4')])

        subprocess.call(['ffmpeg.exe', '-framerate','1/2','-i', os.path.join(str(images_out), 'Temperature (Celsius)%03d.tif'), os.path.join(str(images_out), 'movie.webm')])

            #a = subprocess.call(['ffmpeg.exe', '-version'])

        QMessageBox.about(self,'Created', str('The video was created. Please see in the folder. Click on Play button to display the video.'))

        

        

            

    def plot_create(self):

        #i = 1

        inf = self.inputLayerCombo.currentText()

        point = self.inputshppointCombo.currentText()

       

        for file in glob.glob(str(inf) + '\*.sdat'): 

            substring = 'mask'

            out_point = os.path.join(str(inf), 'point'+ os.path.splitext(os.path.basename(file))[0] + '.shp')

            

            # if mask in name

            if substring in os.path.join(str(inf) + os.path.splitext(os.path.basename(file))[0]):

                try:

                    Processing.runAlgorithm("saga:addgridvaluestopoints",None,point,file,0,out_point)

                except:

                    Processing.runAlgorithm("saga:addgridvaluestopoints",None,point,file,0,out_point)              

        

        lista = []

        datestrings =  []

        hours = []

        for shp in glob.glob(str(inf) + '\*.shp'):  

            substring_ = 'pointmask'

            if substring_ in os.path.join(str(inf) + os.path.splitext(os.path.basename(shp))[0]):

                filename=os.path.splitext(os.path.basename(shp))[0]

                datestrings = datestrings + [str(filename[9:19])]

                hours = hours + [str(filename[19:25])]

                #QMessageBox.about(self,'ecoal',str(hours))

                vector = QgsVectorLayer(str(shp).encode('utf8'), shp , "ogr")

                fields = vector.pendingFields() 

                field_names = [field.name() for field in fields]

                # get list with temperature values

                for feat in vector.getFeatures():

                    attrb = feat.attributes()[1]

                    lista = lista + [str(attrb)]

                    

        dates_with_duplic = list(set(datestrings))

        # list of string to list of floats (temperature)

        temp_data = list(map(float, lista))

       

        #QMessageBox.about(self,'ecoal',str(dates_with_duplic))

        #QMessageBox.about(self,'ecoal',str(datestrings))

  

        #quit()

        # --------------------------

        #myFilePath = os.path.join(inf, 'Temper.txt')

        

        #c = {}

        #c = dict(zip(hours, temp_data))

        ##for name, value in zip(hours, temp_data):

            ##QMessageBox.about(self,'ecoal',str(name))

            ##QMessageBox.about(self,'ecoal',str(value))

            ###c.setdefault(name, [])

            ##c[name].append(value)        

                         

        #headings = ['Dates','Temperature records']                   

        #data = [sorted(c.keys(), c.values())]

        ##QMessageBox.about(self,'ecoal',str(c))

        ##quit()

                    

        ##with open(myFilePath,'wb') as myFile:

            ##wr = csv.writer(myFile, dialect='excel')

            ##wr.writerow(headings)

            ##for row in zip(*data):

                ##wr.writerow(row)  

         

        #with open(myFilePath,'w') as f:

            #f.write('\t'.join(headings) + '\n')

            #for i in range(0,len(c)): 

                #f.write(str(c.keys()[i]) + '\t' + str(c.values()[i]) + '\n' )       

        #f.close()  

        ##quit()

        

        # -----------------------------

        

        

        

        

       

        import numpy

        aux_date = datestrings[0]

        ii = 0

        a = {}

        for name, value in zip(datestrings, temp_data):

            #QMessageBox.about(self,'ecoal',str(name))

            a.setdefault(name, [])

            if name == aux_date:

                a[name].append(value)

                ii = ii +1

            else:

                aux_date = datestrings[ii]

                a[name].append(value)

        #QMessageBox.about(self,'ecoal',str(a))

              

        import collections

        order = collections.OrderedDict(sorted(a.items()))

        #QMessageBox.about(self,'ecoal',str(order))

        myFilePath2 = os.path.join(inf, 'Temperature.txt')

                                 

        headings2 = ['Dates','Temperature records']                   

        data2 = [sorted(a.keys()), list(a.values())]

       

                    

        #with open(myFilePath,'wb') as myFile:

            #wr = csv.writer(myFile, dialect='excel')

            #wr.writerow(headings)

            #for row in zip(*data):

                #wr.writerow(row)  

         

        with open(myFilePath2,'w') as f2:

            f2.write('\t'.join(headings2) + '\n')

            for i in range(0,len(list(a.keys()))): 

                f2.write(str(list(a.keys())[i]) + '\t' + str(list(a.values())[i]) + '\n')       

        f2.close()        

        #quit()

        

        #minimo e maximo de todos os valores 

        max_temp = max(temp_data)

        min_temp = min(temp_data)        

       

        median_list = []

        average_list = []

        maxvalue_list = []

        minvalue_list = []

        std_list = []

        coef_list = []

        ampli_list = []

        records_list = []

        # dicionario para os valores da mediana em cada dia

        b = {}

        for elem, items in order.items():

            median = numpy.median(numpy.array(items))

            average = numpy.average(numpy.array(items))

            maxvalue = numpy.amax(numpy.array(items))

            minvalue = numpy.amin(numpy.array(items))

            std = numpy.std(numpy.array(items))

            coef_variation = std/average*100

            amplitude = maxvalue-minvalue

            median_list.append(median)

            average_list.append(average)

            maxvalue_list.append(maxvalue)

            minvalue_list.append(minvalue)

            std_list.append(std)

            coef_list.append(coef_variation)

            ampli_list.append(amplitude)

            records = len(items)

            records_list.append(records)

            b[elem]=median

            #

       

        #QMessageBox.about(self,'ecoal',str(b))

        #QMessageBox.about(self,'ecoal',str(median_list))

       

        #get maximum and minimum median value in dicionario

        import operator

        date_max_median = max(iter(b.items()), key=operator.itemgetter(1))[0] 

        date_min_median = min(b, key=b.get)

        date_fmt = '%Y-%m-%d'

        dt_median_max = dt.datetime.strptime(str(date_max_median), date_fmt)

        dt_median_min = dt.datetime.strptime(str(date_min_median), date_fmt)

        date_max = md.date2num(dt_median_max)

        date_min = md.date2num(dt_median_min)

        max_median = b[date_max_median]

        min_median = b[date_min_median]

        #QMessageBox.about(self,'ecoal',str(date_max))

        #QMessageBox.about(self,'ecoal',str(date_min))

        #QMessageBox.about(self,'ecoal',str(date_max_median))

        #QMessageBox.about(self,'ecoal',str(date_min_median))

        #quit()

        

        #QMessageBox.about(self,'ecoal',str(b.keys()))

        #QMessageBox.about(self,'ecoal',str(min_temp))

        

        

        

        

        # create csv file in the folder  

        

        myFilePath = os.path.join(inf, 'Temperature_analysis.txt')

                         

        headings = ['Dates','Temperature records','Average (\xb0C)','Median (\xb0C)','Maximum (\xb0C)','Minimum (\xb0C)','Standard Deviation (\xb0C)','Coefficient of Variation (%)','Amplitude (\xb0C)']                   

        data = [sorted(b.keys()), records_list, average_list, median_list, maxvalue_list, minvalue_list, std_list, coef_list, ampli_list]

       

                    

        #with open(myFilePath,'wb') as myFile:

            #wr = csv.writer(myFile, dialect='excel')

            #wr.writerow(headings)

            #for row in zip(*data):

                #wr.writerow(row)  

         

        with open(myFilePath,'w') as f:

            f.write('\t'.join(headings) + '\n')

            for i in range(0,len(average_list)): 

                f.write(str(list(b.keys())[i]) + '\t' + str(records_list[i]) + '\t' + str(average_list[i]) + '\t' + str(median_list[i]) + '\t' + str(maxvalue_list[i]) + '\t' + str(minvalue_list[i]) + '\t' + str(std_list[i]) + '\t' + str(coef_list[i]) + '\t' + str(ampli_list[i]) + '\n')       

        f.close()

               

        #list_dates_dict.sort()        



        #QMessageBox.about(self,'ecoal',str(median_list))

        # plot temperature measurement

        fig= plt.figure(dpi = 110)

        # create a subplot with 1 row, 1 column and the plot number is 1

        ax = fig.add_subplot(111)

        

        #date_fmt = '%Y-%m-%d %Hh%M'

        date_fmt = '%Y-%m-%d'

        #dt_x = [dt.datetime.strptime(str(i), date_fmt) for i in datestrings]

        dt_x = [dt.datetime.strptime(str(i), date_fmt) for i in dates_with_duplic]

        dates = [md.date2num(i) for i in dt_x]

        dates.sort()

        

        #QMessageBox.about(self,'ecoal',str(dt_x))

        #QMessageBox.about(self,'ecoal',str(dates))

    

            

        

        

        #quit()        

        

        #plt_data = range(5,9)

        plt.subplots_adjust(bottom=0.2)

        plt.xticks( rotation=25 )

        

        ax=plt.gca()

        ax.set_xticks(dates)

        

        zed = [tick.label.set_fontsize(6) for tick in ax.xaxis.get_major_ticks()]

        

        ax.set_xticklabels(dates)        

        

        spacing = 2

        #QMessageBox.about(self,'ecoal',str(dt_x))

        for label in ax.xaxis.get_ticklabels()[::spacing]:

            #QMessageBox.about(self,'ecoal',str(label))

            label.set_visible(False)        

       

        # maximum and minimum temperature values

        #annotation = ('Maximum temperature value \n' '\n' + '{0:.1f}'.format(max_temp) + '\n' '\n' 'Minimum temperature value \n' '\n' + '{0:.1f}'.format(min_temp) + '\n' )

        #ax.text(max(dates) + 0.2,min(median_list)+0.5,annotation, size = 8, bbox = dict(facecolor = 'mediumaquamarine', edgecolor = 'black'))                  

        ax.annotate('Maximum temperature', xy = (date_max,max_median), xytext = (date_max+0.2,max_median+0.02), arrowprops = dict(facecolor = 'black', shrink = 0.05))

        ax.annotate('Minimum temperature', xy = (date_min,min_median), xytext = (date_min+0.2,min_median+0.02), arrowprops = dict(facecolor = 'black', shrink = 0.05))        

        

        

        #xfmt = md.DateFormatter('%Y-%m-%d %Hh%M')

        xfmt = md.DateFormatter('%Y-%m-%d')

        ax.xaxis.set_major_formatter(xfmt)

        ax.set_xlabel('Time')

        ax.set_ylabel('Temperature (Celsius)')

        plt.title('Distributed temperature measurement')  

        plt.subplots_adjust(right = 0.7)

        #QMessageBox.about(self,'ecoal',str(median_list))

        #QMessageBox.about(self,'ecoal',str(dates))

        dates_sort = sorted(dates)

        #QMessageBox.about(self,'ecoal',str(dates_sort))

        #plt.plot(dates_sort,median_list, "o-",label = 'Temperature data')

        plt.plot(dates_sort,median_list, "o-")

        #plt.plot(dates,temp_data, "o-",label = 'Temperature data')

        ax.legend()

        plt.show()        

        QMessageBox.warning( self, self.tr( "Plot created" ), self.tr( "Please close the ECOAL window and evaluate the plot results." ) )

        

      