SciPy - convert_temperature() Method



The SciPy convert_temperature() method is used to calculate the temperature scale in the form of any one scales such as Celcius, Kelvin, Fahrenheit, and Rankine.

Following is the key understanding of various scales −

  • Celcius − Unit of temperature.
  • Farenheit − This is a scale of temperature when water freezes at 32 degree and boils at 212 degree.
  • Kelvin − This is unit of temperature when 0 reflects.
  • Rankine − The absolute unit of temperature.

Syntax

Following is the syntax of the SciPy convert_temperature() method −

convert_temperature(val, old_scale, new_scale)

Parameters

This method accepts the following parameters −

  • val− Specify the temperature range value.
  • old_scale− Specify the original string.
  • new_scale− Specify the new string in which the temperature will be converted.

Return value

This function returns an array of float values.

Example 1

Following is the basic SciPy convert_temperature() method illustrates the conversion of Celcius, Farenheit, and Kelvin.

import scipy.constants as const

def convert_temperature(value, from_unit, to_unit):
   if from_unit == to_unit:
       return value

    # Convert the input temperature to Celsius first
   if from_unit == 'Celsius':
       temp_c = value
   elif from_unit == 'Fahrenheit':
       temp_c = (value - 32) * 5/9
   elif from_unit == 'Kelvin':
       temp_c = value - const.zero_Celsius
   else:
       raise ValueError("Unsupported temperature unit")

    # Convert from Celsius to the desired unit
   if to_unit == 'Celsius':
       return temp_c
   elif to_unit == 'Fahrenheit':
       return temp_c * 9/5 + 32
   elif to_unit == 'Kelvin':
       return temp_c + const.zero_Celsius
   else:
       raise ValueError("Unsupported temperature unit")

# Show the result
print(convert_temperature(100, 'Celsius', 'Fahrenheit')) 
print(convert_temperature(32, 'Fahrenheit', 'Celsius'))  
print(convert_temperature(273, 'Kelvin', 'Celsius'))   

Output

The above code produces the following result −

212.0
0.0
-0.14999999999997726

Example 2

Below the example uses some more scipy constant to convert the base unit into temperature unit.

import scipy.constants as const

def convert_temperature(value, from_unit, to_unit):
   if from_unit == to_unit:
       return value

   # convert input to Kelvin
   if from_unit == 'Celsius':
       temp_k = value + const.zero_Celsius
   elif from_unit == 'Fahrenheit':
       temp_k = (value - 32) * 5/9 + const.zero_Celsius
   elif from_unit == 'Kelvin':
       temp_k = value
   else:
       raise ValueError("Unsupported temperature unit")

   # convert Kelvin to the desired unit
   if to_unit == 'Celsius':
       return temp_k - const.zero_Celsius
   elif to_unit == 'Fahrenheit':
       return (temp_k - const.zero_Celsius) * 9/5 + 32
   elif to_unit == 'Kelvin':
       return temp_k
   else:
       raise ValueError("Unsupported temperature unit")

# Show the result
print(convert_temperature(100, 'Celsius', 'Fahrenheit')) 
print(convert_temperature(32, 'Fahrenheit', 'Kelvin'))    
print(convert_temperature(273.15, 'Kelvin', 'Celsius'))   

Output

The above code produces the following result −

212.0
273.15
0.0
scipy_reference.htm
Advertisements