gpt4 book ai didi

Python:生成包含反馈机制的模块图

转载 作者:行者123 更新时间:2023-11-28 18:52:45 25 4
gpt4 key购买 nike

我对编程相当陌生,我正在尝试在 Python 2.7 IDLE 中生成一个简单的零维能量平衡模型,以计算地球的表面温度并添加了冰反照率反馈,即如果温度输出模型的反照率高于 280K,反照率保持在 0.3(30% 的能量反射),如果低于 250k,反照率是 0.7(70% 的能量反射,因为它更冷,因此地球上有更大的冰(白色)覆盖),和如果温度介于两者之间;反照率是用公式计算的。这个新的反照率值然后从模型中运行回来,以提供更准确的温度。

在我的模块中我已经定义了;

最终的气候模型反照率的计算考虑新反照率的新最终气候模型

我正在尝试生成一个图表,以比较具有不同太阳能输入但反照率一致的第一个气候模型的输出与具有不同反照率和太阳能输出的第二次运行的输出。但是不断出错;

这是我的图形脚本:

  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 ()

我的错误信息是:

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()

我相信这是指我模块的这一部分中的某些内容:

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

感谢任何帮助

谢谢

最佳答案

上面的评论是正确的,不清楚你想做什么,但如果你想检查你的数组中的所有元素是否都验证了条件,那么你可以这样做:

if tb.all() > 280.0:

如果您对数组中是否存在一个值来填充它感兴趣,您可以这样做:

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

以上两个示例都应该只是第三个条件的简单 else 语句。

如果您想单独评估职位,您也可以,但我会选择以下内容:

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

这将使第一个条件适用的 tb_test 数组为一个,第二个为两个,第三个为三个。

当然你可以直接插入你的计算而不是上面的不同条件适用的标识......

关于Python:生成包含反馈机制的模块图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9746183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com