0

I'm looking for a way to only add numbers (np.coeff) to my list (T_g) if this number is real and positive. (See last loop) In the loop, if I print np.roots(coeff) right before appending those values to T_g, it outputs an array of four values, where one of them is real positive. This value is not always at the same position in the array. Any idea how to do that? I thought of doing an if loop like:

if *value of np.roots is real positive*: 
    T_g.append(rootss(coeff))

But as you can see I'm not sure how to write this at all.

from numpy import *
import numpy as np
import pylab
import scipy
from scipy.optimize import leastsq
from math import *
import matplotlib.pyplot as plt
from scipy import integrate

#define constants used in equations:
alb = 0.2 #constant albedo
F = 866 #J/s*m**2
R = 287 #J/K*kg
U = 5 #m/s
C_p = 1000 #J/K*kg
C_d = 0.0015
p1 = 10**4
p2 = 10**5
p3 = 10**6 #Pa
sig = 5.67*10**-8 #J/s*m**2*K**4 #Stefan-Boltzmann cst


T_a = 424.663545093 #K  ###Found in code FindT_a.py


#define other constants in equation:
rho = p1/(R*T_a)      #FIRST USING P = 10**4 Pa
a = rho*C_p*C_d*U



#define a function (frange), that accepts float arguments
import math
def xfrange(start, stop, step):
    while start < stop:
        yield start
        start += step


z_daylist = []
T_g = []
roots = []

for i in range(len(z_daylist)):
    coeff = array([(sig),0,0,(a),((-a*T_a)-z_daylist[i])])
    def rootss(coeff):
        return np.roots(coeff)
    #print np.roots(coeff) #this prints an array of four values where one is real positive, not always at same position
        #keep only the positive real roots > How???
    T_g.append(rootss(coeff))
jgritty
  • 11,660
  • 3
  • 38
  • 60
JadeChee
  • 89
  • 1
  • 17
  • possible duplicate of [Efficient thresholding filter of an array with numpy](http://stackoverflow.com/questions/7994394/efficient-thresholding-filter-of-an-array-with-numpy) – ivan_pozdeev Feb 27 '15 at 23:52

1 Answers1

0
T_g.append([x for x in rootss(coeff) if np.isreal(x) and x > 0])
A.P.
  • 1,109
  • 8
  • 6