0

I am trying to run a loop in python to get an array of P values. I have an array of t and h values. I need to calculate the difference of h2-h1 for each iteration. For example, for the first iteration, I need to calculate 200-10, and the second 350-200, etc.

I want to calculate P for each iteration, using the previous iteration's P value as Po. Here's my code:

import matplotlib.pyplot as plt 
import numpy as np

h = np.array([10,200,350,500,700,850,1000,1500,2000,2500,3000]) #height (m)

t = np.array([20,17,15,13,15,17,19,14,10,7,4]) #Temp (C)

t = t+273.15 #Temp (K)

ws = np.array([3,12,15,16,17,18,19,19.6,20,20.2,21]) #Wind Speed (m/s)

q = np.array([16,15,14,13,3,2,2,1.8,1.7,1.5,1.4]) #specific humidity (g/kg)

#define variables for calculation

Po= 101325 #pa 

m = 0.029 #kg/mol

g = 9.8 #m/s

R = 287.058 #J/kg/K #specific gas constant for dry air 

#calculate virtual potential temperature using Hypsometric equation

### must calculate change in pressure with height

def pressure():

    pressure = []

    for eachvalue in t:

        P = Po/np.exp((g/(R*t))*(np.diff[h]))

        pressure.append(P) 

    return pressure 
Psidom
  • 209,562
  • 33
  • 339
  • 356
astrogirl79
  • 59
  • 1
  • 2
  • 7
  • 1
    What is the question? – Psidom Oct 12 '17 at 01:11
  • This link may help: https://stackoverflow.com/questions/36480778/how-to-subtract-the-next-value-from-the-previous-value-in-python – wanderweeer Oct 12 '17 at 01:16
  • I don't understand how to do a loop properly with these 3 different arrays. I'm getting the error that my function is not callable – astrogirl79 Oct 12 '17 at 01:18
  • you get a few errors because np.diff[h] is trying to slice and not doing np.diff(h). Now if that is true, then 'h' doesn't have enough values to maintain shapes of (10,) so to see if that made sense, I added ground elevation to h .... h = np. array([ 0, 10, 200, 350, 500, 700, 850, 1000, 1500, 2000, 2500, 3000]) Then your final equation is P = Po/np.exp(g/(R * t)) * np.diff(h). If those values don't make sense... never mind ;) – NaN Oct 12 '17 at 01:24
  • ps... there is no need for the def and the looping, just the arrays (corrected)and the fix to diff(h) – NaN Oct 12 '17 at 01:38

0 Answers0