How to use tb_frame method in Slash

Best Python code snippet using slash

cosmo-serv.py

Source:cosmo-serv.py Github

copy

Full Screen

1import eventlet2eventlet.monkey_patch()3# app.py4import multiprocessing5import threading6from flask import Flask, jsonify, request, render_template, session, copy_current_request_context7from flask_compress import Compress8import yt9from yt import YTArray10import trident11import numpy as np12from astropy.io import fits13from astropy.table import Table14import json15import yt.units as units16from yt.visualization.volume_rendering.api import PointSource17from yt.units import kpc18import pylab19from itertools import product20from flask import Response21from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect22import random23import mpi4py24from mpi4py import MPI25import os.path26from os import path27from trident.config import trident_path28from scipy import interpolate29import sys,os30import copy31import pprint32import psutil33import time34#Flask is used as web framework to run python scripts35#Flask-io / socketio : gives Flask applications 36# access to low latency bi-directional communications37# between the clients and the server.38 39async_mode = 'eventlet'40app = Flask(__name__)41app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 # clears cache on load for debugging42app.config['SECRET_KEY'] = 'secret!'43app.config['TEMPLATES_AUTO_RELOAD'] = True44Compress(app)45# socketio = SocketIO(app, cors_allowed_origins="*", async_mode=async_mode,async_handlers=True,upgradeTimeout=240000,logger=True, engineio_logger=True)46socketio = SocketIO(app,cors_allowed_origins="https://cosmovis-dev.nrp-nautilus.io", async_mode=async_mode,async_handlers=True,upgradeTimeout=240000)#,logger=True, engineio_logger=True)47# thread = None48# thread_lock = Lock()49multiprocessing.set_start_method('spawn')50yt.enable_parallelism()51sgs = []52rpts = {}53spectrum_hdul = fits.HDUList()54try:55 EAGLE_12Mpc = yt.load('static/data/RefL0012N0188/snapshot_028_z000p000/snap_028_z000p000.0.hdf5')56 EAGLE_12Mpc_ad = EAGLE_12Mpc.all_data()57except Exception as e:58 print('error: '+ str( e ))59 exc_type, exc_obj, exc_tb = sys.exc_info()60 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]61 print(exc_type, fname, exc_tb.tb_lineno) 62try:63 EAGLE_25Mpc = yt.load('static/data/RefL0025N0376/snapshot_028_z000p000/snap_028_z000p000.0.hdf5')64 #EAGLE_25Mpc = yt.load('/cv-vol/EAGLE25_z0_0/RefL0025N0376/snapshot_028_z000p000/snap_028_z000p000.0.hdf5')65 EAGLE_25Mpc_ad = EAGLE_25Mpc.all_data()66except Exception as e:67 print('error: '+ str( e ))68 exc_type, exc_obj, exc_tb = sys.exc_info()69 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]70 print(exc_type, fname, exc_tb.tb_lineno) 71try:72 EAGLE_100Mpc = yt.load('static/data/RefL0100N1504/snapshot/RefL0100N1504/snapshot_028_z000p000/snap_028_z000p000.0.hdf5')73 # EAGLE_100Mpc = yt.load('/cv-vol/EAGLE100_z0_0/RefL0100N1504/snapshot_028_z000p000/snap_028_z000p000.0.hdf5')74 EAGLE_100Mpc_ad = EAGLE_100Mpc.all_data()75except Exception as e:76 print('error: '+ str( e ))77 exc_type, exc_obj, exc_tb = sys.exc_info()78 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]79 print(exc_type, fname, exc_tb.tb_lineno)80try:81 TNG100_snap030 = yt.load('static/data/TNG100_z2.3/snapshot/snap_030.0.hdf5')82 # TNG100_snap030 = yt.load('/cv-vol/TNG100_z2_3/snap_030.0.hdf5')83 TNG100_snap030_ad = TNG100_snap030.all_data()84except Exception as e:85 print('error: '+ str( e ))86 exc_type, exc_obj, exc_tb = sys.exc_info()87 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]88 print(exc_type, fname, exc_tb.tb_lineno) 89try:90 TNG100_snap099 = yt.load('static/data/TNG100_z0.0/snapshot/snap_099.0.hdf5')91 # TNG100_snap099 = yt.load('/cv-vol/TNG100_z0.0/snap_099.0.hdf5')92 TNG100_snap099_ad = TNG100_snap099.all_data()93except Exception as e:94 print('error: '+ str( e ))95 exc_type, exc_obj, exc_tb = sys.exc_info()96 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]97 print(exc_type, fname, exc_tb.tb_lineno) 98def truncate(f, n):99 '''Truncates/pads a float f to n decimal places without rounding'''100 s = '{}'.format(f)101 if 'e' in s or 'E' in s:102 return '{0:.{1}f}'.format(f, n)103 i, p, d = s.partition('.')104 return '.'.join([i, (d+'0'*n)[:n]])105def interpol8(oldx,oldy,newdx):106 107 # H_I = n.array([np.array(ray.r[('gas', 'l')].to('kpc').tolist()),np.array(ray.r[('gas', 'H_p0_number_density')] * ray.r[('gas', 'dl')].to('kpc').tolist())])108 oldx = np.array(oldx.tolist())109 oldy = np.array(oldy.tolist())110 # newdx = 1111 terpfunc = interpolate.interp1d(oldx, oldy,fill_value="extrapolate")112 newx = np.arange(np.min(oldx), np.max(oldx)+newdx, newdx)113 newy = terpfunc(newx)114 return newx.tolist(), newy.tolist()115# createLookup()116# import pdb; pdb.set_trace()117# host the index.html web page118# default is localhost:5000119@app.route('/webhook', methods=['POST'])120def webhook():121 if request.headers['Content-Type'] == 'application/json':122 info = json.dumps(request.json)123 print(info)124# threading.Thread(target=lambda: [time.sleep(2), os.system('systemctl restart cosmovis.service')]).start()125 os.system('/bin/bash update.sh')126 return info127@app.route('/')128def index():129 return render_template('index.html', async_mode=socketio.async_mode)130#getSkewerSimpleRay -- this function is used to get the 'quick' data along the skewer and sends it back to the frontend131@socketio.on('getSkewerSimpleRay',namespace="/test")132def handle_ray_selection(simID,idx, start, end):133 # try:134 135 print(multiprocessing.cpu_count())136 sys.stdout.flush()137 socketio.start_background_task(handle_skewer_simple_ray,simID,idx, start, end)138 # th = multiprocessing.Process(target=handle_skewer_simple_ray(simID,idx, start, end))139 # th.start()140 # th.join()141 # except Exception as e:142 # print('error: '+ str( e ))143 # exc_type, exc_obj, exc_tb = sys.exc_info()144 # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]145 # print(exc_type, fname, exc_tb.tb_lineno) 146def handle_skewer_simple_ray(simID,idx,start,end):147 yt.enable_parallelism()148 print('system cpu cores: ' + str(multiprocessing.cpu_count()))149 sys.stdout.flush()150 print('cpu cores available to python: ' + str(len(psutil.Process().cpu_affinity())))151 sys.stdout.flush()152 print('mpi parallelism: ' + str(yt.enable_parallelism()))153 sys.stdout.flush()154 print('received simple ray request')155 socketio.emit( 'retrievingLineData', {'index': idx}, namespace = '/test' )156 socketio.sleep(0)157 fn = ''158 # ds = []159 160 if simID == 'RefL0012N0188':161 ds = EAGLE_12Mpc162 elif simID == 'RefL0025N0376':163 ds = EAGLE_25Mpc164 elif simID == 'RefL0100N1504':165 ds = EAGLE_100Mpc166 elif simID == 'TNG100_z2.3':167 ds = TNG100_snap030168 elif simID == 'TNG100_z0.0':169 ds = TNG100_snap099170 else:171 # if 'RefL' in simID:172 # fn = 'static/data/'+simID+'/snapshot_028_z000p000/snap_028_z000p000.0.hdf5'173 # if 'TNG' in simID:174 # fn = 'static/data/'+simID+'/snapshot/snap_030.0.hdf5'175 # print("loading" + str(fn))176 print(fn)177 ds = yt.load(fn)178 socketio.sleep(0)179 ad = ds.all_data()180 socketio.sleep(0)181 print(simID)182 socketio.sleep(0)183 sim_width = np.float(ds.domain_right_edge[0].in_units('Mpc') - ds.domain_left_edge[0].in_units('Mpc'))184 # print(ds.domain_right_edge)185 ray_start = np.float_(start)186 ray_end = np.float_(end)187 # print('ray start: ' + str(ray_start))188 # print('ray end : ' + str(ray_end))189 ray_start = ds.arr(ray_start/sim_width * np.float(ds.domain_right_edge[0]), 'code_length') #list(np.float_(start))190 ray_end = ds.arr(ray_end/sim_width * np.float(ds.domain_right_edge[0]), 'code_length') #list(np.float_(end))191 # ray_start = ds.arr(ray_start, 'Mpc')/sim_width #list(np.float_(start))192 # ray_end = ds.arr(ray_end, 'Mpc')/sim_width #list(np.float_(end))193 print(ray_start.in_units('Mpc'))194 print(ray_end.in_units('Mpc'))195 line_list = ['H', 'C', 'N', 'O', 'Mg']196 socketio.sleep(0)197 # This LightRay object is a yt dataset of a 1D data structure representing the skewer path as it traverses the dataset. 198 if simID[:3] == 'Ref':199 ray = trident.make_simple_ray(ds, start_position=ray_start,200 end_position=ray_end,201 data_filename="ray.h5",202 lines=line_list,203 ftype='gas',204 fields=[('PartType0','Entropy')])205 elif simID[:3] == 'TNG':206 ray = trident.make_simple_ray(ds, start_position=ray_start,207 end_position=ray_end,208 data_filename="ray.h5",209 lines=line_list)210 socketio.sleep(0)211 trident.add_ion_fields(ray, ions=['O VI', 'C IV', 'N', 'He I', 'He II', 'He III', 'O II', 'O III', 'O V', 'Ne III', 'Ne IV', 'Ne V', 'Ne VI', 'Ne VIII', 'Na I', 'Na IX', 'Mg X', 'Si II', 'Si III', 'Si IV', 'Si XII', 'S II', 'S III', 'S IV', 'S V', 'S VI', 'O IV'], ftype="PartType0")212 socketio.sleep(0)213 # for field in ray.derived_field_list: print(field)214 # ('gas', 'l') -- the 1D location of the gas going from nearby (0) to faraway along the LightRay215 # convert to kpc216 l = ray.r[('gas', 'l')].to('kpc')217 print(ray)218 # ('gas', 'temperature') -- the gas temperature along l (K)219 # the way trident+yt represents ions as fields is a little weird220 # ex: For H I, which is neutral hydrogen, which is H (plus zero energy state), it's represented as H_p0221 # multiply number density by path length (dl) to get column density of ions (units cm^-2)222 socketio.sleep(0)223 dx = 4224 # n_density in units of particles/cm^3225 226 dl_cm = ray.r[('gas', 'dl')].to('cm')227 ## FULL RANGE VALUES228 ## used for graphing229 socketio.sleep(0)230 try:231 H_I = (ray.r[('gas', 'H_p0_number_density')] * dl_cm).tolist()232 i_H_I = interpol8(l,ray.r[('gas', 'H_p0_number_density')] * dl_cm,dx)[1]233 except Exception as e:234 H_I = [] 235 i_H_I = []236 print('error: '+ str( e ))237 exc_type, exc_obj, exc_tb = sys.exc_info()238 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]239 print(exc_type, fname, exc_tb.tb_lineno) 240 241 socketio.sleep(0)242 try:243 H_II = (ray.r[('gas', 'H_p1_number_density')] * dl_cm).tolist()244 i_H_II = interpol8(l,ray.r[('gas', 'H_p1_number_density')] * dl_cm,dx)[1]245 except Exception as e:246 H_II = []247 i_H_II= []248 print('error: '+ str( e ))249 exc_type, exc_obj, exc_tb = sys.exc_info()250 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]251 print(exc_type, fname, exc_tb.tb_lineno) 252 socketio.sleep(0)253 try:254 C_I = (ray.r[('gas', 'C_p0_number_density')] * dl_cm).tolist()255 i_C_I = interpol8(l,ray.r[('gas', 'C_p0_number_density')] * dl_cm,dx)[1]256 except Exception as e:257 C_I = []258 i_C_I = []259 print('error: '+ str( e ))260 exc_type, exc_obj, exc_tb = sys.exc_info()261 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]262 print(exc_type, fname, exc_tb.tb_lineno) 263 socketio.sleep(0)264 try:265 C_II = (ray.r[('gas', 'C_p1_number_density')] * dl_cm).tolist()266 i_C_II = interpol8(l,ray.r[('gas', 'C_p1_number_density')] * dl_cm,dx)[1]267 except Exception as e:268 C_II = []269 i_C_II= []270 print('error: '+ str( e ))271 exc_type, exc_obj, exc_tb = sys.exc_info()272 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]273 print(exc_type, fname, exc_tb.tb_lineno) 274 socketio.sleep(0)275 try:276 C_III = (ray.r[('gas', 'C_p2_number_density')] * dl_cm).tolist()277 i_C_III = interpol8(l,ray.r[('gas', 'C_p2_number_density')] * dl_cm,dx)[1]278 except Exception as e:279 C_III = []280 i_C_III= []281 print('error: '+ str( e ))282 exc_type, exc_obj, exc_tb = sys.exc_info()283 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]284 print(exc_type, fname, exc_tb.tb_lineno) 285 socketio.sleep(0)286 try:287 C_IV = (ray.r[('gas', 'C_p3_number_density')] * dl_cm).tolist()288 i_C_IV = interpol8(l,ray.r[('gas', 'C_p3_number_density')] * dl_cm,dx)[1]289 except Exception as e:290 C_IV = []291 i_C_IV= []292 print('error: '+ str( e ))293 exc_type, exc_obj, exc_tb = sys.exc_info()294 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]295 print(exc_type, fname, exc_tb.tb_lineno) 296 socketio.sleep(0)297 try:298 C_V = (ray.r[('gas', 'C_p4_number_density')] * dl_cm).tolist()299 i_C_V = interpol8(l,ray.r[('gas', 'C_p4_number_density')] * dl_cm,dx)[1]300 except Exception as e:301 C_V = []302 i_C_V = []303 print('error: '+ str( e ))304 exc_type, exc_obj, exc_tb = sys.exc_info()305 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]306 print(exc_type, fname, exc_tb.tb_lineno) 307 socketio.sleep(0)308 try:309 C_VI = (ray.r[('gas', 'C_p5_number_density')] * dl_cm).tolist()310 i_C_VI = interpol8(l,ray.r[('gas', 'C_p5_number_density')] * dl_cm,dx)[1]311 except Exception as e:312 C_VI = []313 i_C_VI = []314 print('error: '+ str( e ))315 exc_type, exc_obj, exc_tb = sys.exc_info()316 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]317 print(exc_type, fname, exc_tb.tb_lineno) 318 socketio.sleep(0)319 try:320 He_I = (ray.r[('gas', 'He_p0_number_density')] * dl_cm).tolist()321 i_He_I = interpol8(l,ray.r[('gas', 'He_p0_number_density')] * dl_cm,dx)[1]322 except Exception as e:323 He_I = []324 i_He_I = []325 print('error: '+ str( e ))326 exc_type, exc_obj, exc_tb = sys.exc_info()327 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]328 print(exc_type, fname, exc_tb.tb_lineno) 329 socketio.sleep(0)330 try:331 He_II = (ray.r[('gas', 'He_p1_number_density')] * dl_cm).tolist()332 i_He_II = interpol8(l,ray.r[('gas', 'He_p1_number_density')] * dl_cm,dx)[1]333 except Exception as e:334 He_II = []335 i_He_II= []336 print('error: '+ str( e ))337 exc_type, exc_obj, exc_tb = sys.exc_info()338 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]339 print(exc_type, fname, exc_tb.tb_lineno) 340 socketio.sleep(0)341 try:342 He_III = (ray.r[('gas', 'He_p2_number_density')] * dl_cm).tolist()343 i_He_III= interpol8(l,ray.r[('gas', 'He_p2_number_density')] * dl_cm,dx)[1]344 except Exception as e:345 He_III = []346 i_He_III= []347 print('error: '+ str( e ))348 exc_type, exc_obj, exc_tb = sys.exc_info()349 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]350 print(exc_type, fname, exc_tb.tb_lineno) 351 socketio.sleep(0)352 try:353 Mg_I = (ray.r[('gas', 'Mg_p0_number_density')] * dl_cm).tolist()354 i_Mg_I = interpol8(l,ray.r[('gas', 'Mg_p0_number_density')] * dl_cm,dx)[1]355 except Exception as e:356 Mg_I = []357 i_Mg_I= []358 print('error: '+ str( e ))359 exc_type, exc_obj, exc_tb = sys.exc_info()360 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]361 print(exc_type, fname, exc_tb.tb_lineno) 362 socketio.sleep(0)363 try:364 Mg_II = (ray.r[('gas', 'Mg_p1_number_density')] * dl_cm).tolist()365 i_Mg_II = interpol8(l,ray.r[('gas', 'Mg_p1_number_density')] * dl_cm,dx)[1]366 except Exception as e:367 Mg_II = []368 i_Mg_II= []369 print('error: '+ str( e ))370 exc_type, exc_obj, exc_tb = sys.exc_info()371 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]372 print(exc_type, fname, exc_tb.tb_lineno) 373 socketio.sleep(0)374 try:375 Mg_X = (ray.r[('gas', 'Mg_p9_number_density')] * dl_cm).tolist()376 i_Mg_X = interpol8(l,ray.r[('gas', 'Mg_p9_number_density')] * dl_cm,dx)[1]377 except Exception as e:378 Mg_X = []379 i_Mg_X= []380 print('error: '+ str( e ))381 exc_type, exc_obj, exc_tb = sys.exc_info()382 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]383 print(exc_type, fname, exc_tb.tb_lineno) 384 socketio.sleep(0)385 try:386 N_II = (ray.r[('gas', 'N_p1_number_density')] * dl_cm).tolist()387 i_N_II = interpol8(l,ray.r[('gas', 'N_p1_number_density')] * dl_cm,dx)[1]388 389 except Exception as e:390 N_II = []391 i_N_II= []392 print('error: '+ str( e ))393 exc_type, exc_obj, exc_tb = sys.exc_info()394 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]395 print(exc_type, fname, exc_tb.tb_lineno) 396 socketio.sleep(0)397 try:398 N_III = (ray.r[('gas', 'N_p2_number_density')] * dl_cm).tolist()399 i_N_III = interpol8(l,ray.r[('gas', 'N_p2_number_density')] * dl_cm,dx)[1]400 except Exception as e:401 N_III = []402 i_N_III= []403 print('error: '+ str( e ))404 exc_type, exc_obj, exc_tb = sys.exc_info()405 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]406 print(exc_type, fname, exc_tb.tb_lineno) 407 socketio.sleep(0)408 try:409 N_IV = (ray.r[('gas', 'N_p3_number_density')] * dl_cm).tolist()410 i_N_IV = interpol8(l,ray.r[('gas', 'N_p3_number_density')] * dl_cm,dx)[1]411 except Exception as e:412 N_IV = []413 i_N_IV= []414 print('error: '+ str( e ))415 exc_type, exc_obj, exc_tb = sys.exc_info()416 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]417 print(exc_type, fname, exc_tb.tb_lineno) 418 socketio.sleep(0)419 try:420 N_V = (ray.r[('gas', 'N_p4_number_density')] * dl_cm).tolist()421 i_N_V = interpol8(l,ray.r[('gas', 'N_p4_number_density')] * dl_cm,dx)[1]422 except Exception as e:423 N_V = []424 i_N_V = []425 print('error: '+ str( e ))426 exc_type, exc_obj, exc_tb = sys.exc_info()427 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]428 print(exc_type, fname, exc_tb.tb_lineno) 429 socketio.sleep(0)430 try:431 N_VI = (ray.r[('gas', 'N_p5_number_density')] * dl_cm).tolist()432 i_N_VI = interpol8(l,ray.r[('gas', 'N_p5_number_density')] * dl_cm,dx)[1]433 except Exception as e:434 N_VI = []435 i_N_VI = []436 print('error: '+ str( e ))437 exc_type, exc_obj, exc_tb = sys.exc_info()438 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]439 print(exc_type, fname, exc_tb.tb_lineno) 440 socketio.sleep(0)441 try:442 N_VII = (ray.r[('gas', 'N_p6_number_density')] * dl_cm).tolist()443 i_N_VII = interpol8(l,ray.r[('gas', 'N_p6_number_density')] * dl_cm,dx)[1]444 except Exception as e:445 N_VII = []446 i_N_VII= []447 print('error: '+ str( e ))448 exc_type, exc_obj, exc_tb = sys.exc_info()449 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]450 print(exc_type, fname, exc_tb.tb_lineno) 451 socketio.sleep(0)452 try:453 Na_I = (ray.r[('gas', 'Na_p0_number_density')] * dl_cm).tolist()454 i_Na_I = interpol8(l,ray.r[('gas', 'Na_p0_number_density')] * dl_cm,dx)[1]455 except Exception as e:456 Na_I = []457 i_Na_I= []458 print('error: '+ str( e ))459 exc_type, exc_obj, exc_tb = sys.exc_info()460 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]461 print(exc_type, fname, exc_tb.tb_lineno) 462 socketio.sleep(0)463 try:464 Na_IX = (ray.r[('gas', 'Na_p8_number_density')] * dl_cm).tolist()465 i_Na_IX = interpol8(l,ray.r[('gas', 'Na_p8_number_density')] * dl_cm,dx)[1]466 except Exception as e:467 Na_IX = []468 i_Na_IX= []469 print('error: '+ str( e ))470 exc_type, exc_obj, exc_tb = sys.exc_info()471 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]472 print(exc_type, fname, exc_tb.tb_lineno) 473 socketio.sleep(0)474 try:475 Ne_III = (ray.r[('gas', 'Ne_p2_number_density')] * dl_cm).tolist()476 i_Ne_III= interpol8(l,ray.r[('gas', 'Ne_p2_number_density')] * dl_cm,dx)[1]477 except Exception as e:478 Ne_III = []479 i_Ne_III= []480 print('error: '+ str( e ))481 exc_type, exc_obj, exc_tb = sys.exc_info()482 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]483 print(exc_type, fname, exc_tb.tb_lineno) 484 socketio.sleep(0)485 try:486 Ne_IV = (ray.r[('gas', 'Ne_p3_number_density')] * dl_cm).tolist()487 i_Ne_IV = interpol8(l,ray.r[('gas', 'Ne_p3_number_density')] * dl_cm,dx)[1]488 except Exception as e:489 Ne_IV = []490 i_Ne_IV= []491 print('error: '+ str( e ))492 exc_type, exc_obj, exc_tb = sys.exc_info()493 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]494 print(exc_type, fname, exc_tb.tb_lineno) 495 socketio.sleep(0)496 try:497 Ne_V = (ray.r[('gas', 'Ne_p4_number_density')] * dl_cm).tolist()498 i_Ne_V = interpol8(l,ray.r[('gas', 'Ne_p4_number_density')] * dl_cm,dx)[1]499 except Exception as e:500 Ne_V = []501 i_Ne_V= []502 print('error: '+ str( e ))503 exc_type, exc_obj, exc_tb = sys.exc_info()504 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]505 print(exc_type, fname, exc_tb.tb_lineno) 506 socketio.sleep(0)507 try:508 Ne_VI = (ray.r[('gas', 'Ne_p5_number_density')] * dl_cm).tolist()509 i_Ne_VI = interpol8(l,ray.r[('gas', 'Ne_p5_number_density')] * dl_cm,dx)[1]510 except Exception as e:511 Ne_VI = []512 i_Ne_VI= []513 print('error: '+ str( e ))514 exc_type, exc_obj, exc_tb = sys.exc_info()515 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]516 print(exc_type, fname, exc_tb.tb_lineno) 517 socketio.sleep(0)518 try:519 Ne_VIII= (ray.r[('gas', 'Ne_p7_number_density')] * dl_cm).tolist()520 i_Ne_VIII=interpol8(l,ray.r[('gas', 'Ne_p7_number_density')] * dl_cm,dx)[1]521 except Exception as e:522 Ne_VIII = []523 i_Ne_VIII= []524 print('error: '+ str( e ))525 exc_type, exc_obj, exc_tb = sys.exc_info()526 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]527 print(exc_type, fname, exc_tb.tb_lineno) 528 socketio.sleep(0)529 try:530 O_I = (ray.r[('gas', 'O_p0_number_density')] * dl_cm).tolist()531 i_O_I = interpol8(l,ray.r[('gas', 'O_p0_number_density')] * dl_cm,dx)[1]532 except Exception as e:533 O_I = []534 i_O_I = []535 print('error: '+ str( e ))536 exc_type, exc_obj, exc_tb = sys.exc_info()537 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]538 print(exc_type, fname, exc_tb.tb_lineno) 539 socketio.sleep(0)540 try:541 O_II = (ray.r[('gas', 'O_p1_number_density')] * dl_cm).tolist()542 i_O_II = interpol8(l,ray.r[('gas', 'O_p1_number_density')] * dl_cm,dx)[1]543 except Exception as e:544 O_II = []545 i_O_II= []546 print('error: '+ str( e ))547 exc_type, exc_obj, exc_tb = sys.exc_info()548 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]549 print(exc_type, fname, exc_tb.tb_lineno) 550 socketio.sleep(0)551 try:552 O_III = (ray.r[('gas', 'O_p2_number_density')] * dl_cm).tolist()553 i_O_III = interpol8(l,ray.r[('gas', 'O_p2_number_density')] * dl_cm,dx)[1]554 except Exception as e:555 O_III = []556 i_O_III= []557 print('error: '+ str( e ))558 exc_type, exc_obj, exc_tb = sys.exc_info()559 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]560 print(exc_type, fname, exc_tb.tb_lineno) 561 socketio.sleep(0)562 try:563 O_IV = (ray.r[('gas', 'O_p3_number_density')] * dl_cm).tolist()564 i_O_IV = interpol8(l,ray.r[('gas', 'O_p3_number_density')] * dl_cm,dx)[1]565 except Exception as e:566 O_IV = []567 i_O_IV= []568 print('error: '+ str( e ))569 exc_type, exc_obj, exc_tb = sys.exc_info()570 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]571 print(exc_type, fname, exc_tb.tb_lineno) 572 socketio.sleep(0)573 try:574 O_V = (ray.r[('gas', 'O_p4_number_density')] * dl_cm).tolist()575 i_O_V = interpol8(l,ray.r[('gas', 'O_p4_number_density')] * dl_cm,dx)[1]576 except Exception as e:577 O_V = []578 i_O_V= []579 print('error: '+ str( e ))580 exc_type, exc_obj, exc_tb = sys.exc_info()581 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]582 print(exc_type, fname, exc_tb.tb_lineno) 583 socketio.sleep(0)584 try:585 O_VI = (ray.r[('gas', 'O_p5_number_density')] * dl_cm).tolist()586 i_O_VI = interpol8(l,ray.r[('gas', 'O_p5_number_density')] * dl_cm,dx)[1]587 except Exception as e:588 O_VI = []589 i_O_VI= []590 print('error: '+ str( e ))591 exc_type, exc_obj, exc_tb = sys.exc_info()592 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]593 print(exc_type, fname, exc_tb.tb_lineno) 594 socketio.sleep(0)595 try:596 O_VII = (ray.r[('gas', 'O_p6_number_density')] * dl_cm).tolist()597 i_O_VII = interpol8(l,ray.r[('gas', 'O_p6_number_density')] * dl_cm,dx)[1]598 except Exception as e:599 O_VII = []600 i_O_VII= []601 print('error: '+ str( e ))602 exc_type, exc_obj, exc_tb = sys.exc_info()603 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]604 print(exc_type, fname, exc_tb.tb_lineno) 605 socketio.sleep(0)606 try:607 O_VIII = (ray.r[('gas', 'O_p7_number_density')] * dl_cm).tolist()608 i_O_VIII= interpol8(l,ray.r[('gas', 'O_p7_number_density')] * dl_cm,dx)[1]609 except Exception as e:610 O_VIII = []611 i_O_VIII= []612 print('error: '+ str( e ))613 exc_type, exc_obj, exc_tb = sys.exc_info()614 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]615 print(exc_type, fname, exc_tb.tb_lineno) 616 socketio.sleep(0)617 try:618 S_II = (ray.r[('gas', 'S_p1_number_density')] * dl_cm).tolist()619 i_S_II = interpol8(l,ray.r[('gas', 'S_p1_number_density')] * dl_cm,dx)[1]620 except Exception as e:621 S_II = []622 i_S_II= []623 print('error: '+ str( e ))624 exc_type, exc_obj, exc_tb = sys.exc_info()625 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]626 print(exc_type, fname, exc_tb.tb_lineno) 627 socketio.sleep(0)628 try:629 S_III = (ray.r[('gas', 'S_p2_number_density')] * dl_cm).tolist()630 i_S_III = interpol8(l,ray.r[('gas', 'S_p2_number_density')] * dl_cm,dx)[1]631 except Exception as e:632 S_III = []633 i_S_III= []634 print('error: '+ str( e ))635 exc_type, exc_obj, exc_tb = sys.exc_info()636 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]637 print(exc_type, fname, exc_tb.tb_lineno) 638 socketio.sleep(0)639 try:640 S_IV = (ray.r[('gas', 'S_p3_number_density')] * dl_cm).tolist()641 i_S_IV = interpol8(l,ray.r[('gas', 'S_p3_number_density')] * dl_cm,dx)[1]642 except Exception as e:643 S_IV = []644 i_S_IV= []645 print('error: '+ str( e ))646 exc_type, exc_obj, exc_tb = sys.exc_info()647 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]648 print(exc_type, fname, exc_tb.tb_lineno) 649 socketio.sleep(0)650 try:651 S_V = (ray.r[('gas', 'S_p4_number_density')] * dl_cm).tolist()652 i_S_V = interpol8(l,ray.r[('gas', 'S_p4_number_density')] * dl_cm,dx)[1]653 except Exception as e:654 S_V = []655 i_S_V= []656 print('error: '+ str( e ))657 exc_type, exc_obj, exc_tb = sys.exc_info()658 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]659 print(exc_type, fname, exc_tb.tb_lineno) 660 socketio.sleep(0)661 try:662 S_VI = (ray.r[('gas', 'S_p5_number_density')] * dl_cm).tolist()663 i_S_VI = interpol8(l,ray.r[('gas', 'S_p5_number_density')] * dl_cm,dx)[1]664 except Exception as e:665 S_VI = []666 i_S_VI= []667 print('error: '+ str( e ))668 exc_type, exc_obj, exc_tb = sys.exc_info()669 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]670 print(exc_type, fname, exc_tb.tb_lineno) 671 socketio.sleep(0)672 try:673 Si_II = (ray.r[('gas', 'Si_p1_number_density')] * dl_cm).tolist()674 i_Si_II = interpol8(l,ray.r[('gas', 'Si_p1_number_density')] * dl_cm,dx)[1]675 except Exception as e:676 Si_II = []677 i_Si_II= []678 print('error: '+ str( e ))679 exc_type, exc_obj, exc_tb = sys.exc_info()680 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]681 print(exc_type, fname, exc_tb.tb_lineno) 682 socketio.sleep(0)683 try:684 Si_III = (ray.r[('gas', 'Si_p2_number_density')] * dl_cm).tolist()685 i_Si_III= interpol8(l,ray.r[('gas', 'Si_p2_number_density')] * dl_cm,dx)[1]686 except Exception as e:687 Si_III = []688 i_Si_III= []689 print('error: '+ str( e ))690 exc_type, exc_obj, exc_tb = sys.exc_info()691 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]692 print(exc_type, fname, exc_tb.tb_lineno) 693 socketio.sleep(0)694 try:695 Si_IV = (ray.r[('gas', 'Si_p3_number_density')] * dl_cm).tolist()696 i_Si_IV = interpol8(l,ray.r[('gas', 'Si_p3_number_density')] * dl_cm,dx)[1]697 except Exception as e:698 Si_IV = []699 i_Si_IV= []700 print('error: '+ str( e ))701 exc_type, exc_obj, exc_tb = sys.exc_info()702 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]703 print(exc_type, fname, exc_tb.tb_lineno) 704 socketio.sleep(0)705 try:706 Si_XII = (ray.r[('gas', 'Si_p11_number_density')] * dl_cm).tolist()707 i_Si_XII= interpol8(l,ray.r[('gas', 'Si_p11_number_density')] *dl_cm,dx)[1]708 except Exception as e:709 Si_XII = []710 i_Si_XII = []711 print('error: '+ str( e ))712 exc_type, exc_obj, exc_tb = sys.exc_info()713 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]714 print(exc_type, fname, exc_tb.tb_lineno) 715 716 finally:717 socketio.sleep(0)718 log_density = np.log10(ray.r[('gas', 'density')])+23.77+0.21 # dividing by mean molecular mass, mass of proton719 density = (10**log_density).tolist() # divide by mean molecular mass... somewhere between ~(10^-6, 1)720 try:721 if simID[:3] == 'Ref':722 entropy = (ray.r[('gas', 'Entropy')]).tolist()723 i_entropy = interpol8(l,ray.r[('gas', 'Entropy')],dx)[1]724 elif simID[:3] == 'TNG':725 entropy = (ray.r[('gas', 'entropy')]).tolist()726 i_entropy = interpol8(l,ray.r[('gas', 'entropy')],dx)[1]727 except Exception as e:728 entropy = []729 i_entropy = []730 print('error: '+ str( e ))731 exc_type, exc_obj, exc_tb = sys.exc_info()732 fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]733 print(exc_type, fname, exc_tb.tb_lineno) 734 socketio.sleep(0)735 metallicity = (ray.r[('gas', 'metallicity')].to('Zsun')).tolist()736 temperature = (ray.r[('gas', 'temperature')]).tolist()737 log_density = np.log10(ray.r[('gas', 'density')])+23.77+0.21 # dividing by mean molecular mass, mass of proton738 i_density = (10**np.array(interpol8(l, log_density,dx)[1])).tolist() # divide by mean molecular mass... somewhere between ~(10^-6, 1)739 i_metallicity = interpol8(l,ray.r[('gas', 'metallicity')].to('Zsun'),dx)[1]740 i_l, i_temperature = interpol8(l,ray.r[('gas', 'temperature')],dx)741 socketio.sleep(0)742 socketio.emit('simple_line_data',{ 'index': idx,743 'start': start,744 'end': end,745 'l': l.tolist(),746 'N(H I)': H_I,747 'N(H II)': H_II,748 'N(C I)': C_I,749 'N(C II)': C_II,750 'N(C III)': C_III,751 'N(C IV)': C_IV,752 'N(C V)': C_V,753 'N(C VI)': C_VI,754 'N(He I)': He_I,755 'N(He II)': He_II,756 'N(He III)': He_III,757 'N(Mg I)': Mg_I,758 'N(Mg II)': Mg_II,759 'N(Mg X)': Mg_X,760 'N(N II)': N_II,761 'N(N III)': N_III,762 'N(N IV)': N_IV,763 'N(N V)': N_V,764 'N(N VI)': N_VI,765 'N(N VII)': N_VII,766 'N(Na I)': Na_I,767 'N(Na IX)': Na_IX,768 'N(Ne III)': Ne_III,769 'N(Ne IV)': Ne_IV,770 'N(Ne V)': Ne_V,771 'N(Ne VI)': Ne_VI,772 'N(Ne VIII)': Ne_VIII,773 'N(O I)': O_I,774 'N(O II)': O_II,775 'N(O III)': O_III,776 'N(O IV)': O_IV,777 'N(O V)': O_V,778 'N(O VI)': O_VI,779 'N(O VII)': O_VII,780 'N(O VIII)': O_VIII,781 'N(S II)': S_II,782 'N(S III)': S_III,783 'N(S IV)': S_IV,784 'N(S V)': S_V,785 'N(S VI)': S_VI,786 'N(Si II)': Si_II,787 'N(Si III)': Si_III,788 'N(Si IV)': Si_IV,789 'N(Si XII)': Si_XII,790 'density': density,791 'entropy': entropy,792 'metallicity': metallicity,793 'temperature': temperature,794 'i_l': i_l,795 'i_N(H I)': i_H_I,796 'i_N(H II)': i_H_II,797 'i_N(C I)': i_C_I,798 'i_N(C II)': i_C_II,799 'i_N(C III)': i_C_III,800 'i_N(C IV)': i_C_IV,801 'i_N(C V)': i_C_V,802 'i_N(C VI)': i_C_VI,803 'i_N(He I)': i_He_I,804 'i_N(He II)': i_He_II,805 'i_N(He III)': i_He_III,806 'i_N(Mg I)': i_Mg_I,807 'i_N(Mg II)': i_Mg_II,808 'i_N(Mg X)': i_Mg_X,809 'i_N(N II)': i_N_II,810 'i_N(N III)': i_N_III,811 'i_N(N IV)': i_N_IV,812 'i_N(N V)': i_N_V,813 'i_N(N VI)': i_N_VI,814 'i_N(N VII)': i_N_VII,815 'i_N(Na I)': i_Na_I,816 'i_N(Na IX)': i_Na_IX,817 'i_N(Ne III)': i_Ne_III,818 'i_N(Ne IV)': i_Ne_IV,819 'i_N(Ne V)': i_Ne_V,820 'i_N(Ne VI)': i_Ne_VI,821 'i_N(Ne VIII)': i_Ne_VIII,822 'i_N(O I)': i_O_I,823 'i_N(O II)': i_O_II,824 'i_N(O III)': i_O_III,825 'i_N(O IV)': i_O_IV,826 'i_N(O V)': i_O_V,827 'i_N(O VI)': i_O_VI,828 'i_N(O VII)': i_O_VII,829 'i_N(O VIII)': i_O_VIII,830 'i_N(S II)': i_S_II,831 'i_N(S III)': i_S_III,832 'i_N(S IV)': i_S_IV,833 'i_N(S V)': i_S_V,834 'i_N(S VI)': i_S_VI,835 'i_N(Si II)': i_Si_II,836 'i_N(Si III)': i_Si_III,837 'i_N(Si IV)': i_Si_IV,838 'i_N(Si XII)': i_Si_XII,839 'i_density': i_density,840 'i_entropy': i_entropy,841 'i_metallicity': i_metallicity,842 'i_temperature': i_temperature843 }, namespace='/test')844 socketio.sleep(0)845 print('sent simple ray data')846# selectRay is called from index.html when 847@socketio.on('selectRay', namespace='/test')848def handle_ray_selection(simID,idx, start, end):849 try:850 if path.exists('static/data/skewers/'+simID+'_'+str(idx)+'_'+str(start)+'_'+str(end)+'.json'):851 socketio.emit( 'synthetic_spectrum_saved', {'index': 'static/data/skewers/'+simID+'_'+str(idx)+'_'+str(start)+'_'+str(end)+'.json'}, namespace = '/test' )852 else:853 socketio.start_background_task(handle_ray_selection_background,simID,idx, start, end)854 except:855 socketio.emit( 'spectrumError', {'index': idx}, namespace = '/test' )856 857def handle_ray_selection_background(simID,idx,start,end):858 # socketio = SocketIO(message_queue='amqp://')859 socketio.emit( 'processingRay', {'index': idx}, namespace = '/test' )860 eventlet.sleep()861 socketio.sleep(0)862 # fn = ''863 # if 'RefL' in simID:864 # fn = 'static/data/'+simID+'/snapshot_028_z000p000/snap_028_z000p000.0.hdf5'865 # if 'TNG' in simID:866 # fn = 'static/data/'+simID+'/snapshot/snap_030.0.hdf5'867 # # ds = yt.load(fn)868 # socketio.sleep(0)869 # # ad = ds.all_data()870 fn = ''871 # ds = []872 873 if simID == 'RefL0012N0188':874 ds = EAGLE_12Mpc875 elif simID == 'RefL0025N0376':876 ds = EAGLE_25Mpc877 elif simID == 'RefL0100N1504':878 ds = EAGLE_100Mpc879 elif simID == 'TNG100_z2.3':880 ds = TNG100_snap030881 elif simID == 'TNG100_z0.0':882 ds = TNG100_snap099883 else:884 # if 'RefL' in simID:885 # fn = 'static/data/'+simID+'/snapshot_028_z000p000/snap_028_z000p000.0.hdf5'886 # if 'TNG' in simID:887 # fn = 'static/data/'+simID+'/snapshot/snap_030.0.hdf5'888 # print("loading" + str(fn))889 print(fn)890 ds = yt.load(fn)891 ad = ds.all_data()892 print(simID)893 print('received args: ' + str(start) + str(end))894 ray_start = np.float_(start)895 ray_end = np.float_(end)896 sim_width = np.float(ds.domain_right_edge[0].in_units('Mpc') - ds.domain_left_edge[0].in_units('Mpc'))897 # ray_start = ds.arr(ray_start, 'Mpc') #list(np.float_(start))898 # ray_end = ds.arr(ray_end, 'Mpc')899 ray_start = ds.arr(ray_start/sim_width * np.float(ds.domain_right_edge[0]), 'code_length') #list(np.float_(start))900 ray_end = ds.arr(ray_end/sim_width * np.float(ds.domain_right_edge[0]), 'code_length') #list(np.float_(end))901 print(ray_start.in_units('Mpc'))902 print(ray_end.in_units('Mpc'))903 line_list = ['H', 'C', 'N', 'O', 'Mg']904 instrument = 'COS'905 grating = 'G130M'906 inst = instrument + '-' + grating907 lambda_min = float(trident.valid_instruments[inst].lambda_min)908 lambda_max = float(trident.valid_instruments[inst].lambda_max)909 path = trident_path() + '/data/line_lists/lines.txt'910 path2 = trident_path() + '/data/line_lists/lines_subset_test.txt'911 912 lines_data = open(path,"r")913 open(path2, 'w').close()914 lines_subset_test = open(path2,"a")915 socketio.sleep(0)916 c = 0917 lines = {} # keeps track of lines and whether or not they are added to the subset918 for line in lines_data:919 if c == 0:920 lines_subset_test.write(line)921 c+=1922 continue923 field = line.split()924 # print(field)925 wavelength = float(field[2])926 element = field[0] + field[1] + truncate(wavelength,0)927 element = element[:-1]928 if (wavelength >= lambda_min) and (wavelength <= lambda_max):929 lines_subset_test.write(line)930 lines[element] = True931 else:932 lines[element] = False933 continue934 # import pdb; pdb.set_trace()935 936 lines_data.close()937 lines_subset_test.close()938 print("making simple ray")939 socketio.sleep(0)940 ray = trident.make_simple_ray(ds, start_position=ray_start,941 end_position=ray_end,942 data_filename="ray.h5", # update file name if we need multiple943 lines=line_list)944 socketio.sleep(0)945 sg = trident.SpectrumGenerator(inst ,line_database="lines_subset_test.txt")#, dlambda=1)946 # sg = trident.SpectrumGenerator('COS-G130M',line_database="lines.txt")947 948 # emit('my_response', {'data': 'Connected', 'count': -1})949 socketio.sleep(0)950 print('making spectrum')951 sg.make_spectrum(ray, lines=line_list) #spitting information through std_out, scrape information952 socketio.sleep(0)953 # sg.add_qso_spectrum(emitting_redshift=0.5)954 955 MW = False956 if MW == True:957 sg.add_milky_way_foreground() #fine with COS G130M958 socketio.sleep(0)959 sg.apply_lsf() #instrumental profile960 socketio.sleep(0)961 noise = True962 SNR = 50963 if noise == True:964 sg.add_gaussian_noise(SNR)965 socketio.sleep(0)966 error = sg.error_func(sg.flux_field)967 968 socketio.sleep(0)969 sgs.append([start,end,sg,idx])970 socketio.sleep(0)971 socketio.emit( 'sentRay', {'index': idx}, namespace = '/test' )972 socketio.sleep(0)973 print('emitting spectrum')974 socketio.sleep(0)975 socketio.emit('synthetic_spectrum',{'index':idx,'start':start,'end':end,'lambda':sgs[-1][2].lambda_field.tolist(),'flux':sgs[-1][2].flux_field.tolist()}, namespace='/test')976 socketio.sleep(0)977 print('sent spectrum')978 # sg.save_spectrum('spec_raw.txt')979 # sg.plot_spectrum('spec_raw.png')980 # print(jsonify(sg.lambda_field.tolist()))981 # print(sg.flux_field.tolist())982 tab = Table((sgs[-1][2].lambda_field.tolist(), sgs[-1][2].flux_field.tolist(), error.tolist() ), names=["lambda","flux","error"])983 # tab = Table((sgs[-1][2].lambda_field.tolist(), sgs[-1][2].flux_field.tolist() ), names=["lambda","flux"])984 hdul = fits.table_to_hdu(tab)985 hdr = hdul.header986 # Software987 hdr['COSMOVIS'] = 0.1 # Cosmovis version988 hdr['TRIDENT'] = trident.__version__ # Trident version989 hdr['YT'] = yt.__version__ # Yt version990 # Simulation991 hdr['SIM'] = 'EAGLE' # Simulation (EAGLE, FIRE, etc.)992 hdr['SIM_VERS'] = 'RefL0012N0188' # Sim version993 hdr['BOX_SIZE'] = ds.parameters['BoxSize'] # Box size994 hdr['Z'] = ds.parameters['Redshift'] # Redshift995 # Spectrum996 hdr['X1Y1Z1'] = str(start[0]) + ', ' + str(start[1]) + ', ' + str(start[2]) 997 # X1, Y1, Z1998 hdr['X2Y2Z2'] = str(end[0]) + ', ' + str(end[1]) + ', ' + str(end[2]) 999 # X2, Y2, Z2 1000 hdr['MW_FRGND'] = MW # MWforeground (True, False)1001 hdr['NOISEADD'] = noise # NoiseAdded (True, False)1002 hdr['NOISETYP'] = 'Gaussian' # NoiseType (Gaussian, custom)1003 hdr['SNR'] = SNR # SNR1004 hdr['SEED'] = False # NoiseSeed1005 hdr['INSTRMNT'] = instrument # Instrument1006 hdr['GRATING'] = grating # Grating1007 hdr['ROTATION'] = 'null' # Grating rotation1008 hdr['RESOLUTN'] = float(sg.dlambda*6) # Spectral resolution (Angstroms)1009 # Lines added (each boolean)1010 for line in lines:1011 # print(lines[line])1012 hdr[line] = lines[line]1013 # HI12151014 # HI10251015 # HI9721016 # OVI10311017 # OVI10371018 spectrum_hdul.append(hdul)1019 spectrum_hdul.writeto('static/data/spectra.fits', overwrite=True)1020 1021 socketio.sleep(0)1022 spec = {'index':idx,'start':start,'end':end,'lambda':sgs[-1][2].lambda_field.tolist(),'flux':sgs[-1][2].flux_field.tolist()}1023 socketio.sleep()1024 with open('static/data/skewers/'+simID+'_'+str(idx)+'_'+str(start)+'_'+str(end)+'.json', 'w') as file:1025 json.dump(spec, file)1026 socketio.sleep(0)1027 socketio.emit( 'synthetic_spectrum_saved', {'index': 'static/data/skewers/'+simID+'_'+str(idx)+'_'+str(start)+'_'+str(end)+'.json'}, namespace = '/test' )1028 socketio.sleep(0)1029 print("spectrum saved")1030 1031@socketio.on('simIDtoServer', namespace='/test')1032def updateSimID(simID):1033 1034 global fn1035 global ds1036 global fl 1037 global ad1038 global right_edge 1039 global left_edge1040 if simID == 'RefL0012N0188':1041 if fn != 'static/data/RefL0012N0188/snapshot_028_z000p000/snap_028_z000p000.0.hdf5':1042 fn = 'static/data/RefL0012N0188/snapshot_028_z000p000/snap_028_z000p000.0.hdf5'1043 # ds = yt.load(fn)1044 fl = sorted(ds.field_list) # contains list of fields in the dataset1045 ad = ds.all_data()1046 right_edge = [float(ad.right_edge[0]),float(ad.right_edge[1]),float(ad.right_edge[2])]1047 left_edge = [float(ad.left_edge[0]),float(ad.left_edge[1]),float(ad.left_edge[2])]1048 emit('field_list', {'data': fl},namespace='/test')1049 emit('domain_edges', {'left_edge': left_edge, 'right_edge': right_edge},namespace='/test')1050 elif simID == 'RefL0025N0376':1051 if fn != 'static/data/RefL0025N0376/snapshot_028_z000p000/snap_028_z000p000.0.hdf5':1052 fn = 'static/data/RefL0025N0376/snapshot_028_z000p000/snap_028_z000p000.0.hdf5'1053 # ds = yt.load(fn)1054 fl = sorted(ds.field_list) # contains list of fields in the dataset1055 ad = ds.all_data()1056 right_edge = [float(ad.right_edge[0]),float(ad.right_edge[1]),float(ad.right_edge[2])]1057 left_edge = [float(ad.left_edge[0]),float(ad.left_edge[1]),float(ad.left_edge[2])]1058 emit('field_list', {'data': fl},namespace='/test')1059 emit('domain_edges', {'left_edge': left_edge, 'right_edge': right_edge},namespace='/test')1060 with open('static/data/'+simID+'/simMetadata.json', 'w') as file:1061 simMetadata = {1062 "left_edge": left_edge,1063 "right_edge": right_edge,1064 "field_list": fl1065 }1066 json.dump(simMetadata, file)1067@socketio.on('connect', namespace='/test')1068def test_connect():1069 print("connected")1070 emit('my_response', {'data': 'Connected', 'count': 0})1071if __name__ == '__main__':1072 # socketio.run(app, debug=False)1073 # socketio.run(app, host='0.0.0.0', debug=True)...

Full Screen

Full Screen

__init__.py

Source:__init__.py Github

copy

Full Screen

1import re2import sys3from types import CodeType4from types import FrameType5from types import TracebackType6try:7 from __pypy__ import tproxy8except ImportError:9 tproxy = None10try:11 from .cpython import tb_set_next12except ImportError:13 tb_set_next = None14if not tb_set_next and not tproxy:15 raise ImportError("Cannot use tblib. Runtime not supported.")16__version__ = '1.7.0'17__all__ = 'Traceback', 'TracebackParseError', 'Frame', 'Code'18PY3 = sys.version_info[0] == 319FRAME_RE = re.compile(r'^\s*File "(?P<co_filename>.+)", line (?P<tb_lineno>\d+)(, in (?P<co_name>.+))?$')20class _AttrDict(dict):21 __slots__ = ()22 def __getattr__(self, name):23 try:24 return self[name]25 except KeyError:26 raise AttributeError(name)27# noinspection PyPep8Naming28class __traceback_maker(Exception):29 pass30class TracebackParseError(Exception):31 pass32class Code(object):33 """34 Class that replicates just enough of the builtin Code object to enable serialization and traceback rendering.35 """36 co_code = None37 def __init__(self, code):38 self.co_filename = code.co_filename39 self.co_name = code.co_name40 self.co_argcount = 041 self.co_kwonlyargcount = 042 self.co_varnames = ()43 self.co_nlocals = 044 self.co_stacksize = 045 self.co_flags = 6446 self.co_firstlineno = 047 # noinspection SpellCheckingInspection48 def __tproxy__(self, operation, *args, **kwargs):49 """50 Necessary for PyPy's tproxy.51 """52 if operation in ('__getattribute__', '__getattr__'):53 return getattr(self, args[0])54 else:55 return getattr(self, operation)(*args, **kwargs)56class Frame(object):57 """58 Class that replicates just enough of the builtin Frame object to enable serialization and traceback rendering.59 """60 def __init__(self, frame):61 self.f_locals = {}62 self.f_globals = {63 k: v64 for k, v in frame.f_globals.items()65 if k in ("__file__", "__name__")66 }67 self.f_code = Code(frame.f_code)68 self.f_lineno = frame.f_lineno69 def clear(self):70 """71 For compatibility with PyPy 3.5;72 clear() was added to frame in Python 3.473 and is called by traceback.clear_frames(), which74 in turn is called by unittest.TestCase.assertRaises75 """76 # noinspection SpellCheckingInspection77 def __tproxy__(self, operation, *args, **kwargs):78 """79 Necessary for PyPy's tproxy.80 """81 if operation in ('__getattribute__', '__getattr__'):82 if args[0] == 'f_code':83 return tproxy(CodeType, self.f_code.__tproxy__)84 else:85 return getattr(self, args[0])86 else:87 return getattr(self, operation)(*args, **kwargs)88class Traceback(object):89 """90 Class that wraps builtin Traceback objects.91 """92 tb_next = None93 def __init__(self, tb):94 self.tb_frame = Frame(tb.tb_frame)95 # noinspection SpellCheckingInspection96 self.tb_lineno = int(tb.tb_lineno)97 # Build in place to avoid exceeding the recursion limit98 tb = tb.tb_next99 prev_traceback = self100 cls = type(self)101 while tb is not None:102 traceback = object.__new__(cls)103 traceback.tb_frame = Frame(tb.tb_frame)104 traceback.tb_lineno = int(tb.tb_lineno)105 prev_traceback.tb_next = traceback106 prev_traceback = traceback107 tb = tb.tb_next108 def as_traceback(self):109 """110 Convert to a builtin Traceback object that is usable for raising or rendering a stacktrace.111 """112 if tproxy:113 return tproxy(TracebackType, self.__tproxy__)114 if not tb_set_next:115 raise RuntimeError("Unsupported Python interpreter!")116 current = self117 top_tb = None118 tb = None119 while current:120 f_code = current.tb_frame.f_code121 code = compile('\n' * (current.tb_lineno - 1) + 'raise __traceback_maker', current.tb_frame.f_code.co_filename, 'exec')122 if hasattr(code, "replace"):123 # Python 3.8 and newer124 code = code.replace(co_argcount=0,125 co_filename=f_code.co_filename, co_name=f_code.co_name,126 co_freevars=(), co_cellvars=())127 elif PY3:128 code = CodeType(129 0, code.co_kwonlyargcount,130 code.co_nlocals, code.co_stacksize, code.co_flags,131 code.co_code, code.co_consts, code.co_names, code.co_varnames,132 f_code.co_filename, f_code.co_name,133 code.co_firstlineno, code.co_lnotab, (), ()134 )135 else:136 code = CodeType(137 0,138 code.co_nlocals, code.co_stacksize, code.co_flags,139 code.co_code, code.co_consts, code.co_names, code.co_varnames,140 f_code.co_filename.encode(), f_code.co_name.encode(),141 code.co_firstlineno, code.co_lnotab, (), ()142 )143 # noinspection PyBroadException144 try:145 exec(code, dict(current.tb_frame.f_globals), {})146 except Exception:147 next_tb = sys.exc_info()[2].tb_next148 if top_tb is None:149 top_tb = next_tb150 if tb is not None:151 tb_set_next(tb, next_tb)152 tb = next_tb153 del next_tb154 current = current.tb_next155 try:156 return top_tb157 finally:158 del top_tb159 del tb160 to_traceback = as_traceback161 # noinspection SpellCheckingInspection162 def __tproxy__(self, operation, *args, **kwargs):163 """164 Necessary for PyPy's tproxy.165 """166 if operation in ('__getattribute__', '__getattr__'):167 if args[0] == 'tb_next':168 return self.tb_next and self.tb_next.as_traceback()169 elif args[0] == 'tb_frame':170 return tproxy(FrameType, self.tb_frame.__tproxy__)171 else:172 return getattr(self, args[0])173 else:174 return getattr(self, operation)(*args, **kwargs)175 def as_dict(self):176 """177 Converts to a dictionary representation. You can serialize the result to JSON as it only has178 builtin objects like dicts, lists, ints or strings.179 """180 if self.tb_next is None:181 tb_next = None182 else:183 tb_next = self.tb_next.to_dict()184 code = {185 'co_filename': self.tb_frame.f_code.co_filename,186 'co_name': self.tb_frame.f_code.co_name,187 }188 frame = {189 'f_globals': self.tb_frame.f_globals,190 'f_code': code,191 'f_lineno': self.tb_frame.f_lineno,192 }193 return {194 'tb_frame': frame,195 'tb_lineno': self.tb_lineno,196 'tb_next': tb_next,197 }198 to_dict = as_dict199 @classmethod200 def from_dict(cls, dct):201 """202 Creates an instance from a dictionary with the same structure as ``.as_dict()`` returns.203 """204 if dct['tb_next']:205 tb_next = cls.from_dict(dct['tb_next'])206 else:207 tb_next = None208 code = _AttrDict(209 co_filename=dct['tb_frame']['f_code']['co_filename'],210 co_name=dct['tb_frame']['f_code']['co_name'],211 )212 frame = _AttrDict(213 f_globals=dct['tb_frame']['f_globals'],214 f_code=code,215 f_lineno=dct['tb_frame']['f_lineno'],216 )217 tb = _AttrDict(218 tb_frame=frame,219 tb_lineno=dct['tb_lineno'],220 tb_next=tb_next,221 )222 return cls(tb)223 @classmethod224 def from_string(cls, string, strict=True):225 """226 Creates an instance by parsing a stacktrace. Strict means that parsing stops when lines are not indented by at least two spaces227 anymore.228 """229 frames = []230 header = strict231 for line in string.splitlines():232 line = line.rstrip()233 if header:234 if line == 'Traceback (most recent call last):':235 header = False236 continue237 frame_match = FRAME_RE.match(line)238 if frame_match:239 frames.append(frame_match.groupdict())240 elif line.startswith(' '):241 pass242 elif strict:243 break # traceback ended244 if frames:245 previous = None246 for frame in reversed(frames):247 previous = _AttrDict(248 frame,249 tb_frame=_AttrDict(250 frame,251 f_globals=_AttrDict(252 __file__=frame['co_filename'],253 __name__='?',254 ),255 f_code=_AttrDict(frame),256 f_lineno=int(frame['tb_lineno']),257 ),258 tb_next=previous,259 )260 return cls(previous)261 else:...

Full Screen

Full Screen

exception_test.py

Source:exception_test.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2#3# Copyright 2015-2022 Flavio Garcia4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16"""17Datetime tests18"""19from tests import WarningsHandler20from cartola import exception21import logging22import warnings23import unittest24logger = logging.getLogger(__name__)25class MockLoggingHandler(logging.Handler):26 """Mock logging handler to check for expected logs.27 Messages are available from an instance's ``messages`` dict, in order,28 indexed by a lowercase log level string (e.g., 'debug', 'info', etc.).29 SEE: https://stackoverflow.com/a/20553331/288798930 """31 def __init__(self, *args, **kwargs):32 self.messages = {'debug': [], 'info': [], 'warning': [], 'error': [],33 'critical': []}34 super(MockLoggingHandler, self).__init__(*args, **kwargs)35 def emit(self, record):36 """Store a message from ``record`` in the instance's ``messages``37 dict."""38 try:39 self.messages[record.levelname.lower()].append(record)40 except Exception:41 self.handleError(record)42 def reset(self):43 self.acquire()44 try:45 for message_list in self.messages.values():46 message_list.clear()47 finally:48 self.release()49class ExceptionTestCase(unittest.TestCase):50 @classmethod51 def setUpClass(cls):52 super(ExceptionTestCase, cls).setUpClass()53 # Assuming you follow Python's logging module's documentation's54 # recommendation about naming your module's logs after the module's55 # __name__,the following getLogger call should fetch the same logger56 # you use in the foo module57 exception_log = logging.getLogger(__name__)58 cls._exception_log_handler = MockLoggingHandler(level='DEBUG')59 exception_log.addHandler(cls._exception_log_handler)60 cls.exception_log_messages = cls._exception_log_handler.messages61 def setUp(self):62 super(ExceptionTestCase, self).setUp()63 self._exception_log_handler.reset() # So each test is independent64 def test_stack(self):65 """ Checking if full_exc_info will return the division by 0 error at66 line 83. If the line changes this test will fail."""67 try:68 1/069 except:70 logger.warning("The message", exc_info=exception.full_exc_info())71 for record in self.exception_log_messages['warning']:72 tb_frame = record.exc_info[2]73 while tb_frame.tb_next:74 tb_frame = tb_frame.tb_next75 self.assertEqual(76 tb_frame.tb_frame.f_locals['record'].filename,77 "exception_test.py")78 self.assertEqual(79 tb_frame.tb_frame.f_locals['record'].funcName,80 "test_stack")81 self.assertEqual(82 tb_frame.tb_frame.f_locals['record'].exc_info[0],83 ZeroDivisionError)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful