-
Notifications
You must be signed in to change notification settings - Fork 3
/
podi_photflat.py
executable file
·1100 lines (891 loc) · 34.9 KB
/
podi_photflat.py
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import os, sys
import numpy
import astropy.io.fits as pyfits
import scipy
import scipy.spatial
import scipy.interpolate
import bottleneck
import math
import multiprocessing
import queue
import ctypes
import astLib.astWCS as astWCS
from podi_commandline import *
import podi_associations
import podi_logging
from podi_definitions import *
import sharedmemory
class PhotFlatFrame(object):
def __init__(self, filename):
self.logger = logging.getLogger("PhotFlatFrame")
self.nmax = 250
# load the FITS and extract the photcalib table
self.logger.debug("Initializing %s (type: %s)" % (filename, type(filename)))
if (type(filename) == str):
self.filename = filename
hdulist = pyfits.open(self.filename)
else:
self.filename = "*in_memory*"
hdulist = filename
# load and store all WCS structures
self.is_valid = False
self.wcs = {}
self.ref_header = None
self.ota_list = []
self.extname_list = []
for ext in hdulist:
if (not is_image_extension(ext)):
continue
ota = ext.header['OTA']
self.logger.debug("reading wcs for %s in %s" % (ext.name, self.filename))
#self.wcs[ext.name] = astWCS.WCS(ext.header, mode='pyfits')
self.wcs[ota] = astWCS.WCS(ext.header, mode='pyfits')
if (self.ref_header is None):
self.ref_header = ext.header
self.logger.debug("Reading: fn=%s, OTA=%02d, extname=%s" % (
self.filename, ext.header['OTA'], ext.name))
self.ota_list.append(ext.header['OTA'])
self.extname_list.append(ext.name)
#
# now read the photcalib table
#
self.field_names = {}
try:
photcalib_tbhdu = hdulist['CAT.PHOTCALIB']
except:
self.logger.warning("No PHOTCALIB table found in %s" % (self.filename))
photcalib_tbhu = None
msg = "no PHOTCALIB extension in %s" % (self.filename)
raise Exception(msg)
#
# Now extract all sources for each OTA
#
tfields = photcalib_tbhdu.header['TFIELDS']
nstars = photcalib_tbhdu.header['NAXIS2']
catalog = numpy.empty((nstars, tfields))
for field in range(tfields):
catalog[:, field] = photcalib_tbhdu.data.field(field)
ttype = 'TTYPE%d' % (field + 1)
if (ttype in photcalib_tbhdu.header):
fieldname = photcalib_tbhdu.header[ttype]
self.field_names[fieldname] = field
self.catalog = catalog
self.logger.info("Read %d matched sources from %s" % (
catalog.shape[0], self.filename))
_, bn = os.path.split(self.filename)
dbg_catfile = "cat_%s.cat" % (bn[:-5])
self.logger.debug("Saving debug catalog to %s" % (dbg_catfile))
numpy.savetxt(dbg_catfile, self.catalog)
# now compute all zeropoints
mag0size = hdulist[0].header['MAG0SIZE']
photfilt = hdulist[0].header['PHOTFILT'].upper()
self.logger.debug("Mag0size=%s, photfilt=%s" % (hdulist[0].header['MAG0SIZE'], hdulist[0].header['PHOTFILT']))
# self.logger.info("PHOTOMETRIC FILTER: %s" % (photfilt))
if (photfilt not in ['U', 'G', 'R', 'I', 'Z']):
self.logger.critical("Unable to identify PHOTFILT value: %s" % (photfilt))
return None
odimag = self.catalog[:, self.field_names['ODI_MAG_D%d' % (int(mag0size*10))]]
refmag = self.catalog[:, self.field_names['REF_%s' % (photfilt)]]
odierr = self.catalog[:, self.field_names['ODI_ERR_D%d' % (int(mag0size*10))]]
referr = self.catalog[:, self.field_names['REF_ERR_%s' % (photfilt)]]
self.zeropoint = refmag - odimag
self.zeropoint_error = numpy.hypot(odierr, referr)
self.logger.info("computed %d zeropoints" % (odimag.shape[0]))
# also store some info about the zeropoints already computed
self.ref_ra = self.ref_header['CRVAL1']
self.ref_dec = self.ref_header['CRVAL2']
self.cos_dec = numpy.cos(numpy.radians(self.ref_dec))
# print("cos-dec=", self.cos_dec)
self.deprojected_coords = numpy.array(self.catalog[:, 0:2])
self.deprojected_coords[:,0] *= self.cos_dec
self.coord_tree = scipy.spatial.cKDTree(self.deprojected_coords)
self.is_valid = True
#print self.field_names
def valid(self):
return self.is_valid
def get_source_indices(self, ra, dec, radius=1, nmax=None, relative_coords=False):
_ra = ra * self.cos_dec
k = self.nmax if nmax is None else nmax
if (relative_coords):
_ra = self.ref_ra*self.cos_dec - ra/60
dec = self.ref_dec - dec/60
self.logger.debug("Searching for %d sources within %f arcmin of %f, %f" % (k, radius, _ra, dec))
# query which sources match
d, i = self.coord_tree.query(
[_ra,dec],
distance_upper_bound=radius/60.,
k=k,
p=2) #, n_jobs=-1)
valid_indices = numpy.isfinite(d)
idx = i[valid_indices]
return idx
def get_zeropoints(self, ra, dec, radius=1, nmax=None, relative_coords=False, max_error=None):
"""
:param ra: right ascension of search cone
:param dec: declination of search cone
:param radius: radius of search cone, in arcmin
:return: list of zeropoints as numpy.ndarray
"""
idx = self.get_source_indices(ra, dec, radius, nmax, relative_coords)
# print idx
zp = self.zeropoint[idx]
zperr = self.zeropoint_error[idx]
if (max_error is not None):
good_error = zperr < max_error
zp = zp[good_error]
zperr = zperr[good_error]
return zp
def get_ota_list(self):
return self.ota_list
def get_extname_list(self):
return self.extname_list
def get_ota_zeropoints(self, ota=None, x=2048, y=2048, radius=2048, strict_ota=False, return_error=True):
if (ota is None):
return None
#print ota
#select_ota = (self.catalog[:, self.field_names['OTA']] == ota)
#ota_cat = self.catalog[select_ota]
# convert pixel position to ra/dec
radec = self.wcs[ota].pix2wcs(x,y)
#print radec
#return radec
indices = self.get_source_indices(radec[0], radec[1],
radius=radius*0.11/60)
return_cat = self.catalog[indices]
zp = self.zeropoint[indices]
zperr = self.zeropoint_error[indices]
if (strict_ota):
# only return sources from the selected ota
ota_num = ota #int(ota[3:5])
this_ota = (return_cat[:, self.field_names['OTA']] == ota_num)
return_cat = return_cat[this_ota]
zp = zp[this_ota]
zperr = zperr[this_ota]
#print return_cat.shape,
if (return_error):
return zp, zperr
return zp
# class PhotFrame(object):
# def __init__(self, filename):
#
# self.field_names = {}
# self.catalog = None
# self.wcs = {}
# self.file_loaded = False
# self.zeropoints_computed = False
# self.apertures = numpy.array([20, 30, 40, 50, 60, 80, 100, 120])
#
# self.filename = filename
# _, bn = os.path.split(filename)
# self.filebase = bn
#
# self.logger = logging.getLogger("PF(%s)" % (bn))
#
# self.neighbor_count = 250
# self.neighbor_radius = 0
#
# self.scaling_factor = 0
#
# self.read_frame()
#
# def read_frame(self):
#
# if (self.file_loaded):
# return self.file_loaded
#
# self.logger.debug("Reading %s" % (self.filename))
# hdulist = pyfits.open(self.filename)
#
# #
# # Read general relavant properties
# #
#
# self.phot_reference = hdulist[0].header['PHOTMCAT']
# # self.magzero = hdulist[0].header['MAGZERO']
# self.magzero = hdulist[0].header['PHOTZP_X']
# self.ref_filter = hdulist[0].header['PHOTFILT']
#
# #
# # Read the photometric catalog(s)
# #
#
# try:
# photcalib_tbhdu = hdulist['CAT.PHOTCALIB']
# except:
# self.logger.warning("No PHOTCALIB table found in %s" % (self.filename))
# photcalib_tbhu = None
# return False
#
# #
# # Now extract all sources for each OTA
# #
# tfields = photcalib_tbhdu.header['TFIELDS']
# nstars = photcalib_tbhdu.header['NAXIS2']
# catalog = numpy.empty((nstars, tfields))
#
# for field in range(tfields):
# catalog[:, field] = photcalib_tbhdu.data.field(field)
# ttype = 'TTYPE%d' % (field + 1)
# if (ttype in photcalib_tbhdu.header):
# fieldname = photcalib_tbhdu.header[ttype]
# self.field_names[fieldname] = field
#
# self.catalog = catalog
# self.logger.info("Read %d matched sources from %s" % (
# catalog.shape[0], self.filename))
#
# #
# # Read all WCS headers
# #
# for ext in hdulist:
# if (not is_image_extension(ext)):
# continue
# wcs = astWCS.WCS(ext.header, mode='pyfits')
# extname = ext.name
#
# self.wcs[extname] = wcs
#
# #
# # compute a simplified WCS solution, ignoring distortion and the fact
# # there are multiple OTAs
# #
# self.setup_quick_wcs()
#
# #
# # Update the neighbor radius
# #
# self.neighbor_radius = 4. * 60. / self.pixelscale
#
# hdulist.close()
# self.file_loaded = True
# self.logger.debug("File read completed!")
# return True
#
# def setup_quick_wcs(self):
# ota44 = self.wcs['OTA44.SCI'].header
# simple = pyfits.ImageHDU()
# for key in ['CD1_1', 'CD2_2', 'CD1_2', 'CD2_1',
# 'CRVAL1', 'CRVAL2',
# 'CRPIX1', 'CRPIX2',
# 'NAXIS', 'NAXIS1', 'NAXIS2']:
# simple.header[key] = ota44[key]
# self.simple_wcs = self.wcs['OTA44.SCI'] # astWCS.WCS(simple.header, mode='pyfits')
#
# self.simple_coords = numpy.array(self.simple_wcs.wcs2pix(self.catalog[:, 0], self.catalog[:, 1]))
#
# self.simple_tree = scipy.spatial.cKDTree(self.simple_coords)
# self.pixelscale = self.simple_wcs.getPixelSizeDeg() * 3600.
#
# # numpy.savetxt("dummy_%s.cat" % (self.filebase),
# # numpy.append(self.simple_coords, self.catalog, axis=1))
# # numpy.savetxt("dummy2_%s.cat" % (self.filebase), self.simple_coords)
#
# def compute_zeropoints(self):
#
# self.logger.debug("computing zeropoints")
#
# ref_mag = "SDSS_MAG_%s" % (self.ref_filter.upper())
# ref_err = "SDSS_ERR_%s" % (self.ref_filter.upper())
#
# self.zeropoints = numpy.empty((self.catalog.shape[0], self.apertures.shape[0]))
# self.zeropoints[:, :] = numpy.NaN
#
# for idx, ap in enumerate(self.apertures):
#
# odi_mag = "ODI_MAG_D%d" % (ap)
# odi_err = "ODI_ERR_D%d" % (ap)
#
# if (odi_mag in self.field_names and
# ref_mag in self.field_names):
# zp = self.catalog[:, self.field_names[ref_mag]] - \
# self.catalog[:, self.field_names[odi_mag]]
# self.zeropoints[:, idx] = zp
#
# self.zeropoints_computed = True
#
# def convert_to_relative(self):
#
# # # compute position in the bottom left corner of OTA44
# # ra_dec = self.wcs['OTA44.SCI'].pix2wcs(0,0)
# # print ra_dec
#
# #
# # select all objects within 4 arcmin of the assumed center.
# # define the median zeropoint as reference zeropoint
# #
# d, i = self.simple_tree.query(
# [0, 0],
# p=2,
# k=1000, # use 1000 sources at most
# distance_upper_bound=(4 * 60 / self.pixelscale),
# )
# good_match = numpy.isfinite(d) & (i < self.catalog.shape[0])
#
# near_center = self.zeropoints[i[good_match]]
# # numpy.savetxt("center.cat", near_center)
#
# center_zp = bottleneck.nanmedian(near_center, axis=0)
#
# # self.zeropoints_relative = self.zeropoints - center_zp
# self.zeropoints_relative = self.zeropoints - self.magzero
#
# # print center_zp.shape
# # print center_zp
#
# def prep(self):
# self.read_frame()
# self.compute_zeropoints()
# self.convert_to_relative()
#
# def get_correction(self, ra_dec):
#
# # make sure Ra/Dec has the right dimensions
# if (ra_dec.ndim == 1):
# ra_dec = ra_dec.reshape((1, -1))
#
# #
# # convert Ra/Dec to X/Y in the simple projected image
# #
# xy = numpy.array(self.simple_wcs.wcs2pix(ra_dec[:, 0], ra_dec[:, 1]))
#
# #
# # Query all stars around the given coordinates
# #
# d, i = self.simple_tree.query(xy, p=2,
# k=self.neighbor_count,
# distance_upper_bound=self.neighbor_radius,
# )
# # print d.shape
#
# valid = numpy.isfinite(d) & (i < self.catalog.shape[0])
#
# # prepare the result buffer
# all_corrections = numpy.empty((ra_dec.shape[0], self.zeropoints_relative.shape[1]))
# all_corrections[:, :] = numpy.NaN
#
# for idx in range(ra_dec.shape[0]):
# # print idx, ra_dec[idx], xy[idx]
#
# nearby_sources = i[idx, :][valid[idx, :]]
# # this_valid = valid[idx, :]
# # print this_valid.shape, numpy.sum(this_valid), nearby_sources.shape
#
# rel_zp = self.zeropoints_relative[nearby_sources]
# # print rel_zp.shape
#
# all_corrections[idx, :] = numpy.median(rel_zp, axis=0)
# # print correction.shape
#
# return all_corrections
#
# def get_weight(self, targetzp=25.):
# scaling_factor = math.pow(10, 0.4 * (targetzp - self.magzero))
# return 1. / scaling_factor
#
#
# def get_zeropoint(self, ra, dec):
# pass
#
class PhotFlatHandler(object):
def __init__(self, filelist=None, input_hdus=None):
self.logger = logging.getLogger("PhotFlat")
self.filelist = filelist
self.input_hdus = input_hdus
self.phot_frames = {}
self.ota_from_extname = {}
self.extname_from_ota = {}
pass
def read_catalogs(self):
self.logger.debug("Reading PHOTCALIB catalogs")
#
# Read all files
#
if (self.filelist is not None):
for idx, fn in enumerate(self.filelist):
if (not os.path.isfile(fn)):
self.logger.warning("File %s does not exist" % (fn))
continue
new_frame = PhotFlatFrame(fn)
self.add_new_frame(new_frame, fn)
#
# Next read all catalogs from already open HDUList(s)
#
if (self.input_hdus is not None):
for idx, hdulist in enumerate(self.input_hdus):
fn = "hdu_%d" % (idx)
new_frame = PhotFlatFrame(hdulist)
self.add_new_frame(new_frame, fn)
def add_new_frame(self, new_frame, fn):
if (new_frame.is_valid):
self.phot_frames[fn] = new_frame
for i in range(len(new_frame.get_ota_list())): #
ota = new_frame.get_ota_list()[i]
extname = new_frame.get_extname_list()[i]
# ,extname in new_frame.get_ota_list(),new_frame.get_extname_list():
self.ota_from_extname[extname] = ota
self.extname_from_ota[ota] = extname
def extname2ota(self, extname):
return self.ota_from_extname[extname]
def ota2extname(self, ota):
return self.extname_from_ota[ota]
def get_reference_zeropoint(self,
ra, dec, radius,
relative_coords=True,
max_error=0.05):
reference_zp = {}
for framename in self.phot_frames:
self.logger.debug("Adding photometric data from file %s" % (framename))
frame = self.phot_frames[framename]
zps = frame.get_zeropoints(ra=ra, dec=dec, radius=radius,
relative_coords=relative_coords,
max_error=max_error)
#print zps
reference_zp[framename] = numpy.median(zps)
return reference_zp
def get_ota_set(self):
list_of_otas = []
for framename in self.phot_frames:
frame = self.phot_frames[framename]
list_of_otas.extend(frame.get_ota_list())
return set(list_of_otas)
def get_extname_set(self):
list_of_extnames = []
for framename in self.phot_frames:
frame = self.phot_frames[framename]
list_of_extnames.extend(frame.get_extname_list())
return set(list_of_extnames)
def expand_to_fullres_worker(job_queue, photflat, blocksize, shmem_out, shmem_shape, memlock):
logger = logging.getLogger("PF_expand2fullres")
# Prepare the relative coordinates
x, y = numpy.indices((blocksize, blocksize), dtype=numpy.float)
y /= blocksize
x /= blocksize
# print x[:5,:5], x[-5:,-5:]
omx = 1. - x
omy = 1. - y
omx_omy = omx * omy
omx_y = omx * y
x_omy = x * omy
x_y = x * y
# out_buffer = numpy.zeros((4096,4096))
# out_buffer[:,:] = numpy.NaN
# print ix,iy
# out_buffer = shmem_as_ndarray(shmem_out).reshape(shmem_shape)
out_buffer = shmem_out.to_ndarray()
out = numpy.empty((blocksize, blocksize))
while (True):
try:
job = job_queue.get_nowait()
except queue.Empty:
# print "done!"
break
(ix, iy) = job
#print ix,iy
#
# Follow the algorithm outlined in
# https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_Square
#
try:
f_00 = photflat[ix, iy]
f_01 = photflat[ix, iy+1]
f_10 = photflat[ix+1, iy]
f_11 = photflat[ix+1, iy+1]
# f_00 = photflat[iy, ix]
# f_01 = photflat[iy, ix + 1]
# f_10 = photflat[iy + 1, ix]
# f_11 = photflat[iy + 1, ix + 1]
except IndexError:
logger.warning("Index error accessing %d,%d" % (ix, iy))
continue
#if (ix>=15 or iy>=15):
# print ix,iy, f_00, f_01, f_10, f_11
# handle bad values
filler_value = numpy.nanmean([f_00, f_01, f_10, f_11])
f_00 = f_00 if numpy.isfinite(f_00) else filler_value
f_01 = f_01 if numpy.isfinite(f_01) else filler_value
f_10 = f_10 if numpy.isfinite(f_10) else filler_value
f_11 = f_11 if numpy.isfinite(f_11) else filler_value
out = f_00 * omx_omy + f_10 * omx_y + f_01 * x_omy + f_11 * x_y
# out = f_00 * omx_omy + f_10 * x_omy + f_01 * omx_y + f_11 * x_y
#out[:,:] = f_00
memlock.acquire()
#print "writing line",iy
out_buffer[iy * blocksize:(iy + 1) * blocksize,
ix * blocksize:(ix + 1) * blocksize] = out
memlock.release()
def expand_to_fullres(photflat, blocksize, out_dimension=None, mag2flux=True):
if (out_dimension is None):
out_dimension = (4096, 4096)
#
# Prepare parallel interpolation upwards to full-resolution
#
_x,_y = out_dimension
out_shmem = sharedmemory.SharedMemory(_type=ctypes.c_float, shape=out_dimension)
out_buffer = out_shmem.to_ndarray()
# print(out_shmem, out_buffer, out_shmem.is_allocated())
# out_shmem = multiprocessing.RawArray(ctypes.c_float, _x*_y)
# out_buffer = shmem_as_ndarray(out_shmem).reshape(out_dimension)
out_buffer[:, :] = numpy.NaN
job_queue = multiprocessing.JoinableQueue()
data_lock = multiprocessing.Lock()
# prepare all jobs - each job interpolates one little block
# print("preparing job queue")
for ix, iy in itertools.product(range(photflat.shape[0]-1), repeat=2):
job_queue.put((ix,iy))
processes = []
for i in range(7):
p = multiprocessing.Process(target=expand_to_fullres_worker,
kwargs={
'job_queue': job_queue,
'shmem_out': out_shmem,
'shmem_shape': out_buffer.shape,
'photflat': photflat, #correction_2d,
'blocksize': blocksize,
'memlock': data_lock,
}
)
p.start()
processes.append(p)
for p in processes:
p.join()
# Now we have the photometric flat-field, convert it to scaling frame
#print("writing results")
if (mag2flux):
photflat_2d = numpy.power(10, 0.4*out_buffer)
else:
photflat_2d = numpy.copy(out_buffer)
# print("freeing memory")
out_shmem.free()
return photflat_2d
def parallel_create_photometric_flatfields_worker(
input_queue,
result_queue,
pf,
reference_zp,
sampling = 512,
smoothing=1024,
resolution=60.
):
logger = logging.getLogger("ParPhotflat")
while (True):
cmd = input_queue.get()
if (cmd is None):
logger.debug("Received NOne as shutdown command")
break
extname = cmd
logger.debug("Creating photflat for ext %s in parallel" % (extname))
imghdu, photflat, photflat_err = create_photometric_flatfield_single_ota(
extname=extname,
pf=pf,
reference_zp=reference_zp,
sampling=sampling,
smoothing=smoothing,
)
result_queue.put((imghdu, photflat, photflat_err))
input_queue.task_done()
logger.debug("done with extension %s" % (extname))
logger.debug("Shutting down parallel photflat worker")
def create_photometric_flatfield_single_ota(
extname,
pf,
reference_zp,
sampling=512,
smoothing=1024,
min_error=0.005,
small_error_limit=0.03,
strict_ota=False,
):
n_samples = int(math.ceil(4096. / sampling)) + 1
sample_pixels = int(math.floor((4096. / (n_samples-1))))
logger = logging.getLogger("PhotFlatSingleOTA")
logger.debug("Using pixel-grid of %d^2 samples every %d pixels" % (
n_samples, sample_pixels))
ref_points = numpy.arange(n_samples) * sample_pixels
# print n_samples, sample_pixels, n_samples*sample_pixels
iy,ix = numpy.indices((n_samples, n_samples)) * sample_pixels
running_sum = 0
ota = pf.extname2ota(extname)
photflat = numpy.empty((n_samples, n_samples))
photflat_err = numpy.empty((n_samples, n_samples))
# dump = open("dump_%d" % (ota), "w")
for _x, _y in itertools.product(range(n_samples), repeat=2):
# print x,y
x = ref_points[_x]
y = ref_points[_y]
full_zp_list = None
full_zperr_list = None
for framename in pf.phot_frames:
frame = pf.phot_frames[framename]
# print framename, ota, x, y, frame
zp_list, zperr_list = frame.get_ota_zeropoints(
ota=ota, x=x, y=y, radius=smoothing,
strict_ota=strict_ota,
return_error=True)
# correct zp for the specific offset
zp_list = zp_list - reference_zp[framename]
if (full_zp_list is None):
full_zp_list = zp_list
full_zperr_list = zperr_list
else:
full_zp_list = numpy.append(full_zp_list, zp_list, axis=0)
full_zperr_list = numpy.append(full_zperr_list, zperr_list,
axis=0)
# print ota, ota[3:5], x, y, full_zp_list.shape[0]
running_sum += full_zp_list.shape[0]
# ideally, do some outlier rejection here
_, mask = three_sigma_clip(full_zp_list, return_mask=True)
full_zp_list = full_zp_list[mask]
full_zperr_list = full_zperr_list[mask]
# ensure minimum error to avoid having a few points dominate the solution
full_zperr_list[full_zperr_list < min_error] = min_error
# select good datapoints
good_data = (full_zperr_list < small_error_limit) & \
numpy.isfinite(full_zp_list) & numpy.isfinite(full_zperr_list)
full_zp_list = full_zp_list[good_data]
full_zperr_list = full_zperr_list[good_data]
# compute the weighted mean correction factor
logger.debug("%s [%d/%d]: %d %d" % (extname, x, y, full_zp_list.shape[0], full_zperr_list.shape[0]))
if (full_zp_list.shape[0] > 0 and full_zperr_list.shape[0] > 0):
photflat[_x, _y] = numpy.sum(
full_zp_list / full_zperr_list) / numpy.sum(1. / full_zperr_list)
photflat_err[_x, _y] = numpy.std(full_zp_list)
else:
photflat[_x,_y] = numpy.NaN
photflat_err[_x, _y] = numpy.NaN
# numpy.savetxt(dump,
# numpy.array([full_zp_list, full_zperr_list,
# numpy.ones((full_zp_list.shape[0])) *
# photflat[_x, _y]]).T)
# numpy.append(full_zp_list.reshape((-1,1)), full_zperr_list.reshape((-1,1)), axis=1))
# print >> dump, "\n" * 10
# photflat[_x,_y] = numpy.median(full_zp_list)
# combined = numpy.empty((n_samples ** 2, 4))
# combined[:, 0] = ix.ravel()
# combined[:, 1] = iy.ravel()
# combined[:, 2] = photflat.ravel()
# numpy.savetxt("photflat.%02d.combined" % (ota), combined)
# numpy.savetxt("photflat.%02d.photflat" % (ota), photflat)
#
# combined[:, 3] = photflat_err.ravel()
# numpy.savetxt("photflat.%02d.err" % (ota), combined)
fullres = expand_to_fullres(photflat, blocksize=sampling)
imghdu = pyfits.ImageHDU(data=fullres, name=extname)
# add some headers to allow for mosaic viewing
otax, otay = int(math.floor(ota / 10)), int(ota % 10)
s = 4096
detsec = "[%d:%d,%d:%d]" % (
otax * s, (otax + 1) * s, otay * s, (otay + 1) * s)
imghdu.header["DETSEC"] = (detsec, "position of OTA in focal plane")
return imghdu, photflat, photflat_err
def create_photometric_flatfield(
filelist=None,
input_hdus=None,
strict_ota=False,
smoothing=None,
debug=False,
return_interpolator=False,
parallel=True,
n_processes=-1,
):
logger = logging.getLogger("PhotFlat")
if (n_processes == 0):
n_processes = multiprocessing.cpu_count()
elif (n_processes < 0):
n_processes = sitesetup.number_cpus
if (smoothing is None):
smoothing = 120.
smoothing_pixels = smoothing / 0.11
logger.info("Using PF smoothing length of %.1f arcsec" % (smoothing))
pf = PhotFlatHandler(
filelist=filelist,
input_hdus=input_hdus
)
n_frames = len(filelist) if filelist is not None else 0
n_hdus = len(input_hdus) if input_hdus is not None else 0
logger.info("Computing photometric flatfield from %d disk-files and %d memory-files" % (
n_frames, n_hdus)
)
# logger.info("Input files:\n-- %s" % ("\n-- ".join(filelist)))
pf.read_catalogs()
reference_pos = [4., -4.]
# that's in arc-min relative to reference point from CRVAL1/2
reference_zp = pf.get_reference_zeropoint(
ra=reference_pos[0],
dec=reference_pos[1],
radius=3,
relative_coords=True,
max_error=0.05)
logger.debug("Using reference ZP: %s" % (reference_zp))
# reference_zp = {}
# list_of_otas = []
# list_of_extnames = []
# for framename in pf.phot_frames:
# logger.info("Adding photometric data from file %s" % (framename))
# frame = pf.phot_frames[framename]
#
# zps = frame.get_zeropoints(ra=reference_pos[0],
# dec=reference_pos[1],
# radius=3,
# relative_coords=True, max_error=0.05)
# print zps
# reference_zp[framename] = numpy.median(zps)
#
# # also collect a list of all available OTAs
# list_of_otas.extend(frame.get_ota_list())
# list_of_extnames.extend(frame.get_extname_list())
#
# print reference_zp
unique_otas = pf.get_ota_set()
unique_extnames = pf.get_extname_set()
# set(list_of_otas)
# print list_of_otas
#
# unique_extnames = set(list_of_extnames)
# print unique_extnames
#
# Now extract the relative ZP differences for each of the sectors in each ota
#
sampling = 512
otalist = [pyfits.PrimaryHDU()]
running_sum = 0
all_photflat = []
all_photflat_err = []
all_extnames = []
if (parallel):
logger.debug("Calculating photometric flatfield in parallel")
# prepare jobs
extname_queue = multiprocessing.JoinableQueue()
for i, extname in enumerate(unique_extnames):
extname_queue.put(extname)
result_queue = multiprocessing.Queue()
# start parallel execution in separate processes
processes = []
for i in range(n_processes):
# start the process
p = multiprocessing.Process(
target=parallel_create_photometric_flatfields_worker,
kwargs=dict(
input_queue=extname_queue,
result_queue=result_queue,
pf=pf,
reference_zp=reference_zp,
sampling=sampling,
smoothing=smoothing_pixels,
)
)
# p.daemon = True
p.start()
processes.append(p)
# also add a termination command to the job queue
extname_queue.put(None)
# Gather all results
for _ in unique_extnames:
(imghdu, photflat, photflat_err) = result_queue.get()
otalist.append(imghdu)
all_photflat.append(photflat)
all_photflat_err.append(photflat_err)
all_extnames.append(imghdu.name)
logger.info("Received %d phot-flat extensions from parallel workers" % (len(otalist)-1))
else:
logger.debug("Using the serial approach towards the photometric flatfield")
for i, extname in enumerate(unique_extnames):
logger.info("Computing photometric flat-field for OTA %s (%2d of %2d)" % (extname, i+1, len(unique_otas)))
imghdu, photflat, photflat_err = create_photometric_flatfield_single_ota(
extname=extname,
pf=pf,
reference_zp=reference_zp,
sampling=sampling,
enlarge=enlarge,
)
otalist.append(imghdu)
all_photflat.append(photflat)
all_photflat_err.append(photflat_err)
all_extnames.append(imghdu.name)
logger.debug("Total sum of reference values: %d" % (running_sum))
# break
#
# Calculate the mean and/or median level of the photflat across
# the mean level
#
all_photflat = numpy.array(all_photflat)
fluxcorr = numpy.power(10., 0.4*all_photflat)
numpy.savetxt("photcorr", all_photflat.ravel())
numpy.savetxt("flatcorr", fluxcorr.ravel())
numpy.save("photcorr_npy", all_photflat)
mean_level = numpy.nanmean(fluxcorr)
mean_mag = numpy.nanmean(all_photflat)
logger.info("Mean photometric flatfield level: %8.5f (delta-mag=%7.4f)" % (mean_level, mean_mag))
import pickle
fluxcorr /= mean_level
pickle.dump((fluxcorr, all_extnames), open("photflat.pickle", "wb"))
logger.debug("Correcting photometric flatfield mean level")
for ota in otalist[1:]:
ota.data /= mean_level