Elsri
Elsri

Reputation: 49

RuntimeWarning: invalid value encountered in double_scalars with function working in calculators

I have an equation that I try to run in python for a range of values:

T = 15
H = range(0, 100)
for hum in H:
  ED = 0.942 * (hum ** 0.679) + 11 * exp((hum - 100) / 10) + 0.18 * (21.1 - T) ** (1 - exp(-0.115 * hum))

This results in this error:

RuntimeWarning: invalid value encountered in double_scalars
  ED = 0.942 * (hum ** 0.679) + 11 * exp((hum - 100) / 10) + 0.18 * (21.1 - T) ** (1 - exp(-0.115 * hum))

When entering the same equation in calculators or even Excel, the function works.

Since the equation contains two double scalars, of which the second more complex, I am guessing the error results in the second double scalar. How can I rewrite this part of the equation to solve the problem?

Edit:

The equation is part of a bigger code in python. A snippet relating to the equation is this:

import numpy as np
from netCDF4 import Dataset
import os
from numpy.ma.core import MaskedArray
import gdal
from math import exp
from math import log

nlat = 600
nlon = 465

years = range(1996,2005)
for year in years:
    starting_FFMC_input = np.zeros(shape = (nlat, nlon))

    #Temperature at 2m above ground
    temperature_at_2m_raw: MaskedArray()
    temperature_at_2m_input: MaskedArray()
    with Dataset(dir + "WRFoutputTemperature.nc") as file_temperature_at_2m:
        temperature_at_2m_raw = file_temperature_at_2m.variables['T2MEAN'][number_of_days_in_timeperiod * 8 : (number_of_days_in_timeperiod + days_in_year) * 8 - 1, :, :] - 273.15
        dimsizes_temperature_at_2m = temperature_at_2m_raw.shape
        temperature_at_2m_input = temperature_at_2m_raw[4 : (dimsizes_temperature_at_2m[0]-4) : 8, :, :] #3hourly, source data starts from 01-12-1995
    del temperature_at_2m_raw


    #Relative humidity at 2m above ground
    relative_humidity_at_2m_raw: MaskedArray()
    relative_humidity_at_2m_input: MaskedArray()
    with Dataset(dir + 'WRFoutputHumidity') as file_relative_humidity_at_2m:
        relative_humidity_at_2m_raw = file_relative_humidity_at_2m.variables['HURS'][number_of_days_in_timeperiod * 8 : (number_of_days_in_timeperiod + days_in_year) * 8 - 1, :, :] * 100
        dimsizes_relative_humidity_at_2m = relative_humidity_at_2m_raw.shape
        relative_humidity_at_2m_input = relative_humidity_at_2m_raw[4 : (dimsizes_relative_humidity_at_2m[0]-4) : 8, :, :] #3hourly, source data starts from 01-12-1995
    del relative_humidity_at_2m_raw

    index_i = range(0, 13132)
    for i in index_i:
        index_x = coords[i,0]
        index_y = coords[i,1]

        days = range(0, days_in_year-1)
        for day in days:
            temperature = temperature_at_2m_input[day, index_y, index_x]
            humidities = relative_humidity_at_2m_input[day, index_y, index_x]

            if humidities > 100:
                humidities = 100
            elif humidities < 0:
                humidities = 0
            else:
                humidities = humidities

            ED = 0.942 * (humidities ** 0.679) + 11 * exp((humidities - 100) / 10) + 0.18 * ((21.1 - temperature) ** (1 - exp(-0.115 * humidities))

Humidities and Temperature values appear normal:

Temperature is: 12.101776
Humidity is: 75.87772
Temperature is: 11.955383
Humidity is: 90.84902
Temperature is: 11.471436
Humidity is: 99.00755
Temperature is: 13.040009
Humidity is: 95.4102

Upvotes: 0

Views: 48

Answers (1)

Elsri
Elsri

Reputation: 49

The equation does not allow for negative temperature values to the power of a decimal, even though freezing temperatures on a Celsius scale can dip below zero. Including an if/else statement that changes this power to an integer fixed the issue for these 'problematic' temperatures.

Upvotes: 0

Related Questions