Python Forum
Mathematical Conversion Scripts for Weather Station - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Mathematical Conversion Scripts for Weather Station (/thread-27660.html)

Pages: 1 2


Mathematical Conversion Scripts for Weather Station - Mickey53usa - Jun-16-2020

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


RE: Mathematical Conversion Scripts for Weather Station - pyzyx3qwerty - Jun-16-2020

Please, use proper code tags while posting a thread - see BBCode


RE: Mathematical Conversion Scripts for Weather Station - buran - Jun-16-2020

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.


RE: Mathematical Conversion Scripts for Weather Station - Mickey53usa - Jun-16-2020

 	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.


RE: Mathematical Conversion Scripts for Weather Station - Mickey53usa - Jun-18-2020

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


RE: Mathematical Conversion Scripts for Weather Station - buran - Jun-18-2020

(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


RE: Mathematical Conversion Scripts for Weather Station - Mickey53usa - Jun-20-2020

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


RE: Mathematical Conversion Scripts for Weather Station - buran - Jun-20-2020

(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


RE: Mathematical Conversion Scripts for Weather Station - Mickey53usa - Jun-20-2020

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)



RE: Mathematical Conversion Scripts for Weather Station - buran - Jun-20-2020

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