-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_superBIT_data_optics.py
1345 lines (1106 loc) · 49.2 KB
/
mock_superBIT_data_optics.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
# GalSim is opyright (c) 2012-2019 by the GalSim developers team on GitHub
# https://github.com/GalSim-developers
#
# GalSim is free software: redistribution and use in source and binary forms,
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions, and the disclaimer given in the accompanying LICENSE
# file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the disclaimer given in the documentation
# and/or other materials provided with the distribution.
##
## TO DO: find a way to write n_obj, exptime, filter, and other useful info to a FITS header
import sys
import os
import math
import logging
import time
import galsim
import galsim.des
import galsim.convolve
import pdb
from glob import glob
import pickle
import scipy
import yaml
import numpy as np
import fitsio
from astropy.io import fits
from numpy.random import SeedSequence, default_rng
from functools import reduce
from astropy.table import Table
from argparse import ArgumentParser
from mpi_helper import MPIHelper
from multiprocessing import Pool
import superbit_lensing.utils as utils
def parse_args():
parser = ArgumentParser()
parser.add_argument('config_file', action='store', type=str,
help='Configuration file for mock sims')
parser.add_argument('-run_name', action='store', type=str, default='',
help='Name of mock simulation run')
parser.add_argument('-outdir', action='store', type=str,
help='Output directory of simulated files')
parser.add_argument('-ncores', action='store', type=int, default=1,
help='Number of cores to use for multiproessing')
parser.add_argument('--mpi', action='store_true', default=False,
help='Use to turn on mpi')
parser.add_argument('--clobber', action='store_true', default=False,
help='Turn on to overwrite existing files')
parser.add_argument('--vb', action='store_true', default=False,
help='Turn on for verbose prints')
return parser.parse_args()
class truth():
def __init__(self):
'''
class to store attributes of a mock galaxy or star
:x/y: object position in full image
:ra/dec: object position in WCS --> may need revision?
:g1/g2: NFW shear moments
:mu: NFW magnification
:z: galaxy redshift
'''
self.cosmos_index = -1
self.x = None
self.y = None
self.ra = None
self.dec = None
self.g1 = 0.0
self.g2 = 0.0
self.mu = 1.0
self.z = 0.0
self.fwhm = 0.0
self.mom_size = 0.0
self.n = 0.0
self.hlr = 0.0
self.scale_h_over_r = 0.0
def nfw_lensing(nfw_halo, pos, nfw_z_source):
"""
- For some much-needed tidiness in main(), place the function that shears each galaxy here
- Usage is borrowed from demo9.py
- nfw_halo is galsim.NFW() object created in main()
- pos is position of galaxy in image
- nfw_z_source is background galaxy redshift
"""
g1,g2 = nfw_halo.getShear( pos , nfw_z_source )
nfw_shear = galsim.Shear(g1=g1,g2=g2)
nfw_mu = nfw_halo.getMagnification( pos , nfw_z_source )
if nfw_mu < 0:
print("Warning: mu < 0 means strong lensing! Using mu=25.")
nfw_mu = 25
elif nfw_mu > 25:
print("Warning: mu > 25 means strong lensing! Using mu=25.")
nfw_mu = 25
return nfw_shear, nfw_mu
def make_obj_runner(batch_indices, *args, **kwargs):
'''
Handles the batch running of make_obj() over multiple cores
'''
res = []
for i in batch_indices:
res.append(make_obj(i, *args, **kwargs))
return res
def make_obj(i, obj_type, *args, **kwargs):
'''
Runs the approrpriate "make_a_{obj}" function given object type.
Particularly useful for multiprocessing wrappers
'''
logprint = args[-1]
func = None
func_map = {
'gal': make_a_galaxy,
'cluster_gal': make_cluster_galaxy,
'star': make_a_star
}
obj_types = func_map.keys()
if obj_type not in obj_types:
raise ValueError(f'Object type must be one of {obj_types}!')
func = func_map[obj_type]
try:
obj_index = int(i)
logprint(f'Starting {obj_type} {i}')
stamp, truth = func(*args, **kwargs,obj_index=i)
logprint(f'{obj_type} {i} completed succesfully')
except galsim.errors.GalSimError:
logprint(f'{obj_type} {i} has failed, skipping...')
return i, None, None
return i, stamp, truth
def combine_objs(make_obj_outputs, full_image, truth_catalog, exp_num):
'''
(i, stamps, truths) are the output of make_obj
exp_num is the exposure number. Only add to truth table if == 1
'''
# flatten outputs into 1 list
make_obj_outputs = [item for sublist in make_obj_outputs
for item in sublist]
for i, stamp, truth in make_obj_outputs:
if (stamp is None) or (truth is None):
continue
# Find the overlapping bounds:
bounds = stamp.bounds & full_image.bounds
# Finally, add the stamp to the full image.
try:
full_image[bounds] += stamp[bounds]
except galsim.errors.GalSimBoundsError as e:
print(e)
this_flux = np.sum(stamp.array)
if exp_num == 1:
row = [i, truth.cosmos_index, truth.x, truth.y,
truth.ra, truth.dec,
truth.g1, truth.g2,
truth.mu,truth.z,
this_flux, truth.fwhm, truth.mom_size,
truth.n, truth.hlr, truth.scale_h_over_r,
truth.obj_class
]
truth_catalog.addRow(row)
return full_image, truth_catalog
def make_a_galaxy(ud, wcs, affine, cosmos_cat, nfw, psf, sbparams, logprint, obj_index=None):
"""
Method to make a single galaxy object and return stamp for
injecting into larger GalSim image
"""
# Choose a random RA, Dec around the sky_center.
# Note that for this to come out close to a square shape, we need to account for the
# cos(dec) part of the metric: ds^2 = dr^2 + r^2 d(dec)^2 + r^2 cos^2(dec) d(ra)^2
# So need to calculate dec first.
dec = sbparams.center_dec + (ud()-0.5) * sbparams.image_ysize_arcsec * galsim.arcsec
ra = sbparams.center_ra + (ud()-0.5) * sbparams.image_xsize_arcsec / np.cos(dec) * galsim.arcsec
world_pos = galsim.CelestialCoord(ra,dec)
# We will need the image position as well, so use the wcs to get that
image_pos = wcs.toImage(world_pos)
# We also need this in the tangent plane, which we call "world coordinates" here.
# This is still an x/y corrdinate
uv_pos = affine.toWorld(image_pos)
logprint.debug('created galaxy position')
## Draw a Galaxy from scratch
## Note units of sbparams.gain is assumed to be be e-/ADU.
index = int(np.floor(ud()*len(cosmos_cat)))
gal_z = cosmos_cat[index]['ZPDF']
gal_flux = cosmos_cat[index][sbparams.bandpass] * sbparams.exp_time / sbparams.gain
phi = cosmos_cat[index]['c10_sersic_fit_phi'] * galsim.radians
q = cosmos_cat[index]['c10_sersic_fit_q']
# Cosmos HLR is in units of HST pix, convert to arcsec.
half_light_radius=cosmos_cat[index]['c10_sersic_fit_hlr']*0.03*np.sqrt(q)
n = cosmos_cat[index]['c10_sersic_fit_n']
logprint.debug(f'galaxy z={gal_z} flux={gal_flux} hlr={half_light_radius} ' + \
f'sersic_index={n}')
# Sersic class requires index n >= 0.3
if (n < 0.3):
n = 0.3
gal = galsim.Sersic(n = n,
flux = gal_flux,
half_light_radius = half_light_radius)
gal = gal.shear(q = q, beta = phi)
logprint.debug('created galaxy')
## Apply a random rotation
theta = ud()*2.0*np.pi*galsim.radians
gal = gal.rotate(theta)
## Apply a random rotation
theta = ud()*2.0*np.pi*galsim.radians
gal = gal.rotate(theta)
## Get the reduced shears and magnification at this point
try:
nfw_shear, mu = nfw_lensing(nfw, uv_pos, gal_z)
g1=nfw_shear.g1; g2=nfw_shear.g2
gal = gal.lens(g1, g2, mu)
except galsim.errors.GalSimError:
logprint(f'could not lens galaxy at z = {gal_z}, setting default values...')
g1 = 0.0; g2 = 0.0
mu = 1.0
final = galsim.Convolve([psf, gal])
logprint.debug('Convolved star and PSF at galaxy position')
stamp = final.drawImage(wcs=wcs.local(image_pos))
stamp.setCenter(image_pos.x,image_pos.y)
logprint.debug('drew & centered galaxy!')
galaxy_truth=truth()
galaxy_truth.cosmos_index = cosmos_cat[index]['NUMBER']
galaxy_truth.ra=ra.deg; galaxy_truth.dec=dec.deg
galaxy_truth.x=image_pos.x; galaxy_truth.y=image_pos.y
galaxy_truth.g1=g1; galaxy_truth.g2=g2
galaxy_truth.mu = mu; galaxy_truth.z = gal_z
galaxy_truth.flux = stamp.added_flux
galaxy_truth.n = n; galaxy_truth.hlr = half_light_radius
#galaxy_truth.inclination = inclination.deg # storing in degrees for human readability
galaxy_truth.scale_h_over_r = q
galaxy_truth.obj_class = 'gal'
logprint.debug('created truth values')
try:
galaxy_truth.fwhm=final.calculateFWHM()
except galsim.errors.GalSimError:
logprint.debug('fwhm calculation failed')
galaxy_truth.fwhm=-9999.0
try:
galaxy_truth.mom_size=stamp.FindAdaptiveMom().moments_sigma
except galsim.errors.GalSimError:
logprint.debug('sigma calculation failed')
galaxy_truth.mom_size=-9999.
logprint.debug('stamp made, moving to next galaxy')
return stamp, galaxy_truth
def make_cluster_galaxy(ud, wcs,affine, centerpix, cluster_cat, psf, sbparams, logprint, obj_index=None):
"""
Method to make a single galaxy object and return stamp for
injecting into larger GalSim image
Galaxies defined here are not lensed, and are magnified to
look more "cluster-like."
"""
# Choose a random position within 200 pixels of the sky_center
radius = 200
max_rsq = radius**2
while True: # (This is essentially a do..while loop.)
x = (2.*ud()-1) * radius
y = (2.*ud()-1) * radius
rsq = x**2 + y**2
if rsq <= max_rsq: break
# We will need the image position as well, so use the wcs to get that,
# plus a small gaussian jitter so cluster doesn't look too box-like
image_pos = galsim.PositionD(x+centerpix.x+(ud()-0.5)*100,y+centerpix.y+(ud()-0.5)*100)
world_pos = wcs.toWorld(image_pos)
ra=world_pos.ra; dec = world_pos.dec
# We also need this in the tangent plane, which we call "world coordinates" here,
# This is still an x/y corrdinate
uv_pos = affine.toWorld(image_pos)
# Fixed redshift for cluster galaxies
gal_z = sbparams.nfw_z_halo
g1 = 0.0; g2 = 0.0
mu = 1.0
# Create galaxy
gal = cluster_cat.makeGalaxy(gal_type='parametric', rng=ud)
logprint.debug('created cluster galaxy')
# Apply a random rotation
theta = ud()*2.0*np.pi*galsim.radians
gal = gal.rotate(theta)
# The "magnify" is just for drama
gal *= sbparams.flux_scaling
gal.magnify(2)
logprint.debug(f'rescaled galaxy with scaling factor {sbparams.flux_scaling}')
final = galsim.Convolve([psf, gal])
logprint.debug('Convolved star and PSF at galaxy position')
# Draw galaxy image
this_stamp_image = galsim.Image(128, 128,wcs=wcs.local(image_pos))
#cluster_stamp = final.drawImage(bandpass,image=this_stamp_image)
cluster_stamp = final.drawImage(image=this_stamp_image)
#cluster_stamp.setCenter(ix_nominal,iy_nominal)
cluster_stamp.setCenter(image_pos.x,image_pos.y)
logprint.debug('drew & centered galaxy!')
cluster_galaxy_truth=truth()
cluster_galaxy_truth.ra=ra.deg; cluster_galaxy_truth.dec=dec.deg
#cluster_galaxy_truth.x=ix_nominal; cluster_galaxy_truth.y=iy_nominal
cluster_galaxy_truth.x=image_pos.x; cluster_galaxy_truth.y=image_pos.y
cluster_galaxy_truth.g1=g1; cluster_galaxy_truth.g2=g2
cluster_galaxy_truth.mu = mu; cluster_galaxy_truth.z = gal_z
cluster_galaxy_truth.flux = cluster_stamp.added_flux
cluster_galaxy_truth.obj_class = 'cluster_gal'
logprint.debug('created truth values')
try:
cluster_galaxy_truth.fwhm=final.calculateFWHM()
except galsim.errors.GalSimError:
logprint.debug('fwhm calculation failed')
cluster_galaxy_truth.fwhm=-9999.0
try:
cluster_galaxy_truth.mom_size=cluster_stamp.FindAdaptiveMom().moments_sigma
except:
logprint.debug('sigma calculation failed')
cluster_galaxy_truth.mom_size=-9999.
return cluster_stamp, cluster_galaxy_truth
def make_a_star(ud, pud, k, wcs, affine, psf, sbparams, logprint, obj_index=None):
"""
makes a star-like object for injection into larger image.
"""
# Choose a random RA, Dec around the sky_center.
dec = sbparams.center_dec + (ud()-0.5) * sbparams.image_ysize_arcsec * galsim.arcsec
ra = sbparams.center_ra + (ud()-0.5) * sbparams.image_xsize_arcsec / np.cos(dec) * galsim.arcsec
world_pos = galsim.CelestialCoord(ra,dec)
# We will need the image position as well, so use the wcs to get that
image_pos = wcs.toImage(world_pos)
# We also need this in the tangent plane, which we call "world coordinates" here,
# This is still an x/y corrdinate
uv_pos = affine.toWorld(image_pos)
# Draw star flux at random; based on either a semi-analytic distribution or GAIA stars
# Default to blue stars, which are plenty bright
index = obj_index - 1
# Now that all star fluxes have been implemented, all bitfluxes can be used.
if sbparams.star_cat is not None:
if sbparams.bandpass == 'crates_lum':
star_flux = sbparams.star_cat['bitflux_electrons_lum'][index]
elif sbparams.bandpass == 'crates_nir':
star_flux = sbparams.star_cat['bitflux_electrons_nir'][index]
elif sbparams.bandpass == 'crates_r':
star_flux = sbparams.star_cat['bitflux_electrons_r'][index]
elif sbparams.bandpass == 'crates_u':
star_flux = sbparams.star_cat['bitflux_electrons_u'][index]
elif sbparams.bandpass == 'crates_g':
star_flux = sbparams.star_cat['bitflux_electrons_g'][index]
elif sbparams.bandpass == 'crates_b':
star_flux = sbparams.star_cat['bitflux_electrons_b'][index]
else:
raise NotImplementedError('Star catalog sampling only implemented ' +
'for crates_shape, crates_b, crates_g, crates_u, crates_r, and crates_nir!')
star_flux *= sbparams.exp_time / sbparams.gain
else:
pud = np.random.default_rng()
p = pud.power(0.4)
flux_p = (10/p) - 10.
star_flux = flux_p
if sbparams.bandpass=='crates_b':
star_flux *= 0.8271672
else:
raise NotImplementedError('Star power law only implemented for crates_b!')
# Generate PSF at location of star, convolve with optical model to make a star
deltastar = galsim.DeltaFunction(flux=star_flux)
star = galsim.Convolve([psf, deltastar])
star_stamp = star.drawImage(wcs=wcs.local(image_pos)) # before it was scale = 0.206, and that was bad!
star_stamp.setCenter(image_pos.x, image_pos.y)
star_truth = truth()
star_truth.ra = ra.deg; star_truth.dec = dec.deg
star_truth.x = image_pos.x; star_truth.y = image_pos.y
star_truth.obj_class = 'star'
try:
star_truth.fwhm = star.calculateFWHM()
except galsim.errors.GalSimError:
logprint.debug('fwhm calculation failed')
star_truth.fwhm =- 9999.0
try:
star_truth.mom_size=star_stamp.FindAdaptiveMom().moments_sigma
except galsim.errors.GalSimError:
logprint.debug('sigma calculation failed')
star_truth.mom_size=-9999.
return star_stamp, star_truth
class SuperBITParameters:
__req_params = ['run_name', 'outdir']
__req_defaults = ['', None]
def __init__(self, config_file, logprint, args=None):
"""
Initialize default params and overwirte with config_file params and / or commmand line
parameters.
"""
self.logprint = logprint
self.logprint(f'Loading parameters from {config_file}')
self._load_config_file(config_file)
# Check for command line args to overwrite config_file and / or defaults
if args is not None:
self._load_command_line(args)
# Check that certain params are set either on command line or in config
utils.check_req_params(self, self.__req_params, self.__req_defaults)
self._set_seeds()
# Setup stellar injection
self._setup_stars()
return
def _load_config_file(self, config_file):
"""
Load parameters from configuration file. Only parameters that exist in the config_file
will be overwritten.
"""
with open(config_file) as fsettings:
config = yaml.load(fsettings, Loader=yaml.FullLoader)
self._load_dict(config)
return
def _args_to_dict(self, args):
"""
Converts argparse command line arguments to a dictionary.
"""
return vars(args)
# d = {}
# for arg in args[1:]:
# optval = arg.split("=", 1)
# option = optval[0]
# value = optval[1] if len(optval) > 1 else None
# d[option] = value
# return d
def _load_command_line(self, args):
"""
Load parameters from the command line argumentts. Only parameters that are provided in
the command line will be overwritten.
"""
self.logprint('Processing command line args')
# Parse arguments here
self._load_dict(self._args_to_dict(args))
return
def _load_dict(self, d):
"""
Load parameters from a dictionary.
"""
ignore = ['config_file', 'vb']
for (option, value) in d.items():
if option in ignore:
# config file already processed above
continue
# This would be much simpler:
# else:
# setattr(self, option, value)
elif option == "pixel_scale":
self.pixel_scale = float(value)
elif option == "sky_bkg":
self.sky_bkg = float(value)
elif option == "sky_sigma":
self.sky_sigma = float(value)
elif option == "gain":
self.gain = float(value)
elif option == "read_noise":
self.read_noise = float(value)
elif option == "dark_current":
self.dark_current = float(value)
elif option == "dark_current_std":
self.dark_current_std = float(value)
elif option == "image_xsize":
self.image_xsize = int(value)
elif option == "image_ysize":
self.image_ysize = int(value)
elif option == "center_ra":
self.center_ra = float(value) * galsim.hours
elif option == "center_dec":
self.center_dec = float(value) * galsim.degrees
elif option == "nexp":
self.nexp = int(value)
elif option == "exp_time":
self.exp_time = float(value)
elif option == "nobj":
self.nobj = int(value)
elif option == "nclustergal":
self.nclustergal = int(value)
elif option == "nstars":
self.nstars = value
elif option == "tel_diam":
self.tel_diam = float(value)
elif option == "lam":
self.lam = float(value)
elif option == "mass":
self.mass = float(value)
elif option == "nfw_conc":
self.nfw_conc = float(value)
elif option == "nfw_z_halo":
self.nfw_z_halo = float(value)
elif option == "omega_m":
self.omega_m = float(value)
elif option == "omega_lam":
self.omega_lam = float(value)
elif option == "cosmosdir":
self.cosmosdir = str(value)
elif option == "datadir":
self.datadir = str(value)
elif option == "cat_file_name":
self.cat_file_name = str(value)
elif option == "fit_file_name":
self.fit_file_name = str(value)
elif option == "cluster_cat_name":
self.cluster_cat_name = str(value)
elif option == "star_cat_name":
self.star_cat_name = str(value)
elif option == "bp_file":
self.bp_file = str(value)
elif option == "outdir":
self.outdir = str(value)
elif option == "master_seed":
self.master_seed = int(value)
elif option == "noise_seed":
self.noise_seed = int(value)
elif option == "dithering_seed":
self.dithering_seed = int(value)
elif option == "cluster_seed":
self.cluster_seed = int(value)
elif option == "stars_seed":
self.stars_seed = int(value)
elif option == "nstruts":
self.nstruts = int(value)
elif option == "nstruts":
self.nstruts = int(value)
elif option == "strut_thick":
self.strut_thick = float(value)
elif option == "strut_theta":
self.strut_theta = float(value)
elif option == "obscuration":
self.obscuration = float(value)
elif option == "bandpass":
self.bandpass=str(value)
elif option == "jitter_fwhm":
self.jitter_fwhm=float(value)
elif option == "run_name":
self.run_name=str(value)
elif option == "clobber":
self.clobber=bool(value)
elif option == "mpi":
self.mpi = bool(value)
elif option == "ncores":
self.ncores = int(value)
elif option == "use_optics":
self.use_optics = bool(value)
elif option == "sample_gaia_cats":
self.sample_gaia_cats = bool(value)
elif option == "gaia_dir":
self.gaia_dir = str(value)
elif option == "noise_seed":
try:
self.noise_seed = int(value)
except:
self.noise_seed = None
elif option == "galobj_seed":
try:
self.galobj_seed = int(value)
except:
self.galobj_seed = None
elif option == "cluster_seed":
try:
self.cluster_seed = int(value)
except:
self.cluster_seed = None
elif option == "stars_seed":
try:
self.stars_seed = int(value)
except:
self.stars_seed = None
elif option == "zernike_band":
self.zernike_band = str(value)
elif option == "strehl_ratio":
self.strehl_ratio = int(value)
elif option == "OBA_SIM_DATA_DIR":
self.OBA_SIM_DATA_DIR = str(value)
else:
raise ValueError("Invalid parameter \"%s\" with value \"%s\"" % (option, value))
# Derive image parameters from the base parameters
self.image_xsize_arcsec = self.image_xsize * self.pixel_scale
self.image_ysize_arcsec = self.image_ysize * self.pixel_scale
self.center_coords = galsim.CelestialCoord(self.center_ra,self.center_dec)
self.strut_angle = self.strut_theta * galsim.degrees
# OUR NEW CATALOG IS ALREADY SCALED TO SUPERBIT 0.5 m MIRROR.
# Scaling used for cluster galaxies, which are drawn from default GalSim-COSMOS catalog
hst_eff_area = 2.4**2 #* (1.-0.33**2)
sbit_eff_area = self.tel_diam**2 #* (1.-0.380**2)
self.flux_scaling = (sbit_eff_area/hst_eff_area) * self.exp_time
if not hasattr(self,'jitter_fwhm'):
self.jitter_fwhm = 0.1
return
def _setup_stars(self):
valid_args = ['nstars', 'star_cat_name', 'sample_gaia_cats', 'gaia_dir']
for arg in valid_args:
if not hasattr(self, arg):
setattr(self, arg, None)
assert (self.nstars is not None) or \
(self.star_cat_name is not None) or \
(self.sample_gaia_cats is not None)
if (self.star_cat_name is not None) and (self.sample_gaia_cats is not None):
raise AttributeError('Cannot set both `star_cat_name` and ' +\
'`sample_gaia_cats`!')
# if sampling from a GAIA cat, do that first
if self.sample_gaia_cats is True:
if self.gaia_dir is None:
raise AttributeError('Must set `gaia_dir` if sampling from gaia cats!')
gaia_cats = glob(f'{self.gaia_dir}/GAIA*.csv')
sample_gaia_rng = np.random.default_rng(self.stars_seed)
self.star_cat_name = sample_gaia_rng.choice(gaia_cats)
if self.star_cat_name is not None:
star_fname = os.path.join(self.datadir, self.star_cat_name)
self.star_cat = Table.read(star_fname)
else:
self.star_cat = None
if self.nstars is None:
self.nstars = len(self.star_cat)
return
def _set_seeds(self):
'''
Handle the setting of various seeds
'''
seed_types = ['galobj_seed', 'cluster_seed', 'stars_seed', 'noise_seed',
'dithering_seed']
Nseeds = len(seed_types)
needed_seeds = Nseeds
master_seed = None
seeds = dict(zip(seed_types, Nseeds*[None]))
if hasattr(self, 'master_seed'):
# can't pass separate seeds if a master seed is passed
for seed in seed_types:
if hasattr(self, seed):
raise AttributeError(f'Cannot set {seed} if a ' +\
'master_seed is set!')
master_seed = self.master_seed
else:
for seed_name in seeds.keys():
if hasattr(self, seed_name):
seeds[seed_name] = getattr(self, seed_name)
needed_seeds -= 1
assert needed_seeds >= 0
if needed_seeds > 0:
# Create safe, independent obj seeds given a master seed
new_seeds = utils.generate_seeds(
needed_seeds, master_seed=master_seed
)
k = 0
for seed_name, val in seeds.items():
if val is None:
val = new_seeds.pop()
seeds[seed_name] = val
setattr(self, seed_name, val)
k += 1
assert k == needed_seeds
assert len(new_seeds) == 0
assert not (None in dict(seeds).values())
for seed_name, val in seeds.items():
print(seed_name, val)
return
# TODO: This should be updated to be sensible. see issue #10
def make_mask_files(self, logprint, clobber):
mask_dir = os.path.join(self.outdir, 'mask_files')
mask_file = 'forecast_mask.fits'
mask_outfile = os.path.join(mask_dir, mask_file)
if not os.path.exists(mask_dir):
os.mkdir(mask_dir)
if os.path.exists(mask_outfile):
self.logprint('Removing old mask file...')
os.remove(mask_outfile)
Nx = self.image_xsize
Ny = self.image_ysize
# x and y are flipped in fits convention vs. np array
mask = np.zeros((Ny, Nx), dtype='i4')
mask_fits = fitsio.FITS(mask_outfile, 'rw')
for ext in range(self.nexp):
try:
mask_fits.write(mask, ext=ext, clobber=clobber)
logprint(f'Wrote mask to {mask_outfile}')
except OSError as e:
logprint(f'OSError: {e}')
raise e
return
# TODO: This should be updated to be sensible. see issue #10
def make_weight_files(self, logprint, clobber):
weight_dir = os.path.join(self.outdir, 'weight_files')
weight_file = 'forecast_weight.fits'
weight_outfile = os.path.join(weight_dir, weight_file)
if not os.path.exists(weight_dir):
os.mkdir(weight_dir)
if os.path.exists(weight_outfile):
self.logprint('Removing old weight file...')
os.remove(weight_outfile)
Nx = self.image_xsize
Ny = self.image_ysize
# x and y are flipped in fits convention vs. np array
weight = np.ones((Ny, Nx), dtype='f8')
weight_fits = fitsio.FITS(weight_outfile, 'rw')
for ext in range(self.nexp):
try:
weight_fits.write(weight, ext=ext, clobber=clobber)
logprint(f'Wrote weight to {weight_outfile}')
except OSError as e:
logprint(f'OSError: {e}')
raise e
return
# function to help with reducing MPI results from each process to single result
def combine_images(im1, im2):
"""Combine two galsim.Image objects into one."""
# easy since they support +. Try using in-place operation to reduce memory
im1 += im2
return im1
def combine_catalogs(t1, t2):
"""Combine two galsim.OutputCatalog objects into one"""
# as far as I can tell, they expose no way of doing this aside from messing
# with the internal lists directly.
t1.rows.extend(t2.rows)
t1.sort_keys.extend(t2.sort_keys)
return t1
def get_zernike(zernike_band, strehl_ratio, csv_dir):
sr = str(int(strehl_ratio))
fname = os.path.join(csv_dir, f'psf/{zernike_band}_{sr}.csv')
return np.genfromtxt(fname, delimiter=',')[:, 1][1:]
def main(args):
"""
Make images using model PSFs and galaxy cluster shear:
- The galaxies come from a processed COSMOS 2015 Catalog, scaled to match
anticipated SuperBIT 2023 observations
- The galaxy shape parameters are assigned in a probabilistic way through matching
galaxy fluxes and redshifts to similar GalSim-COSMOS galaxies (see A. Gill+ 2023)
"""
config_file = args.config_file
run_name = args.run_name
mpi = args.mpi
ncores = args.ncores
clobber = args.clobber
vb = args.vb
start_time = time.time()
# If outdir is None, will need to move it later after it is set
if args.outdir is None:
temp_log = True
else:
temp_log = False
logfile = f'generate_mocks.log'
log = utils.setup_logger(logfile, logdir=args.outdir)
logprint = utils.LogPrint(log, vb)
if mpi is True:
M = MPIHelper()
# Define some parameters we'll use below.
sbparams = SuperBITParameters(config_file, logprint, args=args)
# if galsim config run_name does not agree with passed arg,
# it should be overridden (to match pipeline run_name)
assert run_name == sbparams.run_name
# Set up the NFWHalo:
nfw = galsim.NFWHalo(mass=sbparams.mass, conc=sbparams.nfw_conc, redshift=sbparams.nfw_z_halo,
omega_m=sbparams.omega_m, omega_lam=sbparams.omega_lam)
logprint('Set up NFW halo for lensing')
# Read in galaxy catalog, as well as catalog containing
# information from COSMOS fits like redshifts, hlr, etc.
# cosmos_cat = galsim.COSMOSCatalog(sbparams.cat_file_name, dir=sbparams.datadir)
# fitcat = Table.read(os.path.join(sbparams.cosmosdir, sbparams.fit_file_name))
cosmos_cat = Table.read(os.path.join(sbparams.datadir,
sbparams.cat_file_name))
logprint(f'Read in {len(cosmos_cat)} galaxies from catalog and associated fit info')
size_wg = (cosmos_cat['FLUX_RADIUS'] > 0) & (cosmos_cat['c10_sersic_fit_hlr'] < 50)
cosmos_cat = cosmos_cat[size_wg]
try:
cluster_cat = galsim.COSMOSCatalog(sbparams.cluster_cat_name,
dir=sbparams.cosmosdir)
except:
cluster_cat = galsim.COSMOSCatalog(sbparams.cluster_cat_name)
### Now create PSF. First, define Zernicke polynomial component
### note: aberrations were definined for lam = 550, and close to the
### center of the camera. The PSF degrades at the edge of the FOV
lam_over_diam = sbparams.lam * 1.e-9 / sbparams.tel_diam # radians
lam_over_diam *= 206265.
"""
aberrations = np.zeros(57) # Set the initial size.
aberrations[0] = 0. # First entry must be zero
aberrations[1] = 0.0494679
aberrations[4] = 0.00597539 # Noll index 4 = Defocus
aberrations[11] = -0.01476287 # Noll index 11 = Spherical
aberrations[22] = 0.00230853
aberrations[37] = 0.0000001
aberrations[56] = 0.00000001
"""
aberrations = get_zernike(zernike_band=sbparams.zernike_band, strehl_ratio=sbparams.strehl_ratio, csv_dir=sbparams.OBA_SIM_DATA_DIR)
logprint(f'Calculated lambda over diam = {lam_over_diam} arcsec')
# gaussian jitter component from gondola instabilities
jitter_psf = galsim.Gaussian(flux=1, fwhm=sbparams.jitter_fwhm)
# due to how the config is structured...
if hasattr(sbparams, 'use_optics'):
use_optics = sbparams.use_optics
else:
use_optics = True
if use_optics is False:
optics = None
psf = jitter_psf
logprint('\nuse_optics is False; using jitter-only PSF\n')
elif use_optics is True:
# will store the Zernicke component of the PSF
optics = galsim.OpticalPSF(lam=sbparams.lam,diam=sbparams.tel_diam,
obscuration=sbparams.obscuration, nstruts=sbparams.nstruts,
strut_angle=sbparams.strut_angle, strut_thick=sbparams.strut_thick,
aberrations=aberrations)
psf = galsim.Convolve([jitter_psf, optics])
logprint('\n Use_optics is True; convolving telescope optics PSF profile\n')
###
### Make generic WCS
###
# If you wanted to make a non-trivial WCS system, could set theta to a non-zero number
fiducial_full_image = galsim.ImageF(sbparams.image_xsize, sbparams.image_ysize)
theta = 0.0 * galsim.degrees
dudx = np.cos(theta) * sbparams.pixel_scale
dudy = -np.sin(theta) * sbparams.pixel_scale
dvdx = np.sin(theta) * sbparams.pixel_scale
dvdy = np.cos(theta) * sbparams.pixel_scale
affine = galsim.AffineTransform(dudx, dudy, dvdx, dvdy, origin=fiducial_full_image.true_center)
sky_center = galsim.CelestialCoord(ra=sbparams.center_ra, dec=sbparams.center_dec)
wcs = galsim.TanWCS(affine, sky_center, units=galsim.arcsec)
##
## Define RNG for dither offsets
##
rng = np.random.default_rng(sbparams.dithering_seed)
###
### MAKE SIMULATED OBSERVATIONS
### ITERATE n TIMES TO MAKE n SEPARATE IMAGES
###
for i in np.arange(1, sbparams.nexp+1):
if mpi is True:
# get MPI processes in sync at start of each image
M.barrier()
outnum = str(i).zfill(3)
outname = f'{run_name}_{outnum}.fits'
file_name = os.path.join(sbparams.outdir, outname)
# Set up a truth catalog during first image generation
if i == 1: