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 from django.shortcuts import render import matplotlib.pyplot as plt # Create your views here. def index(request): 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): 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) result = dict() O = [] N2 = [] O2 = [] 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)) for o1 in ds.O.data.tolist(): for o2 in o1: for o3 in o2: for o4 in o3: O.append(float(o4)) for n1 in ds.N2.data.tolist(): for n2 in n1: for n3 in n2: for n4 in n3: N2.append(float(n4)) for o21 in ds.O2.data.tolist(): for o22 in o21: for o23 in o22: for o24 in o23: O2.append(float(o24)) for n1 in ds.N.data.tolist(): for n2 in n1: for n3 in n2: for n4 in n3: N.append(float(n4)) 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 return HttpResponse(json.dumps({ "data": result } )) def msise_flat_string(request): date = request.GET.get('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')) result = dict() O = [] N2 = [] O2 = [] H = [] N = [] # try: # alts = [200, 300, 400] # = [200, 300, 400] [km] alts = np.arange(height1, height2, interval) 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) for h in ds.H.data.tolist(): for h2 in h: for h3 in h2: for h4 in h3: print(h4) print("{0:X}".format(int(h4))) H.append("{0:X}".format(int(h4))) for o1 in ds.O.data.tolist(): for o2 in o1: for o3 in o2: for o4 in o3: O.append("{0:X}".format(int(o4))) for n1 in ds.N2.data.tolist(): for n2 in n1: for n3 in n2: for n4 in n3: N2.append("{0:X}".format(int(n4))) for o21 in ds.O2.data.tolist(): for o22 in o21: for o23 in o22: for o24 in o23: O2.append("{0:X}".format(int(o24))) for n1 in ds.N.data.tolist(): for n2 in n1: for n3 in n2: for n4 in n3: N.append("{0:X}".format(int(n4))) result['H'] = H result['O'] = O result['N2'] = N2 result['O2'] = O2 result['N'] = N result['alts'] = list(map(int, alts)) # except: # pass 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') print(lonList) 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) 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])) else: lat.append(int(latList[1:-1].split(',')[i].split('"')[1])) alt.append(int(altList[1:-1].split(',')[i].split('"')[1])) results.append(str(year) + str(month)) 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)) for o1 in ds.O.data.tolist(): for o2 in o1: for o3 in o2: for o4 in o3: O.append(float(o4)) for n1 in ds.N2.data.tolist(): for n2 in n1: for n3 in n2: for n4 in n3: N2.append(float(n4)) for o21 in ds.O2.data.tolist(): for o22 in o21: for o23 in o22: for o24 in o23: O2.append(float(o24)) for ar1 in ds.Ar.data.tolist(): for ar2 in ar1: for ar3 in ar2: for ar4 in ar3: Ar.append(float(ar4)) for r1 in ds.rho.data.tolist(): for r2 in r1: for r3 in r2: for r4 in r3: rho.append(float(r4)) for te in ds.Texo.data.tolist(): for t2 in te: for t3 in t2: for t4 in t3: Texo.append(float(t4)) for ta in ds.Talt.data.tolist(): for t2 in ta: for t3 in t2: for t4 in t3: Talt.append(float(t4)) for l in ds.lst.data.tolist(): for l2 in l: lst.append(float(l2)) for a in ds.Ap.data.tolist(): Ap.append(float(a)) for f7 in ds.f107.data.tolist(): f107.append(float(f7)) for fa in ds.f107a.data.tolist(): f107a.append(float(fa)) # 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))}) 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') def runing_model_alts(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') print(lonList) 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) 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])) else: lat.append(int(latList[1:-1].split(',')[i].split('"')[1])) alt.append(int(altList[1:-1].split(',')[i].split('"')[1])) results.append(str(year) + str(month)) ds = msise_4d(dt, alt, lat, lon) for h1 in ds.He[0,:,0,0].data.tolist(): He.append(float(h1)) for o1 in ds.O[0,:,0,0].data.tolist(): O.append(float(o1)) for n1 in ds.N2[0,:,0,0].data.tolist(): N2.append(float(n1)) for o21 in ds.O2[0,:,0,0].data.tolist(): O2.append(float(o21)) for ar1 in ds.Ar[0,:,0,0].data.tolist(): Ar.append(float(ar1)) for r1 in ds.rho[0,:,0,0].data.tolist(): rho.append(float(r1)) for te in ds.Texo[0,:,0,0].data.tolist(): Texo.append(float(te)) for ta in ds.Talt[0,:,0,0].data.tolist(): Talt.append(float(ta)) for l in ds.lst.data.tolist(): for l1 in l: lst.append(float(l1)) for a in ds.Ap.data.tolist(): print(a) Ap.append(float(a)) for f7 in ds.f107.data.tolist(): print(f7) f107.append(float(f7)) for fa in ds.f107a.data.tolist(): print(fa) f107a.append(float(fa)) # 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))}) 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, 'alt': list(map(int, alt)) })) return render(request, 'dashboard/xulie.html') def create_gif(request): # 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') # # 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])) # # lats.append(int(latList[1:-1].split(',')[i].split('"')[1])) # # alts.append(int(altList[1:-1].split(',')[i].split('"')[1])) # 日期 dt1 = datetime.datetime(2020, 1, 1, 8, 5, 20) # 年、月、日、时、分、秒 dt2 = datetime.datetime(2020, 2, 1, 8, 5, 20) # 年、月、日、时、分、秒 dt3 = datetime.datetime(2020, 3, 1, 8, 5, 20) dt4 = datetime.datetime(2020, 4, 1, 8, 5, 20) dt5 = datetime.datetime(2020, 5, 1, 8, 5, 20) dt6 = datetime.datetime(2020, 6, 1, 8, 5, 20) dts = [dt1, dt2, dt3, dt4, dt5, dt6] dtss = ['2020-1-1','2020-2-1','2020-3-1','2020-4-1','2020-5-1','2020-6-1'] # 海拔高度 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] 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.title(u'按时间',fontproperties='SimHei') # plt.legend() # plt.show() # time.sleep(2) # f = '/static/image/%d.png' % (counter) f = '/var/www/p3/atmosphericinversion/static/image/%d.png' % (counter) fs.append(f) # fig.savefig(f, dpi=200) gif_name = 'nrlmsise00.gif' path_dest = '/var/www/p3/atmosphericinversion/static/image/' duration = 0.35 frames = [] for image_name in fs: print(image_name) frames.append(imageio.imread(image_name)) imageio.mimsave(path_dest + gif_name, frames, 'GIF', duration=duration) t = dtss lo = lons la = lats al = alts return HttpResponse(json.dumps({ "image": fs, 't':list(t), 'lo':list(map(int, lo)), 'la':list(map(int, la)), 'al':list(map(int, al)) }))