Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.85 KB | None | 0 0
  1. """Internal Skeleton Code"""
  2. """
  3. Changelog:
  4. v1: original
  5. v1.2: added changelog; adjusted degrees to being in K rather than C; fixed a number of small
  6. bugs.
  7. v1.3: added tolerances for negatives
  8. v2: starting the GUI with a .kv language string, to load a three tabbed panel and a button for
  9. calculations at the bottom of the screen, adding colouring to buttons and text
  10.    Unresolved: canvas.before issue for colouring
  11.                putting colours in hex rather than the 0-1 scale system
  12. v2.2: added a new tab for basic graph work; adding entryfields
  13.    Unresolved: For the things I need to call again, can i call them without printing all the labels
  14.    etc again?
  15. v2.3: adding functionality to entries, with a popup window to display outputs
  16.    Unresolved: Add focus switching with tab
  17.    Resolved: canvas.before colouration; hex colouration
  18. v3: adding a save function, which saves all inputs in a list and loads them back in on a label
  19. """
  20. #---------------------------#Importing Needed Libraries#---------------------------#
  21. from kivy.app import App
  22. from kivy.lang import Builder
  23. from kivy.uix.popup import Popup
  24. from kivy.uix.label import Label
  25. from kivy.uix.button import Button
  26. from math import sin, cos, tan, e, pi
  27. from kivy.uix.textinput import TextInput
  28. from kivy.properties import ObjectProperty
  29. from kivy.uix.tabbedpanel import TabbedPanel
  30. from kivy.uix.floatlayout import FloatLayout
  31. from kivy.uix.screenmanager import ScreenManager, Screen
  32. #----------------------------------#Build String#----------------------------------#
  33. root_widget = Builder.load_string("""
  34. <TabSys>:
  35.    size_hint: 1, 1
  36.    pos_hint: {"center_x": .5, "center_y": .5}
  37.    do_default_tab: False
  38.    canvas.before:
  39.        Color:
  40.            rgba: (1, 1, 1, .2)
  41.        Rectangle:
  42.            size: self.size
  43.            pos: self.pos
  44.  
  45.    TabbedPanelItem:
  46.        id: PSC_calculator
  47.        text: "PSC"
  48.        canvas:
  49.            Color:
  50.                rgba: (1, 1, 1, .15)
  51.            Rectangle:
  52.                size: self.size
  53.                pos: self.pos
  54.        FloatLayout:
  55.            Label:
  56.                id: CP_label
  57.                size: self.texture_size
  58.                text_size: cm(7), cm(2)
  59.                text: "Cp = Specific heat of air at constant pressure [kJ kg−1 °K^−1]"
  60.                font_size: 30
  61.                font_name: "Arial"
  62.                pos_hint: {"center_x": .175, "center_y": 1}
  63.            TextInput:
  64.                id: Cp_entry
  65.                multiline: False
  66.                size_hint: .3, .08
  67.                pos_hint: {"center_x": .152, "center_y": .87}
  68.                hint_text: 'Specific heat of air'
  69.                font_size: 30
  70.                font_name: "Arial"
  71.                font_color: (1, 1, 1, 1)
  72.                background_color: (.3, .3, .3, 1)
  73.                hint_text_color: (1, 1, 1, .5)
  74.                cursor_color: (1, 1, 1, 1)
  75.                selection_color: (1, 1, 1, .3)
  76.                foreground_color: (1, 1, 1, 1)
  77.                disabled_foreground_color: (1, 1, 1, .5)
  78.            Label:
  79.                id: P_label
  80.                size: self.texture_size
  81.                text_size: cm(7), cm(2)
  82.                text: "P = Atmospheric Pressure [kPa]"
  83.                font_size: 30
  84.                font_name: "Arial"
  85.                pos_hint: {"center_x": .175, "center_y": .8}
  86.            TextInput:
  87.                id: P_entry
  88.                multiline: False
  89.                size_hint: .3, .08
  90.                pos_hint: {"center_x": .152, "center_y": .67}
  91.                hint_text: 'Atmospheric pressure'
  92.                font_size: 30
  93.                font_name: "Arial"
  94.                font_color: (1, 1, 1, 1)
  95.                background_color: (.3, .3, .3, 1)
  96.                hint_text_color: (1, 1, 1, .5)
  97.                cursor_color: (1, 1, 1, 1)
  98.                selection_color: (1, 1, 1, .3)
  99.                foreground_color: (1, 1, 1, 1)
  100.                disabled_foreground_color: (1, 1, 1, .5)
  101.            Label:
  102.                id: lhv_label
  103.                size: self.texture_size
  104.                text_size: cm(7), cm(2)
  105.                text: "λ = Latent heat of vaporization [kJ kg^−1]"
  106.                font_size: 30
  107.                font_name: "Arial"
  108.                pos_hint: {"center_x": .175, "center_y": .6}
  109.            TextInput:
  110.                id: lhv_entry
  111.                multiline: False
  112.                size_hint: .3, .08
  113.                pos_hint: {"center_x": .152, "center_y": .47}
  114.                hint_text: 'Latent heat of vaporization'
  115.                font_size: 30
  116.                font_name: "Arial"
  117.                font_color: (1, 1, 1, 1)
  118.                background_color: (.3, .3, .3, 1)
  119.                hint_text_color: (1, 1, 1, .5)
  120.                cursor_color: (1, 1, 1, 1)
  121.                selection_color: (1, 1, 1, .3)
  122.                foreground_color: (1, 1, 1, 1)
  123.                disabled_foreground_color: (1, 1, 1, .5)
  124.            Label:
  125.                id: MWr_label
  126.                size: self.texture_size
  127.                text_size: cm(7), cm(2)
  128.                text: "*Here we assume that the ratio molecular weight of water vapour/dry air (MWr) is 0.622"
  129.                font_size: 30
  130.                font_name: "Arial"
  131.                pos_hint: {"center_x": .175, "center_y": .3}
  132.            Button:
  133.                id: calculate_psc
  134.                text: "[color=e6e6e6]Calculate[/color]"
  135.                font_name: "Arial"
  136.                font_size: 30
  137.                pos_hint: {"x": 0, "y": 0}
  138.                size_hint: 1, .1
  139.                markup: True
  140.                on_release: root.calculate_psc_clicked(Cp_entry.text, P_entry.text, lhv_entry.text)
  141.            Label:
  142.                id: psc_equation
  143.                size: self.texture_size
  144.                text_size: cm(10), cm(2)
  145.                text: "Psychrometric Constant = (Cp*P)/(λ*MWr)"
  146.                font_size: 40
  147.                font_name: "Arial"
  148.                pos_hint: {"center_x": .7, "center_y": 1}
  149.            Label:
  150.                id: psc_notes
  151.                size: self.texture_size
  152.                text_size: cm(10), cm(5)
  153.                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."
  154.                font_size: 30
  155.                font_name: "Arial"
  156.                pos_hint: {"center_x": .7, "center_y": .95}
  157.            Button:
  158.                id: psc_save_button
  159.                text: "[color=e6e6e6]Calculate[/color]"
  160.                font_name: "Arial"
  161.                font_size: 30
  162.                pos_hint: {"x": 0, "y": 0}
  163.                size_hint: 1, .1
  164.                markup: True
  165.                on_release: root.calculate_psc_clicked(Cp_entry.text, P_entry.text, lhv_entry.text)
  166.  
  167.    TabbedPanelItem:
  168.        text: "ETP"
  169.        canvas:
  170.            Color:
  171.                rgba: (1, 1, 1, .15)
  172.            Rectangle:
  173.                size: self.size
  174.                pos: self.pos
  175.        FloatLayout:
  176.            Label:
  177.                id: k2_label
  178.                size: self.texture_size
  179.                text_size: cm(7), cm(2)
  180.                text: "k2 = Specific heat of air at constant pressure [kJ kg−1 °K^−1]"
  181.                font_size: 30
  182.                font_name: "Arial"
  183.                pos_hint: {"center_x": .175, "center_y": 1}
  184.            TextInput:
  185.                id: k2_entry
  186.                multiline: False
  187.                size_hint: .3, .08
  188.                pos_hint: {"center_x": .152, "center_y": .87}
  189.                hint_text: 'Specific heat of air'
  190.                font_size: 30
  191.                font_name: "Arial"
  192.                font_color: (1, 1, 1, 1)
  193.                background_color: (.3, .3, .3, 1)
  194.                hint_text_color: (1, 1, 1, .5)
  195.                cursor_color: (1, 1, 1, 1)
  196.                selection_color: (1, 1, 1, .3)
  197.                foreground_color: (1, 1, 1, 1)
  198.                disabled_foreground_color: (1, 1, 1, .5)
  199.            Label:
  200.                id: k3_label
  201.                size: self.texture_size
  202.                text_size: cm(7), cm(2)
  203.                text: "k3 = Air density [Kg m^3]"
  204.                font_size: 30
  205.                font_name: "Arial"
  206.                pos_hint: {"center_x": .175, "center_y": .8}
  207.            TextInput:
  208.                id: k3_entry
  209.                multiline: False
  210.                size_hint: .3, .08
  211.                pos_hint: {"center_x": .152, "center_y": .67}
  212.                hint_text: 'Air density'
  213.                font_size: 30
  214.                font_name: "Arial"
  215.                font_color: (1, 1, 1, 1)
  216.                background_color: (.3, .3, .3, 1)
  217.                hint_text_color: (1, 1, 1, .5)
  218.                cursor_color: (1, 1, 1, 1)
  219.                selection_color: (1, 1, 1, .3)
  220.                foreground_color: (1, 1, 1, 1)
  221.                disabled_foreground_color: (1, 1, 1, .5)
  222.            Label:
  223.                id: psc_label
  224.                size: self.texture_size
  225.                text_size: cm(7), cm(2)
  226.                text: "γ = Psychrometric constant [kJ kg^−1]"
  227.                font_size: 30
  228.                font_name: "Arial"
  229.                pos_hint: {"center_x": .175, "center_y": .6}
  230.            TextInput:
  231.                id: psc_entry
  232.                multiline: False
  233.                size_hint: .3, .08
  234.                pos_hint: {"center_x": .152, "center_y": .47}
  235.                hint_text: 'Psychrometric Constant'
  236.                font_size: 30
  237.                font_name: "Arial"
  238.                font_color: (1, 1, 1, 1)
  239.                background_color: (.3, .3, .3, 1)
  240.                hint_text_color: (1, 1, 1, .5)
  241.                cursor_color: (1, 1, 1, 1)
  242.                selection_color: (1, 1, 1, .3)
  243.                foreground_color: (1, 1, 1, 1)
  244.                disabled_foreground_color: (1, 1, 1, .5)
  245.            Label:
  246.                id: gsc_label
  247.                size: self.texture_size
  248.                text_size: cm(7), cm(2)
  249.                text: "Stomatal conductance [mmol m^-2 s^-1]"
  250.                font_size: 30
  251.                font_name: "Arial"
  252.                pos_hint: {"center_x": .175, "center_y": .4}
  253.            TextInput:
  254.                id: gsc_entry
  255.                multiline: False
  256.                size_hint: .3, .08
  257.                pos_hint: {"center_x": .152, "center_y": .27}
  258.                hint_text: 'Stomatal conductance'
  259.                font_size: 30
  260.                font_name: "Arial"
  261.                font_color: (1, 1, 1, 1)
  262.                background_color: (.3, .3, .3, 1)
  263.                hint_text_color: (1, 1, 1, .5)
  264.                cursor_color: (1, 1, 1, 1)
  265.                selection_color: (1, 1, 1, .3)
  266.                foreground_color: (1, 1, 1, 1)
  267.                disabled_foreground_color: (1, 1, 1, .5)
  268.            Label:
  269.                id: D_label
  270.                size: self.texture_size
  271.                text_size: cm(6), cm(2)
  272.                text: "D = Atmospheric saturation deficit [%]"
  273.                font_size: 30
  274.                font_name: "Arial"
  275.                pos_hint: {"center_x": .55, "center_y": 1}
  276.            TextInput:
  277.                id: D_entry
  278.                multiline: False
  279.                size_hint: .3, .08
  280.                pos_hint: {"center_x": .55, "center_y": .87}
  281.                hint_text: 'Atmospheric saturation deficit'
  282.                font_size: 30
  283.                font_name: "Arial"
  284.                font_color: (1, 1, 1, 1)
  285.                background_color: (.3, .3, .3, 1)
  286.                hint_text_color: (1, 1, 1, .5)
  287.                cursor_color: (1, 1, 1, 1)
  288.                selection_color: (1, 1, 1, .3)
  289.                foreground_color: (1, 1, 1, 1)
  290.                disabled_foreground_color: (1, 1, 1, .5)
  291.            Label:
  292.                id: LAI_label
  293.                size: self.texture_size
  294.                text_size: cm(6), cm(2)
  295.                text: "LAI = leaf area index [m^2]"
  296.                font_size: 30
  297.                font_name: "Arial"
  298.                pos_hint: {"center_x": .55, "center_y": .8}
  299.            TextInput:
  300.                id: LAI_entry
  301.                multiline: False
  302.                size_hint: .3, .08
  303.                pos_hint: {"center_x": .55, "center_y": .67}
  304.                hint_text: 'Leaf area index'
  305.                font_size: 30
  306.                font_name: "Arial"
  307.                font_color: (1, 1, 1, 1)
  308.                background_color: (.3, .3, .3, 1)
  309.                hint_text_color: (1, 1, 1, .5)
  310.                cursor_color: (1, 1, 1, 1)
  311.                selection_color: (1, 1, 1, .3)
  312.                foreground_color: (1, 1, 1, 1)
  313.                disabled_foreground_color: (1, 1, 1, .5)
  314.            Label:
  315.                id: etp_equation
  316.                size: self.texture_size
  317.                text_size: cm(10), cm(2)
  318.                text: "Evapotranspiration = [k2*k3/k1*γ]D*gsc*LAI"
  319.                font_size: 40
  320.                font_name: "Arial"
  321.                pos_hint: {"center_x": .642, "center_y": .59}
  322.            Label:
  323.                id: etp_notes
  324.                size: self.texture_size
  325.                text_size: cm(10), cm(5)
  326.                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"
  327.                font_size: 30
  328.                font_name: "Arial"
  329.                pos_hint: {"center_x": .642, "center_y": .5}
  330. """)
  331.  
  332. #-----------------------------------#Main Class#-----------------------------------#
  333. class TabSys(TabbedPanel):
  334.     savedfiles_hydraulus = []
  335.     def calculate_psc_clicked(self, Cp_text, P_text, lhv_text):
  336.         global psychrometric_constant
  337.         #when they click the button which is id'd as "calculate_psc," this function
  338.         #will pull the values and perform the calculations
  339.         try:
  340.             psychrometric_constant = (float(Cp_text)*float(P_text))/(float(lhv_text)*float(2.26))
  341.             self.psc_answer()
  342.         except ValueError:
  343.             """Needs a popup for wrong results"""
  344.  
  345.     def calculate_etp_clicked(self, k2_text, k3_text, psc_text, gsc_text, D_text, LAI_text):
  346.         global evapotranspiration
  347.         #when they click the button which is id'd as "calculate_etp," this function
  348.         #will pull the values and perform Evapotranspiration = [k2*k3/k1*γ]D*gsc*LAI
  349.         try:
  350.             evapotranspiration = ((float(k2_text)*float(k3_text))/(2.26*float(psc_text))*float(D_text)*float(gsc_text)*float(LAI_text))
  351.             etp_answer(self)
  352.         except ValueError: #in the cases of value errors, this should ask them to reprint their data
  353.             print("Please enter a valid input in text inputs.")
  354. #---------------------------------#Popup Windows#----------------------------------#
  355.     #the popups print out the answers and offer the user an option to save or dismiss the window
  356.     def psc_answer(self):
  357.         global Flag
  358.         Flag = False
  359.  
  360.         #Layout
  361.         popupscreen = FloatLayout()
  362.         self.psc_notes_label = Label(text = "Psychrometric constant: (Cp*P)/(λ*MWr)", pos_hint = {"center_x": 0.5, "center_y": 0.7})
  363.         self.psc_answer = Label(text = str(psychrometric_constant), pos_hint = {"center_x": 0.5, "center_y": 0.4})
  364.         popupscreen.add_widget(self.psc_notes_label)
  365.         popupscreen.add_widget(self.psc_answer)
  366.  
  367.         #Save Result
  368.         self.save_psc = Button(text = "Save", pos_hint = {"center_x": .875, "center_y": .065}, size_hint = (.3, .2))
  369.         self.save_psc.bind(on_release = self.save_screen)
  370.         popupscreen.add_widget(self.save_psc)
  371.  
  372.         #Dismiss Window
  373.         self.cancel_psc_answer = Button(text = "Cancel", pos_hint = {"center_x": .575, "center_y": .065}, size_hint = (.3, .2))
  374.         self.cancel_psc_answer.bind(on_release = self.dismisspsc)
  375.         popupscreen.add_widget(self.cancel_psc_answer)
  376.  
  377.         #Window Setup
  378.         self.popup = Popup(title="Result", content = popupscreen,  size_hint=(.5, .5), size=(400, 400),
  379.         separator_color = [217/255, 179/255, 255/255., .85])
  380.         self.popup.open()
  381.  
  382.     #Dismiss Function
  383.     def dismisspsc(self, *args):
  384.         self.popup.dismiss()
  385.  
  386.     def etp_answer(self):
  387.         Flag = True
  388.         popup = Popup(title = "Result",
  389.                 content=Label(text = "Rate of evapotranspiration:"
  390.                               "\n" + "Evapotranspiration = [k2*k3/k1*γ]D*gsc*LAI"
  391.                               "\n"
  392.                               "\n" + str(evapotranspiration), halign = 'center', markup = True),
  393.                               size_hint=(.5, .5), size=(400, 400), separator_color =  [217/255, 179/255, 255/255., .85])
  394.         popup.open()
  395. #--------------------------------#Saving Function#---------------------------------#
  396.     def save_screen(self, *args):
  397.         #I'm creating a secondary popup to name and then finalize the saving of the
  398.         self.namesavescreen = FloatLayout()
  399.         self.filename_prompt = Label(text = "Save as:", pos_hint = {"center_x": 0.5, "center_y": 0.7})
  400.         self.namesavescreen.add_widget(self.filename_prompt)
  401.  
  402.         #Window Setup
  403.         popup_save = Popup(title="Result", content = self.namesavescreen,  size_hint=(.3, .3), size=(400, 400),
  404.         separator_color = [217/255, 179/255, 255/255., .85])
  405.         popup_save.open()
  406.  
  407.  
  408. class HydraulusApp(App):
  409.     def build(self):
  410.         return TabSys()
  411.         #this will just allow the build string to run its function along with returning the
  412.         # tabsys class created at the start of it.
  413.  
  414.  
  415. if __name__ == '__main__':
  416.     HydraulusApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement