Example #1
0
class Menu(ScreenManager):
    def __init__(self, **kwargs):
        """ Create all the default gui elements  """
        super(Menu, self).__init__(**kwargs)
        self.fcv = fire_cv.FireCv()
        self.imT = im_transform.Image_Transform()

        # main screen config
        self.main_screen = Screen(name='Most-g4')
        self.layout = BoxLayout(orientation='vertical')
        self.logo = Image(source='static/logo.png',
                          allow_stretch=True,
                          size_hint_y=1)
        self.layout_down = GridLayout(cols=2, rows=2, size_hint_y=0.2)
        self.layout.add_widget(self.logo)
        self.layout.add_widget(self.layout_down)
        self.main_screen.add_widget(self.layout)
        # self.cols = 1

        # define os and default path
        self.os = platform
        self.path = ""
        if self.os == 'win':
            self.path = dirname(expanduser("~"))
        else:
            self.path = expanduser("~")

        # Define and add buttons to main Screen
        self.vid = Button(text='Dividir Video', size_hint_y=0.1)
        self.vid.bind(on_press=lambda x: (self.browser('vid')))
        self.im = Button(text='Processar Frames', size_hint_y=0.1)
        self.im.bind(on_press=lambda x: (self.browser('frame')))
        self.seg = Button(text='Contornos de Interesse ', size_hint_y=0.1)
        self.seg.bind(on_press=self.seg_workflow)
        self.image_transform = Button(text='Coordenadas Georeferenciadas',
                                      size_hint_y=0.1)
        self.image_transform.bind(on_press=self.image_transformation_workflow)

        self.layout_down.add_widget(self.vid)
        self.layout_down.add_widget(self.im)
        self.layout_down.add_widget(self.seg)
        self.layout_down.add_widget(self.image_transform)

        # init default popup
        self.popup = Popup()

        # Screen Manager
        self.s_open_file = Screen(name='Abrir Ficheiro')
        self.s_save_frame = Screen(name='Gravar Frames')
        self.s_save_area = Screen(name='Gravar Area')
        self.s_coords = Screen(name='Coords')
        self.s_wait = Screen(name='Wait')
        self.add_widget(self.main_screen)
        self.add_widget(self.s_open_file)
        self.add_widget(self.s_save_frame)
        self.add_widget(self.s_save_area)
        self.add_widget(self.s_wait)
        self.add_widget(self.s_coords)

        # filename
        self.text_input = TextInput()
        self.text = ""

        # watershed original fg and bg arrays
        self.ws_original = ""
        self.fg = []
        self.bg = []

        # Image transformation
        self.img_t_og = ""
        self.img_t_map = ""
        self.img_t_cntr = ""

        self.georef_x_min_text = ""
        self.georef_x_max_text = ""
        self.georef_y_min_text = ""
        self.georef_y_max_text = ""

    def browser(self, op):
        """ This function creates the file chooser to select image"""
        # Create Layout for popup
        try:
            self.popup.dismiss()
        except PopupException:
            pass

        self.current = 'Abrir Ficheiro'
        b_main_lay = GridLayout(rows=3)
        if op is 'seg_fg':
            file = FileChooserIconView(path=self.path,
                                       size_hint_y=0.9,
                                       multiselect=True)
        elif op is 'seg_bg':
            file = FileChooserIconView(path=self.path,
                                       size_hint_y=0.9,
                                       multiselect=True)
        else:
            file = FileChooserIconView(path=self.path,
                                       size_hint_y=0.9,
                                       multiselect=False)
        # this popup buttons and actions
        select = Button(text='Abrir', size_hint_y=0.1)
        select.bind(on_press=lambda x: self.open(file.selection, op))

        cancel = Button(text='Cancelar', size_hint_y=0.1)
        cancel.bind(on_press=self.cancel_callback)

        b_main_lay.add_widget(file)
        b_main_lay.add_widget(select)
        b_main_lay.add_widget(cancel)

        self.s_open_file.add_widget(b_main_lay)

    def cancel_callback(self, instance):
        self.current = 'Most-g4'
        self.s_open_file.clear_widgets()
        self.s_save_frame.clear_widgets()
        self.s_save_area.clear_widgets()

    def open(self, filename, op):
        """ Selects the according action bases on operation type (op) and file size"""
        if len(filename) is 1:
            if op is 'frame':
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                try:
                    im = cv2.imread(filename[0])
                    if im is not None:
                        self.analize_frame(im)
                    else:
                        cv2.imshow('img', im)
                except cv2.error as e:
                    cv2.destroyAllWindows()
                    self.end_action(
                        "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                    )
            elif op is 'vid':
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                try:
                    vid = cv2.VideoCapture(filename[0])
                    print(vid)
                    self.save_frame(
                        vid,
                        str(os.path.basename(filename[0])).split(".")[0])
                except cv2.error as e:
                    self.end_action(
                        "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                    )
            elif op is 'original':
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                try:
                    self.ws_original = cv2.imread(filename[0])
                    cntr, ws = self.fcv.watershed(self.ws_original, self.fg,
                                                  self.bg)
                except cv2.error as e:
                    self.end_action(
                        "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                    )
                finally:
                    self.save_builder(ws, cntr)
            elif op is 'img_t_og':
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                try:
                    self.img_t_og = cv2.imread(filename[0])
                    self.image_transformation_workflow(instance="", it=1)
                except cv2.error as e:
                    self.end_action(
                        "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                    )
            elif op is 'img_t_map':
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                try:
                    self.img_t_map = cv2.imread(filename[0])
                    self.image_transformation_workflow(instance="", it=2)
                except cv2.error as e:
                    self.end_action(
                        "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                    )
            elif op is 'img_t_cntr':
                try:
                    self.img_t_cntr = np.load(filename[0], allow_pickle=True)
                    self.georef_coords()
                except IOError as e:
                    self.end_action(
                        "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                    )

        else:
            if op is 'seg_fg':
                self.fg = []
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                for i in filename:
                    try:
                        print(i)
                        aux = cv2.imread(i)
                        self.fg.append(aux)
                        self.seg_workflow(instance="", it=1)
                    except cv2.error as e:
                        self.end_action(
                            "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                        )

            elif op is 'seg_bg':
                self.current = 'Most-g4'
                self.s_open_file.clear_widgets()
                self.bg = []
                for i in filename:
                    try:
                        print(i)
                        aux = cv2.imread(i)
                        self.bg.append(aux)
                        self.seg_workflow(instance="", it=2)
                    except cv2.error as e:
                        self.end_action(
                            "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
                        )

    def save_frame(self, video, folder):
        grid_s = GridLayout(rows=5)

        spinner = Spinner(
            # default value shown
            text='Tipo de ficheiro',
            text_autoupdate=True,
            # available values
            values=('.jpg', '.png'),
            # just for positioning in our example
            size_hint=(None, None),
            size=(100, 44),
            pos_hint={
                'center_x': .5,
                'center_y': .5
            })

        spinner2 = Spinner(
            # default value shown
            text='intervalo(segundos)',
            text_autoupdate=True,
            # available values
            values=('10', '20', '30', '40', '50', '60'),
            # just for positioning in our example
            size_hint=(None, None),
            size=(100, 44),
            pos_hint={
                'center_x': .5,
                'center_y': .5
            })

        spinner.bind(text=self.show_selected_value)
        input_layout = BoxLayout(orientation='horizontal', size_hint_y=0.1)
        input_label = Label(text="Timestamp base",
                            size_hint=(None, None),
                            size=(200, 44))
        self.text_input = TextInput(
            text="",
            hint_text="Ano Mês Dia Hora Minuto Segundo",
            multiline=False,
            size_hint=(None, None),
            size=(300, 44))
        self.text_input.bind(text=self.set_text)

        input_layout.add_widget(self.text_input)
        input_layout.add_widget(input_label)

        # save file
        save = Button(text="Selecionar", size_hint_y=None, size=(100, 44))
        file = FileChooserIconView(path=self.path)
        grid_s.add_widget(file)
        grid_s.add_widget(spinner2)
        grid_s.add_widget(spinner)
        grid_s.add_widget(input_layout)
        grid_s.add_widget(save)
        self.s_save_frame.add_widget(grid_s)
        self.current = 'Gravar Frames'
        save.bind(
            on_release=lambda x: self.video_helper(o=self.os,
                                                   folder=folder,
                                                   path=file.path,
                                                   ext=spinner.text,
                                                   time=int(spinner2.text),
                                                   video=video,
                                                   timestamp=self.text))

    def video_helper(self, o, folder, path, ext, time, video, timestamp):
        """ Call divide_video and report output to user """
        if timestamp is "":
            self.end_action(
                "Timestamp vazio!\nPor favor insira um valor válido")
            self.s_save_frame.clear_widgets()
            return

        self.popup = Popup(title="Divisão de video",
                           separator_height=0,
                           content=Label(text="A processsar video"),
                           size_hint=(None, None),
                           size=(300, 100))

        self.popup.bind(
            on_open=lambda x: self.vid_helper_cont(o=o,
                                                   folder=folder,
                                                   path=path,
                                                   ext=ext,
                                                   time=time,
                                                   video=video,
                                                   timestamp=timestamp))
        self.popup.open()

    def vid_helper_cont(self, o, folder, path, ext, time, video, timestamp):

        result = self.fcv.divide_video(o=o,
                                       folder=folder,
                                       path=path,
                                       ext=ext,
                                       time=time,
                                       video=video,
                                       timestamp=timestamp)
        if result is True:
            self.popup.bind(on_dismiss=lambda x: self.end_action(
                "Video separado com sucesso!\nVer" + path))
        else:
            self.popup.bind(on_dismiss=lambda x: self.end_action(
                "Oops! Algo correu mal!\nVerifique se selecionou o ficheiro correto"
            ))
        self.s_save_frame.clear_widgets()
        self.s_wait.clear_widgets()
        self.popup.dismiss()

    def end_action(self, text):
        """ sucess/error popup """
        self.current = 'Most-g4'
        grid = GridLayout(rows=2)
        label = Label(text=text)
        dismiss = Button(text='OK', size_hint_y=None, size=(50, 30))
        grid.add_widget(label)
        grid.add_widget(dismiss)
        self.popup = Popup(title="Most G4",
                           separator_height=0,
                           content=grid,
                           size_hint=(None, None),
                           size=(300, 200))
        self.popup.open()
        dismiss.bind(on_press=self.popup.dismiss)

    def analize_frame(self, frame):
        """ Auxiliary function to select foreground (relevate areas)
         and background(what to ignore on final segmentation) """

        img = self.fcv.analize_frame(frame=frame)
        self.save_builder(img)

    def save_builder(self, img="", cntr="", converted=""):
        grid_s = GridLayout(rows=5)

        # File extension
        spinner = Spinner(
            # default value shown
            text='Tipo de ficheiro',
            text_autoupdate=True,
            # available values
            values=('.jpg', '.png'),
            # just for positioning in our example
            size_hint=(None, None),
            size=(100, 44),
            pos_hint={
                'center_x': .5,
                'center_y': .5
            })

        spinner.bind(text=self.show_selected_value)

        # filename
        self.text_input = TextInput(text="",
                                    multiline=False,
                                    size_hint=(None, None),
                                    size=(300, 44))
        self.text_input.bind(text=self.set_text)

        # save file
        save = Button(text="Guardar", size_hint_y=None, size=(100, 44))
        cancel = Button(text="Cancelar", size_hint_y=None, size=(100, 44))
        file = FileChooserIconView(path=self.path)
        grid_s.add_widget(file)
        grid_s.add_widget(self.text_input)

        if img is not "":
            grid_s.add_widget(spinner)

        grid_s.add_widget(save)
        grid_s.add_widget(cancel)
        cancel.bind(on_press=self.cancel_callback)
        self.s_save_area.add_widget(grid_s)
        self.current = 'Gravar Area'
        if cntr is not "":
            save.bind(on_press=lambda x: self.save_files(
                path=file.path, extension=spinner.text, img=img, cntr=cntr))
        elif converted is not "":
            save.bind(on_press=lambda x: self.save_files(
                path=file.path, extension=spinner.text, converted=converted))
        else:
            save.bind(on_press=lambda x: self.save_files(
                path=file.path, extension=spinner.text, img=img))

    def seg_workflow(self, instance, it=0):
        """ Workflow control for segmentação button """
        try:
            self.popup.clear_widgets()
            self.popup.dismiss()
        except PopupException:
            pass
        seg_layout = GridLayout(cols=1)
        info = Label(text="Selecionar imagens áreas de interesse")
        info2 = Label(text="Selecionar imagens áreas a omitir")
        info3 = Label(text="Selecionar imagem original")
        confirm = Button(text="OK")

        if it is 0:
            seg_layout.add_widget(info)
            confirm.bind(on_press=lambda x: self.browser('seg_fg'))
        elif it is 1:
            seg_layout.add_widget(info2)
            confirm.bind(on_press=lambda x: self.browser('seg_bg'))
        elif it is 2:
            seg_layout.add_widget(info3)
            confirm.bind(on_press=lambda x: self.browser('original'))
        seg_layout.add_widget(confirm)
        self.popup = Popup(title="Segmentação",
                           separator_height=0,
                           content=seg_layout,
                           size_hint=(None, None),
                           size=(300, 150))
        self.popup.open()
        pass

    def save_files(self, path, extension, img="", cntr="", converted=""):
        self.current = 'Most-g4'

        print(converted)

        if self.os == 'win':
            # Create path with our without folder if saving multiple files
            if cntr is not "":
                path = path + "\\" + self.text
            if os.path.exists(os.path.normpath(path)) is False:
                os.mkdir(path)

            if img is not "":
                cv2.imwrite(path + "\\" + self.text + extension, img)
            if cntr is not "":
                np.save(path + "\\" + self.text, cntr)
            if converted is not "":
                np.save(path + "\\" + self.text, converted)

        else:
            # Create path with our without folder if saving multiple files
            if cntr is not "":
                path = path + "/" + self.text
            if os.path.exists(os.path.normpath(path)) is False:
                os.mkdir(path)

            if img is not "":
                cv2.imwrite(path + "/" + self.text + extension, img)
            if cntr is not "":
                np.save(path + "/" + self.text, cntr)
            if converted is not "":
                np.save(path + "/" + self.text, converted)
        self.s_save_area.clear_widgets()

    def image_transformation_workflow(self, instance, it=0):
        """ Image transformation gui controller """
        try:
            self.popup.clear_widgets()
            self.popup.dismiss()
        except PopupException:
            pass

        imt_layout = GridLayout(cols=1)
        info = Label(text="Selecionar imagem original")
        info2 = Label(text="Selecionar mapa da área")
        info3 = Label(
            text="Selecionar ficheiro dos contorno\n(imagem_original.npy)")
        confirm = Button(text="OK")

        if it is 0:
            imt_layout.add_widget(info)
            confirm.bind(on_press=lambda x: self.browser('img_t_og'))
        elif it is 1:
            imt_layout.add_widget(info2)
            confirm.bind(on_press=lambda x: self.browser('img_t_map'))
        elif it is 2:
            imt_layout.add_widget(info3)
            confirm.bind(on_press=lambda x: self.browser('img_t_cntr'))
        imt_layout.add_widget(confirm)
        self.popup = Popup(title="Transformação",
                           separator_height=0,
                           content=imt_layout,
                           size_hint=(None, None),
                           size=(300, 150))
        self.popup.open()

    def georef_coords(self):
        self.current = 'Coords'
        coods_layout = BoxLayout(orientation="vertical")

        main_in_layout = BoxLayout(orientation="vertical", size_hint_y=0.8)

        input_layout_x_min = BoxLayout(orientation='horizontal',
                                       size_hint_y=0.25)
        input_label_x_min = Label(text="X Mínimo",
                                  size_hint=(None, None),
                                  size=(200, 30))

        input_layout_x_max = BoxLayout(orientation='horizontal',
                                       size_hint_y=0.25)
        input_label_x_max = Label(text="X Máximo",
                                  size_hint=(None, None),
                                  size=(200, 30))

        input_layout_y_min = BoxLayout(orientation='horizontal',
                                       size_hint_y=0.25)
        input_label_y_min = Label(text="Y Mímino",
                                  size_hint=(None, None),
                                  size=(200, 30))

        input_layout_y_max = BoxLayout(orientation='horizontal',
                                       size_hint_y=0.25)
        input_label_y_max = Label(text="Y Máximo",
                                  size_hint=(None, None),
                                  size=(200, 30))

        georef_x_min = TextInput(text="",
                                 multiline=False,
                                 size_hint=(None, None),
                                 size=(300, 30))
        georef_x_max = TextInput(text="",
                                 multiline=False,
                                 size_hint=(None, None),
                                 size=(300, 30))
        georef_y_min = TextInput(text="",
                                 multiline=False,
                                 size_hint=(None, None),
                                 size=(300, 30))
        georef_y_max = TextInput(text="",
                                 multiline=False,
                                 size_hint=(None, None),
                                 size=(300, 30))
        confirm = Button(text="OK", size_hint_y=0.2)

        georef_y_min.bind(text=self.set_y_min)
        georef_y_max.bind(text=self.set_y_max)
        georef_x_min.bind(text=self.set_x_min)
        georef_x_max.bind(text=self.set_x_max)

        input_layout_x_min.add_widget(georef_x_min)
        input_layout_x_min.add_widget(input_label_x_min)

        input_layout_x_max.add_widget(georef_x_max)
        input_layout_x_max.add_widget(input_label_x_max)

        input_layout_y_min.add_widget(georef_y_min)
        input_layout_y_min.add_widget(input_label_y_min)

        input_layout_y_max.add_widget(georef_y_max)
        input_layout_y_max.add_widget(input_label_y_max)

        confirm.bind(on_press=self.coords_helper)

        main_in_layout.add_widget(input_layout_x_min)
        main_in_layout.add_widget(input_layout_x_max)
        main_in_layout.add_widget(input_layout_y_min)
        main_in_layout.add_widget(input_layout_y_max)
        coods_layout.add_widget(main_in_layout)
        coods_layout.add_widget(confirm)

        self.s_coords.add_widget(coods_layout)

    def coords_helper(self, instance):
        result = False
        self.current = 'Most-g4'
        self.s_coords.clear_widgets()
        self.s_open_file.clear_widgets()

        result, converted = self.imT.main(self.img_t_og, self.img_t_map,
                                          self.img_t_cntr,
                                          self.georef_x_min_text,
                                          self.georef_x_max_text,
                                          self.georef_y_min_text,
                                          self.georef_y_max_text)
        if result is True:
            self.save_builder(converted=converted)

    def show_selected_value(self, instance, text):
        """ Get current value from spinner """
        if text is not 'Tipo de ficheiro' and not '':
            return text
        else:
            print("Invalid file extension")

    def set_text(self, instance, input):
        """ Workaround to save input from textInput """
        self.text = input

    def set_x_min(self, instance, input):
        """ Workaround to save x min from textInput """
        self.georef_x_min_text = input

    def set_x_max(self, instance, input):
        """ Workaround to save x max from textInput """
        self.georef_x_max_text = input

    def set_y_min(self, instance, input):
        """ Workaround to save y min from textInput """
        self.georef_y_min_text = input

    def set_y_max(self, instance, input):
        """ Workaround to save x min from textInput """
        self.georef_y_max_text = input
class View(App):
	WINDOW_WIDTH = 800
	WINDOW_HEIGHT = 600
	def __init__(self, controller, **kwargs):
		self.controller = controller
		
		super(View, self).__init__(**kwargs)

	def build(self):
		self.dni = True
		self.startPicker = None
		self.endPicker = None
		self.cosine = False
		self.path = None
		self.fileName = None
		self.progressPopupDescription = None
		print "Building Main Window"
		layout = BoxLayout(orientation='vertical')
		topBarLayout = BoxLayout(orientation='horizontal')

		mapWidget = self._generateMap()
		topBarLayout.add_widget(mapWidget)

		layout.add_widget(topBarLayout)

		self.label = Label(text="Use the pin above to select a latitude and longitude.")
		self.progressBar= ProgressBar(max=1)

		bottomLeftBox = BoxLayout(orientation='vertical')
		bottomLeftBox.add_widget(self.label)
		# bottomLeftBox.add_widget(self.progressBar)
		bottomLeftBox.add_widget(self._generateExportButton())
		bottomLeftBox.add_widget(self._generateExportGHIButton())
		# bottomLeftBox.add_widget(self._generateHeatmapButton())
		# bottomLeftBox.add_widget(self._generateRunSimulationButton())

		self.bottomBarLayout = BoxLayout(size_hint=(1,0.5))
		self.bottomBarLayout.add_widget(bottomLeftBox)
		
		self.bottomBarLayout.add_widget(self._generateDatetimePicker())
		
		layout.add_widget(self.bottomBarLayout)

		return layout

	def updateProgressBar(self, progress):
		if self.progressBar is not None:
			self.progressBar.value = progress
		if self.progressPopupDescription is not None:
			self.progressPopupDescription.text = "Generating ("+str(round(progress * 1000)/10.0) + " %)"

	def _generateDatetimePicker(self):
		layout = BoxLayout(orientation='vertical')
		startDatetime = self.controller.getSolarStartDatetime()
		endDatetime = startDatetime + datetime.timedelta(days=7)

		#create new datetime objects because we need timezone-naive versions for the datetimepicker class. 
		startDatetime = datetime.datetime(year=startDatetime.year, month=startDatetime.month, day=startDatetime.day, hour=startDatetime.hour, minute=startDatetime.minute, tzinfo=None)
		endDatetime = datetime.datetime(year=endDatetime.year, month = endDatetime.month, day = endDatetime.day, hour = endDatetime.hour, minute=endDatetime.minute, tzinfo = None)

		self.startPicker = FlexiDatetimePicker(density=3, initialDatetime= startDatetime)
		self.endPicker = FlexiDatetimePicker(density = 3, initialDatetime = endDatetime)

		startLabel = Label(text="Choose Start Date")
		endLabel = Label(text="Choose End Date")

		layout.add_widget(startLabel)
		layout.add_widget(self.startPicker)
		layout.add_widget(endLabel)
		layout.add_widget(self.endPicker)
		return layout

	def displayErrorMessage(self,message="default"):	
		print "Building Popup"
		button = Button(text="Close")
		label = Label(text=message)
		layout= BoxLayout(orientation='vertical')
		layout.add_widget(label)
		layout.add_widget(button)

		popup = Popup(title="Error",content=layout,size_hint=(None, None), size=(400, 400))
		button.bind(on_press=lambda widget:popup.dismiss())
		popup.open()
		print "Popup Opened."

	def _on_checkbox_active(self,checkbox, value):
	    if value:
	        self.cosine = True
	    else:
	        self.cosine = False

	def _displayHeatmapGenerationPopup(self, press):
		print "Building Heatmap Generation Box"
		self.progressPopupDescription = Label(text = "Generating Heatmaps can take a long time. \nAre you sure you want to do this?")
		self.heatmapButton = Button(text="Generate")
		self.heatmapButton.bind(on_press=self._generateHeatmap)
		checkbox = CheckBox()
		checkbox.bind(active=self._on_checkbox_active)
		cancelButton = Button(text="Cancel")
		layout = BoxLayout(orientation='vertical')
		layout.add_widget(self.progressPopupDescription)
		layout.add_widget(self.heatmapButton)
		layout.add_widget(checkbox)
		layout.add_widget(cancelButton)
		layout.add_widget(self.progressBar)
		popup = Popup(title="Heatmap", content = layout, size_hint=(None, None), size = (400,400))
		cancelButton.bind(on_press = lambda widget:self._cancelHeatmapGeneration(popup))
		popup.open()
		self.heatmapPopup = popup
		print "Heatmap Generation Box Generated"

	def _generateHeatmap(self, touch):
		startDate = self.startPicker.getSelectedDatetime()
		endDate = self.endPicker.getSelectedDatetime()
		self.controller.beginGeneratingHeatmapData(startDate, endDate, cosine = self.cosine)
		if self.heatmapButton is not None and self.heatmapPopup is not None and self.progressPopupDescription is not None:
			self.heatmapPopup.content.remove_widget(self.heatmapButton)
			self.heatmapButton = None
			self.progressPopupDescription.text = "Generating Heatmap..."

	
	def _cancelHeatmapGeneration(self, popup):
		popup.dismiss()
		popup.clear_widgets()
		self.progressBar.parent = None
		self.progressBar.value = 0
		self.heatmapPopup = None
		self.heatmapButton = None
		self.progressPopupDescription = None
		self.controller.cancelHeatmapGeneration()

	def _generateMap(self):
		
		layout = RelativeLayout()
		self.mapImg=Image(source='resources/images/map.png',allow_stretch = True, keep_ratio = False)	
		self.mapPin = MapPin( parent=self.mapImg,view = self,allow_stretch = False, source='resources/images/pin.png', size=(20,34))
		self.mapImg.add_widget(self.mapPin)

		layout.add_widget(self.mapImg)

		print "Scheduling."
		# Clock.schedule_interval(self._updateMapImg, 2)
		Clock.schedule_interval(self._timeseriesGenerated, 2)

		return layout

	def _imageLoaded(self, proxyImage):
		if proxyImage.image.texture:
			self.mapImg.texture = proxyImage.image.texture
	def _timeseriesGenerated(self, timeInterval):
		finished = self.controller.saveTimeseriesIfDataGenerated()
		if finished:
			self._dismissPopup()

	def _generateExportButton(self):
		exportButton = Button(text='Export DNI as CSV')
		exportButton.bind(on_press = self._getSavePath)
		return exportButton

	def _generateExportGHIButton(self):
		exportButton = Button(text='Export GHI as CSV')
		exportButton.bind(on_press = self._getGHISavePath)
		return exportButton

	def _generateRunSimulationButton(self):
		simButton = Button(text="Run Simulation")
		simButton.bind(on_press=self._chooseSimulation)
		return simButton
	
	def _chooseSimulation(self, press):
		info = Label(text="Please select a simulation from the following options.")
		solarMarketButton = Button(text="Solar w/ Market Prices")
		solarPPAButton = Button(text="Solar w/ PPA")
		solarPPAButton.bind(on_press=self._solarPPASimOptions)
		solarPortfolioButton = Button(text="Solar with Contract Portfolio")
		batteryMarketButton = Button(text="Batteries with Market Trading Only")
		batteryPortfolioButton = Button(text="Batteries with Contract Portfolio")


		layout = BoxLayout(orientation="vertical")
		layout.add_widget(info)
		layout.add_widget(solarMarketButton)
		layout.add_widget(solarPPAButton)
		layout.add_widget(solarPortfolioButton)
		layout.add_widget(batteryMarketButton)
		layout.add_widget(batteryPortfolioButton)


		self.simulationPopup = Popup(title="Run Simulation", content=layout, size_hint=(0.9, 0.9))
		self.simulationPopup.open()

	def _solarPPASimOptions(self, press):
		if self.simulationPopup is not None:
			self.simulationPopup.title = "Solar Plant with PPA Simulation"
			contents = self.simulationPopup.content
			contents.clear_widgets()

			location = Label(text="Location = Lat: "+str(round(self.getSelectedLatitude() * 1000)/1000.0)+"  Lon: "+str(round(self.getSelectedLongitude() * 1000)/1000.0))
			contents.add_widget(location)

			startDate = Label(text="Start Date: "+str(self.startPicker.getSelectedDatetime()))
			contents.add_widget(startDate)

			endDate = Label(text="End Date: "+str(self.endPicker.getSelectedDatetime()))
			contents.add_widget(endDate)

			sizeMWLabel=Label(text="Enter Plant Nameplate Capacity (MW):")
			contents.add_widget(sizeMWLabel)
			
			self.sizeMWInput = TextInput(text='1', multiline=False)
			contents.add_widget(self.sizeMWInput)

			runSimButton = Button(text="Run Simulation")
			runSimButton.bind(on_press=self._runSolarPPASim)
			contents.add_widget(runSimButton)

	def _runSolarPPASim(self, press):
		if self.sizeMWInput is not None:
			namePlateMW = float(self.sizeMWInput.text)
			startDate = self.startPicker.getSelectedDatetime()
			endDate = self.endPicker.getSelectedDatetime()
			lat = self.getSelectedLatitude()
			lon = self.getSelectedLongitude()
			state = "nsw"
			data = self.controller.runSolarPPASimulation(state, startDate, endDate, lat, lon, namePlateMW)
			self._displaySolarSimulationResults(data)

	def _displaySolarSimulationResults(self, data):
		self.simulationPopup.title = "Results"
		self.simulationPopup.content.clear_widgets()


	def _generateHeatmapButton(self):
		heatmapButton = Button(text="Generate Heatmap")
		heatmapButton.bind(on_press=self._displayHeatmapGenerationPopup)
		return heatmapButton



	def getDatabasePath(self):
		# return "/Volumes/SOLAR/data.hdf5"
		return "./test.hdf5"

	def getFolderPath(self):
		return "./testFiles"

	def displayImage(image):
		image.show()

	def changeXY(self,x,y):
		labelText = "Current Location - Lat : "+str(round(1000*x)/1000.0) + "  Long:" + str(round(1000*y)/1000.0)
		self.label.text = labelText



	def _getSavePath(self, touch):
		self.dni = True
		if self.controller.checkDatesValid(self.getSelectedStartDate(), self.getSelectedEndDate()):
			saveDialog = SaveDialog(save=self._save, cancel=self._dismissPopup)
			self._popup = Popup(title="Save file", content=saveDialog, size_hint=(0.9, 0.9))
			self._popup.open()
		else:
			self.displayErrorMessage("Selected start and end dates not valid.")

	def _getGHISavePath(self, touch):
		self.dni = False
		if self.controller.checkDatesValid(self.getSelectedStartDate(), self.getSelectedEndDate()):
			saveDialog = SaveDialog(save=self._save, cancel=self._dismissPopup)
			self._popup = Popup(title="Save file", content=saveDialog, size_hint=(0.9, 0.9))
			self._popup.open()
		else:
			self.displayErrorMessage("Selected start and end dates not valid.")

	def _showProgressPopup(self):
		content = BoxLayout(orientation = 'vertical')
		self.progressPopupDescription= Label(text = "0 %")
		content.add_widget(self.progressPopupDescription)
		content.add_widget(self.progressBar)
		self._popup = Popup(title="Progress:", content = content, size_hint=(0.5,0.5), auto_dismiss=False)
		self._popup.open()


	def _dismissPopup(self):
		try:
			self.progressBar= ProgressBar(max=1)
			self._popup.clear_widgets()
			self._popup.dismiss()
		except AttributeError:
			print "No popup to close!"


	def _save(self, path, fileName):
		pattern = ".*\/.*"
		# regex = re.compile(pattern)
		if re.match(pattern, fileName): #If we find the filename is a directory, do nothing.
			path = fileName
		else: #otherwise join path and filename.
			path = path + "/"+ fileName

		self._dismissPopup()
		print "Popup dismissed"
		self._showProgressPopup()
		self.controller.exportTimeseries(path, self.dni)
		
		

	def getSelectedStartDate(self):
		if self.startPicker:
			return self.startPicker.getSelectedDatetime()
		else:
			return self.controller.getSolarStartDatetime()

	def getSelectedEndDate(self):
		if self.endPicker:
			return self.endPicker.getSelectedDatetime()
		else:
			return self.controller.getSolarEndDatetime()

	def getSelectedLatitude(self):
		return self.mapPin.getLatLong()[0]

	def getSelectedLongitude(self):
		return self.mapPin.getLatLong()[1]

	def reportWriteFinished(self):
		self._dismissPopup()