##### Mathematical Conversion Scripts for Weather Station
 Mathematical Conversion Scripts for Weather Station Mickey53usa Programmer named Tim Posts: 6 Threads: 1 Joined: Jun 2020 Reputation: Jun-16-2020, 01:18 AM (This post was last modified: Jun-16-2020, 03:39 AM by buran.) New person here - new to Python. Looking for some scripts to display data in a different format. Example - take a KPa and convert and display as Inces of Mercury. I have one sample for converting Celsius to Fahrenheit - ```bmpTemp = bmp280.read_temperature() TempF = bmpTemp* (9.0/5.0) +32.0 print 'Temperature = \t{0:0.2f} F'.format(TempF)```Again, just a sample and has not yet been tested or inserted into the rest of the code. Need something similar for Meters to Feet and KPA to Inches of Mercury. I thought I would try the same format as the sample and just change the formula and the headers. Thanks Michael Reply pyzyx3qwerty Minister of Silly Walks Posts: 356 Threads: 13 Joined: Mar 2020 Reputation: Jun-16-2020, 02:45 AM Please, use proper code tags while posting a thread - see BBCode pyzyx3qwerty "The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela Need help on the forum? Visit help @ python forum For learning more and more about python, visit Python docs Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jun-16-2020, 04:01 AM (This post was last modified: Jun-16-2020, 04:01 AM by buran.) It's simple conversion. It would have been less typing to just write the code, instead of writing this post :-) meters to feet formula KPA to Inches of Mercury It goes without saying that each conversion should be a separate function, so that it's reusable and easy to test. If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply Mickey53usa Programmer named Tim Posts: 6 Threads: 1 Joined: Jun 2020 Reputation: Jun-16-2020, 09:54 PM (This post was last modified: Jun-17-2020, 03:06 AM by buran.) ``` if (config.BMP280_Present): bmpTemp = bmp280.read_temperature() TempF = bmpTemp* (9.0/5.0) +32.0 print 'Temperature = \t{0:0.2f} F'.format(TempF) #print 'Temperature = \t{0:0.2f} C'.format(bmp280.read_temperature()) print 'Pressure = \t{0:0.2f} KPa'.format(bmp280.read_pressure()/1000) print 'Altitude = \t{0:0.2f} m'.format(bmp280.read_altitude()) print 'Sealevel Pressure = \t{0:0.2f} KPa'.format(bmp280.read_sealevel_pressure()/1000) if (config.OLED_Present): Scroll_SSD1306.addLineOLED(display, 'Press= \t{0:0.2f} KPa'.format(bmp280.read_pressure()/1000)) if (config.HTU21DF_Present == False): Scroll_SSD1306.addLineOLED(display, 'InTemp= \t{0:0.2f} C'.format(bmp280.read_temperature())) print "----------------- " print "----------------- " if (config.HTU21DF_Present == True): print " HTU21DF Temp/Hum" else: print " HTU21DF Temp/Hum Not Present" print "---------------- ```I guess that the other conversion text will used the same coding, just reference different formulas. Reply Mickey53usa Programmer named Tim Posts: 6 Threads: 1 Joined: Jun 2020 Reputation: Jun-18-2020, 06:02 PM OK, here is the code I have in place now - ```print "----------------- " if (config.BMP280_Present): bmpTemp = bmp280.read_temperature() TempF = bmpTemp* (9.0/5.0) +32.0 print 'Temperature = \t{0:0.2f} F'.format(TempF) #print 'Temperature = \t{0:0.2f} C'.format(bmp280.read_temperature()) bmpPress = bmp280.read_pressure() Pressin = bmpPress* .2953 print 'Pressure = \t{0:0.2f} in'.format(Pressin) #print 'Pressure = \t{0:0.2f} KPa'.format(bmp280.read_pressure()/1000) bmpAlt = bmp280.read_altitude() Altft = bmpAlt*3.281 print 'Altitude = \t{0:0.2f} ft'.format(Altft) #print 'Altitude = \t{0:0.2f} m'.format(bmp280.read_altitude()) bmpSEA = bmp280.read_sealevel_pressure() SLpres = bmpSEA*.2953 print 'Sealevel Pressure = \t{0:0.2f} in'.format(SLpres) #print 'Sealevel Pressure = \t{0:0.2f} KPa'.format(bmp280.read_sealevel_pressure()/1000) if (config.OLED_Present): Scroll_SSD1306.addLineOLED(display, 'Press= \t{0:0.2f} in'.format(bmp280.read_pressure()*.2953)) if (config.HTU21DF_Present == False): Scroll_SSD1306.addLineOLED(display, 'InTemp= \t{0:0.2f} F'.format(bmp280.read_temperature()* (9.0/5.0) +32.0)) print "----------------- "```All of the results come out as they should - except for the decimal point format in the Pressure equations. The decimal point ends up two places left of the end of the result, when it should be two places right of the start of the equation. A result which should read 26.78371 displays as 26783.71. What would the code be to change the decimal placement? Elevation also comes out without a comma - 1,968.60 displays as 1968.60. I can actually live with this. TIA Michael Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jun-18-2020, 07:13 PM (This post was last modified: Jun-18-2020, 07:13 PM by buran.) (Jun-18-2020, 06:02 PM)Mickey53usa Wrote: The decimal point ends up two places left of the end of the result, when it should be two places right of the start of the equation. A result which should read 26.78371 displays as 26783.71. What would the code be to change the decimal placement?It's 3 places off i.e. I think it's 1000 higher, so I guess you misinterpret the data you read in what measurment about the comma - check string formatting syntax, you can instruct it to have thousands separator based on your regional settings If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply Mickey53usa Programmer named Tim Posts: 6 Threads: 1 Joined: Jun 2020 Reputation: Jun-20-2020, 01:56 PM buran; No, I don't think that the formula in question is wrong. I say this because using a standard calculator the decimals show in the correct position. I have tracked the problem to this one little piece of code - `print 'Temperature = \t{0:0.2f} F'.format(TempF)`In particular the information between the { } brackets. If I change the "2" to 1 one, then I end up with one digit after the decimal point. If I change it to five, I end up with five digits after the decimal. This piece of code refers to a floating decimal with two digits to the right of the decimal point. I am trying to wrap my head around how to modify this little snippet of code to fit my needs, or to figure out what code needs to be used instead. TIA Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jun-20-2020, 04:15 PM (Jun-20-2020, 01:56 PM)Mickey53usa Wrote: buran; No, I don't think that the formula in question is wrong.Yes, you do. Just print it without any formatting so that you see what you really get the string formatting decimal places does not affect the decimal point ```>>> preasure = 26.78371 >>> f'{preasure:.2f}' '26.78' >>> f'{preasure:.5f}' '26.78371' >>> preasure = 26783.71 >>> f'{preasure:.2f}' '26783.71' >>> f'{preasure:.5f}' '26783.71000'```as you can see it fill in with 0 if not enough decimal digits, it does not move the decimal point If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply Mickey53usa Programmer named Tim Posts: 6 Threads: 1 Joined: Jun 2020 Reputation: Jun-20-2020, 05:48 PM It would probably help is I had included the proper code snippet - `print 'Pressure = \t{0:0.2f} in'.format(Pressin)`So, you are stating to remove the second zero - so it would be `print 'Pressure = \t {0:.2f} in' .format (Pressin)` Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Jun-20-2020, 06:33 PM It's the same. I am saying that Pressin value is 26783.71 and not what you think 26.78371 in which case it would print 26.78 because of the 0.2f If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Copying files from a remote Windows station tester_V 11 856 Jul-17-2021, 02:19 AM Last Post: tester_V About mathematical equations seyidcemkarakas 3 653 Oct-04-2020, 01:21 PM Last Post: Gribouillis Recognising mathematical expressions from word and pdf file Preeti15 0 568 Aug-19-2020, 09:06 AM Last Post: Preeti15 Parsing Date/Time from Metar Reports with 6 hourly weather information Lawrence 0 779 May-03-2020, 08:15 PM Last Post: Lawrence Mathematical Operators in String AgileAVS 1 887 Mar-04-2020, 04:14 PM Last Post: Gribouillis Mathematical function input Tiiill 2 1,303 Sep-10-2019, 09:25 PM Last Post: nilamo Beginner user: mathematical operations Mahdi1994 1 1,538 Mar-19-2018, 11:07 AM Last Post: Larz60+ how to make a mathematical operation without the command "print" ewar2606 4 1,587 Mar-15-2018, 07:19 PM Last Post: sparkz_alot Python code weather Led sign n3vo 21 7,010 May-17-2017, 07:09 PM Last Post: n3vo

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020