forked from Richardjmorton/Development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch_up.pro
104 lines (80 loc) · 3.35 KB
/
patch_up.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;FUNCTION: 'Patchs up' the threads found from 'locate_things.pro', followed by
; 'follow_things.pro'. Follow things can skip forward time frames
; leaving zero results. This uses interpolation to fix these gaps and
; weights these results with a larger error.
;
;PROCEDURE OUTLINE: The first step is to remove any examples where
; the number of pixels in the thread is less than 2 and
; any threads where more than half the pixels could not
; be found with locate things (i.e. given the value 0). The routine then
; locates any points that have the value 0. These values are then
; replaced via linear interpolations given a large error (1
; pixel). This is to ensure the weighted fitting
; routines give limited significance to this value.
;
;
;
;INPUTS: fit_flag - 0-3 - defines which time series to work on (see follow_thread_fg.pro/moscill.pro)
;
;
;HISTORY: created 05/2014 R Morton
;
;TO DO OR THINK ABOUT: Is linear interpolation the best option?
; Should weighting be calculated using traditional error analysis?
; Does funny things if first elements in array is a 0 - need to look into this
;
;
pro patch_up,fit_flag=fit_flag
;Loads common data generated from locate_things_fg
COMMON located_dat, located, nx, nt
COMMON threads_dat, threads
IF n_elements(fit_flag) EQ 0 THEN fit_flag=0
sz=size(threads)
n_threads=sz(1)
IF fit_flag EQ 0 THEN BEGIN
;Designed for simple fitting structures
dummy=threads.pos
dummyerrs=threads.err_pos
ENDIF ELSE BEGIN
;Designed for advanced fitting structures (_FG)
IF fit_flag EQ 1 THEN BEGIN
dummy=threads.pos
dummyerrs=threads.err_pos
ENDIF
IF fit_flag EQ 2 THEN BEGIN
dummy=threads.inten
dummyerrs=threads.err_inten
ENDIF
IF fit_flag EQ 3 THEN BEGIN
dummy=threads.wid ;dummy arrays
dummyerrs=threads.err_wid
ENDIF
ENDELSE
FOR j=0,(n_threads-1) DO BEGIN
;skips enteries with less than 2 positive values
IF (n_elements(where(dummy[*,j] gt 0.))) GT 2. THEN BEGIN
;skips enteries where half the data points are set to zero, i.e. no
;value was obtained at fitting stage.
IF (n_elements(where(dummy[*,j] EQ 0.))) LT 0.5*(n_elements(where(dummy[*,j] GE 0.))) THEN BEGIN
;FIND CONSECUTIVE ZEROS IN THREAD
b=where(dummy[*,j] EQ 0)
IF b[0] GT -1 THEN consec,b,lo,hi,num
;FILL IN USING LINEAR INTERPOLATION
IF b[0] GT -1 THEN FOR i=0,num-1 DO linfill,dummy[*,j],b(lo[i])-1,b(hi[i])+1
IF b[0] GT -1 THEN FOR i=0,num-1 DO dummyerrs[lo[i]:hi[i],j]=1.
;FIND SINGLE ZEROS AND FILL VIA LINEAR INTERPOLATION
b=where(dummy[*,j] EQ 0)
IF b[0] GT -1 THEN FOR i=0,n_elements(b)-1 DO $
dummy[b[i],j,0]=0.5*dummy[b[i]-1,j]+0.5*dummy[b[i]+1,j]
IF b[0] GT -1 THEN FOR i=0,n_elements(b)-1 DO $
dummyerrs[b[i],j]=sqrt(dummy[b[i]-1,j]^2+dummy[b[i]+1,j]^2)
ENDIF ELSE BEGIN
;Erases all entries where number of 0 elements gt 1/2 positive elements
dummy[*,j]=-1.
ENDELSE
ENDIF ELSE BEGIN
;Erases all entries where number of positive elements lt 2
dummy[*,j]=-1.
ENDELSE
ENDFOR
END