3

I'm fairly new to programming and I'm trying to produce a simple zero-dimensional energy balance model in Python 2.7 IDLE, to calculate surface temperatures of the Earth and have added a ice albedo feedback, i.e. if the temperature output of the model is higher than 280K the albedo stays at 0.3 (30% energy reflected), if its below 250k the albedo is 0.7(70% energy reflected, as its cooler therefore a larger ice (white) cover on the Earth), and if the temperature lies in the range in between these; the albedo is calculated with a formula. This new value for albedo is then run back from the model to give a more accurate temperature.

In my module I have defined;

A final Climate model Calculation for albedo A new finalised climate model with the new albedo(s) taken into concideration

I am trying to produce a graph to compare the output of the first climate model with varying solar input but a consistent albedo, to the output of the second run with a varying albedo and solar output. But keep getting errors;

This is my script for my graph:

  import matplotlib.pyplot as plt
  import numpy as np
  from EBM_IceAlbFeedback import *
  # q is for the Solar Constant
  q=np.linspace(2.5e26,4.0e26,150)
  # t= temperature derived from the final climate model
  t= finalCM(Q=q)
  plt.plot(q,t,'b-')
  q=np.linspace(3.0e26,4.5e26,150)
  # tb= is the second set of temperatures derived from the NEWfinalCM which contains an Ice Albedo Feedback
  tb= NEWfinalCM(Q=q)
  plt.plot(q,tb,'r-')
  plt.show ()

My error message is:

Traceback (most recent call last):
 File "K:/python/CompareCMsPlt2.py", line 13, in <module>
tb= NEWfinalCM(Q=q)
 File "K:/python\EBM_IceAlbFeedback.py", line 228, in NEWfinalCM
 NewAlb=NAlb(dist=dist, Q=Q, co2Emissions=co2Emissions, alpha=alpha, cCycleInt=cCycleInt, cCycleSlope=cCycleSlope)
 File "K:/python\EBM_IceAlbFeedback.py", line 190, in NAlb
  if ta>280.0:
 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I believe this is referring something in this part of my module:

def NAlb (dist=150e9, Alb=0.3, Q=3.87e26, co2Emissions=0.0, alpha=3.0, cCycleInt=0.4,    cCycleSlope=0.0001):
'''
Readjusting Albedo to the output temperature

Arguments:

Q = solar ouput (W)
dist = distance from the sun (m)
co2Emissions = Cumulative CO2 emissions since 2010 (GtC)
alpha = climate sensitivity (K/2xCO2)
cCycleInt = Initial value of the airborne fraction (unitless)
cCycleSlope = Increment the airborne fraction per GtC (GtC^-1)

Return Value:
NewAlb= New Albedo (Unitless)
'''
# CALCULATE ABORTIVITY:
#Our model is baselined at an atmospheric CO2 concentration of 390 ppmv in 2010
baselineCO2=390.0
#The official IPCC figure for conversion of mass of emissions (GtC) top atmospheric   concentration (ppmv)
IPCCmassToConc=2.12
#approximate correction for the carbon cycle:
cCycleAdjust=cCycleInt+cCycleSlope*co2Emissions
#convert GtC to CO2 conc in ppmv:
co2=co2Emissions*cCycleAdjust/IPCCmassToConc+baselineCO2
#calculate absorptivity
absrp=absrpFromCO2( CO2=co2, alpha=alpha )

#CALCULATE TEMPERATURE: using the same method as in the finalCM
ta=transATmCM (absrpt=absrp, dist=dist, Alb=0.3, Q=Q)
# define the thresholds for an ice free state.
if ta>280.0:
    NewAlb=0.3
# define the threshold for a snow ball Earth state.
elif ta<250.0:
    NewAlb=0.7# Calculate albedo for temperatures between 280k to 230k
elif 250.0<ta<280.0:
    NewAlb=(0.3+(((0.7-0.3)/(280.0-250.0))*(280.0-ta)))
return NewAlb




  def NEWfinalCM( co2Emissions=0.0, alpha=3., dist=150e9, Q=3.87e26, cCycleInt=0.4, cCycleSlope=0.0001 ):
'''
A New final Climate model which contains and Ice Albedo Feedback

Arguments:

Q = solar ouput (W)
dist = distance from the sun (m)
co2Emissions = Cumulative CO2 emissions since 2010 (GtC)
alpha = climate sensitivity (K/2xCO2)
cCycleInt = Initial value of the airborne fraction (unitless)
cCycleSlope = Increment the airborne fraction per GtC (GtC^-1)

Return Value:
tn = surface temperature (K)
'''
#Our model is baselined at an atmospheric CO2 concentration of 390 ppmv in 2010
baselineCO2=390.0
#The official IPCC figure for conversion of mass of emissions (GtC) top atmospheric concentration (ppmv)
IPCCmassToConc=2.12
#approximate correction for the carbon cycle:
cCycleAdjust=cCycleInt+cCycleSlope*co2Emissions
#convert GtC to CO2 conc in ppmv:
co2=co2Emissions*cCycleAdjust/IPCCmassToConc+baselineCO2


#calculate temperature
absrp=absrpFromCO2(CO2=co2, alpha=alpha)
NewAlb=NAlb(dist=dist, Q=Q, co2Emissions=co2Emissions, alpha=alpha, cCycleInt=cCycleInt, cCycleSlope=cCycleSlope)

tn=transATmCM( absrpt=absrp, dist=dist, Alb=NewAlb, Q=Q)


return tn

Any help is appreciated

Thanks

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    @cillosis - Why? Scientists aren't programmers... As to the actual error, conditions on numpy arrays (e.g. `x > 5`) return boolean arrays (e.g. `array([True, True, False])` rather than a single value. – Joe Kington Mar 17 '12 at 00:51

1 Answers1

1

The comment above is correct, and it's not clear what you want to do, but if you want to check if all elements in your array validate the condition then you could do:

if tb.all() > 280.0:

If you are interested in if there exists a value in the array that fullfills it you could do:

if tb.max() > 280.0:
    ...
elif tb.min() < 250.0:

Both above examples should not need to be more than a simple else-statement for the third condition.

If you want to evaluate positions individually you could as well, but then I would go for the following:

tb_test = np.ones(tb.shape) * 3
tb_test[np.where(tb > 280)] = 1
tb_test[np.where(tb < 250)] = 2

That will make the tb_test array ones for where the first condition applies, twos for the second and threes for the third.

Of course you can insert your calculations directly instead of the above identification of where the different conditions apply...

deinonychusaur
  • 7,094
  • 3
  • 30
  • 44