-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobswise.pl
330 lines (243 loc) · 10.7 KB
/
obswise.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#coding: utf-8
#OBSERVATION-WISE (spatial anaylsis)
#Written by Yamini Nambiar, 1/6/2013
from astropy.io import fits as pyfits
from numpy import *
import math
import numpy as np
file_path_prefix = '/home/yamini/Data/'
###Constructs fully qualified filename of flux image files to read
def construct_flux_filename(observation, ratio, bin, flux):
if bin == 4 or bin == 8:
flux_filename = file_path_prefix + str(observation) + '/repro/f00' + str(bin) + '/' + flux + '_flux.img'
else:
flux_filename = file_path_prefix + str(observation) + '/repro/f0' + str(bin) + '/' + flux + '_flux.img'
return flux_filename
###Constructs fully qualified filename of original image files to read
def construct_img_filename(observation, ratio, bin, flux):
if bin == 4 or bin == 8:
flux_filename = file_path_prefix + str(observation) + '/repro/f00' + str(bin) + '/' + flux + '.img'
else:
flux_filename = file_path_prefix + str(observation) + '/repro/f0' + str(bin) + '/' + flux + '.img'
return flux_filename
###Sums soft flux, medium flux, and hard flux into broad flux image
def create_broad(observation, ratio, bin):
broad_flux = soft_flux_image + medium_flux_image + hard_flux_image
return broad_flux
###This function prints the full & complete ratio filenames so that we can see it
def print_filename(ratio, bin):
a = construct_ratio_filename(observation,ratio, bin)
print a
###Reads in a single ratio image
def read_single_img(fqName):
hdulist = pyfits.open(fqName)
temp_img = zeros((rows_global,columns_global), float64)
for i in range(0,temp_img.shape[0]):
for j in range(0, temp_img.shape[1]):
temp_img[i,j] = hdulist[0].data[i,j]
return temp_img
###MASK Calculates mean of given pixel's flux values over all observations
def calc_flux_mean(row, col, ratio, bin):
mtrx_flux_mean = zeros((1, len(obsid)), float64)
for i in range (0, len(obsid)):
#if g_broad_flux_img_list[i][row,col] > 0:
mtrx_flux_mean[0,i] = g_broad_flux_img_list[i][row,col]
flux_mean = mean(mtrx_flux_mean, axis=1, dtype=float64)
return flux_mean
###MASK Creates mask values - filters through values in matrix and determines those that are meaningful for analysis
def construct_mask(total_rows, total_cols, ratio, bin):
for row in range(0, total_rows):
for col in range(0, total_cols):
flux_mean = calc_flux_mean(row, col, ratio, bin)
# if flux_mean > mask_bin_values[bin]:
if flux_mean > mask_bin_values[bin] and check_band_count_sum(row, col, ratio, bin) == 1 and check_obsid_count_sum(row, col, ratio, bin) == 1:
# if check_obsid_count_sum(row, col, ratio, bin)== 1 and check_band_count_sum(row, col, ratio, bin) == 1:
masked_rows.append(row)
masked_cols.append(col)
return
###MASK Read in flux images into appropriate lists
def read_in_images_to_list(ratio, bin):
for observation in obsid:
#READ IN SOFT, MEDIUM, HARD FLUX IMAGES
fq_soft_flux_filename = construct_flux_filename(observation, ratio, bin, bands[0])
soft_flux_image = read_single_img(fq_soft_flux_filename)
g_soft_flux_img_list.append(soft_flux_image)
fq_medium_flux_filename = construct_flux_filename(observation, ratio, bin, bands[1])
medium_flux_image = read_single_img(fq_medium_flux_filename)
g_medium_flux_img_list.append(medium_flux_image)
fq_hard_flux_filename = construct_flux_filename(observation, ratio, bin, bands[2])
hard_flux_image = read_single_img(fq_hard_flux_filename)
g_hard_flux_img_list.append(hard_flux_image)
broad_flux_image = soft_flux_image + medium_flux_image + hard_flux_image
g_broad_flux_img_list.append(broad_flux_image)
soft = construct_img_filename(observation, ratio, bin, bands[0])
g_soft_img_list.append(read_single_img(soft))
medium = construct_img_filename(observation, ratio, bin, bands[1])
g_medium_img_list.append(read_single_img(medium))
hard = construct_img_filename(observation, ratio, bin, bands[2])
g_hard_img_list.append(read_single_img(hard))
broad = read_single_img(soft) + read_single_img(medium) + read_single_img(hard)
g_broad_img_list.append(broad)
if ratio == ratios[0]:
ratio_image = soft_flux_image/medium_flux_image
g_ratio_img_list.append(ratio_image)
else:
ratio_image = medium_flux_image/hard_flux_image
g_ratio_img_list.append(ratio_image)
return
###MASK Returns true if there is at least 1 count for a pixel across all observations
def check_band_count_sum(row, col, ratio, bin):
for i in range (0, len(obsid)):
soft = g_soft_img_list[i][row, col]
medium = g_medium_img_list[i][row, col]
hard = g_hard_img_list[i][row, col]
if (soft + medium + hard) < 1:
return 0
return 1
###MASK Returns true if there are at least 200 counts for a pixel across all observations,
def check_obsid_count_sum(row, col, ratio, bin):
broad_list = []
for i in range (0, len(obsid)):
broad_value= g_broad_img_list[i][row,col]
broad_list.append(broad_value)
count_sum = sum (broad_list)
if count_sum > 200:
return 1
else:
return 0
###Calculates matrix of log (base 10) values of given lists long as the value does not equal zero
def construct_log_list(mask_list):
log_list = []
for i in range(0,len (mask_list)):
if mask_list[i] != 0:
log_list.append(log10(mask_list[i]))
else:
print '****MASK ERROR: VALUE 0****'
return log_list
###Calculates mean of given list
def calc_mean(mask_list):
mtrx = zeros((1, len(mask_list)), float64)
for i in range(0, len(mask_list)):
mtrx[0,i] = mask_list[i]
mtrx_mean = mean(mtrx, axis=1, dtype=float64)
return mtrx_mean
###Calculates standard deviation of given list
def calc_stddev(mask_list):
mtrx_stddev = zeros ((1, len (mask_list)), float64)
for i in range (0, len (mask_list)):
mtrx_stddev[0,i] = mask_list[i]
stddev = std(mtrx_stddev, axis = 1, dtype=float64) #to take the mean of each row (there’s only one)
return stddev
###Renormalizes values of a given list (of logged values) by dividing each value by the given mean
def construct_renorm_list(mask_list, mean):
renorm_list = []
for i in range (0, len (mask_list)):
renorm_list.append(mask_list[i] - mean)
return renorm_list
###Returns list of most extreme masks values
def flag_vals(values_list, rows_list, cols_list, stddev, mean):
for i in range(0, len (values_list)):
if values_list[i] > (mean+3*stddev) or values_list[i] < (mean - 3*stddev):
#flagged_vals.append(values_list[i])
flagged_rows.append(rows_list[i])
flagged_cols.append(cols_list[i])
return
###Transfers flagged values into an array corresponding to correct coordinates
def transfer_vals_array(rows_list, cols_list):
flagged_values_array = zeros([rows_global, columns_global], float64)
for i in range(0, len (rows_list)):
flagged_vals_array[rows_list[i], cols_list[i]] = 1
return flagged_values_array
###Draws mask
def draw_mask(data, ratio, bin):
import Image
rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8)
im = Image.fromarray(rescaled)
im.save("mask_" + ratio + str(bin) + "_observation.png")
###Draws flagged regions
def draw_flagged_regions(data, ratio, bin):
import Image
rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8)
im = Image.fromarray(rescaled)
im.save("regions_" + ratio + str(bin) + "_observation.png")
#main
import sys
if len(sys.argv) < 5:
print "Four parameters are required: a bin size, a ratio, number of rows, number of columns"
exit (1)
bin_global=sys.argv[1]
ratio_global=sys.argv[2]
rows_global = int(sys.argv[3])
columns_global = int(sys.argv[4])
mask_array = zeros([rows_global, columns_global], float64)
flagged_vals_array = zeros([rows_global, columns_global], float64)
soft_flux_image = zeros([rows_global, columns_global], float64)
medium_flux_image = zeros([rows_global, columns_global], float64)
hard_flux_image = zeros([rows_global, columns_global], float64)
obsid = [198,1952, 5196, 6745, 9117,10935, 10936, 14229]
bands = ['soft', 'medium', 'hard']
ratios = ['one', 'two']
base = 5*(10**-6)
#mask_bin_values = {4:5*(10**-6), 8:5*(10**-6)*4, 16:5*(10**-6)*16, 32:5*(10**-6)*64, 64:5*(10**-6)*256}
mask_bin_values = {'4':base, '8':base*4, '16':base*16, '32':base*64, '64':base*256}
#global lists, each element is an image
g_soft_flux_img_list = []
g_medium_flux_img_list = []
g_hard_flux_img_list = []
g_broad_flux_img_list = []
g_soft_img_list = []
g_medium_img_list = []
g_hard_img_list = []
g_broad_img_list = []
g_ratio_img_list = []
#CONSTRUCT MASK
masked_rows = []
masked_cols = []
read_in_images_to_list(ratio_global, bin_global)
construct_mask(rows_global, columns_global, ratio_global, bin_global)
#initialize to ensure array is float64 array
temp_mask_array = zeros([rows_global, columns_global], float64)
temp_mask_array = transfer_vals_array(masked_rows, masked_cols)
mask_array = temp_mask_array + mask_array
draw_mask(mask_array, ratio_global, bin_global)
#OBSERVATION-WISE
obsid_index = 0
for observation in obsid:
print 'Calculating OBSERVATION-WISE analysis for observation ' + str(observation) + ' ...'
#MASK
masked_vals = []
for k in range (0, len(masked_rows)):
masked_vals.append(g_ratio_img_list[obsid_index][masked_rows[k], masked_cols[k]])
print 'masked_vals'
print len( masked_vals)
#LOG & RENORMALIZE
log_list = construct_log_list(masked_vals) #returns list of logged values of masked values
log_mean = calc_mean(log_list) #using logged values, finds mean
log_stddev = calc_stddev(log_list) #using logged values, finds stddev
renorm_list = construct_renorm_list(log_list,log_mean) #using mean of logged values, renormalizes
renorm_mean = calc_mean(renorm_list)
#ANALYSIS
flagged_vals = []
flagged_rows = []
flagged_cols = []
flag_vals(renorm_list, masked_rows, masked_cols, log_stddev, renorm_mean)
flagged_vals = []
for k in range (0, len(flagged_rows)):
flagged_vals.append(g_ratio_img_list[obsid_index][flagged_rows[k], flagged_cols[k]])
#10^[log10(mean) +log10(x/mean)]
#value = 10**(renorm_list[k] + log_mean)
#flagged_vals.append(value)
print 'flagged_vals'
print flagged_vals
print 'flagged_rows'
print flagged_rows
print 'flagged_cols'
print flagged_cols
print '-----------------------------------------------'
temp_flagged_vals_array = transfer_vals_array(flagged_rows, flagged_cols)
flagged_vals_array = flagged_vals_array + temp_flagged_vals_array
obsid_index = obsid_index + 1
draw_mask(mask_array, ratio_global, bin_global)
draw_flagged_regions(flagged_vals_array, ratio_global, bin_global)
mask_array = zeros([rows_global, columns_global], float64)
flagged_vals_array = zeros([rows_global, columns_global], float64)