Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Internal Skeleton Code"""
- """
- Changelog:
- v1: original
- v1.2: added changelog; adjusted degrees to being in K rather than C; fixed a number of small
- bugs.
- v1.3: added tolerances for negatives
- v2: starting the GUI with a .kv language string, to load a three tabbed panel and a button for
- calculations at the bottom of the screen, adding colouring to buttons and text
- Unresolved: canvas.before issue for colouring
- putting colours in hex rather than the 0-1 scale system
- v2.2: added a new tab for basic graph work; adding entryfields
- Unresolved: For the things I need to call again, can i call them without printing all the labels
- etc again?
- v2.3: adding functionality to entries, with a popup window to display outputs
- Unresolved: Add focus switching with tab
- Resolved: canvas.before colouration; hex colouration
- v3: adding a save function, which saves all inputs in a list and loads them back in on a label
- """
- #---------------------------#Importing Needed Libraries#---------------------------#
- from kivy.app import App
- from kivy.lang import Builder
- from kivy.uix.popup import Popup
- from kivy.uix.label import Label
- from kivy.uix.button import Button
- from math import sin, cos, tan, e, pi
- from kivy.uix.textinput import TextInput
- from kivy.properties import ObjectProperty
- from kivy.uix.tabbedpanel import TabbedPanel
- from kivy.uix.floatlayout import FloatLayout
- from kivy.uix.screenmanager import ScreenManager, Screen
- #----------------------------------#Build String#----------------------------------#
- root_widget = Builder.load_string("""
- <TabSys>:
- size_hint: 1, 1
- pos_hint: {"center_x": .5, "center_y": .5}
- do_default_tab: False
- canvas.before:
- Color:
- rgba: (1, 1, 1, .2)
- Rectangle:
- size: self.size
- pos: self.pos
- TabbedPanelItem:
- id: PSC_calculator
- text: "PSC"
- canvas:
- Color:
- rgba: (1, 1, 1, .15)
- Rectangle:
- size: self.size
- pos: self.pos
- FloatLayout:
- Label:
- id: CP_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "Cp = Specific heat of air at constant pressure [kJ kg−1 °K^−1]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": 1}
- TextInput:
- id: Cp_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .87}
- hint_text: 'Specific heat of air'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: P_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "P = Atmospheric Pressure [kPa]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": .8}
- TextInput:
- id: P_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .67}
- hint_text: 'Atmospheric pressure'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: lhv_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "λ = Latent heat of vaporization [kJ kg^−1]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": .6}
- TextInput:
- id: lhv_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .47}
- hint_text: 'Latent heat of vaporization'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: MWr_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "*Here we assume that the ratio molecular weight of water vapour/dry air (MWr) is 0.622"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": .3}
- Button:
- id: calculate_psc
- text: "[color=e6e6e6]Calculate[/color]"
- font_name: "Arial"
- font_size: 30
- pos_hint: {"x": 0, "y": 0}
- size_hint: 1, .1
- markup: True
- on_release: root.calculate_psc_clicked(Cp_entry.text, P_entry.text, lhv_entry.text)
- Label:
- id: psc_equation
- size: self.texture_size
- text_size: cm(10), cm(2)
- text: "Psychrometric Constant = (Cp*P)/(λ*MWr)"
- font_size: 40
- font_name: "Arial"
- pos_hint: {"center_x": .7, "center_y": 1}
- Label:
- id: psc_notes
- size: self.texture_size
- text_size: cm(10), cm(5)
- text: "This relates the partial pressure of water in air to air temperature - actual vapour pressure can be found from paired dry and wet thermometer bulb readings. This is also important for the equation for evapotranspiration."
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .7, "center_y": .95}
- Button:
- id: psc_save_button
- text: "[color=e6e6e6]Calculate[/color]"
- font_name: "Arial"
- font_size: 30
- pos_hint: {"x": 0, "y": 0}
- size_hint: 1, .1
- markup: True
- on_release: root.calculate_psc_clicked(Cp_entry.text, P_entry.text, lhv_entry.text)
- TabbedPanelItem:
- text: "ETP"
- canvas:
- Color:
- rgba: (1, 1, 1, .15)
- Rectangle:
- size: self.size
- pos: self.pos
- FloatLayout:
- Label:
- id: k2_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "k2 = Specific heat of air at constant pressure [kJ kg−1 °K^−1]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": 1}
- TextInput:
- id: k2_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .87}
- hint_text: 'Specific heat of air'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: k3_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "k3 = Air density [Kg m^3]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": .8}
- TextInput:
- id: k3_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .67}
- hint_text: 'Air density'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: psc_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "γ = Psychrometric constant [kJ kg^−1]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": .6}
- TextInput:
- id: psc_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .47}
- hint_text: 'Psychrometric Constant'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: gsc_label
- size: self.texture_size
- text_size: cm(7), cm(2)
- text: "Stomatal conductance [mmol m^-2 s^-1]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .175, "center_y": .4}
- TextInput:
- id: gsc_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .152, "center_y": .27}
- hint_text: 'Stomatal conductance'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: D_label
- size: self.texture_size
- text_size: cm(6), cm(2)
- text: "D = Atmospheric saturation deficit [%]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .55, "center_y": 1}
- TextInput:
- id: D_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .55, "center_y": .87}
- hint_text: 'Atmospheric saturation deficit'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: LAI_label
- size: self.texture_size
- text_size: cm(6), cm(2)
- text: "LAI = leaf area index [m^2]"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .55, "center_y": .8}
- TextInput:
- id: LAI_entry
- multiline: False
- size_hint: .3, .08
- pos_hint: {"center_x": .55, "center_y": .67}
- hint_text: 'Leaf area index'
- font_size: 30
- font_name: "Arial"
- font_color: (1, 1, 1, 1)
- background_color: (.3, .3, .3, 1)
- hint_text_color: (1, 1, 1, .5)
- cursor_color: (1, 1, 1, 1)
- selection_color: (1, 1, 1, .3)
- foreground_color: (1, 1, 1, 1)
- disabled_foreground_color: (1, 1, 1, .5)
- Label:
- id: etp_equation
- size: self.texture_size
- text_size: cm(10), cm(2)
- text: "Evapotranspiration = [k2*k3/k1*γ]D*gsc*LAI"
- font_size: 40
- font_name: "Arial"
- pos_hint: {"center_x": .642, "center_y": .59}
- Label:
- id: etp_notes
- size: self.texture_size
- text_size: cm(10), cm(5)
- text: "This equation will give you the rate of transpiration, which can then be used to find the overall amount of water which will be lost into the atmosphere over time. The total use of water in production of fruit and in growth is [water rate] - [ETP]. We also assume that k1 (the latent heat of vaporization of water) is 2.26 kJ kg^-1"
- font_size: 30
- font_name: "Arial"
- pos_hint: {"center_x": .642, "center_y": .5}
- """)
- #-----------------------------------#Main Class#-----------------------------------#
- class TabSys(TabbedPanel):
- savedfiles_hydraulus = []
- def calculate_psc_clicked(self, Cp_text, P_text, lhv_text):
- global psychrometric_constant
- #when they click the button which is id'd as "calculate_psc," this function
- #will pull the values and perform the calculations
- try:
- psychrometric_constant = (float(Cp_text)*float(P_text))/(float(lhv_text)*float(2.26))
- self.psc_answer()
- except ValueError:
- """Needs a popup for wrong results"""
- def calculate_etp_clicked(self, k2_text, k3_text, psc_text, gsc_text, D_text, LAI_text):
- global evapotranspiration
- #when they click the button which is id'd as "calculate_etp," this function
- #will pull the values and perform Evapotranspiration = [k2*k3/k1*γ]D*gsc*LAI
- try:
- evapotranspiration = ((float(k2_text)*float(k3_text))/(2.26*float(psc_text))*float(D_text)*float(gsc_text)*float(LAI_text))
- etp_answer(self)
- except ValueError: #in the cases of value errors, this should ask them to reprint their data
- print("Please enter a valid input in text inputs.")
- #---------------------------------#Popup Windows#----------------------------------#
- #the popups print out the answers and offer the user an option to save or dismiss the window
- def psc_answer(self):
- global Flag
- Flag = False
- #Layout
- popupscreen = FloatLayout()
- self.psc_notes_label = Label(text = "Psychrometric constant: (Cp*P)/(λ*MWr)", pos_hint = {"center_x": 0.5, "center_y": 0.7})
- self.psc_answer = Label(text = str(psychrometric_constant), pos_hint = {"center_x": 0.5, "center_y": 0.4})
- popupscreen.add_widget(self.psc_notes_label)
- popupscreen.add_widget(self.psc_answer)
- #Save Result
- self.save_psc = Button(text = "Save", pos_hint = {"center_x": .875, "center_y": .065}, size_hint = (.3, .2))
- self.save_psc.bind(on_release = self.save_screen)
- popupscreen.add_widget(self.save_psc)
- #Dismiss Window
- self.cancel_psc_answer = Button(text = "Cancel", pos_hint = {"center_x": .575, "center_y": .065}, size_hint = (.3, .2))
- self.cancel_psc_answer.bind(on_release = self.dismisspsc)
- popupscreen.add_widget(self.cancel_psc_answer)
- #Window Setup
- self.popup = Popup(title="Result", content = popupscreen, size_hint=(.5, .5), size=(400, 400),
- separator_color = [217/255, 179/255, 255/255., .85])
- self.popup.open()
- #Dismiss Function
- def dismisspsc(self, *args):
- self.popup.dismiss()
- def etp_answer(self):
- Flag = True
- popup = Popup(title = "Result",
- content=Label(text = "Rate of evapotranspiration:"
- "\n" + "Evapotranspiration = [k2*k3/k1*γ]D*gsc*LAI"
- "\n"
- "\n" + str(evapotranspiration), halign = 'center', markup = True),
- size_hint=(.5, .5), size=(400, 400), separator_color = [217/255, 179/255, 255/255., .85])
- popup.open()
- #--------------------------------#Saving Function#---------------------------------#
- def save_screen(self, *args):
- #I'm creating a secondary popup to name and then finalize the saving of the
- self.namesavescreen = FloatLayout()
- self.filename_prompt = Label(text = "Save as:", pos_hint = {"center_x": 0.5, "center_y": 0.7})
- self.namesavescreen.add_widget(self.filename_prompt)
- #Window Setup
- popup_save = Popup(title="Result", content = self.namesavescreen, size_hint=(.3, .3), size=(400, 400),
- separator_color = [217/255, 179/255, 255/255., .85])
- popup_save.open()
- class HydraulusApp(App):
- def build(self):
- return TabSys()
- #this will just allow the build string to run its function along with returning the
- # tabsys class created at the start of it.
- if __name__ == '__main__':
- HydraulusApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement