Python Forum
Mathematical Conversion Scripts for Weather Station
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mathematical Conversion Scripts for Weather Station
#1
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
#2
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
#3
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
#4
 	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
#5
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
#6
(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
#7
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
#8
(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
#9
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
#10
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
  plotting based on the results of mathematical formulas Timur 1 295 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Taking Mathematical Expressions from Strings quest 2 664 Jul-02-2023, 01:38 PM
Last Post: Pedroski55
  Copying files from a remote Windows station tester_V 11 6,749 Jul-17-2021, 02:19 AM
Last Post: tester_V
  About mathematical equations seyidcemkarakas 3 1,961 Oct-04-2020, 01:21 PM
Last Post: Gribouillis
  Recognising mathematical expressions from word and pdf file Preeti15 0 1,741 Aug-19-2020, 09:06 AM
Last Post: Preeti15
  Parsing Date/Time from Metar Reports with 6 hourly weather information Lawrence 0 2,289 May-03-2020, 08:15 PM
Last Post: Lawrence
  Mathematical Operators in String AgileAVS 1 2,345 Mar-04-2020, 04:14 PM
Last Post: Gribouillis
  Mathematical function input Tiiill 2 2,673 Sep-10-2019, 09:25 PM
Last Post: nilamo
  Beginner user: mathematical operations Mahdi1994 1 2,815 Mar-19-2018, 11:07 AM
Last Post: Larz60+
  how to make a mathematical operation without the command "print" ewar2606 4 2,896 Mar-15-2018, 07:19 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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