downloadversionIRdata.py 16.3 KB
Newer Older
Holger Niemann's avatar
Holger Niemann committed
1
2
3
4
5
6
7
8
9
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
73
74
75
76
77
78
79
80
81
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 25 15:51:46 2017

@author: holn
"""

import numpy as np
#import holn.archivedb as AKF_2
import W7Xrest.read_restdb as AKF_1
#import holn.functions as fu
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

def download_LUT(camera,port,time,exposure=0,emissivity=0,camera_filter=0,version=1):
    """
    download_LUT(camera,port,time,exposure=0,emissivity=0,camera_filter=0,version=1):
        time in ns
    """    
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_LUT_"
        query="Filter_"+str(camera_filter)+"_Texp_"+str(exposure)+"us_e_"+str(float(emissivity))
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        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
    
def download_background_by_program(camera,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_background_by_times(camera,port,starttime,stoptime,exposure,version)
    else:
        print("cannot find the program")
        return False,0,0
    
def download_background_by_times(camera,port,starttime,stoptime,exposure,camera_filter=0,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_background_"      
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        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:
                if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
                    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

def download_raw_images_by_program(camera,port,program,time_s=0,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]
        if time_s==0:
            return download_raw_images_by_times(camera,port,starttime,stoptime,version)
        else:
            return download_raw_images_by_times(camera,port,starttime,int(starttime+time_s*1e9),version)
    else:
        print("cannot find the program")
        return False,0,0

def download_raw_images_by_times(camera,port,starttime,stoptime,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_raw_DATASTREAM/V"+str(version)+"/0/raw"
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_raw_DATASTREAM/V"+str(version)+"/0/raw"
    else:
        print("camera unknown, stopping here")
        raise Exception
    if (stoptime-starttime)/1e9>2:        
        nrinterv=int(np.ceil((stoptime-starttime)/1e9/2))
        print("timewindow to large, put it into smaller fractions ("+str(nrinterv)+")")
        intervalls=[]
        for i in range(nrinterv-1):
            intervalls.append(int(starttime-10+i*2*1e9))
        intervalls.append(stoptime)
        for i in range(nrinterv):
            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
                if i==0:
                    ressignal=signal
                    time=t
                else:
                    ressignal=np.append(ressignal,signal)
                    time=np.append(time,t)
                del signal,t
            except urllib.error.URLError as e:
                print(e)
        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
    
def download_raw_parlog_by_program(camera,port,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]
        return download_raw_parlog_by_times(camera,port,starttime,stoptime,version)    
    else:
        print("cannot find the program")
        return False,0,0
    
def download_raw_parlog_by_times(camera,port,starttime,stoptime,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_raw_PARLOG/V"+str(version)
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        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]
        return  get_INFRATEC_filter_by_times(starttime,stoptime,version)
    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: 
        return True, signal_list['dimensions'],signal_list['values'][0]['meta-data']['filter']    
    
def get_exposure_by_program(camera,port,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]
        return get_exposure_by_time(camera,port,starttime,stoptime,version)
    else:
        print("cannot find the program")
        return False,0,0
        
def get_exposure_by_time(camera,port,starttime,stoptime,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        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))
      
def get_camera_temp_by_program(camera,port,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]
        return get_camera_temp_by_times(camera,port,starttime,stoptime,version)
    else:
        print("cannot find the program")
        return False,0,0
        
def get_camera_temp_by_times(camera,port,starttime,stoptime,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        larchivepath=archivepath+"QRT_IRCAM/"+"AEF"+str(port)+"_meta_"
    return AKF_1.read_restdb(larchivepath+"DATASTREAM/V"+str(version)+"/1/cameratemperature/_signal.json?from="+str(starttime)+"&upto="+str(stoptime))
    
def get_sensor_temp_by_program(camera,port,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]
        return get_sensor_by_times(camera,port,starttime,stoptime,version)
    else:
        print("cannot find the program")
        return False,0,0
        
def get_sensor_by_times(camera,port,starttime,stoptime,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        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))

def get_camera_framerate_by_program(camera,port,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]
        return get_camera_framerate_by_times(camera,port,starttime,stoptime,version)
    else:
        print("cannot find the program")
        return False,0,0
        
def get_camera_framerate_by_times(camera,port,starttime,stoptime,version=1):
    if camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
        larchivepath=archivepath+"QRT_INFRATEC/"+"AEF"+str(port)+"_meta_"      
    elif camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
        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))
    

"""
end of functions, below only test of the functions
"""
    
#time=int(TimeToNs([2017,9,26],[8,0,0,0]))        
#testload=download_LUT("IRCAM",10,time,7,1,version=1)
#testNUC=download_NUC_by_program(10,'20171024.020',7,1)