diff --git a/IR_image_tools.py b/IR_image_tools.py index df4496077809cb1f98afdb29ad355a44d115311d..9673df0f7d8124192fc34ffc961778deabb67e27 100644 --- a/IR_image_tools.py +++ b/IR_image_tools.py @@ -68,7 +68,7 @@ def bestimmtheitsmass_general(data,fit): qam=quad_abweich_mittel(fit,mittel) R=qam/(qam+quad_abweich(data,fit)) else: - print("Arrays must have same dimensions") + print("bestimmtheitsmass_general: Arrays must have same dimensions") return R def quad_abweich_mittel(data,mittel): @@ -83,7 +83,7 @@ def quad_abweich(data,fit): for i in range(len(data)): R=R+(data[i]-fit[i])**2 else: - print("Arrays must have same dimensions") + print("quad_abweich: Arrays must have same dimensions") return R def find_nearest(array,value): @@ -131,6 +131,8 @@ def check_coldframe(coldframe,references=None,threshold=0.5,plot_it=False): return False,bestimmtheit def check_coldframe_by_refframe(coldframe,reference_frame,threshold=0.8,plot_it=False): + ''' + ''' references=[] shapi=np.shape(reference_frame) for i in [int(shapi[1]//5),int(shapi[1]//2),int(shapi[1]//4*3)]: @@ -185,8 +187,10 @@ def read_bad_pixels_from_file(port, shot_no=None, program=None,time_ns=None): def find_outlier_pixels(frame,tolerance=3,worry_about_edges=True,plot_it=False): - # This function finds the bad pixels in a 2D dataset. - # Tolerance is the number of standard deviations used for cutoff. + ''' + This function finds the bad pixels in a 2D dataset. + Tolerance is the number of standard deviations used for cutoff. + ''' frame = np.array(frame)#, dtype=int) from scipy.ndimage import median_filter blurred = median_filter(frame, size=9) @@ -222,23 +226,26 @@ def find_outlier_pixels(frame,tolerance=3,worry_about_edges=True,plot_it=False): return bad_pixels -def correct_images(images,badpixels): +def correct_images(images,badpixels,verbose=0): + ''' + ''' if type(badpixels)!=int: if type(images) == list: # return corrected images also as list of 2D arrays for i in range(len(images)): - images[i]=restore_bad_pixels(images[i], np.invert(badpixels==1)) + images[i]=restore_bad_pixels(images[i], np.invert(badpixels==1), verbose=verbose-1) else: # keep shape - images = restore_bad_pixels(images, np.invert(badpixels==1)) + images = restore_bad_pixels(images, np.invert(badpixels==1), verbose=verbose-1) # for i in range(len(images)): # images[i]=(restore_pixels(images[i],np.invert(badpixels==1))).astype(np.float32) - print("done") + if verbose>0: + print("correct_images: done") return images -def restore_bad_pixels(frames, bad_pixel, by_list=True, check_neighbours=True, plot_it=False, verbose=0): +def restore_bad_pixels(frames, bad_pixel, by_list=True, check_neighbours=True, plot_it=False,verbose=0): """Restore bad pixel by interpolation of adjacent pixels. Optionally make sure that adjacent pixels are not bad (time consuming). Default is to use a list of bad pixels and a for loop. For many bad pixels consider using @@ -409,16 +416,20 @@ def restore_bad_pixels(frames, bad_pixel, by_list=True, check_neighbours=True, p def generate_new_hot_image(cold,reference_cold,reference_hot): + ''' + ''' if cold is None or reference_cold is None or reference_hot is None: - raise Exception("Cannot Calculate new Hot image, if images are missing!") + raise Exception("generate_new_hot_image: Cannot Calculate new Hot image, if images are missing!") else: return reference_hot+(cold-reference_cold) -def calculate_gain_offset_image_pix(cold_image,hot_image=None,reference_cold=None,reference_hot=None,bose=1): +def calculate_gain_offset_image_pix(cold_image,hot_image=None,reference_cold=None,reference_hot=None,verbose=0): + ''' + ''' if hot_image is None: hot_image=generate_new_hot_image(cold_image,reference_cold,reference_hot) - if bose>0: - print("calculate gain and offset") + if verbose>0: + print("calculate_gain_offset_image_pix: calculate gain and offset") Sh_ref = hot_image[ ( np.int( np.shape(hot_image)[0] /2 ) ) ][np.int( (np.shape(hot_image)[1] /2 ) ) ] Sc_ref = cold_image[ ( np.int( (np.shape(cold_image)[0]) /2 ) ) ][( np.int( (np.shape(cold_image)[1]) /2 ) ) ] Gain_rel = ( Sh_ref - Sc_ref ) / ( hot_image - cold_image) @@ -431,7 +442,7 @@ def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,re if hot_image is None: hot_image=generate_new_hot_image(cold_image,reference_cold,reference_hot) if verbose>0: - print("calculate gain and offset") + print("calculate_gain_offset_image: calculate gain and offset") # Sh_ref = hot_image[ ( np.int( np.shape(hot_image)[0] /2 ) ) ][np.int( (np.shape(hot_image)[1] /2 ) ) ] # Sc_ref = cold_image[ ( np.int( (np.shape(cold_image)[0]) /2 ) ) ][( np.int( (np.shape(cold_image)[1]) /2 ) ) ] @@ -452,30 +463,32 @@ def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,re #%% functions from Yu Gao """ functions by Yu Gao""" -def load_ref_images(port, exposuretime): - ''' - load the reference cold and hot frame during calibration from local files. - @port: e.g. 'AEF10' - @exposuretime: int number. - ''' - cameraname = portcamdict['OP1.2a'][port] - foldername = cameraname.split('_')[0] + '_' + cameraname.split('_')[2] - scanpath = join(IRCamRefImagespath, foldername) - coldref, hotref = [], [] - for filename in glob.iglob(scanpath + '\*' + str(int(exposuretime)) + 'us.h5', recursive=True): - if 'hot' in filename: - print (filename) - with h5py.File(filename, 'r') as h5in: - hotref = h5in[basename(filename)].value - elif 'cold' in filename: - print (filename) - with h5py.File(filename, 'r') as h5in: - coldref = h5in[basename(filename)].value - return coldref, hotref +def load_ref_images(port, exposuretime, verbose=0): + ''' + load the reference cold and hot frame during calibration from local files. + @port: e.g. 'AEF10' + @exposuretime: int number. + ''' + cameraname = portcamdict['OP1.2a'][port] + foldername = cameraname.split('_')[0] + '_' + cameraname.split('_')[2] + scanpath = join(IRCamRefImagespath, foldername) + coldref, hotref = [], [] + for filename in glob.iglob(scanpath + '\*' + str(int(exposuretime)) + 'us.h5', recursive=True): + if 'hot' in filename: + if verbose>0: + print('load_ref_images: read from ',filename) + with h5py.File(filename, 'r') as h5in: + hotref = h5in[basename(filename)].value + elif 'cold' in filename: + if verbose>0: + print('load_ref_images: read from ',filename) + with h5py.File(filename, 'r') as h5in: + coldref = h5in[basename(filename)].value + return coldref, hotref def reconstruct_coldframe (exposuretime, sT, a, bnew, coldref): - cirebuild = a * sT + bnew * exposuretime + coldref - return cirebuild + cirebuild = a * sT + bnew * exposuretime + coldref + return cirebuild #%% other functions diff --git a/downloadversionIRdata.py b/downloadversionIRdata.py index d12e1fbc777a1c9f726b518192af388e71c4c0da..6be5afd7a87e1c0b8d5766c2d8a1915c3cf8ea6e 100644 --- a/downloadversionIRdata.py +++ b/downloadversionIRdata.py @@ -11,7 +11,11 @@ Version: 3.0.2 import numpy as np import IR_image_tools as IR_tools -from IR_config_constants import archivepath,portcamdict,camlimdict,valid_FOV_circle,valid_background_rectangle,TC_port,TC_channel,IRCamColdframes_fittingpath,exJet,portpathdict,project,project_ana,heatflux_requestlist_path#,exJet_trans +from IR_config_constants import archivepath,portcamdict,camlimdict, \ + valid_FOV_circle,valid_background_rectangle, \ + TC_port,TC_channel,IRCamColdframes_fittingpath, \ + exJet,portpathdict,project,project_ana, \ + heatflux_requestlist_path#,exJet_trans #try: # import W7Xrest.read_restdb as AKF_1 #except: @@ -39,33 +43,40 @@ try: except: config_path="" -def get_OP_by_program(program): - try: - bla=int(program.split(".")[0]) - time_ns=int(TimeToNs([bla // 10000,bla % 10000 // 100,bla % 10000 % 100],[10,0,0,0])) - return get_OP_by_time(time_ns) - except Exception as E: - print(E) - return None - -def get_OP_by_time(time_ns): - dateOP=datetime.datetime.utcfromtimestamp(time_ns/1e9) - if dateOP.year==2017: - if dateOP.month>8 and dateOP.month<12: - return "OP1.2a" - elif dateOP.month==8 and dateOP.day>=28: - return "OP1.2a" - elif dateOP.month==12 and dateOP.day<8: - return "OP1.2a" - else: - return None - elif dateOP.year==2018: - return "OP1.2b" - elif dateOP.year<=2016 and dateOP.year>=2015: - if (dateOP.year==2016 and dateOP.month<=3) or (dateOP.year==2015 and dateOP.month==12): - return "OP1.1" - else: - return None + +# These two functions seem unused and will be deleted. +# They are redundant to +# IR_tools.get_OP_by_time(time_ns=None, shot_no=None, program_str=None) +# which should be used instead. +# +#def get_OP_by_program(program): +# try: +# bla=int(program.split(".")[0]) +# time_ns=int(TimeToNs([bla // 10000,bla % 10000 // 100,bla % 10000 % 100],[10,0,0,0])) +# return get_OP_by_time(time_ns) +# except Exception as E: +# print(E) +# return None +# +#def get_OP_by_time(time_ns): +# dateOP=datetime.datetime.utcfromtimestamp(time_ns/1e9) +# if dateOP.year==2017: +# if dateOP.month>8 and dateOP.month<12: +# return "OP1.2a" +# elif dateOP.month==8 and dateOP.day>=28: +# return "OP1.2a" +# elif dateOP.month==12 and dateOP.day<8: +# return "OP1.2a" +# else: +# return None +# elif dateOP.year==2018: +# return "OP1.2b" +# elif dateOP.year<=2016 and dateOP.year>=2015: +# if (dateOP.year==2016 and dateOP.month<=3) or (dateOP.year==2015 and dateOP.month==12): +# return "OP1.1" +# else: +# return None + def get_latest_version(stream,project="W7X",Test=False): """Find out latest version of given stream @@ -76,8 +87,9 @@ def get_latest_version(stream,project="W7X",Test=False): if Test: base="http://archive-webapi.ipp-hgw.mpg.de/Test/raw/" else: - base=archivepath - request =urllib.request.Request(base +project+"/" + stream + "/_versions.json", headers={"Accept": "application/json"}) + base=archivepath + url = '{0}{1}/{2}/_versions.json'.format(base, project, stream) + request = urllib.request.Request(url, headers={"Accept": "application/json"}) try: response = urllib.request.urlopen(request) d = json.loads(response.read().decode('utf-8')) @@ -104,21 +116,22 @@ def TimeToNs(date,time): nsdate=div.total_seconds()*1e9 return nsdate -def read_program(timestampstart,timestamp_end=0,tol=60): +def read_program(timestamp_start,timestamp_end=0,tol=60): """ read_program() """ program_url = 'http://archive-webapi.ipp-hgw.mpg.de/programs.json?from' if timestamp_end!=0: - jsonurl=program_url+"="+str(int(timestampstart-tol*1e9))+"&upto="+str(int(timestamp_end+tol*1e9)) - else: - jsonurl=program_url+"="+str(int(timestampstart-tol*1e9))+"&upto="+str(int(timestampstart+tol*1e9)) + timestamp_end = timestamp_start + jsonurl = '{0}={1}&upto={2}'.format(program_url, + int(timestamp_start-tol*1e9), + int(timestamp_end+tol*1e9)) try: res = urllib.request.urlopen(jsonurl) prog_raw=res.read() res.close() except urllib.error.URLError as e: - print('!get_program: Error opening URL') + print('read_program: Error opening URL') print(e) return False,0 else: @@ -139,7 +152,7 @@ def read_restdb_old(request_url): try: res = urllib.request.urlopen(request_url) except urllib.error.URLError as e: - print(e) + print('read_restdb_old: Error ', e) return False, 0, -1 else: signal_raw=res.read() @@ -167,12 +180,12 @@ def download_LUT(port,time,exposure=0,emissivity=0,camera_filter=0,version=0, ve elif port in [10,11,20,21,30,31,40,41,51] or OP=="OP1.2b":#camera=="IRCAM" or camera=="IRcam" or camera=="ircam": query="Texp_"+str(int(exposure))+"us_e_"+str(float(emissivity)) else: - print("camera unknown, stopping here") + print("download_LUT: Error! Camera unknown, stopping here.") raise Exception if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(port)]+"LUT_DATASTREAM") if verbose>0: - print("LUT V"+str(version)+" is used") + print("download_LUT: LUT V"+str(version)+" is used") #time=int(fu.TimeToNs([2017,9,26],[8,0,0,0])) LUTpar=read_restdb_old(larchivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(time-10)+"&upto="+str(time+20)) if LUTpar[0]: @@ -184,12 +197,12 @@ def download_LUT(port,time,exposure=0,emissivity=0,camera_filter=0,version=0, ve del LUTpar, LUTs return True,LUT else: - print("Warning: unable to download the LUTs") + print("download_LUT: Warning! unable to download the LUTs") del LUTpar, LUTs return False,0 else: del LUTpar - print("Warning: unable to find LUTs, check your request") + print("download_LUT: Warning! unable to find LUTs, check your request") return False,0 @@ -200,7 +213,7 @@ def read_LUT_from_file(port, this_time, t_exp, emissivity, cfilter, emissivity_s Read LUT V3 from local files for testing new calibration. """ LUT_dir = '\\\\sv-e4-fs-1\\E4-Mitarbeiter\\E4 Diagnostics\\QIR\\testingLUT' - OP = get_OP_by_time(this_time) + OP = IR_tools.get_OP_by_time(time_ns=this_time) cam_name = portcamdict[OP]['AEF{0}'.format(port)] filename = '{0}_thermal_LUT_filter_{1}_texp_{2}us_emiss_{3:.2f}.json'.format(cam_name, cfilter, t_exp, emissivity) # data = json.loads(join(LUT_dir, filename).decode('utf-8')) @@ -209,10 +222,10 @@ def read_LUT_from_file(port, this_time, t_exp, emissivity, cfilter, emissivity_s jfile=json.load(data_file) LUT=np.array(jfile['LUT']).swapaxes(0,1) if verbose>0: - print("succesfully loaded V3 LUT from local directory") + print("read_LUT_from_file: succesfully loaded V3 LUT from local directory") return True, LUT except Exception as E: - print("ERROR in loading V3 LUTs",E) + print("read_LUT_from_file: ERROR in loading V3 LUTs",E) return False, [] def download_NUC_by_program(port,program,exposure,version=0): @@ -226,12 +239,12 @@ def download_NUC_by_program(port,program,exposure,version=0): return download_NUC_by_times(port,starttime,stoptime,exposure,version) else: # except: - print("cannot find the program") + print("download_NUC_by_program: cannot find the program") return False,0,0 def download_NUC_by_times(port,starttime,stoptime,exposure,version=0): project="W7X" - OP=get_OP_by_time(starttime) + OP=IR_tools.get_OP_by_time(time_ns=starttime) larchivepath=archivepath+project+"/"+portpathdict[OP]["AEF"+str(port)]+"NUC_" # NUC_parlog=AKF_1.read_restdb_old(archivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) if version==0: @@ -242,7 +255,7 @@ def download_NUC_by_times(port,starttime,stoptime,exposure,version=0): res.close() goon=True except urllib.error.URLError as e: - print(e) + print('download_NUC_by_times: Error! ', e) goon=False if goon: n=0 @@ -259,10 +272,10 @@ def download_NUC_by_times(port,starttime,stoptime,exposure,version=0): images=np.vsplit(NUC[2],np.shape(NUC[2])[0]/offset_i) return True,images,['gain','offset','cold','badpixels','gain_error','offset_error'] else: - print("NUC image for requested exposure time not found") + print("download_NUC_by_times: NUC image for requested exposure time not found") return False, 0, 0 else: - print("NUC image for requested exposure time not found") + print("download_NUC_by_times: NUC image for requested exposure time not found") return False,0,0 else: return False,0,0 @@ -278,10 +291,15 @@ def get_NUC_by_program(port,program,exposure,version=0,verbose=0): return get_NUC_by_times(port,starttime,stoptime,exposure,version=0,verbose=verbose-1) else: # except: - print("cannot find the program") + print("get_NUC_by_program: Error! cannot find the program") return False,0,0 def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): + """ + Loads NUC elements (gain, offset cold image, bad pixels) for an IR camera + in a given port and time interval. Depending on time and camera, reconstruct + teh cold frame and/or compute the gain,offset from the hot and cold frame. + """ OP=IR_tools.get_OP_by_time(time_ns=endtime) if OP=="OP1.2a": t1=endtime @@ -289,8 +307,8 @@ def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): prog=read_program(t1) program=prog[1]['id'] if (port == 31) or (port == 21 and float(program[4:]) > 1110): - - print("rebuilding coldframe") + if verbose>0: + print("get_NUC_by_times: rebuilding coldframe") #use any way the rebuilt coldframe. sT = get_sensor_temp_by_program(port, program)[2][0] # coldref, hotref = IR_tools.load_ref_images('AEF' + str(port), t_exp) @@ -299,7 +317,7 @@ def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): coldref=hotcold[1] hotref=hotcold[0] else: - raise Exception("unable to download reference frames") + raise Exception("get_NUC_by_times: unable to download reference frames") filestring = 'AEF' + str(port) + '_et' + str(t_exp) amap = np.load(join(IRCamColdframes_fittingpath, filestring + '_a.npy')) bmap = np.load(join(IRCamColdframes_fittingpath, filestring + '_b.npy')) @@ -307,7 +325,7 @@ def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): gain, offset = IR_tools.calculate_gain_offset_image(cirebuild, None, coldref, hotref,verbose=verbose-1) gain[gain == np.inf] = 0 offset[offset == np.inf] = 0 - badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10) + badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10,verbose=verbose-1) gain_error=0 offset_error=0 return True,[gain,offset,cirebuild,badpixels,gain_error,offset_error],['gain','offset','cold','badpixels','gain_error','offset_error'] @@ -318,16 +336,16 @@ def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): coldref=hotcold[1] hotref=hotcold[0] else: - raise Exception("unable to download reference frames") + raise Exception("get_NUC_by_times: unable to download reference frames") NUC_DL=download_NUC_by_times(port,t0,t1,t_exp,version) if NUC_DL[0]==False: - raise Exception("NUC was not found") + raise Exception("get_NUC_by_times: NUC was not found") else: ci = NUC_DL[1][2] gain, offset = IR_tools.calculate_gain_offset_image(ci, None, coldref, hotref,verbose=verbose-1) gain[gain == np.inf] = 0 offset[offset == np.inf] = 0 - badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10) + badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10,verbose=verbose-1) gain_error=0 offset_error=0 return True,[gain,offset,ci,badpixels,gain_error,offset_error],['gain','offset','cold','badpixels','gain_error','offset_error'] @@ -341,9 +359,9 @@ def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): coldref=hotcold[1] hotref=hotcold[0] else: - raise Exception("unable to download reference frames") + raise Exception("get_NUC_by_times: unable to download reference frames") ### get the cold frame: 1. get the metastream frametype and exposuretime and check in it for the positions, if -1 type is unknown, when if needs to be identified in a different way - exist,ctime,coldframes=download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametype=0,version=0) + exist,ctime,coldframes=download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametype=0,version=0,verbose=verbose-1) if exist: cold=np.zeros(np.shape(coldframes[0]),dtype=np.uint64) for ele in coldframes: @@ -352,12 +370,12 @@ def get_NUC_by_times(port,starttime,endtime,t_exp,version=0,verbose=0): else: return False,[0],[0] gain, offset = IR_tools.calculate_gain_offset_image(cold, None, coldref, hotref,verbose=verbose-1) - badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10) + badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10,verbose=verbose-1) return True,[gain,offset,cold,badpixels,gain_error,offset_error],['gain','offset','cold','badpixels','gain_error','offset_error'] else: - raise Exception("unknown Operation phase or NUC method not implemented for this OP") + raise Exception("get_NUC_by_times: unknown Operation phase or NUC method not implemented for this OP") -def download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametype=0,version=0): +def download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametype=0,version=0,verbose=0): """ download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametype=0,version=0) frametype: 0 for closed shutter frames (cold), 1 for open shutter frames (background) @@ -367,10 +385,10 @@ def download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametyp if gotit: expinds=np.where(texp_t==t_exp)[0] if len(expinds)==0: - print("cannot find the exposure time in the given data") + print("download_calibration_raw_files_by_time: Error! cannot find the exposure time in the given data") return False,[0],[0] else: - print("exposure time not found") + print("download_calibration_raw_files_by_time: Error! exposure time not found") return False,[0],[0] gotitf,timef,values_f=get_frametype_by_times(port,int(starttime-100),int(endtime-20e6)) if gotitf: @@ -380,7 +398,8 @@ def download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametyp # print((ref_t[1]-ref_t[0])/1e9) # print(len(timef),len(timef[expinds]),len(timef[expinds][typiinds])) else:#okay the early data stuff or strange stuff - print("frame type was not identified assuming that the first part is the cold and the second one the background") + if verbose>0: + print("download_calibration_raw_files_by_time: frame type was not identified assuming that the first part is the cold and the second one the background") frametimes=time_t[expinds] diftime=[frametimes[i]-frametimes[i-1] for i in range(1,len(frametimes))] turnpoint=np.where(np.asarray(diftime)>11e6)[0][0] @@ -392,9 +411,9 @@ def download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametyp ref_t=[np.min(frametimes[turnpoint+1:]),np.max(frametimes[turnpoint+1:])] print((ref_t[1]-ref_t[0])/1e9) else: - raise Exception("requested Frametype unknown and not implemented!") + raise Exception("download_calibration_raw_files_by_time: requested Frametype unknown and not implemented!") else: - print("frametype not found") + print("download_calibration_raw_files_by_time: Error! frametype not found") return False,[0],[0] t1date=datetime.datetime.utcfromtimestamp((endtime-100)/1e9) t1date=t1date.isoformat() @@ -406,13 +425,18 @@ def download_calibration_raw_files_by_time(port,t_exp,starttime,endtime,frametyp timest0=AKF_2.get_time_intervals(larchivepath,t0date.replace("T"," "),t1date.replace("T"," ")) t_offset=timest0[-1][0]-time_t[0] if t_offset>10e6: - print("time offset detected, try to correct this, offset is",t_offset) + if verbose>0: + print("download_calibration_raw_files_by_time: time offset detected, try to correct this, offset is",t_offset) else: t_offset=0 # print("starttime frames:",np.min(timest0),"starttime metachannels:",time_t[0],"offset",t_offset) - return download_raw_images_by_times(port,ref_t[0]+t_offset,ref_t[1]+t_offset) + return download_raw_images_by_times(port,ref_t[0]+t_offset,ref_t[1]+t_offset,verbose=verbose-1) def download_hot_cold_reference_by_times(port,exposure,starttime=1503907200000000000,version=0): + """ + Loads the most recent hot and cold calibration frames for a starttime. + Uses first calibration frames if time is not defined. + """ OP=IR_tools.get_OP_by_time(time_ns=starttime) larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"raw_" # NUC_parlog=AKF_1.read_restdb_old(archivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) @@ -424,7 +448,7 @@ def download_hot_cold_reference_by_times(port,exposure,starttime=150390720000000 res.close() goon=True except urllib.error.URLError as e: - print(e) + print('download_hot_cold_reference_by_times: Error! ',e) goon=False if goon: COLDtime=0 @@ -432,11 +456,11 @@ def download_hot_cold_reference_by_times(port,exposure,starttime=150390720000000 try: COLDtime=signal_list['values'][0]['structure']['cold_'+str(int(exposure))+'us'] except: - print("cold image for requested exposure time not found") + print("download_hot_cold_reference_by_times: cold image for requested exposure time not found") try: HOTtime=signal_list['values'][0]['structure']['hot_'+str(int(exposure))+'us'] except: - print("hot image for requested exposure time not found") + print("download_hot_cold_reference_by_times: hot image for requested exposure time not found") images=[] if HOTtime!=0: HOT=read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/_signal.json?from="+str(HOTtime-10)+"&upto="+str(HOTtime+10)) @@ -450,7 +474,7 @@ def download_hot_cold_reference_by_times(port,exposure,starttime=150390720000000 if HOT[0] and COLD[0] and len(images)==2: return True,images,['hot','cold'] else: - print("hot and cold image for requested exposure time not found") + print("download_hot_cold_reference_by_times: Error! hot and cold image for requested exposure time not found") return False,0,0 else: return False,0,0 @@ -468,7 +492,7 @@ def download_background_by_program(port,program,exposure,version=0): stoptime=prog[1]['trigger']['1'][0] return download_background_by_times(port,starttime,stoptime,exposure,version) else:#except: - print("cannot find the program") + print("download_background_by_program: Error! cannot find the program {0}".format(program)) return False,0,0 def download_background_by_times(port,starttime,stoptime,exposure,camera_filter=0,version=0): @@ -483,7 +507,7 @@ def download_background_by_times(port,starttime,stoptime,exposure,camera_filter= res.close() goon=True except urllib.error.URLError as e: - print(e) + print('download_background_by_times: Error! ',e) goon=False if goon: n=0 @@ -501,17 +525,19 @@ def download_background_by_times(port,starttime,stoptime,exposure,camera_filter= if backdat[0]: return backdat#[True,backdat[2]] else: - print("background image for requested exposure time(, filter) not found") + print("download_background_by_times: Error! background image for requested exposure time(, filter) not found") return False,0,0 else: - print("background image for requested exposure time(, filter) not found") + print("download_background_by_times: Error! background image for requested exposure time(, filter) not found") return False,0,0 else: return False,0,0 -def get_NUCed_background_by_times(port,t0,t1,t_exp,cfilter,gain,offset,version=0,plot_it=False): - "OP1.2b function" - exist,btime,backgroundframes=download_calibration_raw_files_by_time(port,t_exp,t0,t1,frametype=1,version=version) +def get_NUCed_background_by_times(port,t0,t1,t_exp,cfilter,gain,offset,version=0,plot_it=False,verbose=0): + """ + OP1.2b function + """ + exist,btime,backgroundframes=download_calibration_raw_files_by_time(port,t_exp,t0,t1,frametype=1,version=version,verbose=verbose-1) camera=portpathdict["OP1.2b"]["AEF"+str(port)] background=np.zeros(np.shape(backgroundframes[0]),dtype=np.uint64) for frame in backgroundframes: @@ -524,13 +550,14 @@ def get_NUCed_background_by_times(port,t0,t1,t_exp,cfilter,gain,offset,version=0 if not camera.split("_")[0]=="Infratec": background=apply_NUC([background],gain,offset)[0] if plot_it: - plt.figure() plt.imshow(background,vmin=np.median(background)-500,vmax=np.median(background)+500) plt.title("background image nuced") return exist,btime[0],background -def download_raw_images_by_program(port,program,time_s=0,version=0,verbose=0): +def download_raw_images_by_program(port,program,time_window=0,version=0,verbose=0): + """ + """ # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -538,25 +565,26 @@ def download_raw_images_by_program(port,program,time_s=0,version=0,verbose=0): # if prog[0]: starttime=prog[0]['trigger']['1'][0] stoptime=prog[0]['trigger']['6'][0] - if type(time_s)==list or type(time_s)==np.ndarray: - if len(time_s)>2 or time_s[0]>1000: - raise Exception("invalid input for the timewindow!") - tstart=time_s[0] - tstop=time_s[1] + if type(time_window)==list or type(time_window)==np.ndarray: + if len(time_window)>2 or time_window[0]>1000: + raise Exception("download_raw_images_by_program: invalid input for the time_window!") + tstart=time_window[0] + tstop=time_window[1] if tstop<tstart: - raise Exception("endtime before starttime") + raise Exception("download_raw_images_by_program: endtime before starttime") return download_raw_images_by_times(port,int(starttime+tstart*1e9),int(starttime+tstop*1e9),version,verbose=verbose-1) else: - if time_s==0: + if time_window==0: return download_raw_images_by_times(port,starttime,stoptime,version,verbose=verbose-1) else: - return download_raw_images_by_times(port,starttime,int(starttime+time_s*1e9),version,verbose=verbose-1) + return download_raw_images_by_times(port,starttime,int(starttime+time_window*1e9),version,verbose=verbose-1) except: - if verbose>0: - print("cannot find the program") + print("download_raw_images_by_program: Error! cannot find the program") return False,0,0 def download_raw_images_by_times(port,starttime,stoptime,version=0,intervalSize=1E9,verbose=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=starttime) if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(port)]+"raw_DATASTREAM") @@ -564,6 +592,8 @@ def download_raw_images_by_times(port,starttime,stoptime,version=0,intervalSize= return download_images_by_times(larchivepath,starttime=starttime,stoptime=stoptime,version=version,intervalSize=intervalSize,verbose=verbose-1) def download_images_by_times(larchivepath,starttime,stoptime,version=0,intervalSize=1E9,verbose=0): + """ + """ if '_raw' in larchivepath: typo=np.uint16 else: @@ -571,7 +601,7 @@ def download_images_by_times(larchivepath,starttime,stoptime,version=0,intervalS if (stoptime-starttime)/intervalSize>1: nrinterv=int(np.ceil((stoptime-starttime)/intervalSize)) if verbose>0: - print("timewindow to large, splitting into smaller fractions ("+str(nrinterv)+")",(stoptime-starttime)/1e9) + print("download_images_by_times: time_window to large, splitting {0}s interval into {1} smaller fractions".format((stoptime-starttime)/1e9,nrinterv)) intervalls=[] for i in range(nrinterv): intervalls.append(int(starttime-10+i*intervalSize)) @@ -597,9 +627,7 @@ def download_images_by_times(larchivepath,starttime,stoptime,version=0,intervalS del images,t success=True except urllib.error.URLError as e: - - if verbose>0: - print(e) + print('download_images_by_times: Error in sub-interval {0}! {1}'.format(i,e)) return success,time,allimages else: try: @@ -612,103 +640,108 @@ def download_images_by_times(larchivepath,starttime,stoptime,version=0,intervalS del signal_list return True, t, images except urllib.error.URLError as e: - if verbose>0: - print(e) + print('download_images_by_times: Error! ',e) return False, 0,-1 if fastDL: - def download_raw_images_by_program_via_png(port,program,time_s=0,version=0,threads=1, verbose=0): + def download_raw_images_by_program_via_png(port,program,time_window=0,version=0,threads=1,verbose=0): + """ + """ prog=get_program_from_PID(program) # try: # t_program = AKF_2.get_program_from_to(program) # prog =AKF_2.get_program_list(t_program[0], t_program[1]) if not prog[0]: - if verbose>0: - print("program not found, cannot download the data") - return False,[0],[0] + print("download_raw_images_by_program_via_png: Error! program not found, cannot download the data") + return False,[0],[0] else: starttime=prog[1]['trigger']['1'][0] stoptime=prog[1]['trigger']['6'][0] - OP=get_OP_by_time(starttime) + OP=IR_tools.get_OP_by_time(time_ns=starttime) if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(port)]+"raw_DATASTREAM") larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"raw_DATASTREAM/V"+str(version)+"/0/raw" - return download_images_by_time_via_png(larchivepath,starttime,stoptime,time_s=time_s,threads=threads,version=version,verbose=verbose-1) + return download_images_by_time_via_png(larchivepath,starttime,stoptime, + time_window=time_window,threads=threads, + version=version,verbose=verbose-1) - def download_images_by_time_via_png(larchivepath,starttime,stoptime,time_s=0,threads=1,version=0,verbose=0): - success=True - if type(time_s)==list: - tstart=time_s[0] - tstop=time_s[1] - if tstop<tstart: - raise Exception("endtime before starttime") - stdate=datetime.datetime.utcfromtimestamp((starttime-100)/1e9+tstart) - stdate=stdate.isoformat() - enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+tstop) + def download_images_by_time_via_png(larchivepath,starttime,stoptime,time_window=0, + threads=1,version=0,verbose=0): + """ + """ + success=True + if type(time_window)==list: + tstart=time_window[0] + tstop=time_window[1] + if tstop<tstart: + raise Exception("download_images_by_time_via_png: endtime before starttime") + stdate=datetime.datetime.utcfromtimestamp((starttime-100)/1e9+tstart) + stdate=stdate.isoformat() + enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+tstop) + enddate=enddate.isoformat() + else: + stdate=datetime.datetime.utcfromtimestamp((starttime-100)/1e9) + stdate=stdate.isoformat() + if time_window==0: + enddate=datetime.datetime.utcfromtimestamp(stoptime/1e9) enddate=enddate.isoformat() else: - stdate=datetime.datetime.utcfromtimestamp((starttime-100)/1e9) - stdate=stdate.isoformat() - if time_s==0: - enddate=datetime.datetime.utcfromtimestamp(stoptime/1e9) - enddate=enddate.isoformat() + enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+time_window) + enddate=enddate.isoformat() + #"2017-11-15 08:00:00" + try: + times=AKF_2.get_time_intervals(larchivepath,stdate.replace("T"," "),enddate.replace("T"," "))# + except Exception as E: + print('download_images_by_time_via_png: Error loading times from ',larchivepath) + raise Warning(E) + return False,[0],[0] + time=[] + images=[] + lnt=len(times) + if threads==1: + for i in range(lnt): + ele=times[lnt-1-i] + imag=download_last_raw_image_by_time(larchivepath,ele[0]-10,ele[0]+10) + if imag[0]: + time.append(ele[0]) + images.append(np.array(imag[1],dtype=np.uint16)) else: - enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+time_s) - enddate=enddate.isoformat() - #"2017-11-15 08:00:00" - try: - times=AKF_2.get_time_intervals(larchivepath,stdate.replace("T"," "),enddate.replace("T"," "))# - except Exception as E: - print(larchivepath) - raise Warning(E) - return False,[0],[0] - time=[] - images=[] - lnt=len(times) - if threads==1: - for i in range(lnt): - ele=times[lnt-1-i] - imag=download_last_raw_image_by_time(larchivepath,ele[0]-10,ele[0]+10) - if imag[0]: - time.append(ele[0]) - images.append(np.array(imag[1],dtype=np.uint16)) - else: - success=False - return success,np.array(time),images - else: - tim=[] - for i in range(lnt): - tim.append(times[lnt-1-i][0]) - intervalls=[] - intervalSize=int(lnt/threads) - for i in range(threads): - intervalls.append(int(i*intervalSize)) - intervalls.append(lnt) - jobs = [] - resultdict = [] - for i in range(threads): - if verbose>0: - print("Start Thread ",i+1) - TH=download_images_thread(i,larchivepath,tim[intervalls[i]:intervalls[i+1]],resultdict,version) - jobs.append(TH) - TH.start() - for p in jobs: - p.join() + success=False + return success,np.array(time),images + else: + tim=[] + for i in range(lnt): + tim.append(times[lnt-1-i][0]) + intervalls=[] + intervalSize=int(lnt/threads) + for i in range(threads): + intervalls.append(int(i*intervalSize)) + intervalls.append(lnt) + jobs = [] + resultdict = [] + for i in range(threads): if verbose>0: - print("all threads are done") - order=[] - for ele in resultdict: - order.append(ele[0]) - if len(np.where(np.asarray(ele[1])==False)[0])>0: - success=False - times=[]#np.array(resultdict[order.index(0)][2]) - images=[]#np.array(resultdict[order.index(0)][3]) - for i in range(threads): - images=images+resultdict[order.index(i)][3] - times=times+resultdict[order.index(i)][2] - resultdict[order.index(i)]=[] - del resultdict - return success,np.array(times),images + print("download_images_by_time_via_png: Start Thread ",i+1) + TH=download_images_thread(i,larchivepath,tim[intervalls[i]:intervalls[i+1]],resultdict,version) + jobs.append(TH) + TH.start() + for p in jobs: + p.join() + if verbose>0: + print("download_images_by_time_via_png: all threads are done") + order=[] + for ele in resultdict: + order.append(ele[0]) + if len(np.where(np.asarray(ele[1])==False)[0])>0: + success=False + times=[]#np.array(resultdict[order.index(0)][2]) + images=[]#np.array(resultdict[order.index(0)][3]) + for i in range(threads): + images=images+resultdict[order.index(i)][3] + times=times+resultdict[order.index(i)][2] + resultdict[order.index(i)]=[] + del resultdict + return success,np.array(times),images class download_images_thread(threading.Thread): def __init__(self, threadID, larchivepath,times,resultlist,version=0): @@ -733,7 +766,9 @@ if fastDL: self.resultlist.append([self.threadID,successes,time,images]) class convert_raw_to_temp_thread(threading.Thread): - def __init__(self,larchivepath,times,resultlist,threadID,version=0,background=0,LUT=[[],[],[]],refT=28.5,gain=0,offset=0,gain_error=0,offset_error=0,give_ERROR=False,FOV=0,badpixels=[]): + def __init__(self,larchivepath,times,resultlist,threadID,version=0,background=0, + LUT=[[],[],[]],refT=28.5,gain=0,offset=0,gain_error=0,offset_error=0, + give_ERROR=False,FOV=0,badpixels=[],verbose=0): threading.Thread.__init__(self) self.threadID = threadID self.times = times @@ -750,6 +785,7 @@ if fastDL: self.give_ERROR=give_ERROR self.FOV=FOV self.badpixels=badpixels + self.verbose=verbose def run(self): images=[] time=[] @@ -763,18 +799,21 @@ if fastDL: else: successes.append(False) if self.give_ERROR: - sucess,images,error_images=apply_calib_on_raw(images,self.background,self.LUT,self.refT,self.gain,self.offset,self.gain_error,self.offset_error,False,self.give_ERROR) + sucess,images,error_images=apply_calib_on_raw(images,self.background,self.LUT,self.refT,self.gain,self.offset,self.gain_error,self.offset_error,False,self.give_ERROR,verbose=self.verbose-1) else: - success,images=apply_calib_on_raw(images,self.background,self.LUT,self.refT,self.gain,self.offset,self.gain_error,self.offset_error,False,self.give_ERROR) + success,images=apply_calib_on_raw(images,self.background,self.LUT,self.refT,self.gain,self.offset,self.gain_error,self.offset_error,False,self.give_ERROR,verbose=self.verbose-1) for i in range(len(images)): images[i]=(images[i]*self.FOV).astype(np.float32) - print(datetime.datetime.now(),"correcting bad pixels") - images=IR_tools.correct_images(images,self.badpixels) + if self.verbose>0: + print(datetime.datetime.now(),"convert_raw_to_temp_thread: correcting bad pixels") + images=IR_tools.correct_images(images,self.badpixels,verbose=self.verbose-1) self.resultlist.append([self.threadID,successes,time,images]) class NUC_raw_thread(threading.Thread): - def __init__ (self,larchivepath,times,resultlist,threadID,version=0,background=0,LUT=[[],[],[]],refT=28.5,gain=0,offset=0,gain_error=0,offset_error=0,give_ERROR=False,FOV=0,badpixels=[]): + def __init__ (self,larchivepath,times,resultlist,threadID,version=0,background=0, + LUT=[[],[],[]],refT=28.5,gain=0,offset=0,gain_error=0,offset_error=0, + give_ERROR=False,FOV=0,badpixels=[]): threading.Thread.__init__(self) self.threadID = threadID self.times = times @@ -803,6 +842,8 @@ if fastDL: successes.append(True) else: successes.append(False) + if self.verbose>0: + print(datetime.datetime.now(),"NUC_raw_thread: applying NUC") if self.give_ERROR: images,error_images=apply_NUC(images,self.gain,self.offset,self.gain_error,self.offset_error,self.give_ERROR) else: @@ -810,17 +851,20 @@ if fastDL: for i in range(len(images)): images[i]=(images[i]*self.FOV).astype(np.float32) - print(datetime.datetime.now(),"correcting bad pixels") - images=IR_tools.correct_images(images,self.badpixels) + if self.verbose>0: + print(datetime.datetime.now(),"NUC_raw_thread: correcting bad pixels") + images=IR_tools.correct_images(images,self.badpixels,verbose=self.verbose-1) self.resultlist.append([self.threadID,successes,time,images]) -def download_raw_FLIR_images_via_png(t1,t6,time_s=0,version=0,threads=1,verbose=0): +def download_raw_FLIR_images_via_png(t1,t6,time_window=0,version=0,threads=1,verbose=0): + """ + """ larchivepath=archivepath+"W7X/QSR07_FLIR/AEK51_raw" - if type(time_s)==list: - tstart=time_s[0] - tstop=time_s[1] + if type(time_window)==list: + tstart=time_window[0] + tstop=time_window[1] if tstop<tstart: - raise Exception("endtime before starttime") + raise Exception("download_raw_FLIR_images_via_png: endtime before starttime") stdate=datetime.datetime.utcfromtimestamp((t1-100)/1e9+tstart) stdate=stdate.isoformat() enddate=datetime.datetime.utcfromtimestamp((t1)/1e9+tstop) @@ -828,11 +872,11 @@ def download_raw_FLIR_images_via_png(t1,t6,time_s=0,version=0,threads=1,verbose= else: stdate=datetime.datetime.utcfromtimestamp((t1-100)/1e9) stdate=stdate.isoformat() - if time_s==0: + if time_window==0: enddate=datetime.datetime.utcfromtimestamp(t6/1e9) enddate=enddate.isoformat() else: - enddate=datetime.datetime.utcfromtimestamp((t1)/1e9+time_s) + enddate=datetime.datetime.utcfromtimestamp((t1)/1e9+time_window) enddate=enddate.isoformat() #"2017-11-15 08:00:00" typ1_version=get_latest_version("QSR07_FLIR/AEK51_raw_DATASTREAM") @@ -844,11 +888,11 @@ def download_raw_FLIR_images_via_png(t1,t6,time_s=0,version=0,threads=1,verbose= ### okay the data is stored probably as different sets (so recorded with FLIR software) timess,versions=get_FLIR_Pset_times(stdate,enddate,version) if verbose>0: - print("found ",np.shape(timess[0]),np.shape(timess[1]),np.shape(timess[2]),np.shape(timess[3])," frames in the different sets") + print("download_raw_FLIR_images_via_png: found {0} frames in the different sets".format([np.shape(x) for x in timess])) ### we not know the times for all presets for the program, now we have to download them, if times is not empty exist,time,frames,tsets=download_FLIR_Psets(larchivepath,timess,threads,versions,verbose=verbose-1) elif mode==2:##the data is stored only in one datastream, at least this datastream exist - raise Exception("not implemented") + raise Exception("download_raw_FLIR_images_via_png: not implemented") #download data and downlaod LUT # time=[] @@ -865,11 +909,13 @@ def download_raw_FLIR_images_via_png(t1,t6,time_s=0,version=0,threads=1,verbose= # success=False # return success,np.array(time),images else: - raise Exception("cannot identifie the way the searched data is stored!") + raise Exception("download_raw_FLIR_images_via_png: cannot identifie the way the searched data is stored!") return exist,time,frames,mode,tsets,versions def get_FLIR_Pset_times(stdate,enddate,version=0,verbose=0): + """ + """ larchivepath=archivepath+"W7X/QSR07_FLIR/AEK51_raw" if version==0: versions=[] @@ -880,18 +926,21 @@ def get_FLIR_Pset_times(stdate,enddate,version=0,verbose=0): else: versions=[version,version,version,version] timess=[] - if version==0:##no version specified search for data in all versions + if version==0: + if verbose>0: + print('get_FLIR_Pset_times: no version specified search for data in all versions') counter=-1 for version0 in versions: counter+=1 if version0>0: found=False while (version0>0 and not found): - try: - times_0=AKF_2.get_time_intervals(larchivepath+"_PS"+str(counter)+"_DATASTREAM/V"+str(version0)+"/0/raw",stdate.replace("T"," "),enddate.replace("T"," "))# + try: + url = '{0}_PS{1}_DATASTREAM/V{2}/0/raw'.format(larchivepath,counter,version0) + times_0=AKF_2.get_time_intervals(url,stdate.replace("T"," "),enddate.replace("T"," "))# found=True except Exception as E: - print(E,larchivepath+"_PS"+str(counter)+"_DATASTREAM/V"+str(version0)+"/0/raw") + print('get_FLIR_Pset_times: Error querrying {0}\n{1}'.format(url,E)) found=False times_0=[] version0=version0-1 @@ -902,15 +951,18 @@ def get_FLIR_Pset_times(stdate,enddate,version=0,verbose=0): counter=-1 for version0 in versions: counter+=1 - try: - times_0=AKF_2.get_time_intervals(larchivepath+"_PS"+str(counter)+"_DATASTREAM/V"+str(version0)+"/0/raw",stdate.replace("T"," "),enddate.replace("T"," "))# + try: + url = '{0}_PS{1}_DATASTREAM/V{2}/0/raw'.format(larchivepath,counter,version0) + times_0=AKF_2.get_time_intervals(url,stdate.replace("T"," "),enddate.replace("T"," "))# except Exception as E: - print(E,larchivepath+"_PS"+str(counter)+"_DATASTREAM/V"+str(version0)+"/0/raw") + print('get_FLIR_Pset_times: Error querrying {0}\n{1}'.format(url,E)) times_0=[] timess.append(times_0) return timess,versions def download_FLIR_Psets(larchivepath,timess,threads,versions,verbose=0): + """ + """ if len(timess[0])==len(timess[1]) and len(timess[2])==len(timess[3]) and len(timess[0])==0: exist=False time=[] @@ -918,7 +970,7 @@ def download_FLIR_Psets(larchivepath,timess,threads,versions,verbose=0): tsets=[] else:#timess is not empty, there is data in at least one Pset if verbose>0: - print(datetime.datetime.now(),"data found, start the download") + print(datetime.datetime.now(),"download_FLIR_Psets: data found, start the download") exist=True timesets=[] framess=[] @@ -964,14 +1016,14 @@ def download_FLIR_Psets(larchivepath,timess,threads,versions,verbose=0): resultdict = [] for i in range(threads): if verbose>0: - print("Start Thread ",i+1) + print("download_FLIR_Psets: Start Thread ",i+1) TH=download_images_thread(i,larchivepaths[i],tim[i],resultdict,versionss[i]) jobs.append(TH) TH.start() for p in jobs: p.join() if verbose>0: - print("all threads are done") + print("download_FLIR_Psets: all threads are done") order=[] for ele in resultdict: order.append(ele[0]) @@ -1009,19 +1061,24 @@ def download_FLIR_Psets(larchivepath,timess,threads,versions,verbose=0): tsets=[] return exist,time,frames,tsets -def download_last_raw_image_by_time(larchivepath,starttime,stoptime,version=0): +def download_last_raw_image_by_time(larchivepath,starttime,stoptime,version=0): + """ + """ try: - res = urllib.request.urlopen(larchivepath+"/_signal.png?from="+str(starttime-10)+"&upto="+str(stoptime)) + url='{0}/_signal.png?from={1}&upto='.format(larchivepath,starttime-10,stoptime) + res = urllib.request.urlopen(url) img = Image.open(BytesIO(res.read())) res.close() # pixelarray = np.array(img.getdata()).reshape(img.size[1], img.size[0]) pixelarray = np.array(img,dtype=np.uint16)#.swapaxes(0,1) return True, pixelarray except urllib.error.URLError as e: - print(e,larchivepath+"/_signal.png?from="+str(starttime-10)+"&upto="+str(stoptime)) + print('download_last_raw_image_by_time: Error querrying {0}\n{1}'.format(url,e)) return False, [-1] def download_raw_parlog_by_program(port,program,version=0): + """ + """ # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -1031,10 +1088,12 @@ def download_raw_parlog_by_program(port,program,version=0): stoptime=prog[0]['trigger']['6'][0] return download_raw_parlog_by_times(port,starttime,stoptime,version) except: - print("cannot find the program") + print("download_raw_parlog_by_program: Error! cannot find the program") return False,0,0 def download_raw_parlog_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=stoptime) if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(port)]+"raw_PARLOG") @@ -1045,13 +1104,15 @@ def download_raw_parlog_by_times(port,starttime,stoptime,version=0): res.close() goon=True except urllib.error.URLError as e: - print(e) + print('download_raw_parlog_by_times: Error! ',e) goon=False if goon: return True, signal_list['dimensions'],signal_list['values'][0]['meta-data'] def get_INFRATEC_filter_by_program(program,version=0): + """ + """ prog=get_program_from_PID(program) # try: # t_program = AKF_2.get_program_from_to(program) @@ -1061,10 +1122,12 @@ def get_INFRATEC_filter_by_program(program,version=0): stoptime=prog[1]['trigger']['6'][0] return get_INFRATEC_filter_by_times(starttime,stoptime,50,version) else:#except: - print("cannot find the program") + print("get_INFRATEC_filter_by_program: Error! cannot find the program") return False,0,0 def get_INFRATEC_filter_by_times(starttime,stoptime,port=50,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=starttime) if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(port)]+"raw_PARLOG") @@ -1075,7 +1138,7 @@ def get_INFRATEC_filter_by_times(starttime,stoptime,port=50,version=0): res.close() goon=True except urllib.error.URLError as e: - print(e) + print('get_INFRATEC_filter_by_times: Error! ',e) goon=False if goon: return True, signal_list['dimensions'],signal_list['values'][0]['meta-data']['filter'] @@ -1083,6 +1146,8 @@ def get_INFRATEC_filter_by_times(starttime,stoptime,port=50,version=0): return False, 0,-1 def get_exposure_by_program(port,program,version=0): + """ + """ prog=get_program_from_PID(program) # try: # t_program = AKF_2.get_program_from_to(program) @@ -1093,10 +1158,12 @@ def get_exposure_by_program(port,program,version=0): return get_exposure_by_times(port,starttime,stoptime,version) else: # except Exception as E: - print("cannot find the program, no exposure time available;")#, E) + print("get_exposure_by_program: Error! cannot find the program, no exposure time available;")#, E) return False,0,0 def get_exposure_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=stoptime) larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"meta_" stream=portpathdict[OP]["AEF"+str(port)]+"meta_" @@ -1105,6 +1172,8 @@ def get_exposure_by_times(port,starttime,stoptime,version=0): return read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/3/exposuretime"+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) def get_camera_temp_by_program(port,program,version=0): + """ + """ # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -1114,10 +1183,12 @@ def get_camera_temp_by_program(port,program,version=0): stoptime=prog[0]['trigger']['6'][0] return get_camera_temp_by_times(port,starttime,stoptime,version) except: - print("cannot find the program") + print("get_camera_temp_by_program: Error! cannot find the program") return False,0,0 def get_camera_temp_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=stoptime) stream=portpathdict[OP]["AEF"+str(port)]+"meta_" larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"meta_" @@ -1130,6 +1201,8 @@ def get_camera_temp_by_times(port,starttime,stoptime,version=0): return read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/1/cameratempetarure/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) def get_sensor_temp_by_program(port,program,version=0): + """ + """ prog=get_program_from_PID(program) # try: # t_program = AKF_2.get_program_from_to(program) @@ -1139,10 +1212,12 @@ def get_sensor_temp_by_program(port,program,version=0): stoptime=prog[1]['trigger']['6'][0] return get_sensor_temp_by_times(port,starttime,stoptime,version) else:#except: - print("cannot find the program") + print("get_sensor_temp_by_program: Error! cannot find the program") return False,0,0 def get_sensor_temp_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=stoptime) stream=portpathdict[OP]["AEF"+str(port)]+"meta_" larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"meta_" @@ -1151,6 +1226,8 @@ def get_sensor_temp_by_times(port,starttime,stoptime,version=0): return read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/0/sensortemperature/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) def get_camera_framerate_by_program(port,program,version=0): + """ + """ prog=get_program_from_PID(program) # try: # t_program = AKF_2.get_program_from_to(program) @@ -1161,10 +1238,12 @@ def get_camera_framerate_by_program(port,program,version=0): return get_camera_framerate_by_times(port,starttime,stoptime,version) # except: else: - print("cannot find the program") + print("get_camera_framerate_by_program: Error! cannot find the program") return False,0,0 def get_camera_framerate_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=stoptime) stream=portpathdict[OP]["AEF"+str(port)]+"meta_" larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"meta_" @@ -1174,6 +1253,8 @@ def get_camera_framerate_by_times(port,starttime,stoptime,version=0): def get_frametype_by_program(port,program,version=0): + """ + """ # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -1183,10 +1264,12 @@ def get_frametype_by_program(port,program,version=0): stoptime=prog[0]['trigger']['6'][0] return get_frametype_by_times(port,starttime,stoptime,version) except: - print("cannot find the program") + print("get_frametype_by_program: Error! cannot find the program") return False,0,0 def get_frametype_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=starttime) stream=portpathdict[OP]["AEF"+str(port)]+"meta_" larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"meta_" @@ -1195,6 +1278,8 @@ def get_frametype_by_times(port,starttime,stoptime,version=0): return read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/5/frametype/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) def get_framenumber_by_program(port,program,version=0): + """ + """ # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -1204,10 +1289,12 @@ def get_framenumber_by_program(port,program,version=0): stoptime=prog[0]['trigger']['6'][0] return get_framenumber_by_times(port,starttime,stoptime,version) except: - print("cannot find the program") + print("get_framenumber_by_program: Error! cannot find the program") return False,0,0 def get_framenumber_by_times(port,starttime,stoptime,version=0): + """ + """ OP=IR_tools.get_OP_by_time(time_ns=starttime) stream=portpathdict[OP]["AEF"+str(port)]+"meta_" larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(port)]+"meta_" @@ -1216,6 +1303,8 @@ def get_framenumber_by_times(port,starttime,stoptime,version=0): return read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/4/framenumber/_signal.json?from="+str(starttime)+"&upto="+str(stoptime)) def get_average_divertor_TC_for_camtemp(port,starttime,endtime,part="all",TC=0): + """ + """ urlstart="http://archive-webapi.ipp-hgw.mpg.de/ArchiveDB/raw/W7X/CoDaStationDesc.115/" if port==50 or port==51: urlstart=urlstart+"DataModuleDesc.19471_DATASTREAM/" @@ -1228,7 +1317,7 @@ def get_average_divertor_TC_for_camtemp(port,starttime,endtime,part="all",TC=0): portnr=port+1 offset=(port//10-1)*32+16 else: - print("unknown port!") + print("get_average_divertor_TC_for_camtemp: Error! unknown port") raise Exception if portnr==50: offset=0 @@ -1284,28 +1373,32 @@ def get_average_divertor_TC_for_camtemp(port,starttime,endtime,part="all",TC=0): time=0 success=False except Exception as E: - print(E) + print('get_average_divertor_TC_for_camtemp: Error! ',E) success=False try: return success,time,signal/n except Exception as E: - print(E) + print('get_average_divertor_TC_for_camtemp: Error! ',E) return False,0,-1 else: - print("same numbers!") + print("get_average_divertor_TC_for_camtemp: Error! rs and re are the same numbers") return False,0,-1 def get_divertor_TC_coordinate(cameraport,TC=0): + """ + """ from IR_config_constants import TC_database if cameraport %10==1:##uneven cameraport, even TC port portnr=cameraport-1 elif cameraport%10==0:##even cameraport, uneven TC port portnr=cameraport+1 else: - raise Exception("unknown port!") + raise Exception("get_divertor_TC_coordinate: unknown port!") return TC_database[portnr][TC] def get_TC_data_for_port_by_program(port,program,part=0,version=0): + """ + """ # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -1315,10 +1408,12 @@ def get_TC_data_for_port_by_program(port,program,part=0,version=0): stoptime=prog[0]['trigger']['6'][0] return get_TC_data_for_port(port,starttime,stoptime,part,version) except: - print("cannot find the program") + print("get_TC_data_for_port_by_program: Error! cannot find the program") return False,0,0 def get_TC_data_for_port(port,starttime,endtime,part=0,version=0): + """ + """ urlstart="http://archive-webapi.ipp-hgw.mpg.de/ArchiveDB/raw/W7X/CoDaStationDesc.115/DataModuleDesc.19471_DATASTREAM/" #66,67(10),72,73(20),78,79(30),84,85(40/50_2),90,91(50_1) n=0 @@ -1340,7 +1435,7 @@ def get_TC_data_for_port(port,starttime,endtime,part=0,version=0): else: success=False except Exception as E: - print(E) + print('get_TC_data_for_port: Error! ',E) success=False n=1 else: @@ -1356,20 +1451,21 @@ def get_TC_data_for_port(port,starttime,endtime,part=0,version=0): success=False n=1 except Exception as E: - print(E) + print('get_TC_data_for_port: Error! ',E) success=False n=1 except Exception as E: - print(E) + print('get_TC_data_for_port: Error! ',E) success=False n=1 except: success=False n=1 return success,time,signal/n -""" -end of download functions, below combination to get temperature -""" + + +#%% end of download functions, below combination of tools to get temperature + #def apply_LUT_to_images(LUT,images): # LUTd={LUT[0][i]: LUT[1][i] for i in range(0, len(LUT[0]))} @@ -1382,8 +1478,10 @@ def terror_from_LUT(LUT,images): return LUT[1,images] def check_temperature_range(time,maxvalue_C,minvalue_C,port,exposure,cfilter=0): + """ + """ try: - cam=portcamdict[get_OP_by_time(time)]['AEF'+str(port)] + cam=portcamdict[IR_tools.get_OP_by_time(time_ns=time)]['AEF'+str(port)] if port==50: ranges=camlimdict[cam][cfilter][exposure] else: @@ -1391,20 +1489,23 @@ def check_temperature_range(time,maxvalue_C,minvalue_C,port,exposure,cfilter=0): # if minvalue_C>=ranges[0] and maxvalue_C<=ranges[1]: # return True if minvalue_C<ranges[0] and maxvalue_C<=ranges[1]: - raise Exception("min value ({0:.2f}°C) is out of the valid temperature range (T>{1:.2f}°C)".format(minvalue_C,ranges[0])) + raise Exception("check_temperature_range: min value ({0:.2f}°C) is out of the valid temperature range (T>{1:.2f}°C)".format(minvalue_C,ranges[0])) # return False elif minvalue_C>=ranges[0] and maxvalue_C>ranges[1]: - raise Exception("max value ({0:.2f}°C) is out of the valid temperature range (T<{1:.2f}°C)".format(maxvalue_C,ranges[1])) + raise Exception("check_temperature_range: max value ({0:.2f}°C) is out of the valid temperature range (T<{1:.2f}°C)".format(maxvalue_C,ranges[1])) # return False elif minvalue_C<ranges[0] and maxvalue_C>ranges[1]: - raise Exception("max and min value ({0:.2f}°C,{1:.2f}°C) is out of the valid temperature range ({2:.2f}°C<T<{3:.2f}!C)".format(maxvalue_C,minvalue_C,ranges[0],ranges[1])) + raise Exception("check_temperature_range: max and min value ({0:.2f}°C,{1:.2f}°C) is out of the valid temperature range ({2:.2f}°C<T<{3:.2f}!C)".format(maxvalue_C,minvalue_C,ranges[0],ranges[1])) # return False except Exception as E: - raise Exception(E) + raise Exception('check_temperature_range: Error! '+E) -def get_temp_from_raw_by_program_V1(portnr,program,time_s=0,emi=0.8,divertorpart="all",version=0,intervalSize=1E9,give_ERROR=False): - background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,program,emi,1,version) +def get_temp_from_raw_by_program_V1(portnr,program,time_window=0,emi=0.8,divertorpart="all", + version=0,intervalSize=1E9,give_ERROR=False,verbose=0): + """ + """ + background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,program,emi,1,version,verbose=verbose-1) # prog=AKF_1.get_program_from_PID(program) try: t_program = AKF_2.get_program_from_to(program) @@ -1414,23 +1515,25 @@ def get_temp_from_raw_by_program_V1(portnr,program,time_s=0,emi=0.8,divertorpart t1=prog[0]['trigger']['1'][0] t6=prog[0]['trigger']['6'][0] starttime=t1-10 - if type(time_s)==list: - tstart=time_s[0] - tstop=time_s[1] + if type(time_window)==list: + tstart=time_window[0] + tstop=time_window[1] if tstop<tstart: - raise Exception("endtime before starttime") + raise Exception("get_temp_from_raw_by_program_V1: endtime before starttime") stoptime=int(starttime+tstop*1e9) starttime=int(starttime+tstart*1e9) else: - if time_s==0: + if time_window==0: stoptime=t6 else: - stoptime=int(starttime+time_s*1e9) + stoptime=int(starttime+time_window*1e9) success=True - print(datetime.datetime.now(),"Start download of raw images") + if verbose>0: + print(datetime.datetime.now(),"get_temp_from_raw_by_program_V1: Start download of raw images") if (stoptime-starttime)/intervalSize>1: nrinterv=int(np.ceil((stoptime-starttime)/intervalSize)) - print("timewindow to large, splitting into smaller fractions ("+str(nrinterv)+")") + if verbose>0: + print("get_temp_from_raw_by_program_V1: time_window too large, splitting into {0} smaller fractions".format(nrinterv)) intervalls=[] for i in range(nrinterv): intervalls.append(int(starttime-10+i*intervalSize)) @@ -1438,22 +1541,22 @@ def get_temp_from_raw_by_program_V1(portnr,program,time_s=0,emi=0.8,divertorpart temperatureimages=[] times=[] for i in range(nrinterv): - raw_dl=download_raw_images_by_times(portnr,intervalls[i],intervalls[i+1],version,intervalSize) - print(datetime.datetime.now(),"download of raw images part "+str(i+1)+" of "+str(nrinterv)+" finished") + raw_dl=download_raw_images_by_times(portnr,intervalls[i],intervalls[i+1],version,intervalSize,verbose=verbose-1) + print(datetime.datetime.now(),"get_temp_from_raw_by_program_V1: download of raw images part {0} of {1} finished".format(i+1, nrinterv)) if raw_dl[0]: time=raw_dl[1] images=raw_dl[2] del raw_dl # images=images.swapaxes(1,2) images=[im.swapaxes(0,1) for im in images] - sucess,images=apply_calib_on_raw(images,background,LUT,refT,gain,offset,True) + sucess,images=apply_calib_on_raw(images,background,LUT,refT,gain,offset,True,verbose=verbose-1) temperatureimages=temperatureimages+images times=times+time del time,images else: - raise Exception("cannot download the raw images") + raise Exception("get_temp_from_raw_by_program_V1: cannot download the raw images") else: - raw_dl=download_raw_images_by_times(portnr,starttime,stoptime,version,intervalSize) + raw_dl=download_raw_images_by_times(portnr,starttime,stoptime,version,intervalSize,verbose=verbose-1) if raw_dl[0]: times=raw_dl[1] temperatureimages=raw_dl[2] @@ -1461,17 +1564,17 @@ def get_temp_from_raw_by_program_V1(portnr,program,time_s=0,emi=0.8,divertorpart # temperatureimages=temperatureimages.swapaxes(1,2) temperatureimages=[im.swapaxes(0,1) for im in temperatureimages] if give_ERROR: - success,temperatureimages,error_images=apply_calib_on_raw(temperatureimages,background,LUT,refT,gain,offset,gain_error,offset_error,True,give_ERROR) + success,temperatureimages,error_images=apply_calib_on_raw(temperatureimages,background,LUT,refT,gain,offset,gain_error,offset_error,True,give_ERROR,verbose=verbose-1) else: - success,temperatureimages=apply_calib_on_raw(temperatureimages,background,LUT,refT,gain,offset,gain_error,offset_error,True,give_ERROR) + success,temperatureimages=apply_calib_on_raw(temperatureimages,background,LUT,refT,gain,offset,gain_error,offset_error,True,give_ERROR,verbose=verbose-1) else: - raise Exception("cannot download the raw images") + raise Exception("get_temp_from_raw_by_program_V1: cannot download the raw images") valid=True for i in range(len(temperatureimages)): try: check_temperature_range(times[0],np.max(temperatureimages)-273.15,np.min(temperatureimages)-273.15,portnr,t_exp,cfilter) except Exception as E: - print(E) + print("get_temp_from_raw_by_program_V1: Error! ",E) valid=False i=len(temperatureimages) if give_ERROR: @@ -1479,13 +1582,26 @@ def get_temp_from_raw_by_program_V1(portnr,program,time_s=0,emi=0.8,divertorpart else: return success,times,temperatureimages,valid except: - print("cannot find program") + print("get_temp_from_raw_by_program_V1: Error! cannot find program") return False,0,-1,False -def get_temp_from_raw_by_program_V2(portnr,program,time_s=0,emi=0.8,version=0,threads=1,give_ERROR=False,use_firstframe_as_background=False): - return get_temp_from_raw_by_program(portnr,program,time_s=time_s,emi=emi,T_version=2,version=version,threads=threads,give_ERROR=give_ERROR,use_firstframe_as_background=use_firstframe_as_background) +def get_temp_from_raw_by_program_V2(portnr,program,time_window=0,emi=0.8,version=0, + threads=1,give_ERROR=False,use_firstframe_as_background=False, + verbose=0): + """ + """ + return get_temp_from_raw_by_program(portnr,program,time_window=time_window, + emi=emi,T_version=2,version=version, + threads=threads,give_ERROR=give_ERROR, + use_firstframe_as_background=use_firstframe_as_background, + verbose=verbose-1) -def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,version=0,threads=1,give_ERROR=False,use_firstframe_as_background=False,back_emissivity=0.8,verbose=0): +def get_temp_from_raw_by_program(portnr,program,time_window=0,emi=0.8,T_version=2, + version=0,threads=1,give_ERROR=False, + use_firstframe_as_background=False,back_emissivity=0.8, + verbose=0): + """ + """ FLIR=False prog=get_program_from_PID(program) if prog[0]: @@ -1493,7 +1609,7 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver t1=prog[1]['trigger']['1'][0] t6=prog[1]['trigger']['6'][0] success=True - OP=get_OP_by_time(t1) + OP=IR_tools.get_OP_by_time(time_ns=t1) if type(portnr) is str: try: @@ -1505,17 +1621,17 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver portnr=int(portnr.split("AEF")[1]) goon=True except Exception as E: - raise Exception("unknown Port!"+E) + raise Exception("get_temp_from_raw_by_program: unknown Port!"+E) elif type(portnr) is int: FLIR=False goon=True else: goon=False if not goon: - raise Exception("the given port is neither a number or a valid String!") + raise Exception("get_temp_from_raw_by_program: the given port is neither a number or a valid String!") else: if FLIR: - exist,time,frames,mode,tsets,versions=download_raw_FLIR_images_via_png(t1=t1,t6=t6,time_s=time_s,version=version,threads=threads,verbose=verbose-1) + exist,time,frames,mode,tsets,versions=download_raw_FLIR_images_via_png(t1=t1,t6=t6,time_window=time_window,version=version,threads=threads,verbose=verbose-1) if exist: if mode==1: Psets=IR_tools.check_dublicates(tsets) @@ -1562,9 +1678,9 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver else: Radi_Co.append([]) Temp_Co.append([]) - ### got the raw, knowing which frame needs which calibration, got the calibration, not we have to transform it + ### got the raw, knowing which frame needs which calibration, got the calibration, now we have to transform it if verbose>0: - print(datetime.datetime.now(),"Converting the raw data into temperature, number of frames:",len(frames)) + print(datetime.datetime.now(),"get_temp_from_raw_by_program: Converting the raw data into temperature, number of frames:",len(frames)) for i in range(len(frames)): try: frames[i]=Radi_Co[tsets[i]][0]+frames[i]*Radi_Co[tsets[i]][1] @@ -1574,44 +1690,45 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver temp=temp+Temp_Co[tsets[i]][expo]*np.power(dummy,expo) frames[i]=temp+273.15 except Exception as E: - print(E,i,len(tsets),Radi_Co[tsets[i]],Temp_Co[tsets[i]]) + print('get_temp_from_raw_by_program: Error in frame {0}! {1}'.format(i,E)) + print(' (len(tsets) {0},Radi_Co[tsets[i]] {1},Temp_Co[tsets[i]] {2})'.format(len(tsets),Radi_Co[tsets[i]],Temp_Co[tsets[i]])) if verbose>0: - print(datetime.datetime.now(),"temperature frames done") + print(datetime.datetime.now(),"get_temp_from_raw_by_program: temperature frames done") return exist,time,frames,tsets elif mode==2: - raise Exception("not implemented") + raise Exception("get_temp_from_raw_by_program: not implemented in mode 2") else: - raise Exception("FLIR evaluation, This case should not be possible") + raise Exception("get_temp_from_raw_by_program: FLIR evaluation, This case should not be possible.") else: - print("FLIR data not found") + print("get_temp_from_raw_by_program: Error! FLIR data not found") return False,[0],[0],[] else: cexist,background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,t0,t1,t6,emi,T_version,version,back_emissivity,verbose=verbose-1) if not cexist: if verbose>0: - print("Unable to load the complete calibration data for",program," please confirm that this data was uploaded") + print("get_temp_from_raw_by_program: Unable to load the complete calibration data for",program," please confirm that this data was uploaded") return False,[0],[0],False else: if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(portnr)]+"raw_DATASTREAM") larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(portnr)]+"raw_DATASTREAM/V"+str(version)+"/0/raw" if verbose>0: - print(datetime.datetime.now(),"raw download start") + print(datetime.datetime.now(),"get_temp_from_raw_by_program: raw download start") if fastDL: - exist,time,frames=download_images_by_time_via_png(larchivepath,starttime=t1,stoptime=t6,time_s=time_s,version=version,threads=threads,verbose=verbose-1) + exist,time,frames=download_images_by_time_via_png(larchivepath,starttime=t1,stoptime=t6,time_window=time_window,version=version,threads=threads,verbose=verbose-1) else: - exist,time,frames=download_raw_images_by_program(portnr,program,time_s,version,verbose=verbose-1) + exist,time,frames=download_raw_images_by_program(portnr,program,time_window,version,verbose=verbose-1) if exist: frames=[im.swapaxes(0,1) for im in frames] else: - raise Exception("no data found") + raise Exception("get_temp_from_raw_by_program: no data found") if not exist: - raise Warning("data not found in database!") + raise Warning("get_temp_from_raw_by_program: data not found in database!") return False,[0],[0],False else: if verbose>0: - print(datetime.datetime.now(),"raw download finished") + print(datetime.datetime.now(),"get_temp_from_raw_by_program: raw download finished") FOV=get_FOV_mask(portnr) if use_firstframe_as_background: bim=apply_NUC([frames[0]],gain,offset) @@ -1620,7 +1737,8 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver success,frames,error_images=apply_calib_on_raw(frames,background,LUT,refT,gain,offset,gain_error,offset_error,False,give_ERROR,verbose=verbose-1) else: success,frames=apply_calib_on_raw(frames,background,LUT,refT,gain,offset,gain_error,offset_error,False,give_ERROR,verbose=verbose-1) - print(datetime.datetime.now(),"Temperature calculation done") + if verbose>0: + print(datetime.datetime.now(),"get_temp_from_raw_by_program: Temperature calculation done") if not success: if verbose>0: print("calculation of the temperature failed, calibration could not be applied") @@ -1630,10 +1748,10 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver for i in range(len(frames)): frames[i]=(frames[i]*FOV).astype(np.float32) if verbose>0: - print(datetime.datetime.now(),"correcting bad pixels") + print(datetime.datetime.now(),"get_temp_from_raw_by_program: correcting bad pixels") frames=IR_tools.correct_images(frames,badpixels) if verbose>0: - print(datetime.datetime.now(),"checking temperaturerange") + print(datetime.datetime.now(),"get_temp_from_raw_by_program: checking temperaturerange") valid=True # for i in range(len(frames)): # try: @@ -1650,18 +1768,24 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver starttime=t1#prog[1]['trigger']['1'][0] toff=time[0]-starttime if toff>10e6: - print("time offset detected, offset is "+str(toff)+" ns, correcting") + print("get_temp_from_raw_by_program: time offset of {0}ns detected, correcting".format(toff)) time[:]=time[:]-toff if give_ERROR: return exist,time,frames,valid,error_images else: return exist,time,frames,valid else: - raise Exception("unknown Program") + raise Exception("get_temp_from_raw_by_program: unknown Program") -def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_version=2,version=0,threads=1,give_ERROR=False,check_range=True,verbose=0): +def get_temp_from_raw_by_program_fullthreads(portnr,program,time_window=0,emi=0.8, + T_version=2,version=0,threads=1, + give_ERROR=False,check_range=True,verbose=0): + """ + """ if threads==1 or not fastDL: - return get_temp_from_raw_by_program(portnr,program,time_s,emi,T_version,version,threads,give_ERROR) + return get_temp_from_raw_by_program(portnr,program,time_window,emi, + T_version,version,threads,give_ERROR, + verbose=verbose-1) else: if type(portnr) is str: try: @@ -1674,7 +1798,7 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v portnr=int(portnr.split("AEF")[1]) goon=True except Exception as E: - raise Exception("unknown Port!"+E) + raise Exception("get_temp_from_raw_by_program_fullthreads: unknown Port!"+E) elif type(portnr) is int: FLIR=False goon=True @@ -1695,15 +1819,15 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v starttime=prog[1]['trigger']['1'][0] stoptime=prog[1]['trigger']['6'][0] success=True - OP=get_OP_by_time(starttime) + OP=IR_tools.get_OP_by_time(time_ns=starttime) if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(portnr)]+"raw_DATASTREAM") larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(portnr)]+"raw_DATASTREAM/V"+str(version)+"/0/raw" - if type(time_s)==list: - tstart=time_s[0] - tstop=time_s[1] + if type(time_window)==list: + tstart=time_window[0] + tstop=time_window[1] if tstop<tstart: - raise Exception("endtime before starttime") + raise Exception("get_temp_from_raw_by_program_fullthreads: endtime before starttime") stdate=datetime.datetime.utcfromtimestamp((starttime-100)/1e9+tstart) stdate=stdate.isoformat() enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+tstop) @@ -1711,11 +1835,11 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v else: stdate=datetime.datetime.utcfromtimestamp((starttime-100)/1e9) stdate=stdate.isoformat() - if time_s==0: + if time_window==0: enddate=datetime.datetime.utcfromtimestamp(stoptime/1e9) enddate=enddate.isoformat() else: - enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+time_s) + enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+time_window) enddate=enddate.isoformat() times=AKF_2.get_time_intervals(larchivepath,stdate.replace("T"," "),enddate.replace("T"," "))# @@ -1731,14 +1855,19 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v jobs = [] resultdict = [] for i in range(threads): - print("Start Thread ",i+1) + if verbose>0: + print("get_temp_from_raw_by_program_fullthreads: Start Thread ",i+1) # p = multiprocessing.Process(target=convert_raw_to_temp_thread, args=(larchivepath,tim[intervalls[i]:intervalls[i+1]],out_q,i,version,background,LUT,refT,gain,offset,gain_error,offset_error,give_ERROR,FOV,badpixels,)) - p=convert_raw_to_temp_thread(larchivepath,tim[intervalls[i]:intervalls[i+1]],resultdict,i,version,background,LUT,refT,gain,offset,gain_error,offset_error,give_ERROR,FOV,badpixels) + p=convert_raw_to_temp_thread(larchivepath,tim[intervalls[i]:intervalls[i+1]], + resultdict,i,version,background,LUT, + refT,gain,offset,gain_error,offset_error, + give_ERROR,FOV,badpixels,verbose=verbose-1) jobs.append(p) p.start() for p in jobs: p.join() - print("all threads are done") + if verbose>0: + print("get_temp_from_raw_by_program_fullthreads: all threads are done") order=[] for ele in resultdict: order.append(ele[0]) @@ -1751,13 +1880,14 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v times=times+resultdict[order.index(i)][2] del resultdict if check_range: - print(datetime.datetime.now(),"checking temperaturerange") + if verbose>0: + print(datetime.datetime.now(),"get_temp_from_raw_by_program_fullthreads: checking temperaturerange") valid=True for i in range(len(images)): try: check_temperature_range(times[0],np.max(images[i])-273.15,np.min(images[i][np.nonzero(images[i])])-273.15,portnr,t_exp,cfilter) except Exception as E: - print(E) + print('get_temp_from_raw_by_program_fullthreads: Error! ',E) valid=False i=len(images) else: @@ -1774,18 +1904,23 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v else: return success,np.array(times),images,valid -def get_nuced_raw_by_program(portnr,program,time_s=0,version=0,threads=1,give_ERROR=False): - background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,program,emi=1,T_version=2,version=version) - print(datetime.datetime.now(),"raw download start") +def get_nuced_raw_by_program(portnr,program,time_window=0,version=0,threads=1, + give_ERROR=False,verbose=0): + """ + """ + background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,program,emi=1,T_version=2,version=version,verbose=verbose-1) + if verbose>0: + print(datetime.datetime.now(),"get_nuced_raw_by_program: raw download start") if fastDL: - exist,time,frames=download_raw_images_by_program_via_png(portnr,program,time_s,version,threads) + exist,time,frames=download_raw_images_by_program_via_png(portnr,program,time_window,version,threads,verbose=verbose-1) else: - exist,time,frames=download_raw_images_by_program(portnr,program,time_s,version) + exist,time,frames=download_raw_images_by_program(portnr,program,time_window,version,verbose=verbose-1) if exist: frames=[im.swapaxes(0,1) for im in frames] else: - raise Exception("no data found") - print(datetime.datetime.now(),"raw download finished") + raise Exception("get_nuced_raw_by_program: no data found") + if verbose>0: + print(datetime.datetime.now(),"get_nuced_raw_by_program: raw download finished") FOV=get_FOV_mask(portnr) if give_ERROR: frames,frames_error=apply_NUC(frames,gain,offset,gain_error,offset_error,give_ERROR) @@ -1794,9 +1929,11 @@ def get_nuced_raw_by_program(portnr,program,time_s=0,version=0,threads=1,give_ER for i in range(len(frames)): frames[i]=(frames[i]*FOV).astype(np.float32) - print(datetime.datetime.now(),"correcting bad pixels") - frames=IR_tools.correct_images(frames,badpixels) - print(datetime.datetime.now(),"checking temperaturerange") + if verbose>0: + print(datetime.datetime.now(),"get_nuced_raw_by_program: correcting bad pixels") + frames=IR_tools.correct_images(frames,badpixels,verbose=verbose-1) + if verbose>0: + print(datetime.datetime.now(),"get_nuced_raw_by_program: checking temperaturerange") valid=True if give_ERROR: @@ -1804,27 +1941,32 @@ def get_nuced_raw_by_program(portnr,program,time_s=0,version=0,threads=1,give_ER else: return exist,time,frames,valid -def get_nuced_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_version=2,version=1,threads=1,give_ERROR=False,check_range=True): +def get_nuced_raw_by_program_fullthreads(portnr,program,time_window=0,emi=0.8, + T_version=2,version=1,threads=1, + give_ERROR=False,check_range=True,verbose=0): + """ + """ prog=get_program_from_PID(program) if prog[0]: t0=prog[1]['trigger']['0'][0] t1=prog[1]['trigger']['1'][0] t6=prog[1]['trigger']['6'][0] if threads==1 or not fastDL: - return get_nuced_raw_by_program(portnr,program,time_s,emi,T_version,version,threads,give_ERROR) + return get_nuced_raw_by_program(portnr,program,time_window,emi,T_version, + version,threads,give_ERROR,verbose=verbose-1) else: - exist,background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,t0,t1,t6,emi,T_version,version) + exist,background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,t0,t1,t6,emi,T_version,version,verbose=verbose-1) FOV=get_FOV_mask(portnr) success=True - OP=get_OP_by_time(t1) + OP=IR_tools.get_OP_by_time(time_ns=t1) if version==0: version=get_latest_version(portpathdict[OP]["AEF"+str(portnr)]+"raw_DATASTREAM") larchivepath=archivepath+"W7X/"+portpathdict[OP]["AEF"+str(portnr)]+"raw_DATASTREAM/V"+str(version)+"/0/raw" - if type(time_s)==list: - tstart=time_s[0] - tstop=time_s[1] + if type(time_window)==list: + tstart=time_window[0] + tstop=time_window[1] if tstop<tstart: - raise Exception("endtime before starttime") + raise Exception("get_nuced_raw_by_program_fullthreads: endtime before starttime") stdate=datetime.datetime.utcfromtimestamp((t1-100)/1e9+tstart) stdate=stdate.isoformat() enddate=datetime.datetime.utcfromtimestamp((t1)/1e9+tstop) @@ -1832,11 +1974,11 @@ def get_nuced_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_versi else: stdate=datetime.datetime.utcfromtimestamp((t1-100)/1e9) stdate=stdate.isoformat() - if time_s==0: + if time_window==0: enddate=datetime.datetime.utcfromtimestamp(t6/1e9) enddate=enddate.isoformat() else: - enddate=datetime.datetime.utcfromtimestamp((t1)/1e9+time_s) + enddate=datetime.datetime.utcfromtimestamp((t1)/1e9+time_window) enddate=enddate.isoformat() times=AKF_2.get_time_intervals(larchivepath,stdate.replace("T"," "),enddate.replace("T"," "))# lnt=len(times) @@ -1851,14 +1993,16 @@ def get_nuced_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_versi jobs = [] resultdict = [] for i in range(threads): - print("Start Thread ",i+1) + if verbose>0: + print("get_nuced_raw_by_program_fullthreads: Start Thread ",i+1) # p = multiprocessing.Process(target=NUC_raw_thread, args=(portnr,tim[intervalls[i]:intervalls[i+1]],out_q,i,version,background,LUT,refT,gain,offset,gain_error,offset_error,give_ERROR,FOV,badpixels,)) p = NUC_raw_thread(larchivepath,tim[intervalls[i]:intervalls[i+1]],resultdict,i,version,background,LUT,refT,gain,offset,gain_error,offset_error,give_ERROR,FOV,badpixels) jobs.append(p) p.start() for p in jobs: p.join() - print("all threads are done") + if verbose>0: + print("get_nuced_raw_by_program_fullthreads: all threads are done") order=[] for ele in resultdict: order.append(ele[0]) @@ -1884,7 +2028,8 @@ def get_nuced_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_versi else: return success,np.array(times),images,valid -def apply_calib_on_raw(images,background,LUT,refT=28.5,gain=0,offset=0,gain_error=0,offset_error=0,fullbackground=False,give_ERROR=False, verbose=0): +def apply_calib_on_raw(images,background,LUT,refT=28.5,gain=0,offset=0,gain_error=0, + offset_error=0,fullbackground=False,give_ERROR=False,verbose=0): """ apply_calib_on_raw return success,images and if give_Error: errorimages @@ -1895,7 +2040,7 @@ def apply_calib_on_raw(images,background,LUT,refT=28.5,gain=0,offset=0,gain_erro # images=images.swapaxes(1,2) if type(gain)!=int and type(offset)!=int: if verbose>0: - print(datetime.datetime.now(),"NUCing") + print(datetime.datetime.now(),"apply_calib_on_raw: NUCing") # eliminate bad offset and gain points offset[offset==np.inf] = 0 offset[offset==-np.inf] = 0 @@ -1907,7 +2052,7 @@ def apply_calib_on_raw(images,background,LUT,refT=28.5,gain=0,offset=0,gain_erro else: images=apply_NUC(images,gain,offset) if verbose>0: - print(datetime.datetime.now(),"background treatment") + print(datetime.datetime.now(),"apply_calib_on_raw: background treatment") # if fullbackground: #sbackground=np.zeros(np.shape(images[0]),dtype=np.float32)+background for i in range(len(images)): @@ -1918,7 +2063,7 @@ def apply_calib_on_raw(images,background,LUT,refT=28.5,gain=0,offset=0,gain_erro # images=np.array(images.clip(min=0),dtype=np.uint16) # images=[np.round(im.clip(min=0)).astype(np.uint16) for im in images] if verbose>0: - print(datetime.datetime.now(),"applying LUT") + print(datetime.datetime.now(),"apply_calib_on_raw: applying LUT") LUT=np.array([LUT[1],LUT[2]]) if give_ERROR: terror=[]#np.zeros(np.shape(images)) @@ -1938,7 +2083,7 @@ def apply_calib_on_raw(images,background,LUT,refT=28.5,gain=0,offset=0,gain_erro images[i]=(images[i]+(refT+273.15)).astype(np.float32) return True,images except Exception as E: - raise Warning(E) + raise Warning('apply_calib_on_raw: '+E) return False,[0] @@ -1960,11 +2105,12 @@ def apply_NUC(images,gain,offset,gain_error=0,offset_error=0,give_error=False): images[i]=(images[i]+offset).astype(np.float32) return images except Exception as E: - print("error") - print(E) + print("apply_NUC: Error! ", E) return 0 def get_cooling_water_temperature_by_time(time_ns): + """ + """ starttime=int(time_ns-3e9) endtime=int(time_ns+3e9) outlet_url="95/Ist%20Av06_ABK10_CT006" @@ -1975,10 +2121,10 @@ def get_cooling_water_temperature_by_time(time_ns): if result_in[0]: inlet=np.mean(np.array(result_in[2])) else: - print("inlet water temperature not found",result_in) + print("get_cooling_water_temperature_by_time: inlet water temperature not found in: ",result_in) inlet=0 except: - print("inlet water temperature not found") + print("get_cooling_water_temperature_by_time: inlet water temperature not found") inlet=0 result_in=[False,0,0] try: @@ -1986,15 +2132,15 @@ def get_cooling_water_temperature_by_time(time_ns): if result_out[0]: outlet=np.mean(np.array(result_out[2])) else: - print("outlet water temperature not found",result_out) + print("get_cooling_water_temperature_by_time: outlet water temperature not found in: ",result_out) outlet=0 except: - print("outlet water temperature not found") + print("get_cooling_water_temperature_by_time: outlet water temperature not found") inlet=0 result_out=[False,0,0] return result_in[0] and result_out[0],inlet,outlet -def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivity=0.82, verbose=0): +def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivity=0.82,verbose=0): """ get_calib_data return exist,background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error @@ -2015,7 +2161,8 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi # else: # raise Warning("cannot find the program!") # return False,0,0,0,0,0,0,0,0,0,0 - print(OP,port) + if verbose>0: + print('get_calib_data: loading data for camera {1} in {0}'.format(OP,port)) Camera=portcamdict[OP]['AEF'+str(port)] if Camera.split("_")[0]=="Infratec": # cf=get_INFRATEC_filter_by_program(program,version) @@ -2024,7 +2171,7 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi cfilter=cf[2] else: cfilter=-1 - raise Warning("Filter not found for INFRATEC Camera for Program "+AKF_2.get_program_id(t0)) + raise Warning("get_calib_data: Filter not found for INFRATEC Camera for Program "+AKF_2.get_program_id(t0)) return False,0,0,0,0,0,0,0,0,0,0 else: cfilter=0 @@ -2040,7 +2187,7 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi if cfilter==0 and not Camera.split("_")[0]=="Infratec": NUC_DL=get_NUC_by_times(port,t0,t1,t_exp,version,verbose=verbose-1)#download_NUC_by_times(port,t0,t1,t_exp,version) if NUC_DL[0]==False: - raise Warning("NUC was not found") + raise Warning("get_calib_data: NUC was not found") return False,0,LUT,0,0,0,0,t_exp,cfilter,0,0 else: gain=np.array(NUC_DL[1][0]) @@ -2050,13 +2197,13 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi offset_error=np.array(NUC_DL[1][5]) badpixels=np.array(NUC_DL[1][3],dtype=np.ubyte) if ((port == 11) or (port == 21)) and OP=="OP1.2a": - exist,time,frames=download_raw_images_by_times(port,t1,int(t1+0.02*1e9),version) + exist,time,frames=download_raw_images_by_times(port,t1,int(t1+0.02*1e9),version,verbose=verbose-1) if exist: # frames=[im.swapaxes(0,1) for im in frames]#somehow the archive gives it now already swapped back (09.07.2018) bim=apply_NUC([frames[0]],gain,offset) background=get_average_background_recangle(port,bim[0]) else: - raise Warning("cannot find the first frame of the discharge, reconstruction failed") + raise Warning("get_calib_data: cannot find the first frame of the discharge, reconstruction failed") return False,0,LUT,0,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error else: gain=0 @@ -2067,14 +2214,14 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi if OP=="OP1.2a": back_DL=download_background_by_times(port,t0,t1,t_exp,cfilter,version) elif OP=="OP1.2b": - back_DL=get_NUCed_background_by_times(port,t0,t1,t_exp,cfilter,gain,offset,version) + back_DL=get_NUCed_background_by_times(port,t0,t1,t_exp,cfilter,gain,offset,version,verbose=verbose-1) else: - raise Exception("unknown Operation Phase or calibration method not implemented for this campaign") + raise Exception("get_calib_data: unknown Operation Phase or calibration method not implemented for this campaign") if back_DL[0]: background=back_DL[2] if Temp_V==1: if verbose>0: - print('use temperature calibration version 1') + print('get_calib_data: use temperature calibration version 1') backtime=back_DL[1] backtime=backtime.tolist() divertorpart="all" @@ -2086,10 +2233,10 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi if TCT_dl[0]: refT=np.average(TCT_dl[2]) else: - raise Exception("Unable to find thermocouple data") + raise Exception("get_calib_data: Unable to find thermocouple data") elif Temp_V==2: if verbose>0: - print('use temperature calibration version 2') + print('get_calib_data: use temperature calibration version 2') frame=background.copy() background=get_average_background_recangle(port,background) gotit,waterin,waterout=get_cooling_water_temperature_by_time(t1) @@ -2099,8 +2246,7 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi refT=28.5 elif Temp_V==3: if verbose>0: - print('use temperature calibration version 3, TEST CASE!!!') - print('use temperature calibration version 3, TEST CASE!!!') + print('get_calib_data: use temperature calibration version 3, TEST CASE!!!') frame=background.copy() background=get_average_background_recangle(port,background) refT=28.5 @@ -2108,7 +2254,7 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi LUT_DL_wall = read_LUT_from_file(port, time, t_exp, 0.45, cfilter, verbose=verbose-1) if LUT_DL_wall[0]: if verbose>0: - print('got the wall LUT') + print('get_calib_data: got the wall LUT') LUT_wall = LUT_DL_wall[1] index=IR_tools.find_nearest(LUT_wall[1],refT) background=background-LUT_wall[0][index] @@ -2117,34 +2263,34 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi LUT_DL = read_LUT_from_file(port, time, t_exp, emissivity, cfilter, verbose=verbose-1) if LUT_DL[0]: if verbose>0: - print('got the V3 LUT') + print('get_calib_data: got the V3 LUT') LUT = LUT_DL[1] del LUT_DL # back_off=estimate_offset(port,program) # background=(background-back_off)/(back_emissivity)+back_off else: - raise Warning("Unknown temperature calibration method") + raise Warning("get_calib_data: Unknown temperature calibration method") return False,background,LUT,0,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error else: - raise Warning("no background image found") + raise Warning("get_calib_data: no background image found") return False,0,LUT,0,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error else: - raise Warning("no LUT found") + raise Warning("get_calib_data: no LUT found") return False,0,0,0,0,0,0,t_exp,cfilter,0,0 else: - raise Warning("no exposure time found") + raise Warning("get_calib_data: no exposure time found") return False,0,0,0,0,0,0,0,cfilter,0,0 if np.max(badpixels)==0: if Camera.split("_")[0]=="Infratec": - badpixels=find_badpixels(port,frame-background,offset,niterations=10,tolerance=10,plot_it=False)#find_badpixels(port,gain,offset) + badpixels=find_badpixels(port,frame-background,offset,niterations=10,tolerance=10,plot_it=False,verbose=verbose-1)#find_badpixels(port,gain,offset) else: if verbose>0: - print(datetime.datetime.now(),"Scanning for bad pixel") + print(datetime.datetime.now(),"get_calib_data: Scanning for bad pixel") # initial list from config files init_bp_list = IR_tools.read_bad_pixels_from_file(port, time_ns=t1) # find more bad pixel - badpixels = find_badpixels(port, gain, offset, init_bp_list=init_bp_list, niterations=10, tolerance=10, verbose=verbose-1) + badpixels = find_badpixels(port, gain, offset, init_bp_list=init_bp_list, niterations=10, tolerance=10,verbose=verbose-1) return exist,background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error # except Exception as E: # print("Warning: Program was not found: ",E) @@ -2153,6 +2299,8 @@ def get_calib_data(port,t0,t1,t6,emissivity=0.8,Temp_V=2,version=0,back_emissivi def find_badpixels(port, gain, offset, init_bp_list=None, niterations=3, tolerance=10, plot_it=False, verbose=0): + """ + """ badpixels = np.zeros(np.shape(gain)) # certain_bads=np.zeros(np.shape(gain)) # gainmax=12#100 @@ -2162,22 +2310,22 @@ def find_badpixels(port, gain, offset, init_bp_list=None, niterations=3, # take initial bad pixels into account if init_bp_list is not None: if verbose>0: - print("use {0} initial bad pixels from file".format(len(init_bp_list))) + print("find_badpixels: use {0} initial bad pixels from file".format(len(init_bp_list))) for pix in init_bp_list: try: badpixels[pix] = 1 except Exception as E: - Warning(E) - gain = IR_tools.restore_bad_pixels(gain*FOV, init_bp_list) + raise Warning('find_badpixels: '+E) + gain = IR_tools.restore_bad_pixels(gain*FOV, init_bp_list, verbose=verbose-1) last_number=0 finished=False n=0 while (n<=niterations and not finished):#len(badlist)>=last_number): badlist=IR_tools.find_outlier_pixels(gain*FOV,plot_it=plot_it,tolerance=tolerance) - gain=IR_tools.restore_bad_pixels(gain*FOV,badlist) + gain=IR_tools.restore_bad_pixels(gain*FOV,badlist, verbose=verbose-1) if verbose>0: - print("number of found bad pixels: ",len(badlist)) + print("find_badpixels: number of found bad pixels: ",len(badlist)) n+=1 if len(badlist)>=last_number and n>2: finished=True @@ -2192,15 +2340,17 @@ def find_badpixels(port, gain, offset, init_bp_list=None, niterations=3, badpixels+=badpixels*((1-FOV)*2) return np.array(badpixels,dtype=np.ubyte) -def make_FOV_mask(port): - points=valid_FOV_circle[port] +def make_FOV_mask(port): """ - Sekante erreichnen für Kreis, Gerade ax+by=c, sonderfall: y=c, a=0,b=1 - Kreis definiert durch (x-x0)²+(y-y0)²=r² - loesung: d=c-a*x0-b*y0=c-y0 - x1,2=x0+(ad+-b*sqrt(r²(a²+b²)-d²))/(a²+b²)=x0+-sqrt(r²-d²) - y1,2=y0+(bd-+a*sqrt(r²(a²+b²)-d²))/(a²+b²)=y0+d=c=y """ + points=valid_FOV_circle[port] + + # Sekante errechnen für Kreis, Gerade ax+by=c, sonderfall: y=c, a=0,b=1 + # Kreis definiert durch (x-x0)²+(y-y0)²=r² + # loesung: d=c-a*x0-b*y0=c-y0 + # x1,2=x0+(ad+-b*sqrt(r²(a²+b²)-d²))/(a²+b²)=x0+-sqrt(r²-d²) + # y1,2=y0+(bd-+a*sqrt(r²(a²+b²)-d²))/(a²+b²)=y0+d=c=y + y0=points[1] x0=points[0] r1=points[2] @@ -2230,9 +2380,11 @@ def make_FOV_mask(port): else: plt.imsave(config_path+portcamdict['OP1.2a']['AEF'+str(port)]+"\\"+"AEF"+str(port)+"_FOV.png",FOV) else: - raise Exception("cannot find the background frame to create the FOV file") + raise Exception("make_FOV_mask: cannot find the background frame to create the FOV file") def get_FOV_mask(port): + """ + """ try: if config_path=="": FOV=plt.imread("AEF"+str(port)+"_FOV.png")[:,:,0] @@ -2248,26 +2400,32 @@ def get_FOV_mask(port): return FOV def get_background(port,image): + """ + """ try: # points=valid_background_rectangle[port] FOV=get_FOV_mask(port) dummy=FOV*image#image[points[1]:points[3],points[0]:points[2]] return np.min(dummy[np.nonzero(dummy)]) except Exception as E: - print(E) + print('get_background: Error! ',E) return 0 def get_average_background_recangle(port,image): + """ + """ try: points=valid_background_rectangle[port] # FOV=get_FOV_mask(port) dummy=image[points[1]:points[3],points[0]:points[2]]#FOV*image# return np.min(dummy[np.nonzero(dummy)]) except Exception as E: - print(E) + print('get_average_background_recangle: Error! ',E) return 0 -def estimate_offset(port,program,plot_it=False): +def estimate_offset(port,program,plot_it=False,verbose=0): + """ + """ explist=[1,2,3,4,5,6,7,8,9] meanlist=[] exlist=[] @@ -2286,16 +2444,19 @@ def estimate_offset(port,program,plot_it=False): plt.plot(exlist,meanlist,'b*') plt.plot(exlist,np.array(exlist)*slope+intercept,'k-') plt.show() - print("slope, intercept, r_value, p_value, std_err",slope, intercept, r_value, p_value, std_err) + if verbose>0: + print("estimate_offset: slope, intercept, r_value, p_value, std_err") + print(" ",slope, intercept, r_value, p_value, std_err) return intercept else: return -1 -def download_heatflux_by_program(port,program,timewindow=0,threads=1,testmode=True,version=0,verbose=0,givealpha=False,request=True): +def download_heatflux_by_program(port,program,time_window=0,threads=1,testmode=True, + version=0,verbose=0,givealpha=False,request=True): """ port: string or integer program: string - timewindow: integer,float or list/numpy array of interger/float, single value: t1 until so many seconds, two values, t1+ first value until t1+ second value + time_window: integer,float or list/numpy array of interger/float, single value: t1 until so many seconds, two values, t1+ first value until t1+ second value threads: integer, number of threads for parallel download testmode: True to load data from testarchive version: integer, version number for 0 the highest version will be used @@ -2306,30 +2467,32 @@ def download_heatflux_by_program(port,program,timewindow=0,threads=1,testmode=Tr if prog[0]: t1=prog[1]['trigger']['1'][0] t6=prog[1]['trigger']['6'][0] - if timewindow==0: + if time_window==0: tstart=t1 tend=t6 - elif type(timewindow)==list or type(timewindow)==np.ndarray: - if len(timewindow)>1: - tstart=int(t1+timewindow[0]*1e9) - tend=int(t1+timewindow[1]*1e9) + elif type(time_window)==list or type(time_window)==np.ndarray: + if len(time_window)>1: + tstart=int(t1+time_window[0]*1e9) + tend=int(t1+time_window[1]*1e9) else: tstart=int(t1) - tend=int(t1+timewindow[0]*1e9) - elif type(timewindow)==int or type(timewindow)==float: + tend=int(t1+time_window[0]*1e9) + elif type(time_window)==int or type(time_window)==float: tstart=int(t1) - tend=int(t1+timewindow*1e9) + tend=int(t1+time_window*1e9) else: - raise Exception("given timewindow type is not supported") + raise Exception("download_heatflux_by_program: given time_window type is not supported") if givealpha: tstart=tstart-1.1e9 - return download_heatflux_by_times(port=port,tstart=tstart,tend=tend,timewindow=0,testmode=testmode,version=version,verbose=verbose-1,request=request) + return download_heatflux_by_times(port=port,tstart=tstart,tend=tend,time_window=0, + testmode=testmode,version=version,verbose=verbose-1, + request=request) else: - if verbose>0: - print("program not found") + print("download_heatflux_by_program: Error! program not found") return False,0,-1 -def download_heatflux_by_times(port,tstart,tend,timewindow=0,threads=1,testmode=True,version=0,verbose=1,request=True): +def download_heatflux_by_times(port,tstart,tend,time_window=0,threads=1,testmode=True, + version=0,verbose=0,request=True): """ port: string or integer tstart: int64 timestamp in ns from which point the download should start @@ -2344,7 +2507,7 @@ def download_heatflux_by_times(port,tstart,tend,timewindow=0,threads=1,testmode= base="http://archive-webapi.ipp-hgw.mpg.de/Test/raw/" else: base=archivepath - OP=get_OP_by_time(tstart) + OP=IR_tools.get_OP_by_time(time_ns=tstart) if type(port) is str: try: camera=portcamdict[OP][port] @@ -2355,17 +2518,18 @@ def download_heatflux_by_times(port,tstart,tend,timewindow=0,threads=1,testmode= port=int(port.split("AEF")[1]) goon=True except Exception as E: - raise Exception("unknown Port!"+E) + raise Exception("download_heatflux_by_times: unknown Port!"+E) elif type(port) is int: FLIR=False goon=True else: goon=False if not goon: - raise Exception("the given port is neither a number or a valid String!") + raise Exception("download_heatflux_by_times: the given port is neither a number or a valid String!") else: if FLIR: - print("FLIR heatflux is at the moment unsupported!") + if verbose>0: + print("download_heatflux_by_times: FLIR heatflux is at the moment unsupported!") return False,0,-1 else:## Okay QRT is requested. Maybe there is data available, maybe not, lets find out if version==0: @@ -2377,22 +2541,22 @@ def download_heatflux_by_times(port,tstart,tend,timewindow=0,threads=1,testmode= # larchivepath=base+project_ana+"/QRT_IRCAM_Test/AEF"+str(port)+"_heatflux_Test_2_DATASTREAM/V"+str(version)+"/0/heatflux" ### end of testsample modifikation### if verbose>0: - print(datetime.datetime.now(),"heat flux download started") + print(datetime.datetime.now(),"download_heatflux_by_times: heat flux download started") if False: ## does not work, the png pictures are interpretated as colors, values are lost - exist,time,frames=download_images_by_time_via_png(larchivepath,starttime=tstart,stoptime=tend,time_s=timewindow,version=version,threads=threads,verbose=verbose-1) + exist,time,frames=download_images_by_time_via_png(larchivepath,starttime=tstart,stoptime=tend,time_window=time_window,version=version,threads=threads,verbose=verbose-1) else: exist,time,frames=download_images_by_times(larchivepath,starttime=tstart,stoptime=tend,version=version,verbose=verbose-1) if exist==False: now=datetime.datetime.now() if verbose>0: - print(now,"heat flux data is not available, creating request") + print(now,"download_heatflux_by_times: heat flux data is not available, creating request") if request: f=open(heatflux_requestlist_path+str(now.year)+str(now.month)+"_q_requests.txt",'a') try: programid=AKF_2.get_program_id(tstart) except Exception as E: if verbose>0: - print(E) + print('download_heatflux_by_times: Error! ',E) programid=str(tstart) f.write(programid+"\tAEF"+str(port)+"\n") f.close() @@ -2408,7 +2572,7 @@ def download_heatflux_mapping_reference(timepoint=None,version=0,testmode=True,v base="http://archive-webapi.ipp-hgw.mpg.de/Test/raw/" else: base=archivepath -# OP=get_OP_by_time(timepoint) +# OP=IR_tools.get_OP_by_time(time_ns=timepoint) if version==0: version=get_latest_version("QRT_IRCAM/Mapping_reference_DATASTREAM",project=project_ana,Test=testmode) larchivepath=base+project_ana+"/"+"QRT_IRCAM/Mapping_reference_DATASTREAM/V"+str(version)+"/0/reference" @@ -2500,7 +2664,8 @@ def give_finger_ID(profile,finger=None): success=False return success,fingerID -def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstart=None,tend=None,testmode=True,version=0,verbose=0): +def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstart=None, + tend=None,testmode=True,version=0,verbose=0): """ returns exist,time,s in m, heat flux in W/m2 @@ -2511,11 +2676,12 @@ def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstar """ ### checking whether input is valid and whether the download can be started if program==None and tstart==None: - raise Exception("No Program given, please specify the program you are want heatflux from") + raise Exception("get_heatflux_profile: No Program given, please specify the program you are want heatflux from") else: ### some informations about the program are given, is the timepoint given in a usefull way? if timepoint==None or (type(timepoint)!=int and type(timepoint)!=float): - if verbose>0: print("timepoint is not given in a usefull way, the profile have to be requested for location and time and program!") + if verbose>0: + print("get_heatflux_profile: timepoint is not given in a usefull way, the profile have to be requested for location and time and program!") return False,0,0,0 ### is the location given? possible combinations for profile have to be checked checkprof=give_finger_ID(profile,finger) @@ -2523,7 +2689,8 @@ def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstar fingerID=checkprof[1] inputcheck=True else: - if verbose>0: print(datetime.datetimenow(),"given Finger and Profile combination is not understood!") + if verbose>0: + print(datetime.datetimenow(),"get_heatflux_profile: given Finger and Profile combination is not understood!") return False,0,0,0 ### lets find out which timepoint or timeintervall is requested, to know the OP, only needed for OP2 changes, but who knows if program!=None: @@ -2533,7 +2700,8 @@ def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstar tstart=t1+int((timepoint-0.02)*1e9) tend=t1+int((timepoint+0.02)*1e9) else: - if verbose>0: print("program not found") + if verbose>0: + print("get_heatflux_profile: program not found") return False,0,0,0 else: t1=tstart @@ -2555,7 +2723,7 @@ def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstar port=int(port.split("AEF")[1]) goon=True except Exception as E: - raise Warning("unknown Port!"+E) + raise Warning("get_heatflux_profile: unknown Port!"+E) return False,0,0,0 elif type(port) is int: FLIR=False @@ -2563,40 +2731,41 @@ def get_heatflux_profile(port,profile,finger=None,timepoint=1,program=None,tstar else: goon=False if not goon or FLIR: - raise Exception("Unknown port, FLIR data is unsupported at the moment") + raise Exception("get_heatflux_profile: Unknown port, FLIR data is unsupported at the moment") else: ### if the program goes up to here, time okay, finger okay,port okay, lets download it - exist,timo,frames=download_heatflux_by_times(port,tstart,tend,timewindow=0,threads=1,testmode=True,version=version,verbose=verbose-1) + exist,timo,frames=download_heatflux_by_times(port,tstart,tend,time_window=0,threads=1,testmode=True,version=version,verbose=verbose-1) if exist: return extract_heatflux_profile_from_DL(time=(np.asarray(timo)-t1)/1e9,images=frames,profile=int(fingerID%100),finger=int(fingerID/100),inputchecked=inputcheck,verbose=verbose-1) else: if verbose>0: - print("heatflux data could not be downloaded") + print("get_heatflux_profile: heatflux data could not be downloaded") return False,0,0,0 -def extract_heatflux_profile_from_DL(time,images,profile,finger=None,timewindow=1,inputchecked=False,verbose=0): +def extract_heatflux_profile_from_DL(time,images,profile,finger=None,time_window=1, + inputchecked=False,verbose=0): """ return exist,time,s in m, heat flux in W/m2 time_point default (None) returns entire shot. """ goon=True - if timewindow==None: + if time_window==None: Tid_start=0 Tid_end=len(time) - elif type(timewindow)==int or type(timewindow)==float: + elif type(time_window)==int or type(time_window)==float: try: - Tid_start=IR_tools.find_nearest(time,timewindow) + Tid_start=IR_tools.find_nearest(time,time_window) Tid_end=Tid_start+1 except Exception as E: goon=False - elif type(timewindow)==list or type(timewindow)==np.ndarray and len(timewindow)>0: + elif type(time_window)==list or type(time_window)==np.ndarray and len(time_window)>0: try: - Tid_start=IR_tools.find_nearest(time,timewindow[0]) + Tid_start=IR_tools.find_nearest(time,time_window[0]) except Exception as E: goon=False - if len(timewindow)>1: + if len(time_window)>1: try: - Tid_end=IR_tools.find_nearest(time,timewindow[1]) + Tid_end=IR_tools.find_nearest(time,time_window[1]) except Exception as E: goon=False else: @@ -2606,7 +2775,7 @@ def extract_heatflux_profile_from_DL(time,images,profile,finger=None,timewindow= E="unknown Case" if not goon: if verbose>0: - print(E) + print('extract_heatflux_profile_from_DL: ',E) return False,0,0,0 else: if not inputchecked: @@ -2627,7 +2796,7 @@ def extract_heatflux_profile_from_DL(time,images,profile,finger=None,timewindow= return True,time[Tid_start:Tid_end],S,q else: if verbose>0: - print("mapping was not found, cannot extract the profile") + print("extract_heatflux_profile_from_DL: mapping was not found, cannot extract the profile") return False,0,0,0 @@ -2670,7 +2839,7 @@ def read_restdb(request_url): signal0=np.array(signal_list['values']) t=np.array(signal_list['dimensions']) except ValueError as e: - print(signal_list['status'],e) + print('read_restdb: Error! ',signal_list['status'],e) return False, [0], [-2] else: return True, np.squeeze(t), np.squeeze(np.double(signal0)) @@ -2696,12 +2865,12 @@ def get_program_from_PID(instring): try: string_date,req_PID=instring.split('.') except ValueError as e: - print('!get_program_from_PID: Format of input string should be similar to 20160310.007') + print('get_program_from_PID: Format of input string should be similar to 20160310.007') return False,0 try: day=datetime.datetime.strptime(string_date, "%Y%m%d") except ValueError as e: - print('!get_program_from_PID: Date format should be similar to 20160310') + print('get_program_from_PID: Date format should be similar to 20160310') return False,0 day_plus1=day+datetime.timedelta(days=1) @@ -2718,7 +2887,7 @@ def get_program_from_PID(instring): prog_list = json.loads(res.read().decode('utf-8')) res.close() except urllib.error.URLError as e: - print('!get_program_from_PID: Error opening URL') + print('get_program_from_PID: Error opening URL') return False,0 else: pl=prog_list['programs'] @@ -2729,7 +2898,7 @@ def get_program_from_PID(instring): try: id=id_list.index(int(req_PID)) except ValueError as e: - print('!get_program_from_PID: Could not find requested program ID',e) + print('get_program_from_PID: Could not find requested program ID',e) return False,pl else: return True, pl[id] @@ -2745,7 +2914,7 @@ if __name__=='__main__': #%% temperature download and plotting example # port=51#"AEF51" # prog="20180925.017" -# status,time,images,valid=get_temp_from_raw_by_program(port,prog,time_s=[0,0.15],emi=0.82,T_version=2,version=0,threads=4,give_ERROR=False,use_firstframe_as_background=False,verbose=5) +# status,time,images,valid=get_temp_from_raw_by_program(port,prog,time_window=[0,0.15],emi=0.82,T_version=2,version=0,threads=4,give_ERROR=False,use_firstframe_as_background=False,verbose=5) # if status: # plt.figure() # plt.imshow(images[-1],vmin=330,vmax=1000,cmap=exJet) @@ -2755,7 +2924,7 @@ if __name__=='__main__': #%% heatflux test port=20 prog="20171108.017" - status,time,images=download_heatflux_by_program(port,prog,timewindow=0.1,threads=1,version=1,verbose=0,givealpha=False,request=False) + status,time,images=download_heatflux_by_program(port,prog,time_window=0.1,threads=1,version=1,verbose=1,givealpha=False,request=False) if status: print(prog,port,"OK") else: @@ -2765,8 +2934,8 @@ if __name__=='__main__': # status,mapping=download_heatflux_mapping_reference(verbose=4) # test=get_heatflux_profile(20,1605,timepoint=1,program="20171109.008",verbose=4) #%% HDF5 writing test -# bla=get_temp_from_raw_by_program_fullthreads(51,prog,time_s=[0,4],threads=4) -# bla2=get_nuced_raw_by_program_fullthreads(51,prog,time_s=[0,4],threads=4) +# bla=get_temp_from_raw_by_program_fullthreads(51,prog,time_window=[0,4],threads=4) +# bla2=get_nuced_raw_by_program_fullthreads(51,prog,time_window=[0,4],threads=4) # port=41 # program="20180925.013" # prog=get_program_from_PID(program) @@ -2776,7 +2945,7 @@ if __name__=='__main__': # t6=prog[1]['trigger']['6'][0] # for port in [10]: ## bla=download_raw_images_by_times(port,t0,t1,verbose=5) -# bla=get_temp_from_raw_by_program(port,program,time_s=[0,4],emi=0.82,T_version=1,version=0,threads=4,give_ERROR=False,use_firstframe_as_background=True,verbose=5) +# bla=get_temp_from_raw_by_program(port,program,time_window=[0,4],emi=0.82,T_version=1,version=0,threads=4,give_ERROR=False,use_firstframe_as_background=True,verbose=5) # import h5py as h5 # File=h5.File("AEF"+str(port)+"_"+program+"_temp_v1_b.h5",'w') # imags=np.asarray(bla[2]).swapaxes(0,2).swapaxes(0,1)