{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from tkinter import *\n", "from metpy.calc import *\n", "from metpy.units import units" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class thermo:\n", " \n", " def __init__(self,master):\n", " \n", " # Master frame - entire window\n", " master.title('Thermo Calculations')\n", " self.outer_frame = Frame(master)\n", " self.outer_frame.pack()\n", " \n", " # Inner frame for left side\n", " self.left_frame = Frame(self.outer_frame)\n", " self.left_frame.pack(side=LEFT)\n", " \n", " # Temperature input frame\n", " self.temp_frame = LabelFrame(self.left_frame, text='Temperature (K)', bg='blue', fg='white', height=125, width=250)\n", " self.temp_frame.pack(side=TOP)\n", " self.temp_frame.pack_propagate(0)\n", " self.temp_slider = Scale(self.temp_frame, from_=220, to=330, orient=HORIZONTAL, command=self.calculations)\n", " self.temp_slider.set(275)\n", " self.temp_slider.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Relative humidity deisplay frame\n", " self.rh_frame = LabelFrame(self.left_frame, text='Relative Humidity (%)', bg='blue', fg='white', height=100, width=250)\n", " self.rh_frame.pack()\n", " self.rh_frame.pack_propagate(0)\n", " self.rh_slider = Scale(self.rh_frame, from_=0, to=100, orient=HORIZONTAL, command=self.calculations)\n", " self.rh_slider.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Dewpoint temp display frame\n", " self.dewpt_frame = LabelFrame(self.left_frame, text='Dewpoint Temperature', height=100, width=250)\n", " self.dewpt_frame.pack()\n", " self.dewpt_frame.pack_propagate(0)\n", " self.dewpt_text = StringVar()\n", " self.dewpt_label = Label(self.dewpt_frame, textvariable=self.dewpt_text)\n", " self.dewpt_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Mixing ratio display frame\n", " self.mix_ratio_frame = LabelFrame(self.left_frame, text='Mixing Ratio', height=100, width=250)\n", " self.mix_ratio_frame.pack()\n", " self.mix_ratio_frame.pack_propagate(0)\n", " self.mix_ratio_text = StringVar()\n", " self.mix_ratio_label = Label(self.mix_ratio_frame, textvariable=self.mix_ratio_text)\n", " self.mix_ratio_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Saturation mixing ratio display frame\n", " self.sat_mix_ratio_frame = LabelFrame(self.left_frame, text='Saturation Mixing Ratio', height=100, width=250)\n", " self.sat_mix_ratio_frame.pack()\n", " self.sat_mix_ratio_frame.pack_propagate(0)\n", " self.sat_mix_ratio_text = StringVar()\n", " self.sat_mix_ratio_label = Label(self.sat_mix_ratio_frame, textvariable=self.sat_mix_ratio_text)\n", " self.sat_mix_ratio_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Inner frame for right side\n", " self.right_frame = Frame(self.outer_frame)\n", " self.right_frame.pack()\n", " \n", " # Pressure input frame\n", " self.press_frame = LabelFrame(self.right_frame, text='Pressure (mb)', bg='blue', fg='white', height=125, width=250)\n", " self.press_frame.pack()\n", " self.press_frame.pack_propagate(0)\n", " self.press_slider = Scale(self.press_frame, from_=990, to=1030, orient=HORIZONTAL, command=self.calculations)\n", " self.press_slider.set(1010)\n", " self.press_slider.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Potential temp display frame\n", " self.pot_temp_frame = LabelFrame(self.right_frame, text='Potential Temperature', height=100, width=250)\n", " self.pot_temp_frame.pack()\n", " self.pot_temp_frame.pack_propagate(0)\n", " self.pot_temp_text = StringVar()\n", " self.pot_temp_label = Label(self.pot_temp_frame, textvariable=self.pot_temp_text)\n", " self.pot_temp_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Equiv potential temp display frame\n", " self.equiv_pot_temp_frame = LabelFrame(self.right_frame, text='Equivalent Potential Temperature', height=100, width=250)\n", " self.equiv_pot_temp_frame.pack()\n", " self.equiv_pot_temp_frame.pack_propagate(0)\n", " self.equiv_pot_temp_text = StringVar()\n", " self.equiv_pot_temp_label = Label(self.equiv_pot_temp_frame, textvariable=self.equiv_pot_temp_text)\n", " self.equiv_pot_temp_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Vapor Pressure display frame\n", " self.vap_press_frame = LabelFrame(self.right_frame, text='Vapor Pressure', height=100, width=250)\n", " self.vap_press_frame.pack()\n", " self.vap_press_frame.pack_propagate(0)\n", " self.vap_press_text = StringVar()\n", " self.vap_press_label = Label(self.vap_press_frame, textvariable=self.vap_press_text)\n", " self.vap_press_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", " # Saturation vapor pressure display frame\n", " self.sat_vap_press_frame = LabelFrame(self.right_frame, text='Saturation Vapor Pressure', height=100, width=250)\n", " self.sat_vap_press_frame.pack()\n", " self.sat_vap_press_frame.pack_propagate(0)\n", " self.sat_vap_press_text = StringVar()\n", " self.sat_vap_press_label = Label(self.sat_vap_press_frame, textvariable=self.sat_vap_press_text)\n", " self.sat_vap_press_label.place(relx=.5, rely=.5, anchor='center')\n", " \n", "\n", " def calculations(self, value=0): \n", " # value is a dummy variable, needed when widgets call a command - they also send a value\n", " result = self.thermocalcs(self.temp_slider.get(), self.press_slider.get(), self.rh_slider.get())\n", " return result\n", " \n", " def thermocalcs(self, temperature, pressure, rh):\n", " # Calculate potential temperature and equivalent potential temperature\n", " pot_temp = potential_temperature(pressure * units.mbar, temperature * units.kelvin)\n", " self.pot_temp_text.set('{0:.2f}'.format(pot_temp))\n", " equiv_pot_temp = equivalent_potential_temperature(pressure * units.mbar, temperature * units.kelvin)\n", " self.equiv_pot_temp_text.set('{0:.2f}'.format(equiv_pot_temp))\n", " \n", " # Calculate saturation vapor pressure\n", " sat_vap_press = saturation_vapor_pressure(temperature * units.kelvin)\n", " self.sat_vap_press_text.set('{0:.4f} {1:s}'.format(sat_vap_press.magnitude, 'mb'))\n", " \n", " # Caclculate mixing ratio and saturation mixing ratio\n", " mix_ratio = mixing_ratio((rh * sat_vap_press / 100), pressure * units.mbar)\n", " self.mix_ratio_text.set('{0:.6f} {1:s}'.format(mix_ratio.to('g/kg').magnitude, 'g/kg'))\n", " sat_mix_ratio = saturation_mixing_ratio(pressure * units.mbar, temperature * units.kelvin)\n", " self.sat_mix_ratio_text.set('{0:.6f} {1:s}'.format(sat_mix_ratio.to('g/kg').magnitude, 'g/kg'))\n", " \n", " # Calculate vapor pressure\n", " vap_press = sat_vap_press * (rh / 100)\n", " self.vap_press_text.set('{0:.4f} {1:s}'.format(vap_press.magnitude, 'mb'))\n", " \n", " # Calculate dewpoint temperature\n", " if rh == 0:\n", " self.dewpt_text.set('dry')\n", " else:\n", " dewpt = dewpoint_rh(temperature * units.kelvin, (rh/100))\n", " self.dewpt_text.set('{0:.2f}'.format(dewpt.to('kelvin')))\n", " " ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [], "source": [ "root = Tk()\n", "root.geometry(\"500x525\")\n", "app = thermo(root)\n", "root.mainloop()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }