atmosphericinversion/dashboard/views.py

336 lines
12 KiB
Python
Raw Normal View History

2021-03-26 03:39:49 +00:00
import json
import datetime
import imageio as imageio
from captcha.helpers import captcha_image_url
from captcha.models import CaptchaStore
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
2021-03-05 10:32:10 +00:00
from django.shortcuts import render
2021-03-26 03:39:49 +00:00
import matplotlib.pyplot as plt
2021-03-05 10:32:10 +00:00
# Create your views here.
def index(request):
2021-03-26 03:39:49 +00:00
return render(request, 'dashboard/index.html')
def refresh_captcha(request):
to_json_response = dict()
to_json_response['status'] = 1
to_json_response['new_cptch_key'] = CaptchaStore.generate_key()
to_json_response['new_cptch_image'] = captcha_image_url(to_json_response['new_cptch_key'])
return JsonResponse(to_json_response)
def user_login(request):
username = None
password = None
hash_key = CaptchaStore.generate_key()
image_url = captcha_image_url(hash_key)
if request.method == 'POST':
if not request.POST.get('username'):
messages.error(request, '请输入用户名')
else:
username = request.POST.get('username')
if not request.POST.get('password'):
messages.error(request, '请输入密码')
else:
password = request.POST.get('password')
captcha_input = request.POST.get('captcha_1')
captcha_hashkey = request.POST.get('captcha_0')
try:
CaptchaStore.objects.get(response=captcha_input.lower(), hashkey=captcha_hashkey,
expiration__gt=datetime.now()).delete()
except CaptchaStore.DoesNotExist:
messages.error(request, '验证码错误')
return HttpResponseRedirect('/login')
if username is not None and password is not None:
try:
user = authenticate(username=username, password=password)
if user is not None:
if user.check_password(password):
login(request, user)
return HttpResponseRedirect('/')
else:
messages.error(request, '无效的账号')
else:
messages.error(request, '账号或密码错误,请您确认账号和密码')
except:
messages.error(request, '账号或密码错误,请您确认账号和密码')
return render(request, 'dashboard/login.html', {'hash_key': hash_key, 'image_url': image_url})
def user_logout(request):
logout(request)
return HttpResponseRedirect('/')
from nrlmsise00.dataset import msise_4d
import numpy as np
def msise_flat_data(request):
2021-03-30 03:08:18 +00:00
date = request.GET.get('date')
print(date)
year = int(str(date).split('-')[0])
month = int(str(date).split('-')[1])
day = int(str(date).split('-')[2])
time = int(request.GET.get('time'))
lat = int(request.GET.get('lat'))
lon = int(request.GET.get('lon'))
height1 = int(request.GET.get('height1'))
height2 = int(request.GET.get('height2'))
interval = int(request.GET.get('interval'))
print(date,time,lat,lon,height1,height2,interval)
2021-03-26 03:39:49 +00:00
result = dict()
O = []
N2 = []
O2 = []
2021-03-30 03:08:18 +00:00
H = []
N = []
# try:
# alts = [200, 300, 400] # = [200, 300, 400] [km]
alts = np.arange(height1, height2, interval)
print(alts)
lats = [lat] # = [60, 70] [°N]
lons = [lon] # = [-70, -35, 0, 35, 70] [°E]
ds = msise_4d(datetime.datetime(year, month, day, time, 0, 0), alts, lats, lons)
print(ds.N)
for h in ds.H.data.tolist():
for h2 in h:
for h3 in h2:
for h4 in h3:
H.append(float(h4)/1000000000000000)
for o1 in ds.O.data.tolist():
for o2 in o1:
for o3 in o2:
for o4 in o3:
O.append(float(o4)/1000000000000000)
for n1 in ds.N2.data.tolist():
for n2 in n1:
for n3 in n2:
for n4 in n3:
N2.append(float(n4)/1000000000000000)
for o21 in ds.O2.data.tolist():
for o22 in o21:
for o23 in o22:
for o24 in o23:
O2.append(float(o24)/1000000000000000)
for n1 in ds.N.data.tolist():
for n2 in n1:
for n3 in n2:
for n4 in n3:
N.append(float(n4)/1000000000000000)
result['H'] = H
result['O'] = O
result['N2'] = N2
result['O2'] = O2
result['N'] = N
result['alts'] = list(map(int, alts))
print(result)
# except:
# pass
2021-03-26 03:39:49 +00:00
return HttpResponse(json.dumps({
"data": result
}
))
def runing_model(request):
He = []
O = []
N2 = []
O2 = []
Ar = []
rho = []
Texo = []
Talt = []
lst = []
Ap = []
f107 = []
f107a = []
results = []
dts = []
frames = []
yearList = request.GET.get('yearList')
monthList = request.GET.get('monthList')
dayList = request.GET.get('dayList')
lonList = request.GET.get('lonList')
latList = request.GET.get('latList')
altList = request.GET.get('altList')
2021-03-30 03:47:27 +00:00
print(lonList)
2021-03-26 03:39:49 +00:00
dt = None
lon = []
lat = []
alt = []
dts = []
if yearList:
for i, value in enumerate(yearList[1:-1].split(',')):
year = int(value.split('"')[1])
month = int(monthList[1:-1].split(',')[i].split('"')[1])
dt = datetime.datetime(int(year), int(month), 1, 0, 0, 0)
dts.append(dt)
2021-03-30 03:47:27 +00:00
print(lonList[1:-1].split(',')[i])
if "." in str(lonList[1:-1].split(',')[i]):
print("++++++++++++++++++++++++++++++++++++++++++++")
print(int(lonList[1:-1].split(',')[i].split('"')[1].split('.')[0]))
print(int(lonList[1:-1].split(',')[i].split('"')[1].split('.')[0]))
print(int(lonList[1:-1].split(',')[i].split('"')[1].split('.')[0]))
lon = np.arange(int(lonList[1:-1].split(',')[i].split('"')[1].split('.')[0]), int(lonList[1:-1].split(',')[i].split('"')[1].split('.')[1]), int(lonList[1:-1].split(',')[i].split('"')[1].split('.')[2]))
else:
lon.append(int(lonList[1:-1].split(',')[i].split('"')[1]))
if "," in str(latList[1:-1].split(',')[i]) or "" in str(latList[1:-1].split(',')[i]):
lat = np.arange(int(latList[1:-1].split(',')[i].split('"')[1].split('.')[0]), int(latList[1:-1].split(',')[i].split('"')[1].split('.')[1]), int(latList[1:-1].split(',')[i].split('"')[1].split('.')[2]))
2021-03-26 03:39:49 +00:00
2021-03-30 03:47:27 +00:00
else:
lat.append(int(latList[1:-1].split(',')[i].split('"')[1]))
2021-03-26 03:39:49 +00:00
alt.append(int(altList[1:-1].split(',')[i].split('"')[1]))
results.append(str(year) + str(month))
2021-03-30 03:08:18 +00:00
ds = msise_4d(dt, alt, lat, lon)
for h1 in ds.He.data.tolist():
for h2 in h1:
for h3 in h2:
for h4 in h3:
He.append(float(h4)/1000000)
for o1 in ds.O.data.tolist():
for o2 in o1:
for o3 in o2:
for o4 in o3:
O.append(float(o4)/1000000)
for n1 in ds.N2.data.tolist():
for n2 in n1:
for n3 in n2:
for n4 in n3:
N2.append(float(n4)/1000000)
for o21 in ds.O2.data.tolist():
for o22 in o21:
for o23 in o22:
for o24 in o23:
O2.append(float(o24)/1000000)
for ar1 in ds.Ar.data.tolist():
for ar2 in ar1:
for ar3 in ar2:
for ar4 in ar3:
Ar.append(float(ar4)/1000000)
for r1 in ds.rho.data.tolist():
for r2 in r1:
for r3 in r2:
for r4 in r3:
rho.append(float(r4)/1000000)
for te in ds.Texo.data.tolist():
for t2 in te:
for t3 in t2:
for t4 in t3:
Texo.append(float(t4)/1000000)
for ta in ds.Talt.data.tolist():
for t2 in ta:
for t3 in t2:
for t4 in t3:
Talt.append(float(t4)/1000000)
for l in ds.lst.data.tolist():
for l2 in l:
lst.append(float(l2)/1000000)
for a in ds.Ap.data.tolist():
Ap.append(float(a)/1000000)
for f7 in ds.f107.data.tolist():
f107.append(float(f7)/1000000)
for fa in ds.f107a.data.tolist():
f107a.append(float(fa)/1000000)
# return render(request,'dashboard/runing_model_results.html',{'He':He,'O':O,'N2':N2,'O2':O2,'Ar':Ar,'rho':rho,'Texo':Texo,'Talt':Talt,'lst':lst,'Ap':Ap,'f107':f107,'f107a':f107a,'results':list(map(int, results))})
2021-03-26 03:39:49 +00:00
return HttpResponse(json.dumps({
'He': He, 'O': O, 'N2': N2, 'O2': O2, 'Ar': Ar, 'rho': rho, 'Texo': Texo, 'Talt': Talt, 'lst': lst,
'Ap': Ap, 'f107': f107, 'f107a': f107a, 'results': list(map(int, results))
}))
return render(request, 'dashboard/xulie.html')
2021-03-30 03:08:18 +00:00
def create_gif(request):
2021-03-30 03:47:27 +00:00
# yearList = request.GET.get('yearList')
# monthList = request.GET.get('monthList')
# dayList = request.GET.get('dayList')
# lonList = request.GET.get('lonList')
# latList = request.GET.get('latList')
# altList = request.GET.get('altList')
2021-03-30 03:08:18 +00:00
#
2021-03-30 03:47:27 +00:00
# dt = None
# lons = []
# lats = []
# alts = []
# dts = []
# if yearList:
# for i, value in enumerate(yearList[1:-1].split(',')):
# year = int(value.split('"')[1])
# month = int(monthList[1:-1].split(',')[i].split('"')[1])
# print(month)
# dt = datetime.datetime(int(year), int(month), 1, 0, 0, 0)
# dts.append(dt)
# lons.append(int(lonList[1:-1].split(',')[i].split('"')[1]))
2021-03-30 03:08:18 +00:00
#
2021-03-30 03:47:27 +00:00
# lats.append(int(latList[1:-1].split(',')[i].split('"')[1]))
2021-03-30 03:08:18 +00:00
#
2021-03-30 03:47:27 +00:00
# alts.append(int(altList[1:-1].split(',')[i].split('"')[1]))
# 日期
dt1 = datetime.datetime(2020, 1, 22, 8, 5, 20) # 年、月、日、时、分、秒
dt2 = datetime.datetime(2020, 2, 22, 8, 5, 20) # 年、月、日、时、分、秒
dt3 = datetime.datetime(2020, 3, 22, 8, 5, 20)
dt4 = datetime.datetime(2020, 4, 22, 8, 5, 20)
dt5 = datetime.datetime(2020, 5, 22, 8, 5, 20)
dt6 = datetime.datetime(2020, 6, 22, 8, 5, 20)
dts = [dt1, dt2, dt3, dt4, dt5, dt6]
# 海拔高度
alts = np.arange(1000, 1001, 1.) # = [200, 300, 400] [km]
# 纬度
lats = np.arange(-90, 91, 10.) # = [60, 70] [°N]
# 经度
lons = np.arange(-70, 71, 10) # = [-70, -35, 0, 35, 70] [°E]
2021-03-26 03:39:49 +00:00
fig = plt.figure() # (figsize=(16, 8))
fs = []
for counter, dt in enumerate(dts):
# broadcasting is done internally
ds = msise_4d(dt, alts, lats, lons)
# 创建一个没有 axes 的 figure
# fig.suptitle('Model nrlmsise00') # 添加标题以便我们辨别
lats = ds.lat.data
lons = ds.lon.data
x, y = np.meshgrid(lons, lats)
data = ds.He[0, 0, :, :].data
plt.contourf(x, y, data, cmap=plt.cm.Spectral, alpha=0.8)
plt.pause(0.3)
# plt.legend()
# plt.show()
# time.sleep(2)
2021-03-26 12:58:30 +00:00
# f = '/static/image/%d.png' % (counter)
f = '/var/www/p3/atmosphericinversion/static/image/%d.png' % (counter)
2021-03-26 12:53:17 +00:00
fs.append(f)
2021-03-26 03:39:49 +00:00
fig.savefig(f, dpi=200)
2021-03-26 12:53:17 +00:00
gif_name = 'nrlmsise00.gif'
2021-03-26 13:08:41 +00:00
path_dest = '/var/www/p3/atmosphericinversion/static/image/'
2021-03-26 12:53:17 +00:00
duration = 0.35
frames = []
for image_name in fs:
2021-03-26 12:58:30 +00:00
print(image_name)
2021-03-26 12:53:17 +00:00
frames.append(imageio.imread(image_name))
2021-03-30 03:08:18 +00:00
imageio.mimsave(path_dest + gif_name, frames, 'GIF', duration=duration)
2021-03-26 12:58:30 +00:00
2021-03-26 03:39:49 +00:00
return HttpResponse(json.dumps({
2021-03-30 03:08:18 +00:00
"image": fs
2021-03-26 03:39:49 +00:00
}))
2021-03-26 12:53:17 +00:00