downloadversionIRdata.py 29.1 KB
Newer Older
Holger Niemann's avatar
Holger Niemann committed
1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 25 15:51:46 2017

@author: holn
"""

import numpy as np
9
import W7Xrest.read_restdb as AKF_1
Holger Niemann's avatar
Holger Niemann committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import datetime
import urllib
import json

archivepath="http://archive-webapi.ipp-hgw.mpg.de/Test/raw/W7X/"

def TimeToNs(date,time):
    """
    TimeToNs(date,time)
    (list)  date    [year,month,day]
    (list)  time    [hours,minutes,seconds,microseconds]
    """ 
    date_time=datetime.datetime(date[0],date[1],date[2],time[0],time[1],time[2],time[3])
    div=date_time-datetime.datetime(1970,1,1,0,0,0)
    nsdate=div.total_seconds()*1e9
    return nsdate

def read_program(timestampstart,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))
    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(e)
        return False,0
    else:
        prog_string=prog_raw.decode(encoding='UTF-8')
        prog_list = json.loads(prog_string)
        pl=prog_list['programs'][0]
        return True, pl

def read_restdb_old(request_url):
    """
    Reads JSON data from W7-X REST API
    Returns: 
        valid: access ok
        t: numpy-array of time
        signal: numpy-array of requested signals
    by Thomsen
    """
    try:
        res = urllib.request.urlopen(request_url)
    except urllib.error.URLError as e:
        print(e)
        return False, 0, -1
    else:
        signal_raw=res.read()
        res.close()
        signal_string=signal_raw.decode(encoding='UTF-8')
        signal_list = json.loads(signal_string)
        signal0=np.array(signal_list['values'])
        t=np.array(signal_list['dimensions'])
        return True, t, signal0

73
def download_LUT(port,time,exposure=0,emissivity=0,camera_filter=0,version=1):
Holger Niemann's avatar
Holger Niemann committed
74
75
76
77
    """
    download_LUT(camera,port,time,exposure=0,emissivity=0,camera_filter=0,version=1):
        time in ns
    """    
78
    if port==50:# camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
79
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_LUT_"
Holger Niemann's avatar
Holger Niemann committed
80
        query="Filter_"+str(camera_filter)+"_Texp_"+str(int(exposure))+"us_e_"+str(float(emissivity))
81
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_LUT_"
        query="Texp_"+str(int(exposure))+"us_e_"+str(float(emissivity))
    else:
        print("camera unknown, stopping here")
        raise Exception
    #time=int(fu.TimeToNs([2017,9,26],[8,0,0,0]))
    LUTpar=AKF_1.read_restdb_old(larchivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(time-10)+"&upto="+str(time+20))
    if LUTpar[0]:
        LUTid=LUTpar[2][0]['structure'][query]
        LUTs=read_restdb_old(larchivepath+"DATASTREAM/V"+str(version)+"/_signal.json?from="+str(time-10)+"&upto="+str(time+20))
        if LUTs[0]:
            LUTs=LUTs[2][0]
            LUT=[LUTs[0],LUTs[LUTid],LUTs[LUTid+1]]
            del LUTpar, LUTs
            return True,LUT
        else:
            print("unable to download the LUTs")
            del LUTpar, LUTs
            return False,0
    else:
        del LUTpar
        print("unable to find LUTs, check your request")
        return False,0

def download_NUC_by_program(port,program,exposure,version=1):
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['0'][0]
        stoptime=prog[1]['trigger']['1'][0]
        return download_NUC_by_times(port,starttime,stoptime,exposure,version)
    else:
        print("cannot find the program")
        return False,0,0

def download_NUC_by_times(port,starttime,stoptime,exposure,version=1):
    larchivepath=archivepath+"QRT_IRCAM/AEF"+str(port)+"_NUC_"
#    NUC_parlog=AKF_1.read_restdb_old(archivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
    try:
        res = urllib.request.urlopen(larchivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
        signal_list = json.loads(res.read().decode('utf-8'))
        res.close()
        goon=True
    except urllib.error.URLError as e:
        print(e)
        goon=False
    if goon:        
        n=0
        nuctimes=[0]
        for NUCpar in signal_list['values']:
            if NUCpar['structure']['Texp']==exposure:
                nuctimes=[signal_list['dimensions'][2*n],signal_list['dimensions'][2*n+1]]
#                gain_i=NUCpar['structure']['gain_index']
                offset_i=NUCpar['structure']['offset_index']                
            n+=1 
        if nuctimes[0]!=0:
            NUC=AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/_signal.json?from="+str(nuctimes[0]-10)+"&upto="+str(nuctimes[1]+10))
            if NUC[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")
                return False, 0, 0
        else:
            print("NUC image for requested exposure time not found")
            return False,0,0
    else:
        return False,0,0
    
150
def download_background_by_program(port,program,exposure,version=1):
Holger Niemann's avatar
Holger Niemann committed
151
152
153
154
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['0'][0]
        stoptime=prog[1]['trigger']['1'][0]
155
        return download_background_by_times(port,starttime,stoptime,exposure,version)
Holger Niemann's avatar
Holger Niemann committed
156
157
158
159
    else:
        print("cannot find the program")
        return False,0,0
    
160
161
def download_background_by_times(port,starttime,stoptime,exposure,camera_filter=0,version=1):
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
162
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_background_"      
163
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_background_"   
    else:
        print("camera unknown, stopping here")
        raise Exception
    try:
        res = urllib.request.urlopen(larchivepath+"PARLOG/V"+str(version)+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
        signal_list = json.loads(res.read().decode('utf-8'))
        res.close()
        goon=True
    except urllib.error.URLError as e:
        print(e)
        goon=False
    if goon: 
        n=0
        backtimes=[0]
        for backpar in signal_list['values']:
            if backpar['structure']['Texp']==exposure:
181
                if port==50:# camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
                    if backpar['structure']['filter']==camera_filter:
                        backtimes=[signal_list['dimensions'][2*n],signal_list['dimensions'][2*n+1]]                             
                else:
                    backtimes=[signal_list['dimensions'][2*n],signal_list['dimensions'][2*n+1]]                             
            n+=1
        if backtimes[0]!=0:
            backdat=AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/_signal.json?from="+str(backtimes[0]-10)+"&upto="+str(backtimes[1]+10))
            if backdat[0]:                                    
                return backdat#[True,backdat[2]]
            else:
                print("background image for requested exposure time(, filter) not found")
                return False,0,0
        else:
            print("background image for requested exposure time(, filter) not found")
            return False,0,0
    else:
        return False,0,0

200
def download_raw_images_by_program(port,program,time_s=0,version=1):
Holger Niemann's avatar
Holger Niemann committed
201
202
203
204
205
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
        if time_s==0:
206
            return download_raw_images_by_times(port,starttime,stoptime,version)
Holger Niemann's avatar
Holger Niemann committed
207
        else:
208
            return download_raw_images_by_times(port,starttime,int(starttime+time_s*1e9),version)
Holger Niemann's avatar
Holger Niemann committed
209
210
211
212
    else:
        print("cannot find the program")
        return False,0,0

213
def download_raw_images_by_times(port,starttime,stoptime,version=1,intervalSize=1E9):
214
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
215
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_raw_DATASTREAM/V"+str(version)+"/0/raw"
216
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
217
218
219
220
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_raw_DATASTREAM/V"+str(version)+"/0/raw"
    else:
        print("camera unknown, stopping here")
        raise Exception
221
222
    if (stoptime-starttime)/intervalSize>1:        
        nrinterv=int(np.ceil((stoptime-starttime)/intervalSize))
Holger Niemann's avatar
Holger Niemann committed
223
224
        print("timewindow to large, put it into smaller fractions ("+str(nrinterv)+")")
        intervalls=[]
225
        for i in range(nrinterv):
226
            intervalls.append(int(starttime-10+i*intervalSize))
Holger Niemann's avatar
Holger Niemann committed
227
        intervalls.append(stoptime)
228
        for i in range(nrinterv):
Holger Niemann's avatar
Holger Niemann committed
229
            try:
Holger Niemann's avatar
Holger Niemann committed
230
                res = urllib.request.urlopen(larchivepath+"/_signal.json?from="+str(intervalls[i])+"&upto="+str(intervalls[i+1]))
Holger Niemann's avatar
Holger Niemann committed
231
232
233
234
235
236
237
238
239
                signal_list = json.loads(res.read().decode('utf-8'))
                res.close()
                signal=np.array(signal_list['values'],dtype=np.uint16)
                t=np.array(signal_list['dimensions'])
                del signal_list
                if i==0:
                    ressignal=signal
                    time=t
                else:
240
                    ressignal=np.append(ressignal,signal,axis=0)
Holger Niemann's avatar
Holger Niemann committed
241
242
243
                    time=np.append(time,t)
                del signal,t
            except urllib.error.URLError as e:
Holger Niemann's avatar
Holger Niemann committed
244
                print(e)                
Holger Niemann's avatar
Holger Niemann committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
        return True,time,ressignal
    else:
        try:
            res = urllib.request.urlopen(larchivepath+"/_signal.json?from="+str(starttime-10)+"&upto="+str(stoptime))
            signal_list = json.loads(res.read().decode('utf-8'))
            res.close()
            signal=np.array(signal_list['values'],dtype=np.uint16)
            t=np.array(signal_list['dimensions'])
            del signal_list
            return True, t, signal
        except urllib.error.URLError as e:
            print(e)
            return False, 0,-1
    
259
def download_raw_parlog_by_program(port,program,version=1):
Holger Niemann's avatar
Holger Niemann committed
260
261
262
263
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
264
        return download_raw_parlog_by_times(port,starttime,stoptime,version)    
Holger Niemann's avatar
Holger Niemann committed
265
266
267
268
    else:
        print("cannot find the program")
        return False,0,0
    
269
270
def download_raw_parlog_by_times(port,starttime,stoptime,version=1):
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
271
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_raw_PARLOG/V"+str(version)
272
    elif port in [10,11,20,21,30,31,40,41,51]:#elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_raw_PARLOG/V"+str(version)   
    else:
        print("camera unknown, stopping here")
        raise Exception
    try:
        res = urllib.request.urlopen(larchivepath+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
        signal_list = json.loads(res.read().decode('utf-8'))
        res.close()
        goon=True
    except urllib.error.URLError as e:
        print(e)
        goon=False
    if goon: 
        return True, signal_list['dimensions'],signal_list['values'][0]['meta-data']
    

def get_INFRATEC_filter_by_program(program,version=1):
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
Holger Niemann's avatar
Holger Niemann committed
294
        return  get_INFRATEC_filter_by_times(starttime,stoptime,50,version)
Holger Niemann's avatar
Holger Niemann committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    else:
        print("cannot find the program")
        return False,0,0

def get_INFRATEC_filter_by_times(starttime,stoptime,port=50,version=1):
    larchivepath=archivepath+"QRT_INFRATEC/AEF"+str(port)+"_raw_PARLOG/V"+str(version)
    try:
        res = urllib.request.urlopen(larchivepath+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
        signal_list = json.loads(res.read().decode('utf-8'))
        res.close()
        goon=True
    except urllib.error.URLError as e:
        print(e)
        goon=False
    if goon: 
Holger Niemann's avatar
Holger Niemann committed
310
311
312
        return True, signal_list['dimensions'],signal_list['values'][0]['meta-data']['filter']  
    else:
        return False, 0,-1
Holger Niemann's avatar
Holger Niemann committed
313
    
314
def get_exposure_by_program(port,program,version=1):
Holger Niemann's avatar
Holger Niemann committed
315
316
317
318
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
319
        return get_exposure_by_times(port,starttime,stoptime,version)
Holger Niemann's avatar
Holger Niemann committed
320
321
322
323
    else:
        print("cannot find the program")
        return False,0,0
        
324
def get_exposure_by_times(port,starttime,stoptime,version=1):
325
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
326
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
327
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
328
329
330
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_meta_"   
    return AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/3/exposuretime"+"/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
      
331
def get_camera_temp_by_program(port,program,version=1):
Holger Niemann's avatar
Holger Niemann committed
332
333
334
335
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
336
        return get_camera_temp_by_times(port,starttime,stoptime,version)
Holger Niemann's avatar
Holger Niemann committed
337
338
339
340
    else:
        print("cannot find the program")
        return False,0,0
        
341
342
def get_camera_temp_by_times(port,starttime,stoptime,version=1):
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
343
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
344
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
345
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_meta_"
346
347
348
    else:
        print("Port unknown")
        raise Exception
Holger Niemann's avatar
Holger Niemann committed
349
350
    return AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/1/cameratemperature/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
    
351
def get_sensor_temp_by_program(port,program,version=1):
Holger Niemann's avatar
Holger Niemann committed
352
353
354
355
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
356
        return get_sensor_by_times(port,starttime,stoptime,version)
Holger Niemann's avatar
Holger Niemann committed
357
358
359
360
    else:
        print("cannot find the program")
        return False,0,0
        
361
362
def get_sensor_by_times(port,starttime,stoptime,version=1):
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
363
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
364
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
365
366
367
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_meta_"
    return AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/0/sensortemperature/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))

368
def get_camera_framerate_by_program(port,program,version=1):
Holger Niemann's avatar
Holger Niemann committed
369
370
371
372
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        starttime=prog[1]['trigger']['1'][0]
        stoptime=prog[1]['trigger']['6'][0]
373
        return get_camera_framerate_by_times(port,starttime,stoptime,version)
Holger Niemann's avatar
Holger Niemann committed
374
375
376
377
    else:
        print("cannot find the program")
        return False,0,0
        
378
379
def get_camera_framerate_by_times(port,starttime,stoptime,version=1):
    if port==50:#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
Holger Niemann's avatar
Holger Niemann committed
380
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
381
    elif port in [10,11,20,21,30,31,40,41,51]:#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
Holger Niemann's avatar
Holger Niemann committed
382
383
384
385
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_meta_"
    return AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/2/framerate/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
    

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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/"
    else:
        urlstart=urlstart+"DataModuleDesc.262_DATASTREAM/"
    if port %10==1:##uneven cameraport, even TC port
        portnr=port-1
        offset=(port//10-1)*32
    elif port%10==0:##even cameraport, uneven TC port
        portnr=port+1
        offset=(port//10-1)*32+16
    else:
        print("unknown port!")
        raise Exception
    if portnr==50:
        offset=0
    elif portnr==51:
        offset=16
    ext="/ActV1QRD"+str(portnr)+"CT1"
    n=0
    signal=0
    success=True
    if portnr==21:
        skip=True
        skipnr=1
    else:
        skip=False
        skipnr=0
    if part=="all":
        rs=0
        re=10
    elif part in ["h","horizontal","full h","full horizontal"]:
        rs=0
        re=7
    elif part in ["v","vertical","vertikal"]: 
        rs=7
        re=10
    elif part in ["hli","horizontal low iota"]:
425
426
        rs=0#4
        re=4#7
427
    elif part in ["hhi","horizontal high iota","high iota","hi"]:
428
429
        rs=4#0
        re=7#4
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    elif part in [0,1,2,3,4,5,6,7,8,9,10]:
        rs=part
        re=part+1
    elif part in ["Single","s","single"]:
        rs=TC
        re=TC+1
    else:
        rs=0
        re=0
    if rs!=re:
        for i in range(rs,re,1):
            if skip and i==skipnr:
                i+=1
            url=urlstart+str(offset+i)+ext+"0"+str(i)+"_TC"+str(i%8+1)
            try:
                result=AKF_1.read_restdb(url+"/_signal.json?from="+str(starttime)+"&upto="+str(endtime))
    #                res.append(result)
                if result[0]:
                    n+=1            
                    time=result[1]
                    signal=signal+np.array(result[2])
                else:
                    time=0
                    success=False
            except Exception as E:
                print(E)        
                success=False
Holger Niemann's avatar
Holger Niemann committed
457
458
459
460
        try:
            return success,time,signal/n
        except:
            return False,0,-1
461
462
    else:
        return False,0,-1
Holger Niemann's avatar
Holger Niemann committed
463
"""
464
end of download functions, below combination to get temperature
Holger Niemann's avatar
Holger Niemann committed
465
"""
466
467
468
469
470

def apply_LUT_to_images(LUT,images):
    LUTd={LUT[0][i]: LUT[1][i] for i in range(0, len(LUT[0]))}
    return np.vectorize(LUTd.get)(images)

471
def get_temp_from_raw_by_program_V1(portnr,program,time_s=0,emi=0.8,divertorpart="all",version=1,intervalSize=1E9):
472
473
474
475
476
477
478
479
480
481
    if portnr==50:
        cf=get_INFRATEC_filter_by_program(program,version)
        if cf[0]:
            cfilter=cf[2]
        else:
            cfilter=-1
            print("Filter not found for INFRATEC Camera")
            raise Exception
    else:
        cfilter=0
482
483
484
485
    prog=AKF_1.get_program_from_PID(program)
    if prog[0]:
        t0=prog[1]['trigger']['0'][0]
        t1=prog[1]['trigger']['1'][0]
486
        t6=prog[1]['trigger']['6'][0]
Holger Niemann's avatar
Holger Niemann committed
487
488
489
490
491
        starttime=t1-10
        if time_s==0:                                
            stoptime=t6
        else:
            stoptime=int(starttime+time_s*1e9)
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
        expoDL=get_exposure_by_times(portnr,t1,t6,version)
        if expoDL[0]:
            t_exp=expoDL[2][0]
            del expoDL
            time=int(TimeToNs([2017,9,26],[8,0,0,0]))   
            LUT_DL=download_LUT(portnr,time,t_exp,emi,cfilter,version)
            if LUT_DL[0]:
                LUT=LUT_DL[1]
                del LUT_DL
                back_dl=download_background_by_times(portnr,t0,t1,t_exp,version)
                if back_dl[0]:
                    background=back_dl[2]
    #                background=background.swapaxes(0,1)
                    backtime=back_dl[1]
                    backtime=backtime.tolist()
Holger Niemann's avatar
Holger Niemann committed
507
                    TCT_dl=get_average_divertor_TC_for_camtemp(portnr,int(backtime-2e9),int(backtime+2e9),divertorpart)
508
509
                    if TCT_dl[0]:
                        TCT=np.average(TCT_dl[2])
510
                    else:
Holger Niemann's avatar
Holger Niemann committed
511
512
513
514
515
516
                        TCT_dl=get_average_divertor_TC_for_camtemp(portnr,int(backtime-6e9),int(backtime+6e9),divertorpart)
                        if TCT_dl[0]:
                            TCT=np.average(TCT_dl[2])
                        else:
                            print("loading TC data was not successfull")
                            TCT=0
517
                    del back_dl
Holger Niemann's avatar
Holger Niemann committed
518
                    success=True
519
520
521
522
523
524
525
526
527
528
                    if portnr!=50:
                        #tempcalc for the IRCAMs
                        NUC_DL=download_NUC_by_times(portnr,t0,t1,t_exp,version)
                        if NUC_DL[0]==False:
                            print("NUC was not found")
                            return False,0,-1
                            raise Exception
                        else:
                            gain=NUC_DL[1][0]
                            offset=NUC_DL[1][1]
Holger Niemann's avatar
Holger Niemann committed
529
                            
530
                            print(datetime.datetime.now(),"Start download of raw images")
Holger Niemann's avatar
Holger Niemann committed
531
                            
532
533
534
535
536
537
538
539
540
                            if (stoptime-starttime)/intervalSize>1:        
                                nrinterv=int(np.ceil((stoptime-starttime)/intervalSize))
                                print("timewindow to large, put it into smaller fractions ("+str(nrinterv)+")")
                                intervalls=[]
                                for i in range(nrinterv):
                                    intervalls.append(int(starttime-10+i*intervalSize))
                                intervalls.append(stoptime)
                                
                                for i in range(nrinterv):                            
Holger Niemann's avatar
Holger Niemann committed
541
                                    raw_dl=download_raw_images_by_times(portnr,intervalls[i],intervalls[i+1],version,intervalSize)
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
                                    print(datetime.datetime.now(),"download of raw images part "+str(i+1)+" of "+str(nrinterv)+" finished")
                                    if raw_dl[0]:
                                        time=raw_dl[1]
                                        images=raw_dl[2]
            #                            del raw_dl
                                        images=images.swapaxes(1,2)
                                        images=images[:]*gain
                                        images=images[:]+offset
                                        images=images[:]-background
                                        images=images.clip(min=0)
                                        images=np.array(images,dtype=np.uint16)
                                        images=apply_LUT_to_images(LUT,images)
                                        images=images+(TCT+273.15)
                                        if i==0:
                                            temperatureimages=images
                                            times=time
Holger Niemann's avatar
Holger Niemann committed
558
                                            del time,images
559
                                        else:
Holger Niemann's avatar
Holger Niemann committed
560
                                            temperatureimages=np.append(temperatureimages,images,axis=0)
Holger Niemann's avatar
Holger Niemann committed
561
562
                                            times=np.append(times,time)                                        
                                            del time,images
563
564
565
                                    else:
                                        print("cannot download the raw images")
                                        success=False
Holger Niemann's avatar
Holger Niemann committed
566
567
                                        times=0
                                        temperatureimages=-1
568
                            else:
Holger Niemann's avatar
Holger Niemann committed
569
                                raw_dl=download_raw_images_by_times(portnr,starttime,stoptime,version,intervalSize)
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
                                if raw_dl[0]:
                                    times=raw_dl[1]
                                    temperatureimages=raw_dl[2]
        #                            del raw_dl
                                    temperatureimages=temperatureimages.swapaxes(1,2)
                                    temperatureimages=temperatureimages[:]*gain
                                    temperatureimages=temperatureimages[:]+offset
                                    temperatureimages=temperatureimages[:]-background
                                    temperatureimages=temperatureimages.clip(min=0)
                                    temperatureimages=np.array(temperatureimages,dtype=np.uint16)
                                    temperatureimages=apply_LUT_to_images(LUT,temperatureimages)
                                    temperatureimages=temperatureimages+(TCT+273.15)                                    
                                else:
                                    print("cannot download the raw images")
                                    success=False
                                    times=0
                                    temperatureimages=-1
                            return success,times,temperatureimages                                
                    else:
                        #tempcalc for infratec
Holger Niemann's avatar
Holger Niemann committed
590
591
                        print(datetime.datetime.now(),"Start download of raw images")                        
#                        print(datetime.datetime.now(),"download of raw images finished")
592
593
594
595
596
597
598
599
600
                        if (stoptime-starttime)/intervalSize>1:    
                            nrinterv=int(np.ceil((stoptime-starttime)/intervalSize))
                            print("timewindow to large, put it into smaller fractions ("+str(nrinterv)+")")
                            intervalls=[]
                            for i in range(nrinterv):
                                intervalls.append(int(starttime-10+i*intervalSize))
                            intervalls.append(stoptime)
                            
                            for i in range(nrinterv):                            
Holger Niemann's avatar
Holger Niemann committed
601
                                raw_dl=download_raw_images_by_times(portnr,intervalls[i],intervalls[i+1],version,intervalSize)
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
                                print(datetime.datetime.now(),"download of raw images part "+str(i+1)+" of "+str(nrinterv)+" finished")
                                if raw_dl[0]:
                                    time=raw_dl[1]
                                    images=raw_dl[2]
        #                            del raw_dl
                                    images=images.swapaxes(1,2)
                                    images=images[:]-background
                                    images=images.clip(min=0)
                                    images=np.array(images,dtype=np.uint16)
                                    images=apply_LUT_to_images(LUT,images)
                                    images=images+(TCT+273.15)
                                    if i==0:
                                        temperatureimages=images
                                        times=time
                                    else:
Holger Niemann's avatar
Holger Niemann committed
617
                                        temperatureimages=np.append(temperatureimages,images,axis=0)
618
619
620
621
622
                                        times=np.append(times,times)                                        
                                else:
                                    print("cannot download the raw images")
                                    success=False
                            return success,times,temperatureimages
623
                        else:
Holger Niemann's avatar
Holger Niemann committed
624
                            raw_dl=download_raw_images_by_times(portnr,starttime,stoptime,version,intervalSize)
625
626
627
628
629
630
631
632
633
634
635
636
637
                            if raw_dl[0]:
                                time=raw_dl[1]
                                images=raw_dl[2]
                                images=images.swapaxes(1,2)
                                images=images[:]-background
                                images=images.clip(min=0)
                                images=np.array(images,dtype=np.uint16)
                                images=apply_LUT_to_images(LUT,images)
                                images=images+(TCT+273.15)
                                return True,time,images
                            else:
                                print("cannot download the raw images")
                                return False,0,-1
638
                else:
639
640
                    print("Background image not found for temperature calculation!")
                    return False,0,-1
641
            else:
642
                print("LUT not found, cannot calculate temperature")
643
644
                return False,0,-1
        else:
645
            print("cannot get the used exposure time")
646
647
            return False,0,-1
    else:
648
        print("cannot find program")
649
650
651
652
653
        return False,0,-1
    
if __name__=='__main__':
    status,time,images=get_temp_from_raw_by_program_V1(51,"20171018.019",time_s=0,emi=0.8,divertorpart="all",version=1)