**
不同因子影响下的不同情境的世界气温预测
**
from tkinter import *
from matplotlib import pylab as plt
import requests
import numpy as np
from netCDF4 import Dataset
from matplotlib.figure import Figure
'''获取数据'''
def vars():
list = [ghg.get(), aerosol.get(), aerosol_cl.get(), aerosol_al.get(), solar.get(), volcano.get(), tropo.get(),
strato.get(), snow_al.get(),strato_water.get(),Land_use.get()]
return list
'''模型计算'''
def model_calc(totf, num_year):
start_val = totf[0]
totf = totf - start_val
Tm = np.zeros((num_year, 1))
Td = np.zeros((num_year, 1))
i = 0
while i < num_year - 1:
diffusive_flux = v_diff * density * cp * (Tm[i] - Td[i]) / (0.5 * (hd + hm))
fluxes_to_atmos = totf[i] - Tm[i] / alpha
mixed_layer_energy = time_st * (fluxes_to_atmos - diffusive_flux)
deep_layer_energy = time_st * diffusive_flux
Td[i + 1] = Td[i] + deep_layer_energy / (density * cp * hd)
Tm[i + 1] = Tm[i] + mixed_layer_energy / (density * cp * hm)
i += 1
return Tm
'''可视化'''
def plot():
v = vars()
mean_t = []
t_95percent = []
t_5percent = []
totf = v[0] * ghgf + v[6] * tr3f + v[7] * so3f + v[9]*swvf + v[1] * aerf + v[2] * aclf + v[3] * acaf + v[
8] * snof + v[10]* lndf + v[4] * solf + \
v[5] * volf
# totf = ghg.get() * ghgf + tr3f + so3f + swvf + aerosol.get() * aerf + aerosol_cl.get() * aclf + aerosol_al.get() * acaf + snof + lndf + solar.get() * solf + volcano.get() * volf
tm = model_calc(totf, num_year)
for i in t[:161]:
if i[7] == '-':
mean_t.append(i[7:13])
else:
mean_t.append(i[8:13])
if i[16] == '-':
t_95percent.append(i[16:22])
else:
t_95percent.append(i[17:22])
if i[25] == '-':
t_5percent.append(i[25:31])
else:
t_5percent.append(i[26:31])
mean_t = np.array(mean_t, dtype=float)
mean_t = mean_t.reshape((161, 1))
t_95percent = np.array(t_95percent, dtype=float)
t_95percent = t_95percent.reshape((161, 1))
t_5percent = np.array(t_5percent, dtype=float)
t_5percent = t_5percent.reshape((161, 1))
tm = tm - np.mean(tm[112:141])
bias = np.mean(tm - mean_t)
rms_error = []
calc = tm - mean_t
f = plt.figure(figsize=(10, 10))
plt.axis([1850, 2010, -1.5, 3])
plt.title('Global Mean Temperature')
plt.xlabel('Year')
plt.ylabel('Temperature Anomaly (oC w.r.t. 1961-1990)')
plt.plot(year, tm, 'o-', label='Modelled')
plt.plot(year, mean_t, 'g-.', label='Obeserved')
plt.plot(year, t_95percent, 'b-.', label='Location')
plt.plot(year, t_5percent, 'p-.', label='Northwest')
plt.legend(loc='upper left')
plt.show()
'''UI按钮设置'''
root= Tk()
frame1 = Frame(root)
frame2 = Frame(root)
root.title('Version 1.0.002')
Label(root,text = 'the last five factors should be set to 1, but if you want to explore the effects when they were set to other number, you can do it').pack(side = TOP)
Label(frame1, text='Greenhouse_gases').pack(side=TOP, anchor=W)
ghg = DoubleVar()
Entry(frame1, textvariable=ghg).pack(side=TOP, anchor=W)
ghgf_factor = Scale(frame1, from_=0.0, to_=2.5, variable=ghg, resolution=0.1, length=300, orient=HORIZONTAL)
ghgf_factor.pack(side=TOP, anchor=W)
# Button(root, text='Greenhouse_factor').pack()
Label(frame1, text='solar').pack(side=TOP, anchor=W)
solar = DoubleVar()
Entry(frame1, textvariable=solar).pack(side=TOP, anchor=W)
solar = Scale(frame1, from_=0.0, to_=2.5, resolution=0.1, variable=solar, length=300, orient=HORIZONTAL)
solar.pack(side=TOP, anchor=W)
Label(frame1, text='volcano effect').pack(side=TOP, anchor=W)
volcano = DoubleVar()
Entry(frame1, textvariable=volcano).pack(side=TOP, anchor=W)
volf_factor = Scale(frame1, from_=0.0, to_=2.5, resolution=0.1, variable=volcano, length=300, orient=HORIZONTAL)
volf_factor.pack(side=TOP, anchor=W)
# Button(root, text='Volcanic').pack()
Label(frame1, text='Aerosol_direct').pack(side=TOP, anchor=W)
aerosol = DoubleVar()
Entry(frame1, textvariable=aerosol).pack(side=TOP, anchor=W)
aerf_factor = Scale(frame1,from_=0.0, to_=2.5, resolution=0.1, variable=aerosol, length=300, orient=HORIZONTAL)
aerf_factor.pack(side=TOP, anchor=W)
# Button(root, text='Aerosol Direct').pack()
Label(frame1, text='Aerosol_cloud_lifetime').pack(side=TOP, anchor=W)
aerosol_cl = DoubleVar()
Entry(frame1, textvariable=aerosol_cl).pack(side=TOP, anchor=W)
aclf_factor = Scale(frame1, from_=0.0, to_=2.5, variable=aerosol_cl, resolution=0.1, length=300, orient=HORIZONTAL)
aclf_factor.pack(side=TOP, anchor=W)
# Button(root, text='Aerosol cloud lifetime').pack()
Label(frame2, text='Aerosol_cloud_albedo').pack(side=TOP, anchor=E)
aerosol_al = DoubleVar()
Entry(frame2, textvariable=aerosol_al).pack(side=TOP, anchor=E)
acaf_factor = Scale(frame2, from_=0.0, variable=aerosol_al, to_=2.5, resolution=0.1, length=300, orient=HORIZONTAL)
acaf_factor.pack(side=TOP, anchor=E)
Label(frame2, text='Tropo ozone').pack(side=TOP, anchor=E)
tropo = DoubleVar()
Entry(frame2, textvariable=tropo).pack(side=TOP, anchor=E)
tropo_factor = Scale(frame2, from_=0.0, to_=2.5, resolution=0.1, variable = tropo, length=300, orient=HORIZONTAL)
tropo_factor.pack(side=TOP, anchor=E)
Label(frame2, text='Strato ozone').pack(side=TOP, anchor=E)
strato = DoubleVar()
Entry(frame2, textvariable=strato).pack(side=TOP, anchor=E)
strato_factor = Scale(frame2, from_=0.0, to_=2.5, resolution=0.1, variable=strato, length=300, orient=HORIZONTAL)
strato_factor.pack(side=TOP, anchor=E)
Label(frame2, text='snowalbedo').pack(side=TOP, anchor=E)
snow_al = DoubleVar()
Entry(frame2, textvariable=snow_al).pack(side=TOP, anchor=E)
snow_factor = Scale(frame2, from_=0.0, to_=2.5, resolution=0.1, variable=snow_al, length=300, orient=HORIZONTAL)
snow_factor.pack(side=TOP, anchor=E)
Label(frame2, text='Strato_water').pack(side=TOP, anchor=E)
strato_water = DoubleVar()
Entry(frame2, textvariable=strato_water).pack(side=TOP, anchor=E)
strato_water = Scale(frame2, from_=0.0, to_=2.5, resolution=0.1, variable=strato_water, length=300, orient=HORIZONTAL)
strato_water.pack(side=TOP, anchor=E)
Label(frame2, text='Land_use').pack(side=TOP, anchor=E)
Land_use = DoubleVar()
Entry(frame2, textvariable=Land_use).pack(side=TOP, anchor=E)
land = Scale(frame2, from_=0.0, to_=2.5, resolution=0.1, variable=Land_use, length=300, orient=HORIZONTAL)
land.pack(side=TOP, anchor=E)
# count = IntVar()
# i = Entry(root, textvariable=count).pack()
#
frame1.pack(side = 'left')
frame2.pack(side = 'right')
'''载入变量以及计算'''
forcings = Dataset('Various_forcings.nc')
year = forcings.variables['Year']
num_year = len(year)
v = vars()
ghgf = forcings.variables['Greenhouse_gases'][:]
tr3f = forcings.variables['Tropo_ozone'][:]
so3f = forcings.variables['Strato_ozone'][:]
swvf = forcings.variables['Strato_water'][:]
aerf = forcings.variables['Aerosol_direct'][:]
aclf = forcings.variables['Aerosol_cloud_lifetime'][:]
acaf = forcings.variables['Aerosol_cloud_albedo'][:]
snof = forcings.variables['Snow_albedo'][:]
lndf = forcings.variables['Land_use'][:]
solf = forcings.variables['Solar'][:]
volf = forcings.variables['Volcanic'][:]
temperature_comp = requests.get(
'https://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.6.0.0.annual_ns_avg.txt').text
# print(temperature_comp)
f = open('global NH&SH data.txt', 'w')
f.write(temperature_comp)
f.close()
t = re.findall(r'.{60}', temperature_comp)
t = np.array(t)
Button(root, text='Draw', command=plot).pack(anchor = 'center')
Button(root, text='Quit', command=root.destroy).pack(anchor = 'center')
mainloop()
随便设置一个值,点击DRAW
可以试着设置极端值,比如都是2.5
通过固定一些值可以查看其他因子的影响(这个还没写啊喂准备后面再写)