From 6f04500b902e2c7e3a81f9df0133db2e6214b247 Mon Sep 17 00:00:00 2001
From: Peter Drewelow <peter.drewelow@ipp.mpg.de>
Date: Thu, 2 May 2019 18:59:21 +0200
Subject: [PATCH] downloadversionIRdata: modifications to
 download_heatflux_mapping_reference() - The points of the vertical target in
 'X' and 'Y' get now shifted and rotated to appear on top of the horizontal
 target in order to avaoid overlap when plotting. - Added new boolean peremter
 'get_thickness=False' triggers optional calculation of distance of profile
 points to adjacent profiles. This is used to derive a thickness of the
 profile at each point for integration of heat loads.

---
 downloadversionIRdata.py | 83 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 2 deletions(-)

diff --git a/downloadversionIRdata.py b/downloadversionIRdata.py
index aaa82ae..f7128be 100644
--- a/downloadversionIRdata.py
+++ b/downloadversionIRdata.py
@@ -2644,7 +2644,8 @@ def download_heatflux_by_times(port,tstart,tend,time_window=0,threads=1,testmode
                         print("download_heatflux_by_times: heat flux calculation request logged for automatic processing (within ca. 1 day)")
             return exist,time,frames
 
-def download_heatflux_mapping_reference(timepoint=None,version=0,testmode=False,verbose=0):
+def download_heatflux_mapping_reference(timepoint=None,version=0,testmode=False,
+                                        get_thickness=False, verbose=0):
     """
     return exist(boolean) and dictonary of the mappings informations
     """
@@ -2663,7 +2664,7 @@ def download_heatflux_mapping_reference(timepoint=None,version=0,testmode=False,
 #        version=get_latest_version("QRT_IRCAM_Test/Mapping_reference_Test_1_PARLOG",project=project_ana,Test=testmode)                                        
 #    larchivepath=base+project_ana+"/QRT_IRCAM_Test/Mapping_reference_Test_1_DATASTREAM/V"+str(version)+"/0/reference"
     ### end of testsample ###
-    exist,time,frames=download_images_by_times(larchivepath,starttime=timepoint,stoptime=int(timepoint+1e9),version=version,verbose=verbose)
+    exist,time,frames=download_images_by_times(larchivepath,starttime=timepoint,stoptime=int(timepoint+1e9),version=version,verbose=verbose-1)
     mappings={}
     if exist:
         mappings['s']=frames[0]
@@ -2673,6 +2674,84 @@ def download_heatflux_mapping_reference(timepoint=None,version=0,testmode=False,
         mappings['Finger_Y']=frames[4]
         mappings['Finger_ID']=(frames[5],"legend: first three digits are fingernumber,starting @0, last two are the profile number")
         mappings['Target']=(frames[6],{1:"TM1-4h",2:"TM5-6h",3:"TM7-9h",4:"TM1-3v"})
+        
+        # derive thickness of profile line (for integration)
+        if get_thickness:
+            profile_no = mappings['Finger_ID'][0]
+            profile_ID = np.unique(profile_no)
+            profile_ID = profile_ID[:np.where(np.isnan(profile_ID))[0][0]]
+            profile_ID = profile_ID.astype(np.int16)
+            
+            d = np.zeros(np.shape(mappings['s']))
+            for i_profile in profile_ID:
+                x_f = mappings['Finger_X'][np.where(profile_no==i_profile)]
+                y_f = mappings['Finger_Y'][np.where(profile_no==i_profile)]
+                if i_profile-1 not in profile_ID:
+                    # i_profile is first profile of finger
+                    #
+                    # thickness of this profile at each point [x_f,y_f] is equal to 
+                    # the distance to the next line from [x1, y1] to [x2, y2]
+                    # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
+                    x_f_next = mappings['Finger_X'][np.where(profile_no==i_profile+1)]
+                    x1 = x_f_next[0]
+                    x2 = x_f_next[-1]
+                    y_f_next = mappings['Finger_Y'][np.where(profile_no==i_profile+1)]
+                    y1 = y_f_next[0]
+                    y2 = y_f_next[-1]
+                    d[np.where(profile_no==i_profile)] = \
+                        abs((y2-y1)*x_f - (x2-x1)*y_f + x2*y1 - y2*x1) / np.sqrt((y2-y1)**2 + (x2-x1)**2) 
+                elif i_profile+1 not in profile_ID:
+                    # i_profile is last profile of finger
+                    #
+                    # thickness of this profile at each point [x_f,y_f] is equal to 
+                    # the distance to the previous line from [x1, y1] to [x2, y2]
+                    # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
+                    x_f_prev = mappings['Finger_X'][np.where(profile_no==i_profile-1)]
+                    x1 = x_f_prev[0]
+                    x2 = x_f_prev[-1]
+                    y_f_prev = mappings['Finger_Y'][np.where(profile_no==i_profile-1)]
+                    y1 = y_f_prev[0]
+                    y2 = y_f_prev[-1]
+                    d[np.where(profile_no==i_profile)] = \
+                        abs((y2-y1)*x_f - (x2-x1)*y_f + x2*y1 - y2*x1) / np.sqrt((y2-y1)**2 + (x2-x1)**2) 
+                else:
+                    # a previous and next profile line exists on this finger
+                    #
+                    # thickness of this profile at each point [x_f,y_f] is the sum 
+                    # of half the distance to the previous line from [x1_p, y1_p] to [x2_p, y2_p]
+                    # and half the distance to the next line from [x1_n, y1_n] to [x2_n, y2_n]
+                    # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
+                    x_f_prev = mappings['Finger_X'][np.where(profile_no==i_profile-1)]
+                    x1_p = x_f_prev[0]
+                    x2_p = x_f_prev[-1]
+                    y_f_prev = mappings['Finger_Y'][np.where(profile_no==i_profile-1)]
+                    y1_p = y_f_prev[0]
+                    y2_p = y_f_prev[-1]
+                    x_f_next = mappings['Finger_X'][np.where(profile_no==i_profile+1)]
+                    x1_n = x_f_next[0]
+                    x2_n = x_f_next[-1]
+                    y_f_next = mappings['Finger_Y'][np.where(profile_no==i_profile+1)]
+                    y1_n = y_f_next[0]
+                    y2_n = y_f_next[-1]
+                    d[np.where(profile_no==i_profile)] = \
+                        abs((y2_p-y1_p)*x_f - (x2_p-x1_p)*y_f + x2_p*y1_p - y2_p*x1_p) / np.sqrt((y2_p-y1_p)**2 + (x2_p-x1_p)**2) /2 + \
+                        abs((y2_n-y1_n)*x_f - (x2_n-x1_n)*y_f + x2_n*y1_n - y2_n*x1_n) / np.sqrt((y2_n-y1_n)**2 + (x2_n-x1_n)**2) /2 
+            mappings['thickness'] = d
+            if verbose>0:
+                print("download_heatflux_mapping_reference: 'thickness' of profiles added to mapping reference dictionary")
+                
+        # shift vertical target in X and Y if it is centered on top of horizontal target
+        index_ver = np.where(mappings['Target'][0]==4)
+        X = mappings['X']
+        Y = mappings['Y']
+        if abs(np.nanmean(mappings['X'][index_ver])) < 0.015 and abs(np.nanmean(mappings['Y'][index_ver])) < 0.01:
+            x1 = X[index_ver]
+            y1 = Y[index_ver]
+            X[index_ver] = np.cos(22.5/180*np.pi)*x1 + np.sin(22.5/180*np.pi)*y1 - 0.9
+            Y[index_ver] = -np.sin(22.5/180*np.pi)*x1 + np.cos(22.5/180*np.pi)*y1 + 0.7
+            if verbose>0:
+                print("download_heatflux_mapping_reference: vertical target moved and rotated in 'X' and 'Y' for better plotting")
+                
     return exist,mappings
 
 def download_heatflux_scene_model_reference(port,timepoint=None,program=None,version=0,testmode=False,verbose=0):
-- 
GitLab