Upload_Day_IRdata.py 31.1 KB
Newer Older
Holger Niemann's avatar
Holger Niemann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 26 16:01:06 2017

@author: holn
"""

import uploadingversionIRdata as IRup
import os
import h5py as h5
import numpy as np
import datetime
import sys
14
from time import sleep
15
from os.path import join
Holger Niemann's avatar
Holger Niemann committed
16
17
18
19
#from getfoldernames import Searchdatalocation as gfn

#%% set global parameter

20
21
### default old settings, will be overrided by the loaded config file!!! ###
local= True    # local test on one IRcam PC (not central PC)
Holger Niemann's avatar
Holger Niemann committed
22
active_PC = [0]         # selection of the following camera PCs to loop over
23
24
delayupload=True
startuploadtime="20:00:00"
Holger Niemann's avatar
Holger Niemann committed
25
26
27
reupload=False
reason=""
retry=2
28
port="AEF10"
Holger Niemann's avatar
Holger Niemann committed
29
30
31
32
33
34
35
36
37
38
39
singleupload=False#True#False
singlelist=[]
raw_parlog=True
raw_data=True#False#True
raw1=True#False#True#False#True
raw2=True
back_parlog=True#False#True
background=True#False#True
nuc_parlog=True#False
nuc=True
metastream=True
40
dates=[[2017,11,9]]
41
42
43
temperature=False
only_upload_lattest=False
use_today=False
44
45
logpath=""#"C:\\QIR\\Upload\\"
temppath=""#"C:\\QIR\\Upload\\"
Holger Niemann's avatar
Holger Niemann committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

ports = ['AEF10', 'AEF11', 
         'AEF20', 'AEF21', 
         'AEF30', 'AEF31', 
         'AEF40', 'AEF41', 
         'AEF50', 'AEF51']
cameras = ['IRCAM', 'IRCAM', 
           'IRCAM', 'IRCAM', 
           'IRCAM', 'IRCAM', 
           'IRCAM', 'IRCAM', 
           'INFRATEC', 'IRCAM']
Tup =   [ False,   True, 
          True,    True,    
          True,    False,    
          False,    True,    
          False,   True]
pc_list = ['Pc-e4-qrt-7', 'Pc-e4-qrt-2', 
           'Pc-e4-qrt-3', 'Pc-e4-qrt-4', 
           'Pc-dia-ws-16', 'Pc-e4-qrt-6', 
           'Pc-dia-ws-17', 'Pc-dia-ws-18', 
           'Pc-e4-qrt-5', 'Pc-e4-qrt-1']
67
68
69
70
71
72
73
74
75
76
77
78
79
try:
    cFile=open("upload_config")
    for line in cFile:
        if line[0:3]!="###":
            vars()[line.split("=")[0]]=eval(line.split("=")[1].split("\n")[0])
    cFile.close()
    active_PC=[ports.index(port)]
    Tup[ports.index(port)]=temperature
    if use_today:
        dummy=datetime.datetime.now()
        dates=[[dummy.year,dummy.month,dummy.day]]
except Exception as E:
    raise Exception("Config file was not loaded properly",E)
Holger Niemann's avatar
Holger Niemann committed
80
81
82
83
84
try:
    sysport=os.environ['qir_port']
except:
    sysport=port
    print("unable to read the port from the system, using config port")
Holger Niemann's avatar
Holger Niemann committed
85
toff = 0 #delay time of the programm for the raw stuff in ms
Holger Niemann's avatar
Holger Niemann committed
86
port=sysport
87
88
89
90
91
92
93
94
95
96
97
98
99
100

def findthelatestfilename(Rawdatapath):
    '''
    find the latest modified file name for easy upload.
    @Rawdatapath: the path to search from, e.g: 'R:\\2018.06.07'.
    
    return the latest modified file name in string.
    '''
    filelist = os.listdir(Rawdatapath)
    timearray = [os.path.getmtime(join(Rawdatapath, i)) for i in filelist]
    maxtimestampindex = np.argmax(timearray)
    latestfilename = filelist[maxtimestampindex]
    return latestfilename

Holger Niemann's avatar
Holger Niemann committed
101
102
103
104
105
106
107
108
109
110
111
112
#create upload log file:

class Tee(object):
    def __init__(self, *files):
        self.files = files
    def write(self, obj):
        for f in self.files:
            f.write(obj)
            f.flush() # If you want the output to be visible immediately
    def flush(self) :
        for f in self.files:
            f.flush()
113
114
115
116
117
118
119
120
if delayupload:            
    now=datetime.datetime.now()
    start=datetime.datetime(now.year,now.month,now.day,int(startuploadtime.split(":")[0]),int(startuploadtime.split(":")[1]),int(startuploadtime.split(":")[2]))            
    sleeptime=(start-now).total_seconds() #seconds
    if sleeptime<0:
        sleeptime=1
    sleep(sleeptime)
    
Holger Niemann's avatar
Holger Niemann committed
121
122
123
124
original = sys.stdout


start=datetime.datetime.now()
125
fout=open(logpath+"upload_log_"+str(start.date())+"_"+str(start.hour)+"_"+str(start.minute)+"_"+str(start.second)+".txt",'w')
126
#foutf=open("upload_log_failes_"+str(start.date())+"_"+str(start.hour)+"_"+str(start.minute)+"_"+str(start.second)+".txt",'w')
Holger Niemann's avatar
Holger Niemann committed
127
128
sys.stdout = Tee(sys.stdout, fout)
#sys.stdout = fout
129
f_success=open(logpath+"upload_log_successlist_"+str(start.date())+"_"+str(start.hour)+"_"+str(start.minute)+"_"+str(start.second)+".txt",'w')
Holger Niemann's avatar
Holger Niemann committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#%% decide data path format
for date in dates:
    if date[0]==2017 and date[1]<=9 and date[2]<=25:
        # old file and folder names
        rawdatapath   = "\\\\{0:s}\\r\\{1[0]:4d}-{1[1]:02d}-{1[2]:02d}"
        calibdatapath = "\\\\{0:s}\\r\\Calibration\\{1[0]:4d}-{1[1]:02d}-{1[2]:02d}"
        tempdatapath  = "\\\\{0:s}\\t\\{1[0]:4d}-{1[1]:02d}-{1[2]:02d}"
        """
        have to be finished!!
        """
    else:
        # recent file and folder naming convention
        rawdatapath   = "\\\\{0:s}\\r\\{1[0]:4d}.{1[1]:02d}.{1[2]:02d}"
        calibdatapath = rawdatapath
        tempdatapath  = "\\\\{0:s}\\t\\{1[0]:4d}.{1[1]:02d}.{1[2]:02d}"
        
146
    if local:
Holger Niemann's avatar
Holger Niemann committed
147
148
149
150
151
152
153
154
155
156
        rawdatapath   = "R:\\{1[0]:4d}.{1[1]:02d}.{1[2]:02d}"
        calibdatapath = rawdatapath
        tempdatapath  = "T:\\{1[0]:4d}.{1[1]:02d}.{1[2]:02d}"
    
    #%% loop over different IRcamera PC and upload data    
    if not raw1 and not raw2:
        raw_parlog=False
        raw_data=False
        
    for i in active_PC:
157
158
159
        if only_upload_lattest:
            single_upload=True
            singlelist = [findthelatestfilename(rawdatapath.format(pc_list[i],date))]
Holger Niemann's avatar
Holger Niemann committed
160
161
162
        port = int(ports[i].split('AEF')[1])    
        path = rawdatapath.format(pc_list[i],date)
        print(datetime.datetime.now(),"Starting Upload for "+ports[i],date)
163
        f_success.write(ports[i]+" \n")
164
        f_success.flush()
165
        f_success.write("program folder meta1 meta2 raw1 raw2 \n")
166
        f_success.flush()
167
#        foutf.write(datetime.datetime.now(),"Starting Upload for "+ports[i],date,"\n")
Holger Niemann's avatar
Holger Niemann committed
168
169
170
171
172
173
174
        # find all programs of this date on this PC
        programtimes = []
        if singleupload:
            programtimes=singlelist
        else:
            for o in os.listdir(path):
                if os.path.isdir(os.path.join(path,o)):
175
                    if len(o.split("_"))>1 and not date[0]>=2018:
Holger Niemann's avatar
Holger Niemann committed
176
177
178
179
180
181
182
183
                        print("unsupported folderstructure at the moment!!")
                    else:
                        programtimes.append(o)
                
    #    # loop over all programs and upload data
        for prog_time in programtimes:
        # loop over one specific program for test and upload data
            print("start upload of files for "+str(prog_time)+" for date: ",date)
184
#            foutf.write("start upload of files for "+str(prog_time)+" for date: ",date,"\n")
Holger Niemann's avatar
Holger Niemann committed
185
186
187
188
189
190
191
192
193
194
195
196
            try:
                path = os.path.join(rawdatapath.format(pc_list[i],date), prog_time)
                # sort files into background, cold image, offset, gain and raw data
                filelist = os.listdir(path)
                blist = {} # background dic
                clist = {} # cold file dic
                olist = {} # offset file dic
                glist = {} # gain file dic
                rlist = {} # raw file dic
                if len(filelist)>0:# check for empty folder!
                    for file in filelist:
                        dummy = file.split("_")
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
                        if date[0]>=2018:
                            if dummy[4]=='raw' or dummy[4]=='cal':
                                rlist[dummy[1]]=file
                        else:                        
                            if dummy[1]=='background':
                                blist[dummy[7]] = file
                            elif dummy[1]=='nuc':
                                if dummy[2]=='cold':
                                    clist[dummy[8]] = file
                                elif dummy[2]=='gain':    
                                    glist[dummy[8]] = file
                                elif dummy[2]=='offset':
                                    olist[dummy[8]] = file 
                            elif dummy[1]=='raw':
                                rlist[dummy[3]] = file
                            else:
                                print("unexpected file in the folder!")
214
#                                foutf.write(datetime.datetime.now(),"unexpected file in the folder",prog_time,"\n")
Holger Niemann's avatar
Holger Niemann committed
215
216
217
218
219
                    print(datetime.datetime.now(),"   Start NUC upload")
                    # upload of NUC files, background files
                    if cameras[i]=="IRCAM":
                        newversion=reupload
                        if background or nuc:
Holger Niemann's avatar
Holger Niemann committed
220
                            for t_exp in range(1,10):
Holger Niemann's avatar
Holger Niemann committed
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
                                try:
                                    print(datetime.datetime.now(),"      prepare upload of nuc data set for "+str(t_exp)+"us")
                                    fb = h5.File(os.path.join(path, blist[str(t_exp)+"us"]),'r')
                                    background_image = np.asarray(fb[list(fb.keys())[0]])
                                    fb.close()
                                    fg = h5.File(os.path.join(path, glist[str(t_exp)+"us"]),'r')
                                    gain_image = np.asarray(fg[list(fg.keys())[0]])
                                    fg.close()
                                    fo = h5.File(os.path.join(path, olist[str(t_exp)+"us"]),'r')
                                    offset_image = np.asarray(fo[list(fo.keys())[0]])
                                    fo.close()
                                    fc = h5.File(os.path.join(path, clist[str(t_exp)+"us"]),'r')
                                    cold_image = np.asarray(fc[list(fc.keys())[0]])
                                    fc.close()
                                    bad = []
                                    ftime = blist[str(t_exp)+"us"].split("_")[3].split(".")
                                    ftime = list(map(int, ftime))
                                    ftime[3] = ftime[3]*1000            #µs not ms!
                                    time_stamp = int(IRup.TimeToNs(date,ftime))
                                    #time_stamp = time_stamp #+ 6 * 6E10
                                    if background:
                                        try:
                                            IRup.upload_Background_frame(cameras[i], port, [time_stamp], [t_exp],
                										background_image, newversion=newversion,reason=reason)
                                        except Exception as E:
                                            print("upload of background files for "+str(t_exp)+"us failed")
                                            print(E) 
248
#                                            foutf.write(datetime.datetime.now(),"upload of background files for "+str(t_exp)+"us failed",E,"\n")
Holger Niemann's avatar
Holger Niemann committed
249
250
                                    else:
                                        print("background upload deactivated")
251
#                                        foutf.write(datetime.datetime.now(),"background upload deactivated","\n")
Holger Niemann's avatar
Holger Niemann committed
252
253
254
255
256
257
258
                                    if nuc:
                                        try:
                                            IRup.upload_NUC_ref(cameras[i], port, [time_stamp], [t_exp], 
                                                                gain_image, offset_image, cold_image, bad,parlog=nuc_parlog,data=nuc,newversion=newversion,reason=reason)
                                        except Exception as E:
                                            print("upload of NUC files for "+str(t_exp)+"us failed")
                                            print(E) 
259
#                                            foutf.write(datetime.datetime.now(),"upload of NUC files for "+str(t_exp)+"us failed",E,"\n")
Holger Niemann's avatar
Holger Niemann committed
260
261
                                    else:
                                        print("nuc upload deactivated")
262
#                                        foutf.write(datetime.datetime.now(),"nuc upload deactivated","\n")
Holger Niemann's avatar
Holger Niemann committed
263
264
265
                                    newversion=False#otherwise 6 new versions!
                                except Exception as E:
                                    print(E)
266
#                                    foutf.write(datetime.datetime.now(),"error in reading",E,"\n")
Holger Niemann's avatar
Holger Niemann committed
267
268
                        else:
                            print("background and NUC upload deactivated")
269
#                            foutf.write(datetime.datetime.now(),"background and NUC upload deactivated","\n")
Holger Niemann's avatar
Holger Niemann committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
        			# -->| tested --> works for IRCAM
                    else: #case for the Infratec files
                        newversion=reupload
                        fkeys=list(blist.keys())
                        print(datetime.datetime.now(),"      prepare upload of background data set for "+str(fkeys[0]))
                        filename=blist[fkeys[0]]
                        fb = h5.File(os.path.join(path, filename),'r')
                        background_image = np.asarray(fb[list(fb.keys())[0]])				
                        ftime = filename.split("_")[3].split(".")
                        ftime = list(map(int, ftime))
                        ftime[3] = ftime[3]*1000            #µs not ms!
                        time_stamp = IRup.TimeToNs(date,ftime)
                        
                        t_exp=int(filename.split("_")[7].split("us")[0])
                        cfilter=int(filename.split("_")[5])	
                        if background:                    
                            try:
                                IRup.upload_Background_frame(cameras[i], port, [time_stamp], [t_exp,cfilter],
            										background_image, newversion=newversion,reason=reason)
                            except Exception as E:
                                print("upload of background file for "+str(t_exp)+"us failed")
                                print(E)
292
#                                foutf.write(datetime.datetime.now(),"upload of background file for "+str(t_exp)+"us failed",E,"\n")
Holger Niemann's avatar
Holger Niemann committed
293
294
                        else:
                            print("background upload deactivated")
295
#                            foutf.write(datetime.datetime.now(),"background upload deactivated","\n")
Holger Niemann's avatar
Holger Niemann committed
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
                    
                    newversion=reupload      									
        #%%			###upload now raw files###
                    fkeys=list(rlist.keys())
                    ftime1=fkeys[0].split(".")
                    ftime1 = list(map(int, ftime1))
                    ftime1[3]=ftime1[3]*1000#µs not ms!
                    ftime2=fkeys[1].split(".")
                    ftime2 = list(map(int, ftime2))
                    ftime2[3]=ftime2[3]*1000#µs not ms!
                    t1=int(IRup.TimeToNs(date,ftime1))
                    t2=int(IRup.TimeToNs(date,ftime2))
                    if t1>t2:
                        f1=1
                        f2=0
                        dummy=t2
                        t2=t1
                        t1=dummy
                    else:
                        f1=0
                        f2=1
    #                bla='09.34.44.100000'#'09.29.23.172000'
    #                bla=bla.split(".")
    #                bla = list(map(int, bla))
    #                t2=int(IRup.TimeToNs(date,bla))
                    hf1=h5.File(path+"\\"+rlist[fkeys[f1]],'r')
                    attri1=hf1['dynamic_attributes']
                    metadata1={
        				'serialnumer': rlist[fkeys[f1]].split("_")[-1].split(".")[0],
        				'width':int(np.asarray(attri1['ROI_w'])[0]),
        				'heigth':int(np.asarray(attri1['ROI_h'])[0]),
        				'pos_left':int(np.asarray(attri1['ROI_x'])[0]),
328
        				'pos_top':int(np.asarray(attri1['ROI_y'])[0]),
Holger Niemann's avatar
Holger Niemann committed
329
        				'bitdepth': int(np.asarray(attri1['bitdepth'])[0])
330
331
                    }

Holger Niemann's avatar
Holger Niemann committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        	#        tmeta1={'divertortemperature_K':np.asarray(attri1['divertor_temp_K'])[0],
        	#                'timestamp':np.asarray(attri1['timestamp'])[0]}
                    time1=np.asarray(hf1['timestamps'])
                    sensor1=np.asarray(attri1['sensor_temp_C'])
                    chip1=np.asarray(attri1['camera_temp_C'])
                    framerate1=np.asarray(attri1['frame_rate'])
                    expo1=np.asarray(attri1['integration_time_us'])			
                    hf2=h5.File(path+"\\"+rlist[fkeys[f2]],'r')
                    attri2=hf2['dynamic_attributes']
                    metadata2={
        				'serialnumer': rlist[fkeys[f1]].split("_")[-1].split(".")[0],
        				'width':int(np.asarray(attri2['ROI_w'])[0]),
        				'heigth':int(np.asarray(attri2['ROI_h'])[0]),
        				'pos_left':int(np.asarray(attri2['ROI_x'])[0]),
346
        				'pos_top':int(np.asarray(attri2['ROI_y'])[0]),
Holger Niemann's avatar
Holger Niemann committed
347
        				'bitdepth': int(np.asarray(attri2['bitdepth'])[0])
348
349
                    }
                    
Holger Niemann's avatar
Holger Niemann committed
350
351
352
353
354
355
356
357
358
359
360
        	#        tmeta2={'divertortemperature_K':np.asarray(attri2['divertor_temp_K'])[0],
        	#                'timestamp':np.asarray(attri2['timestamp'])[0]}
                    time2=np.asarray(hf2['timestamps'])
                    sensor2=np.asarray(attri2['sensor_temp_C'])
                    chip2=np.asarray(attri2['camera_temp_C'])
                    framerate2=np.asarray(attri2['frame_rate'])
                    expo2=np.asarray(attri2['integration_time_us'])
                    if cameras[i]=="IRCAM":
                        metadata1['cameraname']= 'IRCam_Caleo768kL'
                        metadata2['cameraname']= 'IRCam_Caleo768kL'
                        metadata1['purpose']='NUC+background'
361
                        metadata2['purpose']='experiment'                        
Holger Niemann's avatar
Holger Niemann committed
362
363
364
365
        				#['ROI_h', 'ROI_w', 'ROI_x', 'ROI_y', 'Size', 
        				  #'bitdepth', 'camera_temp_C', 'divertor_temp_K', 'frame_number', 
        				  #'frame_rate', 'integration_time_us', 'sensor_temp_C', 'timestamp']            
                    else:
366
367
                        metadata1['cameraname']= 'INFRATEC_IMAGEIR9300'                                                
                        metadata2['cameraname']= 'INFRATEC_IMAGEIR9300'                       
Holger Niemann's avatar
Holger Niemann committed
368
369
370
                        metadata1['purpose']='background'
                        metadata2['purpose']='experiment'
                        #metadata2['multi_exposure']=np.asarray(attri2['multi_exposure'])[0]
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
                        #metadata1['multi_exposure']=np.asarray(attri1['multi_exposure'])[0]
                    if date[0]>=2018:
                        metadata1['software_version']=hf1['images'].attrs['software_version'].decode("UTF-8")
                        metadata1['filter']=int(np.asarray(attri1['filter'])[0])
                        metadata1['view_flip_h']=bool(np.asarray(attri1['view_flip_h'])[0])
                        metadata1['view_flip_v']=bool(np.asarray(attri1['view_flip_v'])[0])
                        metadata2['software_version']=hf2['images'].attrs['software_version'].decode("UTF-8")
                        metadata2['filter']=int(np.asarray(attri2['filter'])[0])
                        metadata2['view_flip_h']=bool(np.asarray(attri2['view_flip_h'])[0])
                        metadata2['view_flip_v']=bool(np.asarray(attri2['view_flip_v'])[0])
                        metadata1['cameraname']= hf1['images'].attrs['camera'].decode("UTF-8")                                             
                        metadata2['cameraname']= hf2['images'].attrs['camera'].decode("UTF-8")                    
                        framenumber1=np.asarray(attri1['frame_number'])
                        framenumber2=np.asarray(attri2['frame_number'])
                        try:
386
387
388
389
390
391
392
393
394
395
396
                            frametype1=np.asarray(attri1['frame_type'])
#                            frametype1=[]
#                            for ele in ftype1:
#                                if ele == "nuc":
#                                    frametype1.append(0)
#                                elif ele == "background":
#                                    frametype1.append(1)
#                                elif ele == "discharge":
#                                    frametype1.append(2)
#                                else:
#                                    frametype1.append(-1)
397
398
399
                        except:                                
                            frametype1=np.zeros((len(framenumber1)))-1
                        try:
400
401
402
403
404
405
406
407
408
409
410
                            frametype2=np.asarray(attri2['frame_type'])
#                            frametype2=[]
#                            for ele in ftype2:
#                                if ele == "nuc":
#                                    frametype2.append(0)
#                                elif ele == "background":
#                                    frametype2.append(1)
#                                elif ele == "discharge":
#                                    frametype2.append(2)
#                                else:
#                                    frametype2.append(-1)
411
412
                        except:
                            frametype2=np.zeros((len(framenumber2)))-1
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
                        wrong1,correction_list1=IRup.check_timevector_framenumbers(time1,framenumber1)
                        wrong2,correction_list2=IRup.check_timevector_framenumbers(time2,framenumber2)
                        time1_0=time1.copy()
                        time2_0=time2.copy()
                        if wrong1:
                            sensor1=np.delete(sensor1,correction_list1)
                            chip1=np.delete(chip1,correction_list1)
                            framerate1=np.delete(framerate1,correction_list1)
                            frametype1=np.delete(frametype1,correction_list1)
                            expo1=np.delete(expo1,correction_list1)                            
                            time1=np.delete(time1,correction_list1)
                        if wrong2:
                            sensor2=np.delete(sensor2,correction_list2)
                            chip2=np.delete(chip2,correction_list2)
                            framerate2=np.delete(framerate2,correction_list2)
                            frametype2=np.delete(frametype2,correction_list2)
                            expo2=np.delete(expo2,correction_list2)                            
                            time2=np.delete(time2,correction_list1)
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                        channel_names=["sensortemperature","cameratemperature","framerate","exposuretime","framenumber","frametype"]
                        channel_units=["oC","oC",'Hz','us','none','none']
                        channel_values1=[sensor1, chip1,framerate1,expo1,framenumber1,frametype1]
                        channel_values2=[sensor2, chip2,framerate2,expo2,framenumber2,frametype2]
                        comment_meta1="frametype: 0 cold, 1 background, 2 discharge, -1 unknown"
                        comment_meta2="frametype: 0 cold, 1 background, 2 discharge, -1 unknown"
                        triggered=np.asarray(attri2['triggered'])
                        trig_index=np.where(triggered==1)
                        if len(trig_index[0])>0:
                            trig_index=min(trig_index[0])
                            toff=(time2[0]-time2[trig_index])/1e6
                        else:
                            print("triggered frame was not found! Assumeing that frame 0 is the t1 frame")
                            toff=0
                        print(datetime.datetime.now(),"   regenerate timevectors")
                        time1=IRup.regenerate_timevector(time1,0,t2,0)
447
                        time2,Program=IRup.regenerate_timevector(time2,1,t2,toff,True)
448
                        time1_0=IRup.regenerate_timevector(time1_0,0,t2,0)
Holger Niemann's avatar
Holger Niemann committed
449
                        time2_0=IRup.regenerate_timevector(time2_0,1,t2,toff)
450
                    else:
451
452
                        correction_list1=[]
                        correction_list2=[]
453
454
455
456
457
458
459
460
                        channel_names=["sensortemperature","cameratemperature","framerate","exposuretime"]
                        channel_units=["oC","oC",'Hz','us']
                        channel_values1=[sensor1, chip1,framerate1,expo1]
                        channel_values2=[sensor2, chip2,framerate2,expo2]
                        comment_meta1=""
                        comment_meta2=""
                        print(datetime.datetime.now(),"   regenerate timevectors")
                        time1=IRup.regenerate_timevector(time1,0,t2,0)
461
462
463
                        time2,Program=IRup.regenerate_timevector(time2,[1,6],t2,0,True)
                        time1_0=time1
                        time2_0=time2
Holger Niemann's avatar
Holger Niemann committed
464
465
466
                    hf1.close()
                    del attri1
                    hf2.close()            
467
468
                    del attri2	                        
                    
Holger Niemann's avatar
Holger Niemann committed
469
470
471
                    print(datetime.datetime.now(),"   Upload Meta Stream(s)")
                    if metastream:
                        try:
472
473
474
475
476
                            meta1up=IRup.upload_meta_stream_V2(cameras[i],port,np.asarray(time1),channel_names,channel_units,channel_values1,newversion=newversion,reason=reason, Return=True)
                            if meta1up:
                                metares1=1
                            else:
                                metares1=0
Holger Niemann's avatar
Holger Niemann committed
477
478
                        except Exception as E:
                            print(E)
479
                            metares1=0
480
#                            foutf.write(datetime.datetime.now(),"metastream 1 upload failed",E,"\n")
Holger Niemann's avatar
Holger Niemann committed
481
482
                        del sensor1, chip1,framerate1,expo1
                        try:
483
484
485
486
487
                            meta2up=IRup.upload_meta_stream_V2(cameras[i],port,np.asarray(time2),channel_names,channel_units,channel_values2,newversion=newversion,reason=reason, Return=True)
                            if meta2up:
                                metares2=1
                            else:
                                metares2=0
Holger Niemann's avatar
Holger Niemann committed
488
489
                        except Exception as E:
                            print(E)  
490
                            metares2=0
491
#                            foutf.write(datetime.datetime.now(),"metastream 2 upload failed",E,"\n")
Holger Niemann's avatar
Holger Niemann committed
492
493
494
                        del sensor2,chip2,framerate2,expo2
                    else:
                        print("metastream upload deactivated")
495
496
                        metares1=0
                        metares2=0
497
#                        foutf.write(datetime.datetime.now(),"metastream upload deactivated","\n")
Holger Niemann's avatar
Holger Niemann committed
498
499
500
501
502
503
504
                    if raw_parlog or raw_data:                    
                        print(datetime.datetime.now(),"   Start Raw Data Upload for "+str(prog_time))
                        if raw1:
                            success=False
                            trys=0
                            while (trys<retry and not success):
                                try:
505
                                    response=IRup.upload_raw_data_from_H5(cameras[i],port,np.asarray(time1_0,dtype=np.int64),path+"\\"+rlist[fkeys[f1]],metadata1,parlog=raw_parlog,rawdata=raw_data,newversion=newversion,reason=reason,del_list=correction_list1,temppath=temppath)#upload cold and backgrounds
Holger Niemann's avatar
Holger Niemann committed
506
507
                                    if response[0]:
                                        success=True
508
                                        rawres1=1
Holger Niemann's avatar
Holger Niemann committed
509
510
                                    else:
                                        print(datetime.datetime.now(),"raw 1 upload failed",response[1],response[2])
511
                                        rawres1=0
512
#                                        foutf.write(datetime.datetime.now(),"raw 1 upload failed",response[1],response[2],"\n")
Holger Niemann's avatar
Holger Niemann committed
513
514
515
                                except Exception as E:
                                    trys+=1
                                    print("Upload for rawfile 1 failed!, try:"+str(trys),E)
516
                                    rawres1=0
517
#                                    foutf.write(datetime.datetime.now(),"Upload for rawfile 1 failed!, try:"+str(trys),E,"\n")
Holger Niemann's avatar
Holger Niemann committed
518
                                    
519
520
                        else:
                            rawres1=0
Holger Niemann's avatar
Holger Niemann committed
521
522
523
524
525
526
                        if raw2:
                            success=False
                            trys=0
                            while (trys<retry and not success):
                                print(datetime.datetime.now(),"      Upload second file")
                                try:
527
                                    response=IRup.upload_raw_data_from_H5(cameras[i],port,np.asarray(time2_0,dtype=np.int64),path+"\\"+rlist[fkeys[f2]],metadata2,parlog=raw_parlog,rawdata=raw_data,del_list=correction_list2,temppath=temppath)#upload raw images from plasma
Holger Niemann's avatar
Holger Niemann committed
528
529
                                    if response[0]:
                                        success=True
530
                                        rawres2=1
Holger Niemann's avatar
Holger Niemann committed
531
532
                                    else:
                                        print(datetime.datetime.now(),"raw 2 upload failed",response[1],response[2])
533
                                        rawres2=0
534
#                                        foutf.write(datetime.datetime.now(),"raw 2 upload failed",response[1],response[2],"\n")
Holger Niemann's avatar
Holger Niemann committed
535
536
537
                                except Exception as E:
                                    trys+=1
                                    print("Upload for rawfile 2 failed!, try"+str(trys),E)
538
                                    rawres2=0
539
#                                    foutf.write(datetime.datetime.now(),"Upload for rawfile 2 failed!, try:"+str(trys),E,"\n")
540
541
                        else:
                            rawres2=0
Holger Niemann's avatar
Holger Niemann committed
542
543
544
                        print(datetime.datetime.now(),"      Upload finished")
                    else:
                        print("raw upload deactivated")
545
546
547
                        rawres1=0
                        rawres2=0
                    f_success.write(Program+" "+str(prog_time)+" "+str(metares1)+" "+str(metares2)+" "+str(rawres1)+" "+str(rawres2)+" \n")
548
                    f_success.flush()
549
#                        foutf.write(datetime.datetime.now(),"raw upload deactivated","\n")
Holger Niemann's avatar
Holger Niemann committed
550
                else:#folder is empty
551
                    print("no files in",prog_time)
Holger Niemann's avatar
Holger Niemann committed
552
553
554
555
                reupload=False
            except Exception as E:
                print("Cannot Upload Data for folder "+str(prog_time))
                print(E)
556
#                foutf.write(datetime.datetime.now(),"Cannot Upload Data for folder "+str(prog_time),"\n")
Holger Niemann's avatar
Holger Niemann committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
            ###upload the temp files if allowed###
            """
            if Tup[i]:
                path=tempdatapath.format(pc_list[i],date)+"\\"+prog_time
                filelist=os.listdir(path)
                #assumption that there are only temp files 
                fkeys=list(rlist.keys())
                ftime1=fkeys[0].split(".")
                for e in range(len(ftime1)):
                    ftime1[e]=int(ftime1[e])
                ftime1[3]=ftime1[3]*1000#µs not ms!
                ftime2=fkeys.split(".")
                for e in range(len(ftime2)):
                    ftime2[e]=int(ftime2[e])
                ftime2[3]=ftime2[3]*1000#µs not ms!
                t1=IRup.TimeToNs(date,ftime1)
                t2=IRup.TimeToNs(date,ftime2)
                if t1>t2:
                    f1=1
                    f2=0
                    dummy=t2
                    t2=t1
                    t1=dummy
                else:
                    f1=0
                    f2=1
                IRup.upload_temperaure_data(cameras[i],port,images1,time1,tmeta1)
                del images1, time1, tmeta1
                IRup.upload_temperaure_data(cameras[i],port,images2,time2,tmeta2)
                del images2, time2, tmeta2
    #"""
    ##[o for o in os.listdir(path) if os.path.isdir(os.path.join(path,o)) and (o[0]=='2')]
    ##"{:02d}".format(zahl)
    
fout.close()
592
f_success.close()
593
#foutf.close()
594
595
sys.stdout = original
char=input("upload is done, press any button to close the program.")