From 1e537d25ed715cf4989060d0119417cf257d2326 Mon Sep 17 00:00:00 2001
From: Holger Niemann <holger.niemann@ipp.mpg.de>
Date: Wed, 6 Jun 2018 17:32:21 +0200
Subject: [PATCH] change to reconstruct cold frames and swapped cameras
 corrections

---
 IR_config_constants.py   | 373 ++++++++++++++++++++++
 IR_image_tools.py        |  63 +++-
 __init__.py              |   3 +
 downloadversionIRdata.py | 670 ++++++++++++++++-----------------------
 4 files changed, 701 insertions(+), 408 deletions(-)
 create mode 100644 IR_config_constants.py
 create mode 100644 __init__.py

diff --git a/IR_config_constants.py b/IR_config_constants.py
new file mode 100644
index 0000000..d2a4bfa
--- /dev/null
+++ b/IR_config_constants.py
@@ -0,0 +1,373 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Tue Jun  5 09:26:06 2018
+
+@author: holn
+constants and config parameters
+"""
+#%% Paths
+archivepath="http://archive-webapi.ipp-hgw.mpg.de/Test/raw/W7X/"
+calibpath="\\\\sv-e4-fs-1\\E4-Mitarbeiter\\E4 Diagnostics\\QIR\\Calibrations\\"
+IRCamColdframes_fittingpath=calibpath+"IRCamColdframes_fitting\\"
+IRCamRefImagespath = calibpath+'IRCamReferenceImages\\'
+
+#%% Dictionaries
+portcamdict={
+    'OP1.2a':{
+        'AEF10': 'IRCam_Caleo768kL_0901',
+        'AEF11': 'IRCam_Caleo768kL_0904',#is corrected from 0906, the cameras was swapped in the campaign in restect to the planned position
+        'AEF20': 'IRCam_Caleo768kL_0702',
+        'AEF21': 'IRCam_Caleo768kL_0906',#is corrected from 0904, the cameras was swapped in the campaign in restect to the planned position
+        'AEF30': 'IRCam_Caleo768kL_0907',
+        'AEF31': 'IRCam_Caleo768kL_0903',
+        'AEF40': 'IRCam_Caleo768kL_0701',
+        'AEF41': 'IRCam_Caleo768kL_0902',
+        'AEF50': 'Infratec_9312907',
+        'AEF51': 'IRCam_Caleo768kL_0905'}
+    	}
+     
+camlimdict={
+    'IRCam_Caleo768kL_0901':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1100),
+        6:(50,850),
+        7:(50,800),
+        8:(50,600),
+        9:(50,550)
+         },
+    'IRCam_Caleo768kL_0902':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1100),
+        6:(50,1000),
+        7:(50,800),
+        8:(50,800),
+        9:(50,650)
+         },
+    'IRCam_Caleo768kL_0903':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1100),
+        6:(50,1000),
+        7:(50,900),
+        8:(50,800),
+        9:(50,700)
+         },
+    'IRCam_Caleo768kL_0904':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1100),
+        6:(50,900),
+        7:(50,800),
+        8:(50,700),
+        9:(50,600)
+         },
+    'IRCam_Caleo768kL_0905':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1100),
+        6:(50,900),
+        7:(50,800),
+        8:(50,700),
+        9:(50,600)
+         },
+    'IRCam_Caleo768kL_0906':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1000),
+        6:(50,800),
+        7:(50,750),
+        8:(50,600),
+        9:(50,550)
+         },
+    'IRCam_Caleo768kL_0907':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1200),
+        5:(50,1100),
+        6:(50,900),
+        7:(50,800),
+        8:(50,700),
+        9:(50,600)
+         },
+    'IRCam_Caleo768kL_0701':        
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1580),
+        5:(50,1100),
+        6:(50,900),
+        7:(50,800),
+        8:(50,750),
+        9:(50,700)
+         },
+    'IRCam_Caleo768kL_0702':
+        {
+        1:(50,5000),
+        2:(50,3000),
+        3:(50,2050),
+        4:(50,1580),
+        5:(50,1150),
+        6:(50,1000),
+        7:(50,800),
+        8:(50,750),
+        9:(50,700)
+         },
+     'Infratec_9312907':
+         {
+             0:{
+             25:(50,400),
+             50:(50,300),
+            100:(50,250),
+            200:(50,200),
+            400:(50,150),
+            800:(50,100)
+             },
+             1:{
+             25:(300,1200),
+             50:(200,800),
+            100:(150,650),
+            200:(100,500),
+            400:(50,400),
+            800:(50,300)
+             },
+            2:{
+             25:(200,1200),
+             50:(200,1200),
+            100:(200,1200),
+            200:(200,1200),
+            400:(200,1000),
+            800:(200,700)
+             },
+         }
+}
+
+valid_FOV_circle = {
+    10: [562, 364, 550],#, 600],
+    11: [502, 404, 520],#, 570]
+    20: [535, 374, 510],
+    21: [548, 404, 505],
+    30: [517, 389, 520],
+    31: [562, 364, 520],
+    40: [542, 394, 520],
+    41: [522, 394, 520],
+    50: [640, 454, 620],
+    51: [512, 379, 510]
+    }
+
+valid_background_rectangle = {
+    10: [500,0 ,800 ,100],#10: [300,0 ,800 ,200],
+    11: [50,200,170,290],#11: [200,100,700,200],
+    20: [100,100,275,135],#20: [100,100,400,200],
+    21: [300,0,700,75],#21: [150,0,800,200],
+    30: [200,0,800,100],#30: [200,0,800,200],#
+    31: [200,100,700,200],#
+    40: [550,0,690,140],#
+    41: [120,100,250,200],#
+    50: [390,940,880,1010],#
+    51: [350,0,670,120]#
+    }
+    
+TC_port={
+    10:[10],
+    11:[10],
+    20:[20],
+    21:[20],
+    30:[30],
+    31:[30],
+    40:[10,20,30],
+    41:[10,20,30],
+    50:[50],
+    51:[10,20,30]
+    }
+TC_channel={
+    10:[[66,67],["ActV1QSR10CT001","ActV1QSR10CT002"]], 
+    20:[[72,73],["ActV1QSR20CT001","ActV1QSR20CT002"]],
+    30:[[78,79],["ActV1QSR30CT001","ActV1QSR30CT002"]],
+    50:[[90,91,84,85],["ActV1QRT50CT001","ActV1QRT50CT002","ActV1QRT50CT003","ActV1QRT50CT004"]]
+    }
+    
+TC_database={
+    50: {
+        0: {'Y': -4220.44, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 2947.882, 'Targetelement': 4}, 
+        1: {'Y': -4507.509, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 2673.037, 'Targetelement': 4}, 
+        2: {'Y': -4834.308, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 2339.889, 'Targetelement': 5}, 
+        3: {'Y': -5124.416, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1998.605, 'Targetelement': 5}, 
+        4: {'Y': -5862.386, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 430.664, 'Targetelement': 4}, 
+        5: {'Y': -5971.865, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -15.031, 'Targetelement': 6}, 
+        6: {'Y': -6065.916, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -697.011, 'Targetelement': 6}, 
+        7: {'Y': -4068.477, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 2704.554, 'Targetelement': 5}, 
+        8: {'Y': -4495.307, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 2346.682, 'Targetelement': 6}, 
+        9: {'Y': -4841.007, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1981.551, 'Targetelement': 6}, 
+        10: {'Y': -5872.346, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 24.186, 'Targetelement': 0}, 
+        11: {'Y': -5910.584, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 5.577, 'Targetelement': 0}, 
+        12: {'Y': -4703.059, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 2309.024, 'Targetelement': 0}, 
+        13: {'Y': -4945.311, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 2464.537, 'Targetelement': 0}, 
+        14: {'Y': -4431.787, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 2325.89, 'Targetelement': 0}, 
+        15: {'Y': -4322.24, 'Z': 714.53, 'Targetmodule': 'DR', 'active': True, 'X': 2374.085, 'Targetelement': 0}}, 
+    51: {0: {'Y': -5147.13, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 95.826, 'Targetelement': 4}, 
+         1: {'Y': -5217.823, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 486.915, 'Targetelement': 4}, 
+        2: {'Y': -5286.39, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 948.525, 'Targetelement': 5}, 
+        3: {'Y': -5320.49, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1395.15, 'Targetelement': 5}, 
+        4: {'Y': -4995.908, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 3097.409, 'Targetelement': 4}, 
+        5: {'Y': -4822.505, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 3522.334, 'Targetelement': 6}, 
+        6: {'Y': -4497.737, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 4129.35, 'Targetelement': 6}, 
+        7: {'Y': -4881.164, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 203.361, 'Targetelement': 5}, 
+        8: {'Y': -5016.125, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 743.77, 'Targetelement': 6}, 
+        9: {'Y': -5081.183, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1242.364, 'Targetelement': 6}, 
+        10: {'Y': -4779.849, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 3436.923, 'Targetelement': 0}, 
+        11: {'Y': -4860.039, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 3494.011, 'Targetelement': 0}, 
+        12: {'Y': -5163.219, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 896.724, 'Targetelement': 0}, 
+        13: {'Y': -5447.878, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 912.415, 'Targetelement': 0}, 
+        14: {'Y': -4893.659, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 704.131, 'Targetelement': 0}, 
+        15: {'Y': -4949.37, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 638.444, 'Targetelement': 0}}, 
+    20: {0: {'Y': 5147.13, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 95.826, 'Targetelement': 4}, 
+         1: {'Y': 5217.823, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 486.915, 'Targetelement': 4}, 
+        2: {'Y': 5286.39, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 948.525, 'Targetelement': 5}, 
+        3: {'Y': 5320.49, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1395.15, 'Targetelement': 5}, 
+        4: {'Y': 4995.908, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 3097.409, 'Targetelement': 4}, 
+        5: {'Y': 4822.505, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 3522.334, 'Targetelement': 6}, 
+        6: {'Y': 4497.737, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 4129.35, 'Targetelement': 6}, 
+        7: {'Y': 4881.164, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 203.361, 'Targetelement': 5}, 
+        8: {'Y': 5016.125, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 743.77, 'Targetelement': 6}, 
+        9: {'Y': 5081.183, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1242.364, 'Targetelement': 6}, 
+        10: {'Y': 4765.044, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 3432.112, 'Targetelement': 0}, 
+        11: {'Y': 4785.041, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 3469.642, 'Targetelement': 0}, 
+        12: {'Y': 5162.065, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 896.349, 'Targetelement': 0}, 
+        13: {'Y': 5449.459, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 912.928, 'Targetelement': 0}, 
+        14: {'Y': 4952.514, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 723.255, 'Targetelement': 0}, 
+        15: {'Y': 4892.218, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 619.874, 'Targetelement': 0}}, 
+    21: {0: {'Y': 4220.44, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 2947.882, 'Targetelement': 4}, 
+         1: {'Y': 4507.509, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': False, 'X': 2673.037, 'Targetelement': 4}, 
+        2: {'Y': 4834.308, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 2339.889, 'Targetelement': 5}, 
+        3: {'Y': 5124.416, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1998.605, 'Targetelement': 5}, 
+        4: {'Y': 5862.386, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 430.664, 'Targetelement': 4}, 
+        5: {'Y': 5971.865, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -15.031, 'Targetelement': 6}, 
+        6: {'Y': 6065.916, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -697.011, 'Targetelement': 6}, 
+        7: {'Y': 4068.477, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 2704.554, 'Targetelement': 5}, 
+        8: {'Y': 4495.307, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 2346.682, 'Targetelement': 6}, 
+        9: {'Y': 4841.007, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1981.551, 'Targetelement': 6}, 
+        10: {'Y': 5887.151, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 28.996, 'Targetelement': 0}, 
+        11: {'Y': 5985.582, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 29.945, 'Targetelement': 0}, 
+        12: {'Y': 4704.213, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 2309.399, 'Targetelement': 0}, 
+        13: {'Y': 4943.73, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 2464.024, 'Targetelement': 0}, 
+        14: {'Y': 4372.932, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 2306.766, 'Targetelement': 0}, 
+        15: {'Y': 4379.393, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 2392.655, 'Targetelement': 0}}, 
+    40: {0: {'Y': -4107.791, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -3102.931, 'Targetelement': 4}, 
+         1: {'Y': -3935.106, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -3460.882, 'Targetelement': 4}, 
+        2: {'Y': -3719.25, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -3874.635, 'Targetelement': 5}, 
+        3: {'Y': -3484.318, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4256.006, 'Targetelement': 5}, 
+        4: {'Y': -2221.163, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -5442.378, 'Targetelement': 4}, 
+        5: {'Y': -1831.112, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -5684.226, 'Targetelement': 6}, 
+        6: {'Y': -1211.574, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -5984.417, 'Targetelement': 6}, 
+        7: {'Y': -3829.412, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -3033.599, 'Targetelement': 5}, 
+        8: {'Y': -3620.954, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -3550.127, 'Targetelement': 6}, 
+        9: {'Y': -3380.521, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -3991.738, 'Targetelement': 6}, 
+        10: {'Y': -1837.657, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -5577.46, 'Targetelement': 0}, 
+        11: {'Y': -1831.775, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -5619.576, 'Targetelement': 0}, 
+        12: {'Y': -3649.338, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -3759.348, 'Targetelement': 0}, 
+        13: {'Y': -3872.099, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -3941.686, 'Targetelement': 0}, 
+        14: {'Y': -3581.55, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -3496.14, 'Targetelement': 0}, 
+        15: {'Y': -3593.534, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -3377.062, 'Targetelement': 0}}, 
+    41: {0: {'Y': -1681.686, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -4865.6, 'Targetelement': 4}, 
+         1: {'Y': -2075.48, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -4811.98, 'Targetelement': 4}, 
+        2: {'Y': -2535.686, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -4734.545, 'Targetelement': 5}, 
+        3: {'Y': -2970.989, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4628.962, 'Targetelement': 5}, 
+        4: {'Y': -4489.632, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -3794.239, 'Targetelement': 4}, 
+        5: {'Y': -4840.175, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -3498.014, 'Targetelement': 6}, 
+        6: {'Y': -5317.122, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -3001.562, 'Targetelement': 6}, 
+        7: {'Y': -1701.77, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -4579.421, 'Targetelement': 5}, 
+        8: {'Y': -2257.435, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -4540.781, 'Targetelement': 6}, 
+        9: {'Y': -2751.73, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -4448.581, 'Targetelement': 6}, 
+        10: {'Y': -4745.762, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -3483.839, 'Targetelement': 0}, 
+        11: {'Y': -4824.836, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -3542.463, 'Targetelement': 0}, 
+        12: {'Y': -2448.358, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -4633.41, 'Targetelement': 0}, 
+        13: {'Y': -2551.245, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -4899.289, 'Targetelement': 0}, 
+        14: {'Y': -2181.893, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -4436.558, 'Targetelement': 0}, 
+        15: {'Y': -2136.636, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -4509.841, 'Targetelement': 0}}, 
+    10: {0: {'Y': 1499.415, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 4924.823, 'Targetelement': 4}, 
+         1: {'Y': 1149.312, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 5112.91, 'Targetelement': 4}, 
+        2: {'Y': 731.483, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 5320.766, 'Targetelement': 5}, 
+        3: {'Y': 317.255, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 5491.212, 'Targetelement': 5}, 
+        4: {'Y': -1401.991, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 5708.543, 'Targetelement': 4}, 
+        5: {'Y': -1859.703, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 5674.936, 'Targetelement': 6}, 
+        6: {'Y': -2537.368, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 5553.641, 'Targetelement': 6}, 
+        7: {'Y': 1314.955, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 4705.105, 'Targetelement': 5}, 
+        8: {'Y': 842.701, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 5000.456, 'Targetelement': 6}, 
+        9: {'Y': 388.614, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 5216.404, 'Targetelement': 6}, 
+        10: {'Y': -1791.653, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 5592.407, 'Targetelement': 0}, 
+        11: {'Y': -1821.167, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 5623.023, 'Targetelement': 0}, 
+        12: {'Y': 742.687, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 5186.403, 'Targetelement': 0}, 
+        13: {'Y': 815.729, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 5464.854, 'Targetelement': 0}, 
+        14: {'Y': 842.555, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 4933.619, 'Targetelement': 0}, 
+        15: {'Y': 922.243, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 4844.327, 'Targetelement': 0}}, 
+    11: {0: {'Y': -1499.415, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 4924.823, 'Targetelement': 4}, 
+         1: {'Y': -1149.312, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 5112.91, 'Targetelement': 4}, 
+        2: {'Y': -731.483, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 5320.766, 'Targetelement': 5}, 
+        3: {'Y': -317.255, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 5491.212, 'Targetelement': 5}, 
+        4: {'Y': 1401.991, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 5708.543, 'Targetelement': 4}, 
+        5: {'Y': 1859.703, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 5674.936, 'Targetelement': 6}, 
+        6: {'Y': 2537.368, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 5553.641, 'Targetelement': 6}, 
+        7: {'Y': -1314.955, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 4705.105, 'Targetelement': 5}, 
+        8: {'Y': -842.701, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 5000.456, 'Targetelement': 6}, 
+        9: {'Y': -388.614, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 5216.404, 'Targetelement': 6}, 
+        10: {'Y': 1791.653, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 5607.974, 'Targetelement': 0}, 
+        11: {'Y': 1821.167, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 5701.88, 'Targetelement': 0}, 
+        12: {'Y': -742.687, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 5187.616, 'Targetelement': 0}, 
+        13: {'Y': -815.729, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 5463.192, 'Targetelement': 0}, 
+        14: {'Y': -842.555, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 4871.735, 'Targetelement': 0}, 
+        15: {'Y': -922.243, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 4904.421, 'Targetelement': 0}}, 
+    30: {0: {'Y': 1681.686, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -4865.6, 'Targetelement': 4}, 
+         1: {'Y': 2075.48, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -4811.98, 'Targetelement': 4}, 
+        2: {'Y': 2535.686, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -4734.545, 'Targetelement': 5}, 
+        3: {'Y': 2970.989, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4628.962, 'Targetelement': 5}, 
+        4: {'Y': 4489.632, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -3794.239, 'Targetelement': 4}, 
+        5: {'Y': 4840.175, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -3498.014, 'Targetelement': 6}, 
+        6: {'Y': 5317.122, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -3001.562, 'Targetelement': 6}, 
+        7: {'Y': 1701.77, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -4579.421, 'Targetelement': 5}, 
+        8: {'Y': 2257.435, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -4540.781, 'Targetelement': 6}, 
+        9: {'Y': 2751.73, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -4448.581, 'Targetelement': 6}, 
+        10: {'Y': 4736.612, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -3471.245, 'Targetelement': 0}, 
+        11: {'Y': 4778.485, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -3478.666, 'Targetelement': 0}, 
+        12: {'Y': 2447.645, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -4632.429, 'Targetelement': 0}, 
+        13: {'Y': 2552.222, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -4900.633, 'Targetelement': 0}, 
+        14: {'Y': 2218.267, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -4486.623, 'Targetelement': 0}, 
+        15: {'Y': 2101.314, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -4461.224, 'Targetelement': 0}}, 
+    31: {0: {'Y': 4107.791, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -3102.931, 'Targetelement': 4}, 
+         1: {'Y': 3935.106, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -3460.882, 'Targetelement': 4}, 
+        2: {'Y': 3719.25, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -3874.635, 'Targetelement': 5}, 
+        3: {'Y': 3484.318, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4256.006, 'Targetelement': 5}, 
+        4: {'Y': 2221.163, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -5442.378, 'Targetelement': 4}, 
+        5: {'Y': 1831.112, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -5684.226, 'Targetelement': 6}, 
+        6: {'Y': 1211.574, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -5984.417, 'Targetelement': 5}, 
+        7: {'Y': 3829.412, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -3033.599, 'Targetelement': 5}, 
+        8: {'Y': 3620.954, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -3550.127, 'Targetelement': 6}, 
+        9: {'Y': 3380.521, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -3991.738, 'Targetelement': 6}, 
+        10: {'Y': 1846.807, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -5590.053, 'Targetelement': 0}, 
+        11: {'Y': 1878.126, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -5683.373, 'Targetelement': 0}, 
+        12: {'Y': 3650.051, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -3760.329, 'Targetelement': 0}, 
+        13: {'Y': 3871.122, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -3940.342, 'Targetelement': 0}, 
+        14: {'Y': 3545.175, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -3446.075, 'Targetelement': 0}, 
+        15: {'Y': 3628.857, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -3425.679, 'Targetelement': 0}}}
\ No newline at end of file
diff --git a/IR_image_tools.py b/IR_image_tools.py
index 8725689..fcdd289 100644
--- a/IR_image_tools.py
+++ b/IR_image_tools.py
@@ -2,7 +2,7 @@
 """
 Created on Wed May  9 14:56:32 2018
 
-@author: holn
+@author: Holger Niemann, Peter Drewelow, Yu Gao
 
 mainly to clean up the downloadversionIRdata code
 Tools for:
@@ -14,6 +14,12 @@ Tools for:
 """
 import numpy as np
 import matplotlib.pyplot as plt
+from IR_config_constants import portcamdict,IRCamRefImagespath
+import h5py
+from os.path import join, basename
+import glob
+
+
 
 def bestimmtheitsmaß_general(data,fit):
     R=0
@@ -79,7 +85,7 @@ def check_coldframe(coldframe,references=None,threshold=0.5,plot_it=False):
 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]//4),int(shapi[1]//2),int(shapi[1]//4*3)]:
+    for i in [int(shapi[1]//5),int(shapi[1]//2),int(shapi[1]//4*3)]:
         dataline=reference_frame[0:shapi[0],i]    
         references.append(dataline-np.mean(dataline))
     return check_coldframe(coldframe,references,threshold,plot_it)
@@ -145,7 +151,7 @@ def correct_images(images,badpixels):
         print("done")
     return images
 
-def restore_pixels(frame, bad_pixel):
+def restore_pixels(frame, bad_pixel):# code from Peter from JET
     # make sure bad pixel are provided as mask and list  
     if type(bad_pixel) is list:
         blist = bad_pixel
@@ -247,9 +253,11 @@ def generate_new_hot_image(cold,reference_cold,reference_hot):
     else:
         return reference_hot+(cold-reference_cold)
     
-def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,reference_hot=None):
+def calculate_gain_offset_image_pix(cold_image,hot_image=None,reference_cold=None,reference_hot=None,bose=1):    
     if hot_image==None:
         hot_image=generate_new_hot_image(cold_image,reference_cold,reference_hot)
+    if bose>0:
+        print("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)    
@@ -257,3 +265,50 @@ def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,re
     Off_c_rel = Sc_ref -   cold_image*Gain_rel    
     Offset_rel  = ( Off_h_rel + Off_c_rel ) /2
     return Gain_rel,Offset_rel
+
+def calculate_gain_offset_image(cold_image,hot_image=None,reference_cold=None,reference_hot=None,bose=1):    
+    if hot_image==None:
+        hot_image=generate_new_hot_image(cold_image,reference_cold,reference_hot)
+    if bose>0:
+        print("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 ) ) ]  
+    print(hot_image[( np.int( np.shape(hot_image)[0]/2) )-2: (np.int( np.shape(hot_image)[0]/2))+3,np.int((np.shape(hot_image)[1]/2))-2:np.int((np.shape(hot_image)[1]/2))+3 ])
+    print(cold_image[( np.int( np.shape(hot_image)[0]/2) )-2: (np.int( np.shape(hot_image)[0]/2))+3,np.int((np.shape(hot_image)[1]/2))-2:np.int((np.shape(hot_image)[1]/2))+3 ])
+    Sh_ref =  np.mean( hot_image[( np.int( np.shape(hot_image)[0]/2) )-2: (np.int( np.shape(hot_image)[0]/2))+3,np.int((np.shape(hot_image)[1]/2))-2:np.int((np.shape(hot_image)[1]/2))+3 ])    
+    Sc_ref =  np.mean(cold_image[( np.int( np.shape(cold_image)[0]/2) )-2: (np.int( np.shape(cold_image)[0]/2))+3,np.int((np.shape(cold_image)[1]/2))-2:np.int((np.shape(cold_image)[1]/2))+3 ])    
+    Gain_rel =  ( Sh_ref  - Sc_ref ) / ( hot_image  - cold_image)    
+    Off_h_rel = Sh_ref -   hot_image*Gain_rel
+    Off_c_rel = Sc_ref -   cold_image*Gain_rel    
+    Offset_rel  = ( Off_h_rel + Off_c_rel ) /2
+    return Gain_rel,Offset_rel    
+
+    
+#%% 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 reconstruct_coldframe (exposuretime, sT, a, bnew, coldref):
+  cirebuild = a * sT + bnew * exposuretime + coldref
+  return cirebuild
\ No newline at end of file
diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..1072fc7
--- /dev/null
+++ b/__init__.py
@@ -0,0 +1,3 @@
+from . import IR_image_tools
+from . import downloadversionIRdata
+from . import IR_config_constants
\ No newline at end of file
diff --git a/downloadversionIRdata.py b/downloadversionIRdata.py
index df8826d..d02102d 100644
--- a/downloadversionIRdata.py
+++ b/downloadversionIRdata.py
@@ -7,6 +7,7 @@ Created on Wed Oct 25 15:51:46 2017
 
 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
 try:
     import W7Xrest.read_restdb as AKF_1
 except:
@@ -16,6 +17,7 @@ import urllib
 import json
 from PIL import Image
 from io import BytesIO
+from os.path import join
 import matplotlib.pyplot as plt
 try:        
     import multiprocessing    
@@ -26,22 +28,6 @@ except Exception as E:
     fastDL=False
 
 
-archivepath="http://archive-webapi.ipp-hgw.mpg.de/Test/raw/W7X/"
-
-portcamdict={
-    'OP1.2a':{
-        'AEF10': 'IRCam_Caleo768kL_0901',
-        'AEF11': 'IRCam_Caleo768kL_0904',#is corrected from 0906, the cameras was swapped in the campaign in restect to the planned position
-        'AEF20': 'IRCam_Caleo768kL_0702',
-        'AEF21': 'IRCam_Caleo768kL_0906',#is corrected from 0904, the cameras was swapped in the campaign in restect to the planned position
-        'AEF30': 'IRCam_Caleo768kL_0907',
-        'AEF31': 'IRCam_Caleo768kL_0903',
-        'AEF40': 'IRCam_Caleo768kL_0701',
-        'AEF41': 'IRCam_Caleo768kL_0902',
-        'AEF50': 'Infratec_9312907',
-        'AEF51': 'IRCam_Caleo768kL_0905'}
-    	}
-
 try:
     config_path="\\\\sv-e4-fs-1\\E4-Mitarbeiter\\E4 Diagnostics\\QIR\\Software\\QI-RealTime\\v1.0.0\\release\\QIR-IrAnalysis\\Config\\Thermal calibration\\"
     FOV=plt.imread(config_path+portcamdict['OP1.2a']['AEF'+str(10)]+"\\"+"AEF"+str(10)+"_FOV.png")[:,:,0]   
@@ -49,144 +35,6 @@ try:
 except:
     config_path=""
 
-camlimdict={
-    'IRCam_Caleo768kL_0901':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1100),
-        6:(50,850),
-        7:(50,800),
-        8:(50,600),
-        9:(50,550)
-         },
-    'IRCam_Caleo768kL_0902':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1100),
-        6:(50,1000),
-        7:(50,800),
-        8:(50,800),
-        9:(50,650)
-         },
-    'IRCam_Caleo768kL_0903':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1100),
-        6:(50,1000),
-        7:(50,900),
-        8:(50,800),
-        9:(50,700)
-         },
-    'IRCam_Caleo768kL_0904':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1100),
-        6:(50,900),
-        7:(50,800),
-        8:(50,700),
-        9:(50,600)
-         },
-    'IRCam_Caleo768kL_0905':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1100),
-        6:(50,900),
-        7:(50,800),
-        8:(50,700),
-        9:(50,600)
-         },
-    'IRCam_Caleo768kL_0906':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1000),
-        6:(50,800),
-        7:(50,750),
-        8:(50,600),
-        9:(50,550)
-         },
-    'IRCam_Caleo768kL_0907':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1200),
-        5:(50,1100),
-        6:(50,900),
-        7:(50,800),
-        8:(50,700),
-        9:(50,600)
-         },
-    'IRCam_Caleo768kL_0701':        
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1580),
-        5:(50,1100),
-        6:(50,900),
-        7:(50,800),
-        8:(50,750),
-        9:(50,700)
-         },
-    'IRCam_Caleo768kL_0702':
-        {
-        1:(50,5000),
-        2:(50,3000),
-        3:(50,2050),
-        4:(50,1580),
-        5:(50,1150),
-        6:(50,1000),
-        7:(50,800),
-        8:(50,750),
-        9:(50,700)
-         },
-     'Infratec_9312907':
-         {
-             0:{
-             25:(50,400),
-             50:(50,300),
-            100:(50,250),
-            200:(50,200),
-            400:(50,150),
-            800:(50,100)
-             },
-             1:{
-             25:(300,1200),
-             50:(200,800),
-            100:(150,650),
-            200:(100,500),
-            400:(50,400),
-            800:(50,300)
-             },
-            2:{
-             25:(200,1200),
-             50:(200,1200),
-            100:(200,1200),
-            200:(200,1200),
-            400:(200,1000),
-            800:(200,700)
-             },
-         }
-}
-
 def get_OP_by_program(program):
     try:    
         bla=int(program.split(".")[0])
@@ -298,7 +146,12 @@ def download_LUT(port,time,exposure=0,emissivity=0,camera_filter=0,version=0):
     """
     download_LUT(camera,port,time,exposure=0,emissivity=0,camera_filter=0,version=1):
         time in ns
+    Have to swap 11, 21 until correction in the database
     """    
+    if port == 21:
+        port = 11
+    elif port == 11:
+        port = 21
     if port==50:# camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
         stream="QRT_INFRATEC/"+"AEF"+str(port)+"_LUT_"
         larchivepath=archivepath+stream#"QRT_INFRATEC/"+"AEF"+str(port)+"_LUT_"
@@ -651,6 +504,29 @@ if fastDL:
         print(datetime.datetime.now(),"correcting bad pixels")
         images=IR_tools.correct_images(images,badpixels)
         out_q.put([threadnumber,successes,time,images]) 
+    
+    def NUC_raw_thread(port,times,out_q,threadnumber,version=0,background=0,LUT=[[],[],[]],refT=28.5,gain=0,offset=0,gain_error=0,offset_error=0,give_ERROR=False,FOV=0,badpixels=[]):
+        images=[]
+        time=[]
+        successes=[]
+        for i in times:
+            imag=download_last_raw_image_by_time(port,i-10,i+10,version=version)
+            if imag[0]:
+                images.append(np.array(imag[1],dtype=np.uint16))
+                time.append(i)
+                successes.append(True)
+            else:
+                successes.append(False)
+        if give_ERROR:
+            images,error_images=apply_NUC(images,gain,offset,gain_error,offset_error,give_ERROR)
+        else:
+            images=apply_NUC(images,gain,offset,gain_error,offset_error,give_ERROR)
+        
+        for i in range(len(images)):
+            images[i]=(images[i]*FOV).astype(np.float32)
+        print(datetime.datetime.now(),"correcting bad pixels")
+        images=IR_tools.correct_images(images,badpixels)
+        out_q.put([threadnumber,successes,time,images]) 
         
 def download_last_raw_image_by_time(port,starttime,stoptime,version=0):
     OP=get_OP_by_time(starttime)
@@ -929,174 +805,13 @@ def get_average_divertor_TC_for_camtemp(port,starttime,endtime,part="all",TC=0):
         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!")
-    TC_database={
-    50: {
-        0: {'Y': -4220.44, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 2947.882, 'Targetelement': 4}, 
-        1: {'Y': -4507.509, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 2673.037, 'Targetelement': 4}, 
-        2: {'Y': -4834.308, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 2339.889, 'Targetelement': 5}, 
-        3: {'Y': -5124.416, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1998.605, 'Targetelement': 5}, 
-        4: {'Y': -5862.386, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 430.664, 'Targetelement': 4}, 
-        5: {'Y': -5971.865, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -15.031, 'Targetelement': 6}, 
-        6: {'Y': -6065.916, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -697.011, 'Targetelement': 6}, 
-        7: {'Y': -4068.477, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 2704.554, 'Targetelement': 5}, 
-        8: {'Y': -4495.307, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 2346.682, 'Targetelement': 6}, 
-        9: {'Y': -4841.007, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1981.551, 'Targetelement': 6}, 
-        10: {'Y': -5872.346, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 24.186, 'Targetelement': 0}, 
-        11: {'Y': -5910.584, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 5.577, 'Targetelement': 0}, 
-        12: {'Y': -4703.059, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 2309.024, 'Targetelement': 0}, 
-        13: {'Y': -4945.311, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 2464.537, 'Targetelement': 0}, 
-        14: {'Y': -4431.787, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 2325.89, 'Targetelement': 0}, 
-        15: {'Y': -4322.24, 'Z': 714.53, 'Targetmodule': 'DR', 'active': True, 'X': 2374.085, 'Targetelement': 0}}, 
-    51: {0: {'Y': -5147.13, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 95.826, 'Targetelement': 4}, 
-         1: {'Y': -5217.823, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 486.915, 'Targetelement': 4}, 
-        2: {'Y': -5286.39, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 948.525, 'Targetelement': 5}, 
-        3: {'Y': -5320.49, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1395.15, 'Targetelement': 5}, 
-        4: {'Y': -4995.908, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 3097.409, 'Targetelement': 4}, 
-        5: {'Y': -4822.505, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 3522.334, 'Targetelement': 6}, 
-        6: {'Y': -4497.737, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 4129.35, 'Targetelement': 6}, 
-        7: {'Y': -4881.164, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 203.361, 'Targetelement': 5}, 
-        8: {'Y': -5016.125, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 743.77, 'Targetelement': 6}, 
-        9: {'Y': -5081.183, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1242.364, 'Targetelement': 6}, 
-        10: {'Y': -4779.849, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 3436.923, 'Targetelement': 0}, 
-        11: {'Y': -4860.039, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 3494.011, 'Targetelement': 0}, 
-        12: {'Y': -5163.219, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 896.724, 'Targetelement': 0}, 
-        13: {'Y': -5447.878, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 912.415, 'Targetelement': 0}, 
-        14: {'Y': -4893.659, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 704.131, 'Targetelement': 0}, 
-        15: {'Y': -4949.37, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 638.444, 'Targetelement': 0}}, 
-    20: {0: {'Y': 5147.13, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 95.826, 'Targetelement': 4}, 
-         1: {'Y': 5217.823, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 486.915, 'Targetelement': 4}, 
-        2: {'Y': 5286.39, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 948.525, 'Targetelement': 5}, 
-        3: {'Y': 5320.49, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1395.15, 'Targetelement': 5}, 
-        4: {'Y': 4995.908, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 3097.409, 'Targetelement': 4}, 
-        5: {'Y': 4822.505, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 3522.334, 'Targetelement': 6}, 
-        6: {'Y': 4497.737, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 4129.35, 'Targetelement': 6}, 
-        7: {'Y': 4881.164, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 203.361, 'Targetelement': 5}, 
-        8: {'Y': 5016.125, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 743.77, 'Targetelement': 6}, 
-        9: {'Y': 5081.183, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1242.364, 'Targetelement': 6}, 
-        10: {'Y': 4765.044, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 3432.112, 'Targetelement': 0}, 
-        11: {'Y': 4785.041, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 3469.642, 'Targetelement': 0}, 
-        12: {'Y': 5162.065, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 896.349, 'Targetelement': 0}, 
-        13: {'Y': 5449.459, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 912.928, 'Targetelement': 0}, 
-        14: {'Y': 4952.514, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 723.255, 'Targetelement': 0}, 
-        15: {'Y': 4892.218, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 619.874, 'Targetelement': 0}}, 
-    21: {0: {'Y': 4220.44, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 2947.882, 'Targetelement': 4}, 
-         1: {'Y': 4507.509, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': False, 'X': 2673.037, 'Targetelement': 4}, 
-        2: {'Y': 4834.308, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 2339.889, 'Targetelement': 5}, 
-        3: {'Y': 5124.416, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 1998.605, 'Targetelement': 5}, 
-        4: {'Y': 5862.386, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 430.664, 'Targetelement': 4}, 
-        5: {'Y': 5971.865, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -15.031, 'Targetelement': 6}, 
-        6: {'Y': 6065.916, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -697.011, 'Targetelement': 6}, 
-        7: {'Y': 4068.477, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 2704.554, 'Targetelement': 5}, 
-        8: {'Y': 4495.307, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 2346.682, 'Targetelement': 6}, 
-        9: {'Y': 4841.007, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 1981.551, 'Targetelement': 6}, 
-        10: {'Y': 5887.151, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 28.996, 'Targetelement': 0}, 
-        11: {'Y': 5985.582, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 29.945, 'Targetelement': 0}, 
-        12: {'Y': 4704.213, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 2309.399, 'Targetelement': 0}, 
-        13: {'Y': 4943.73, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 2464.024, 'Targetelement': 0}, 
-        14: {'Y': 4372.932, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 2306.766, 'Targetelement': 0}, 
-        15: {'Y': 4379.393, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 2392.655, 'Targetelement': 0}}, 
-    40: {0: {'Y': -4107.791, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -3102.931, 'Targetelement': 4}, 
-         1: {'Y': -3935.106, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -3460.882, 'Targetelement': 4}, 
-        2: {'Y': -3719.25, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -3874.635, 'Targetelement': 5}, 
-        3: {'Y': -3484.318, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4256.006, 'Targetelement': 5}, 
-        4: {'Y': -2221.163, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -5442.378, 'Targetelement': 4}, 
-        5: {'Y': -1831.112, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -5684.226, 'Targetelement': 6}, 
-        6: {'Y': -1211.574, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -5984.417, 'Targetelement': 6}, 
-        7: {'Y': -3829.412, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -3033.599, 'Targetelement': 5}, 
-        8: {'Y': -3620.954, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -3550.127, 'Targetelement': 6}, 
-        9: {'Y': -3380.521, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -3991.738, 'Targetelement': 6}, 
-        10: {'Y': -1837.657, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -5577.46, 'Targetelement': 0}, 
-        11: {'Y': -1831.775, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -5619.576, 'Targetelement': 0}, 
-        12: {'Y': -3649.338, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -3759.348, 'Targetelement': 0}, 
-        13: {'Y': -3872.099, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -3941.686, 'Targetelement': 0}, 
-        14: {'Y': -3581.55, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -3496.14, 'Targetelement': 0}, 
-        15: {'Y': -3593.534, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -3377.062, 'Targetelement': 0}}, 
-    41: {0: {'Y': -1681.686, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -4865.6, 'Targetelement': 4}, 
-         1: {'Y': -2075.48, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -4811.98, 'Targetelement': 4}, 
-        2: {'Y': -2535.686, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -4734.545, 'Targetelement': 5}, 
-        3: {'Y': -2970.989, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4628.962, 'Targetelement': 5}, 
-        4: {'Y': -4489.632, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -3794.239, 'Targetelement': 4}, 
-        5: {'Y': -4840.175, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -3498.014, 'Targetelement': 6}, 
-        6: {'Y': -5317.122, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -3001.562, 'Targetelement': 6}, 
-        7: {'Y': -1701.77, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -4579.421, 'Targetelement': 5}, 
-        8: {'Y': -2257.435, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -4540.781, 'Targetelement': 6}, 
-        9: {'Y': -2751.73, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -4448.581, 'Targetelement': 6}, 
-        10: {'Y': -4745.762, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -3483.839, 'Targetelement': 0}, 
-        11: {'Y': -4824.836, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -3542.463, 'Targetelement': 0}, 
-        12: {'Y': -2448.358, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -4633.41, 'Targetelement': 0}, 
-        13: {'Y': -2551.245, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -4899.289, 'Targetelement': 0}, 
-        14: {'Y': -2181.893, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -4436.558, 'Targetelement': 0}, 
-        15: {'Y': -2136.636, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -4509.841, 'Targetelement': 0}}, 
-    10: {0: {'Y': 1499.415, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 4924.823, 'Targetelement': 4}, 
-         1: {'Y': 1149.312, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 5112.91, 'Targetelement': 4}, 
-        2: {'Y': 731.483, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 5320.766, 'Targetelement': 5}, 
-        3: {'Y': 317.255, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 5491.212, 'Targetelement': 5}, 
-        4: {'Y': -1401.991, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 5708.543, 'Targetelement': 4}, 
-        5: {'Y': -1859.703, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 5674.936, 'Targetelement': 6}, 
-        6: {'Y': -2537.368, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 5553.641, 'Targetelement': 6}, 
-        7: {'Y': 1314.955, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 4705.105, 'Targetelement': 5}, 
-        8: {'Y': 842.701, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 5000.456, 'Targetelement': 6}, 
-        9: {'Y': 388.614, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 5216.404, 'Targetelement': 6}, 
-        10: {'Y': -1791.653, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 5592.407, 'Targetelement': 0}, 
-        11: {'Y': -1821.167, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 5623.023, 'Targetelement': 0}, 
-        12: {'Y': 742.687, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 5186.403, 'Targetelement': 0}, 
-        13: {'Y': 815.729, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 5464.854, 'Targetelement': 0}, 
-        14: {'Y': 842.555, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 4933.619, 'Targetelement': 0}, 
-        15: {'Y': 922.243, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 4844.327, 'Targetelement': 0}}, 
-    11: {0: {'Y': -1499.415, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': 4924.823, 'Targetelement': 4}, 
-         1: {'Y': -1149.312, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': 5112.91, 'Targetelement': 4}, 
-        2: {'Y': -731.483, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': 5320.766, 'Targetelement': 5}, 
-        3: {'Y': -317.255, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': 5491.212, 'Targetelement': 5}, 
-        4: {'Y': 1401.991, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': 5708.543, 'Targetelement': 4}, 
-        5: {'Y': 1859.703, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': 5674.936, 'Targetelement': 6}, 
-        6: {'Y': 2537.368, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': 5553.641, 'Targetelement': 6}, 
-        7: {'Y': -1314.955, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': 4705.105, 'Targetelement': 5}, 
-        8: {'Y': -842.701, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': 5000.456, 'Targetelement': 6}, 
-        9: {'Y': -388.614, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': 5216.404, 'Targetelement': 6}, 
-        10: {'Y': 1791.653, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': 5607.974, 'Targetelement': 0}, 
-        11: {'Y': 1821.167, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': 5701.88, 'Targetelement': 0}, 
-        12: {'Y': -742.687, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': 5187.616, 'Targetelement': 0}, 
-        13: {'Y': -815.729, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': 5463.192, 'Targetelement': 0}, 
-        14: {'Y': -842.555, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': 4871.735, 'Targetelement': 0}, 
-        15: {'Y': -922.243, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': 4904.421, 'Targetelement': 0}}, 
-    30: {0: {'Y': 1681.686, 'Z': 966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -4865.6, 'Targetelement': 4}, 
-         1: {'Y': 2075.48, 'Z': 1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -4811.98, 'Targetelement': 4}, 
-        2: {'Y': 2535.686, 'Z': 1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -4734.545, 'Targetelement': 5}, 
-        3: {'Y': 2970.989, 'Z': 998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4628.962, 'Targetelement': 5}, 
-        4: {'Y': 4489.632, 'Z': 749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -3794.239, 'Targetelement': 4}, 
-        5: {'Y': 4840.175, 'Z': 676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -3498.014, 'Targetelement': 6}, 
-        6: {'Y': 5317.122, 'Z': 542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -3001.562, 'Targetelement': 6}, 
-        7: {'Y': 1701.77, 'Z': 644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -4579.421, 'Targetelement': 5}, 
-        8: {'Y': 2257.435, 'Z': 719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -4540.781, 'Targetelement': 6}, 
-        9: {'Y': 2751.73, 'Z': 771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -4448.581, 'Targetelement': 6}, 
-        10: {'Y': 4736.612, 'Z': 772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -3471.245, 'Targetelement': 0}, 
-        11: {'Y': 4778.485, 'Z': 726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -3478.666, 'Targetelement': 0}, 
-        12: {'Y': 2447.645, 'Z': 1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -4632.429, 'Targetelement': 0}, 
-        13: {'Y': 2552.222, 'Z': 1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -4900.633, 'Targetelement': 0}, 
-        14: {'Y': 2218.267, 'Z': 740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -4486.623, 'Targetelement': 0}, 
-        15: {'Y': 2101.314, 'Z': 714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -4461.224, 'Targetelement': 0}}, 
-    31: {0: {'Y': 4107.791, 'Z': -966.369, 'Targetmodule': 'TM1h', 'active': True, 'X': -3102.931, 'Targetelement': 4}, 
-         1: {'Y': 3935.106, 'Z': -1000.004, 'Targetmodule': 'TM2h', 'active': True, 'X': -3460.882, 'Targetelement': 4}, 
-        2: {'Y': 3719.25, 'Z': -1006.452, 'Targetmodule': 'TM3h', 'active': True, 'X': -3874.635, 'Targetelement': 5}, 
-        3: {'Y': 3484.318, 'Z': -998.608, 'Targetmodule': 'TM4h', 'active': True, 'X': -4256.006, 'Targetelement': 5}, 
-        4: {'Y': 2221.163, 'Z': -749.666, 'Targetmodule': 'TM7h', 'active': True, 'X': -5442.378, 'Targetelement': 4}, 
-        5: {'Y': 1831.112, 'Z': -676.355, 'Targetmodule': 'TM8h', 'active': True, 'X': -5684.226, 'Targetelement': 6}, 
-        6: {'Y': 1211.574, 'Z': -542.714, 'Targetmodule': 'TM9h', 'active': True, 'X': -5984.417, 'Targetelement': 5}, 
-        7: {'Y': 3829.412, 'Z': -644.281, 'Targetmodule': 'TM1v', 'active': True, 'X': -3033.599, 'Targetelement': 5}, 
-        8: {'Y': 3620.954, 'Z': -719.195, 'Targetmodule': 'TM2v', 'active': True, 'X': -3550.127, 'Targetelement': 6}, 
-        9: {'Y': 3380.521, 'Z': -771.012, 'Targetmodule': 'TM3v', 'active': True, 'X': -3991.738, 'Targetelement': 6}, 
-        10: {'Y': 1846.807, 'Z': -772.15, 'Targetmodule': 'DR_TM8h', 'active': True, 'X': -5590.053, 'Targetelement': 0}, 
-        11: {'Y': 1878.126, 'Z': -726.881, 'Targetmodule': 'TMR_TM8h', 'active': True, 'X': -5683.373, 'Targetelement': 0}, 
-        12: {'Y': 3650.051, 'Z': -1027.995, 'Targetmodule': 'TMR_TM3h', 'active': True, 'X': -3760.329, 'Targetelement': 0}, 
-        13: {'Y': 3871.122, 'Z': -1136.99, 'Targetmodule': 'DR_TM3h', 'active': True, 'X': -3940.342, 'Targetelement': 0}, 
-        14: {'Y': 3545.175, 'Z': -740.545, 'Targetmodule': 'TMR_TM2v', 'active': True, 'X': -3446.075, 'Targetelement': 0}, 
-        15: {'Y': 3628.857, 'Z': -714.53, 'Targetmodule': 'DR_TM2v', 'active': True, 'X': -3425.679, 'Targetelement': 0}}}
+        raise Exception("unknown port!")    
     return TC_database[portnr][TC]
 
 def get_TC_data_for_port_by_program(port,program,part=0,version=0):
@@ -1115,24 +830,7 @@ def get_TC_data_for_port_by_program(port,program,part=0,version=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)
-    TC_port={
-    10:[10],
-    11:[10],
-    20:[20],
-    21:[20],
-    30:[30],
-    31:[30],
-    40:[10,20,30],
-    41:[10,20,30],
-    50:[50],
-    51:[10,20,30]
-    }
-    TC_channel={
-    10:[[66,67],["ActV1QSR10CT001","ActV1QSR10CT002"]], 
-    20:[[72,73],["ActV1QSR20CT001","ActV1QSR20CT002"]],
-    30:[[78,79],["ActV1QSR30CT001","ActV1QSR30CT002"]],
-    50:[[90,91,84,85],["ActV1QRT50CT001","ActV1QRT50CT002","ActV1QRT50CT003","ActV1QRT50CT004"]]
-    }
+    
     n=0
     success=True
     signal=0
@@ -1324,13 +1022,13 @@ def get_temp_from_raw_by_program(portnr,program,time_s=0,emi=0.8,T_version=2,ver
     frames=IR_tools.correct_images(frames,badpixels)
     print(datetime.datetime.now(),"checking temperaturerange")
     valid=True
-    for i in range(len(frames)):
-        try:
-            check_temperature_range(time[0],np.max(frames[i])-273.15,np.min(frames[i][np.nonzero(frames[i])])-273.15,portnr,t_exp,cfilter)
-        except Exception as E:
-            print(E)
-            valid=False
-            i=len(frames)
+#    for i in range(len(frames)):
+    try:
+        check_temperature_range(time[0],np.max(frames)-273.15,np.min(frames[:][np.nonzero(frames[i])])-273.15,portnr,t_exp,cfilter)
+    except Exception as E:
+        print(E)
+        valid=False
+#        i=len(frames)
     if give_ERROR:
         return exist,time,frames,valid,error_images
     else:
@@ -1435,7 +1133,125 @@ def get_temp_from_raw_by_program_fullthreads(portnr,program,time_s=0,emi=0.8,T_v
             return success,np.array(times),images,valid,error_images
         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")
+    if fastDL: 
+        exist,time,frames=download_raw_images_by_program_via_png(portnr,program,time_s,version,threads)
+    else:
+        exist,time,frames=download_raw_images_by_program(portnr,program,time_s,version)
+        if exist:
+            frames=[im.swapaxes(0,1) for im in frames]
+        else:
+            raise Exception("no data found")
+    print(datetime.datetime.now(),"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)
+    else:
+        frames=apply_NUC(frames,gain,offset,gain_error,offset_error,give_ERROR)
+    
+    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")
+    valid=True
+
+    if give_ERROR:
+        return exist,time,frames,valid,frames_error
+    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=0,threads=1,give_ERROR=False,check_range=True):
+    if threads==1 or not fastDL:
+        return get_nuced_raw_by_program(portnr,program,time_s,emi,T_version,version,threads,give_ERROR)
+    else:
+        background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(portnr,program,emi,T_version,version)
+        FOV=get_FOV_mask(portnr)        
+        prog=AKF_1.get_program_from_PID(program)
+        if prog[0]:
+            starttime=prog[1]['trigger']['1'][0]
+            stoptime=prog[1]['trigger']['6'][0]
+            success=True
+            OP=get_OP_by_time(starttime)
+            Cam=portcamdict[OP]['AEF'+str(portnr)]
+            if Cam.split("_")[0]=="Infratec":#camera=="INFRATEC" or camera=="infratec" or camera=="Infratec":
+                if version==0:
+                    version=get_latest_version("QRT_INFRATEC/"+"AEF"+str(portnr)+"_raw_DATASTREAM")
+                larchivepath="Test/raw/W7X/"+"QRT_INFRATEC/"+"AEF"+str(portnr)+"_raw_DATASTREAM/V"+str(version)+"/0/raw"
+            elif Cam.split("_")[0]=="IRCam":#camera=="IRCAM" or camera=="IRcam" or camera=="ircam":
+                if version==0:
+                    version=get_latest_version("QRT_IRCAM/"+"AEF"+str(portnr)+"_raw_DATASTREAM")
+                larchivepath="Test/raw/W7X/"+"QRT_IRCAM/"+"AEF"+str(portnr)+"_raw_DATASTREAM/V"+str(version)+"/0/raw"
+            else:
+                raise Exception("Port number does not fit the known cameras")
+            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)  
+                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()
+                else:
+                    enddate=datetime.datetime.utcfromtimestamp((starttime)/1e9+time_s)  
+                    enddate=enddate.isoformat()        
         
+            times=AKF_2.get_time_intervals(larchivepath,stdate.replace("T"," "),enddate.replace("T"," "))#
+            lnt=len(times)
+            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 = []
+            out_q=multiprocessing.Queue()
+            for i in range(threads):
+                print("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,))
+                jobs.append(p)
+                p.start()
+            resultdict = []
+            for i in range(threads):
+                resultdict.append(out_q.get())
+            for p in jobs:
+                p.join()
+            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]
+            del resultdict
+            valid=True
+        else:
+            success=False
+            times=[0]
+            images=[0]
+            valid=False
+            error_images=[0]
+        
+        if give_ERROR:
+            return success,np.array(times),images,valid,error_images
+        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):
     try:
@@ -1499,6 +1315,7 @@ 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)
         return 0
         
@@ -1561,18 +1378,61 @@ def get_calib_data(port,program,emissivity=0.8,Temp_V=2,version=0,back_emissivit
                     else:
                         raise Exception("Unknown temperature calibration method")
                     if cfilter==0 and not Camera.split("_")[0]=="Infratec":
-                        NUC_DL=download_NUC_by_times(port,t0,t1,t_exp,version)
-                        if NUC_DL[0]==False:
-                            raise Exception("NUC was not found")
+                        if (port == 31) or (port == 21 and float(program[4:]) > 1110):
+                            print("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)
+                            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'))
+                            cirebuild = IR_tools.reconstruct_coldframe(t_exp, sT, amap, bmap, coldref)
+                            gain, offset = IR_tools.calculate_gain_offset_image(cirebuild, None, coldref, hotref)
+                            gain[gain == np.inf] = 0
+                            offset[offset == np.inf] = 0
+                            badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10)
+                            gain_error=0
+                            offset_error=0
+                            exist,time,frames=download_raw_images_by_times(port,t1,int(t1+0.02*1e9),version)
+                            if exist:
+                                frames=[im.swapaxes(0,1) for im in frames]
+                                bim=apply_NUC([frames[0]],gain,offset)
+                                background=get_average_background_recangle(port,bim[0])
+                            else:
+                                raise Exception("cannot find the first frame of the discharge, reconstruction failed")
+                        elif (port == 11) or (port == 21):
+                            coldref, hotref = IR_tools.load_ref_images('AEF' + str(port), t_exp)
+                            NUC_DL=download_NUC_by_times(port,t0,t1,t_exp,version)
+                            if NUC_DL[0]==False:
+                                raise Exception("NUC was not found")
+                            else:
+                                ci = NUC_DL[1][2]
+                            gain, offset = IR_tools.calculate_gain_offset_image(ci, None, coldref, hotref)
+                            gain[gain == np.inf] = 0
+                            offset[offset == np.inf] = 0
+                            badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10)                                
+                            gain_error=0
+                            offset_error=0
+                            exist,time,frames=download_raw_images_by_times(port,t1,int(t1+0.02*1e9),version)
+                            if exist:
+                                frames=[im.swapaxes(0,1) for im in frames]
+                                bim=apply_NUC([frames[0]],gain,offset)
+                                background=get_average_background_recangle(port,bim[0])
+                            else:
+                                raise Exception("background wrong due to wrong NUC, firstframe not found, reconstruction failed")
                         else:
-                            gain=np.array(NUC_DL[1][0])
-                            offset=np.array(NUC_DL[1][1])
-                            offset[offset<-1e300]=0
-                            gain_error=np.array(NUC_DL[1][4])
-                            offset_error=np.array(NUC_DL[1][5])
-                            badpixels=np.array(NUC_DL[1][3],dtype=np.ubyte)
-                            if np.max(badpixels)==0:
-                                badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10)
+                            NUC_DL=download_NUC_by_times(port,t0,t1,t_exp,version)
+                            if NUC_DL[0]==False:
+                                raise Exception("NUC was not found")
+                            else:
+                                gain=np.array(NUC_DL[1][0])
+                                offset=np.array(NUC_DL[1][1])
+                                offset[offset<-1e300]=0
+                                gain_error=np.array(NUC_DL[1][4])
+                                offset_error=np.array(NUC_DL[1][5])
+                                badpixels=np.array(NUC_DL[1][3],dtype=np.ubyte)
+                                if np.max(badpixels)==0:
+                                    badpixels=find_badpixels(port,gain,offset,niterations=10,tolerance=10)
                     else:
                         gain=0
                         offset=0
@@ -1710,34 +1570,6 @@ def estimate_offset(port,program,plot_it=False):
     else:
         return -1
 
-
-
-    
-valid_FOV_circle = {
-    10: [562, 364, 550],#, 600],
-    11: [502, 404, 520],#, 570]
-    20: [535, 374, 510],
-    21: [548, 404, 505],
-    30: [517, 389, 520],
-    31: [562, 364, 520],
-    40: [542, 394, 520],
-    41: [522, 394, 520],
-    50: [640, 454, 620],
-    51: [512, 379, 510]
-    }
-
-valid_background_rectangle = {
-    10: [500,0 ,800 ,100],#10: [300,0 ,800 ,200],
-    11: [50,200,170,290],#11: [200,100,700,200],
-    20: [100,100,275,135],#20: [100,100,400,200],
-    21: [300,0,700,75],#21: [150,0,800,200],
-    30: [200,0,800,100],#30: [200,0,800,200],#
-    31: [200,100,700,200],#
-    40: [550,0,690,140],#
-    41: [120,100,250,200],#
-    50: [390,940,880,1010],#
-    51: [350,0,670,120]#
-    }
     
 if __name__=='__main__':
     print("local function calling")
@@ -1745,26 +1577,26 @@ if __name__=='__main__':
 #    #status,time,images,valid=get_temp_from_raw_by_program_V1(51,"20171018.019",time_s=0,emi=0.8,divertorpart="all",version=1)
 ##    status1,time1,images1,valid1=get_temp_from_raw_by_program_V1(41,"20171024.016",time_s=[0.75,0.99],emi=0.8,version=1)
 ##    exist,time1,frames=download_raw_images_by_program_via_png(41,"20171024.016",time_s=[0.75,0.99],version=1,threads=1)
-    import h5py as h5
-    
-    for prog in ["20171108.015"]:#"20171109.010","20170926.009","20171207.009"]:
-        for port in [20,21]:
-            try:
-                texp=get_exposure_by_program(port,prog)
-            except:
-                texp=[False]
-            if texp[0]:
-                texp=texp[2][0]
-                background=download_background_by_program(port,prog,texp)
-                
-                if background[0]:
-                    
-#                    btest=check_backgroundframe(background[2])
-#                    if not btest[0]:
-                    plt.figure()
-                    plt.imshow(background[2],vmin=np.mean(background[2])-200,vmax=np.mean(background[2])+200)
-#                        plt.colorbar()
-                    plt.title(prog+" "+str(port))
+#    import h5py as h5
+#    
+#    for prog in ["20171108.015"]:#"20171109.010","20170926.009","20171207.009"]:
+#        for port in [20,21]:
+#            try:
+#                texp=get_exposure_by_program(port,prog)
+#            except:
+#                texp=[False]
+#            if texp[0]:
+#                texp=texp[2][0]
+#                background=download_background_by_program(port,prog,texp)
+#                
+#                if background[0]:
+#                    
+##                    btest=check_backgroundframe(background[2])
+##                    if not btest[0]:
+#                    plt.figure()
+#                    plt.imshow(background[2],vmin=np.mean(background[2])-200,vmax=np.mean(background[2])+200)
+##                        plt.colorbar()
+#                    plt.title(prog+" "+str(port))
 
 #                cold=download_NUC_by_program(port,prog,texp)
 #                if cold[0]:
@@ -1825,10 +1657,40 @@ if __name__=='__main__':
                         
                         
 
-#    port=20
-#    prog="20171207.039"
-#    status,time,images,valid=get_temp_from_raw_by_program(port,prog,time_s=[0,6],emi=0.82,T_version=2,version=0,threads=4,give_ERROR=False)
+    port=20
+    prog="20171108.015"
+#    status,time,images,valid=get_temp_from_raw_by_program(port,prog,time_s=[0,0.2],emi=0.82,T_version=2,version=0,threads=1,give_ERROR=False,use_firstframe_as_background=True)
+    bla=get_exposure_by_program(port,prog)
+    status2,time2,images2=download_raw_images_by_program_via_png(port,prog,time_s=[0,0.2],threads=1)
+#    plt.figure()
+#    plt.imshow(images[0],vmin=300,vmax=400)
+#    plt.colorbar()
 #    tmax=[]
+#    port=20
+    NUCblock=download_NUC_by_program(port,prog,bla[2][0])
+    background,LUT,refT,gain,offset,badpixels,t_exp,cfilter,gain_error,offset_error=get_calib_data(port,prog)
+    refcoldhot=IR_tools.load_ref_images("AEF"+str(port),bla[2][0])
+    recal_gain_offset=IR_tools.calculate_gain_offset_image_pix(NUCblock[1][2].copy(),None,refcoldhot[0].copy(),refcoldhot[1].copy())
+    plt.figure()
+    plt.imshow(NUCblock[1][0]-recal_gain_offset[0])
+    plt.title("gain")
+    plt.figure()
+    plt.imshow(NUCblock[1][1]-recal_gain_offset[1])
+    plt.title("offset")
+#    ima1=apply_NUC(images2.copy(),NUCblock[1][0],NUCblock[1][1])
+#    ima2=apply_NUC(images2.copy(),recal_gain_offset[0],recal_gain_offset[1])
+    ima1=apply_calib_on_raw(images2.copy(),background,LUT,gain=NUCblock[1][0],offset=NUCblock[1][1])
+    ima2=apply_calib_on_raw(images2.copy(),background,LUT,gain=recal_gain_offset[0],offset=recal_gain_offset[1])
+    
+    plt.figure()
+    plt.imshow(ima1[0],vmin=300,vmax=400)
+    plt.colorbar()
+    plt.title("aleix nuc")
+    
+    plt.figure()
+    plt.imshow(ima2[0],vmin=300,vmax=400)
+    plt.colorbar()
+    plt.title("adnan nuc")
 #    for i in range(len(images)):
 #        tmax.append(np.max(images[i]))
 #    plt.figure()
-- 
GitLab