之前的博客中推荐了几个在线计算TEC等参数的网站,很方便,但是每次计算还需要手动一个一个在网页上输入信息,稍有一些麻烦,因此博主利用Python实现了一个在本地输入信息,Python自动填充到网页并打印出结果的程序
# -*- coding: utf-8 -*-
# @Time : 2022/1/5 14:21
# @Author : xymeng
# @FileName: 002.py 利用IRI模型在网页上计算TEC等参数专用程序
# @Software: PyCharm
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
import time
import json
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import pandas as pd
import io
'''
定义数据容纳列表
'''
TEC = []
IV = [] #Independent Variable
'''
定义基本信息
'''
year = input('请输入计算年份:'.format())
month = input('请输入计算月份:'.format())
month = month.zfill(2)
Lat = input('请输入要计算的纬度(-90. - 90. deg.):'.format())
Lon = input('请输入要计算的经度(0. - 360. deg.):'.format())
Lat = Lat + '.'
Lon = Lon + '.'
pf = input('请输入配置文件类型:\n1:Height [60. - 2000. km] 2:Latitude [-90. - 90. deg.] 3:Longitude [0. - 360. deg.] 4:Year [1958-2019] 5:Month [1-12] 6:Day of month[1-31] 7:Day of Year[1-365] 8:Hour profile[0.-24.]:\n'.format())
st = input('请输入计算起始度数:'.format())
sp = input('请输入计算截至度数:'.format())
interval = input('请输入度数间隔:'.format())
Upperboundary = input('请输入TEC最上层界限 (110. - 10000. km):'.format())
Upperboundary = Upperboundary + '.'
s = Service("E:\chromedriver_win32\chromedriver.exe") #这里写本地的msedge的所在路径
driver = webdriver.Chrome(service=s)
driver.get("https://ccmc.gsfc.nasa.gov/modelweb/models/iri2016_vitmo.php") #该处为具体网址
driver.refresh() #刷新页面
driver.maximize_window() #浏览器最大化
'''
以下代码是输入计算的基本信息
'''
driver.find_element(By.NAME,'year').clear()
driver.find_element(By.NAME,'year').send_keys(year)
Select(driver.find_element(By.NAME,'month')).select_by_value(month)
driver.find_element(By.NAME,'latitude').clear()
driver.find_element(By.NAME,'latitude').send_keys(Lat)
driver.find_element(By.NAME,'longitude').clear()
driver.find_element(By.NAME,'longitude').send_keys(Lon)
Select(driver.find_element(By.NAME,'profile')).select_by_value(pf)
driver.find_element(By.NAME,'start').clear()
driver.find_element(By.NAME,'start').send_keys(st)
driver.find_element(By.NAME,'stop').clear()
driver.find_element(By.NAME,'stop').send_keys(sp)
driver.find_element(By.NAME,'step').clear()
driver.find_element(By.NAME,'step').send_keys(interval)
driver.find_element(By.NAME,'htec_max').clear()
driver.find_element(By.NAME,'htec_max').send_keys(Upperboundary)
driver.find_element(By.NAME,'height').clear()
driver.find_element(By.NAME,'height').send_keys(450.)
'''
以下是选中和取消独立变量
'''
driver.find_element(By.XPATH,'//input[@value="06"]').click() # click
driver.find_element(By.XPATH,'//input[@value="07"]').send_keys(Keys.SPACE) # send space
'''
以下代码分别取消选中输出值复选框,Electron_density (Ne), m-3 , Ratio of Ne and F2 peak density(Ne/NmF2)>
'''
driver.find_element(By.XPATH,'//input[@value="17"]').click() # click
driver.find_element(By.XPATH,'//input[@value="18"]').send_keys(Keys.SPACE) # send space
'''
以下代码分别选中输出值复选框,ION Temperature , TEC , NmF2 , foF2
'''
driver.find_element(By.XPATH,'//input[@value="20"]').click() # click
driver.find_element(By.XPATH,'//input[@value="29"]').send_keys(Keys.SPACE) # send space
driver.find_element(By.XPATH,'//input[@value="35"]').click() # click
driver.find_element(By.XPATH,'//input[@value="44"]').send_keys(Keys.SPACE) # send space
count = 4 #所选输出参数值
'''
点击计算
'''
driver.find_element(By.XPATH,'//input[@value="Submit"]').click() # send space
'''
以下为提取计算结果
'''
result = driver.find_element(By.TAG_NAME,'pre')
data = result.text
'''
以下为将计算结果先保存到txt文本中
'''
path = r'F:\IRI计算包\result\002.txt'
with open(path,'w') as xym:
xym.write(data)
'''
以下为将计算的结果从保存到的txt文本中提取出来
'''
with open(path,'r') as yxc:
findata = yxc.readlines()
tecnum = 0 #TEC输出值在第几个
tecvalnum = 0 #TEC输出值从第几行开始
for a in range(len(findata)):
if findata[a][2:5] == 'TEC':
tecnum = a
if findata[a] == '\n':
tecvalnum = a + 2
for b in range(tecvalnum,len(findata)):
tecdata = findata[b].split(' ')
for c in range(tecdata.count('')):
tecdata.remove('')
TEC.append(float(tecdata[tecnum-1]))
IV.append(float(tecdata[0]))
print(TEC)
print(IV)
# -*- coding: utf-8 -*-
# @Time : 2022/1/21 15:13
# @Author : xymeng
# @FileName: 002-plot.py 用于绘制IRI模型计算出的TEC曲线
# @Software: PyCharm
import matplotlib.pyplot as plt
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
import time
import json
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import pandas as pd
import io
import matplotlib.pyplot as plt
import numpy as np
'''
定义数据容纳列表
'''
TEC = []
IV = [] #Independent Variable
'''
定义基本信息
'''
year = input('请输入计算年份:'.format())
month = input('请输入计算月份:'.format())
month = month.zfill(2)
Lat = float(input('请输入要计算的纬度(-90. - 90. deg.):'.format()))
Lon = float(input('请输入要计算的经度(0. - 360. deg.):'.format()))
# Lat = Lat + '.'
# Lon = Lon + '.'
pf = input('请输入配置文件类型:\n1:Height [60. - 2000. km] 2:Latitude [-90. - 90. deg.] 3:Longitude [0. - 360. deg.] 4:Year [1958-2019] 5:Month [1-12] 6:Day of month[1-31] 7:Day of Year[1-365] 8:Hour profile[0.-24.]:\n'.format())
st = input('请输入计算起始度数:'.format())
sp = input('请输入计算截至度数:'.format())
interval = input('请输入度数间隔:'.format())
Upperboundary = input('请输入TEC最上层界限 (110. - 10000. km):'.format())
Upperboundary = Upperboundary + '.'
s = Service("E:\chromedriver_win32\chromedriver.exe") #这里写本地的msedge的所在路径
driver = webdriver.Chrome(service=s)
driver.get("https://ccmc.gsfc.nasa.gov/modelweb/models/iri2016_vitmo.php") #该处为具体网址
driver.refresh() #刷新页面
driver.maximize_window() #浏览器最大化
'''
以下代码是输入计算的基本信息
'''
driver.find_element(By.NAME,'year').clear()
driver.find_element(By.NAME,'year').send_keys(year)
Select(driver.find_element(By.NAME,'month')).select_by_value(month)
driver.find_element(By.NAME,'latitude').clear()
driver.find_element(By.NAME,'latitude').send_keys(Lat)
driver.find_element(By.NAME,'longitude').clear()
driver.find_element(By.NAME,'longitude').send_keys(Lon)
Select(driver.find_element(By.NAME,'profile')).select_by_value(pf)
driver.find_element(By.NAME,'start').clear()
driver.find_element(By.NAME,'start').send_keys(st)
driver.find_element(By.NAME,'stop').clear()
driver.find_element(By.NAME,'stop').send_keys(sp)
driver.find_element(By.NAME,'step').clear()
driver.find_element(By.NAME,'step').send_keys(interval)
driver.find_element(By.NAME,'htec_max').clear()
driver.find_element(By.NAME,'htec_max').send_keys(Upperboundary)
driver.find_element(By.NAME,'height').clear()
driver.find_element(By.NAME,'height').send_keys(450.)
'''
以下是选中和取消独立变量
'''
driver.find_element(By.XPATH,'//input[@value="04"]').click() # click
driver.find_element(By.XPATH,'//input[@value="07"]').send_keys(Keys.SPACE) # send space
'''
以下代码分别取消选中输出值复选框,Electron_density (Ne), m-3 , Ratio of Ne and F2 peak density(Ne/NmF2)>
'''
driver.find_element(By.XPATH,'//input[@value="17"]').click() # click
driver.find_element(By.XPATH,'//input[@value="18"]').send_keys(Keys.SPACE) # send space
'''
以下代码分别选中输出值复选框,ION Temperature , TEC , NmF2 , foF2
'''
driver.find_element(By.XPATH,'//input[@value="20"]').click() # click
driver.find_element(By.XPATH,'//input[@value="29"]').send_keys(Keys.SPACE) # send space
driver.find_element(By.XPATH,'//input[@value="35"]').click() # click
driver.find_element(By.XPATH,'//input[@value="44"]').send_keys(Keys.SPACE) # send space
count = 4 #所选输出参数值
'''
点击计算
'''
driver.find_element(By.XPATH,'//input[@value="Submit"]').click() # send space
'''
以下为提取计算结果
'''
result = driver.find_element(By.TAG_NAME,'pre')
data = result.text
'''
以下为将计算结果先保存到txt文本中
'''
path = r'F:\IRI计算包\result\002.txt'
with open(path,'w') as xym:
xym.write(data)
'''
以下为将计算的结果从保存到的txt文本中提取出来
'''
with open(path,'r') as yxc:
findata = yxc.readlines()
tecnum = 0 #TEC输出值在第几个
tecvalnum = 0 #TEC输出值从第几行开始
for a in range(len(findata)):
if findata[a][2:5] == 'TEC':
tecnum = a
if findata[a] == '\n':
tecvalnum = a + 2
for b in range(tecvalnum,len(findata)):
tecdata = findata[b].split(' ')
for c in range(tecdata.count('')):
tecdata.remove('')
TEC.append((float(tecdata[tecnum-1]))*10)
IV.append(float(tecdata[0]))
time = [0,2,4,6,8,10,12,14,16,18,20,22,24]
plt.plot(time,TEC,'k*-',markersize=10)
plt.xlabel('Hour')
plt.ylabel('TEC, 10^16 m-2')
plt.title('IRI2016')
plt.show()