forked from GalSim-developers/GalSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_photon_array.py
More file actions
1790 lines (1569 loc) · 75.5 KB
/
test_photon_array.py
File metadata and controls
1790 lines (1569 loc) · 75.5 KB
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
# Copyright (c) 2012-2026 by the GalSim developers team on GitHub
# https://github.com/GalSim-developers
#
# This file is part of GalSim: The modular galaxy image simulation toolkit.
# https://github.com/GalSim-developers/GalSim
#
# 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.
#
import unittest
import numpy as np
import astropy.units as u
import os
import warnings
# We don't require astroplan. So check if it's installed.
try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
import astroplan
no_astroplan = False
except ImportError:
no_astroplan = True
import galsim
from galsim_test_helpers import *
bppath = os.path.join(galsim.meta_data.share_dir, "bandpasses")
sedpath = os.path.join(galsim.meta_data.share_dir, "SEDs")
@timer
def test_photon_array():
"""Test the basic methods of PhotonArray class
"""
nphotons = 1000
# First create from scratch
photon_array = galsim.PhotonArray(nphotons)
assert len(photon_array.x) == nphotons
assert len(photon_array.y) == nphotons
assert len(photon_array.flux) == nphotons
assert not photon_array.hasAllocatedWavelengths()
assert not photon_array.hasAllocatedAngles()
# Initial values should all be 0
np.testing.assert_array_equal(photon_array.x, 0.)
np.testing.assert_array_equal(photon_array.y, 0.)
np.testing.assert_array_equal(photon_array.flux, 0.)
# Check picklability
check_pickle(photon_array)
# Check assignment via numpy [:]
# jax does not support direct assignment
if is_jax_galsim():
pass
else:
photon_array.x[:] = 5
photon_array.y[:] = 17
photon_array.flux[:] = 23
np.testing.assert_array_equal(photon_array.x, 5.)
np.testing.assert_array_equal(photon_array.y, 17.)
np.testing.assert_array_equal(photon_array.flux, 23.)
# Check assignment directly to the attributes
photon_array.x = 25
photon_array.y = 37
photon_array.flux = 53
np.testing.assert_array_equal(photon_array.x, 25.)
np.testing.assert_array_equal(photon_array.y, 37.)
np.testing.assert_array_equal(photon_array.flux, 53.)
# Now create from shooting a profile
obj = galsim.Exponential(flux=1.7, scale_radius=2.3)
rng = galsim.UniformDeviate(1234)
photon_array = obj.shoot(nphotons, rng)
orig_x = photon_array.x.copy()
orig_y = photon_array.y.copy()
orig_flux = photon_array.flux.copy()
assert len(photon_array.x) == nphotons
assert len(photon_array.y) == nphotons
assert len(photon_array.flux) == nphotons
assert not photon_array.hasAllocatedWavelengths()
assert not photon_array.hasAllocatedAngles()
assert not photon_array.hasAllocatedPupil()
assert not photon_array.hasAllocatedTimes()
# Check arithmetic ops
photon_array.x *= 5
photon_array.y += 17
photon_array.flux /= 23
np.testing.assert_array_almost_equal(photon_array.x, orig_x * 5.)
np.testing.assert_array_almost_equal(photon_array.y, orig_y + 17.)
np.testing.assert_array_almost_equal(photon_array.flux, orig_flux / 23.)
# Check picklability again with non-zero values
check_pickle(photon_array)
# Now assign to the optional arrays
photon_array.dxdz = 0.17
assert photon_array.hasAllocatedAngles()
assert not photon_array.hasAllocatedWavelengths()
np.testing.assert_array_equal(photon_array.dxdz, 0.17)
np.testing.assert_array_equal(photon_array.dydz, 0.)
photon_array.dydz = 0.59
np.testing.assert_array_equal(photon_array.dxdz, 0.17)
np.testing.assert_array_equal(photon_array.dydz, 0.59)
# Check shooting negative flux
obj = galsim.Exponential(flux=-1.7, scale_radius=2.3)
rng = galsim.UniformDeviate(1234)
neg_photon_array = obj.shoot(nphotons, rng)
np.testing.assert_array_equal(neg_photon_array.x, orig_x)
np.testing.assert_array_equal(neg_photon_array.y, orig_y)
np.testing.assert_array_equal(neg_photon_array.flux, -orig_flux)
# Start over to check that assigning to wavelength leaves dxdz, dydz alone.
photon_array = obj.shoot(nphotons, rng)
photon_array.wavelength = 500.
assert photon_array.hasAllocatedWavelengths()
assert not photon_array.hasAllocatedAngles()
assert not photon_array.hasAllocatedPupil()
assert not photon_array.hasAllocatedTimes()
np.testing.assert_array_equal(photon_array.wavelength, 500)
photon_array.dxdz = 0.23
photon_array.dydz = 0.88
photon_array.wavelength = 912.
assert photon_array.hasAllocatedWavelengths()
assert photon_array.hasAllocatedAngles()
assert not photon_array.hasAllocatedPupil()
assert not photon_array.hasAllocatedTimes()
np.testing.assert_array_equal(photon_array.dxdz, 0.23)
np.testing.assert_array_equal(photon_array.dydz, 0.88)
np.testing.assert_array_equal(photon_array.wavelength, 912)
# Add pupil coords
photon_array.pupil_u = 6.0
assert photon_array.hasAllocatedWavelengths()
assert photon_array.hasAllocatedAngles()
assert photon_array.hasAllocatedPupil()
assert not photon_array.hasAllocatedTimes()
np.testing.assert_array_equal(photon_array.dxdz, 0.23)
np.testing.assert_array_equal(photon_array.dydz, 0.88)
np.testing.assert_array_equal(photon_array.wavelength, 912)
np.testing.assert_array_equal(photon_array.pupil_u, 6.0)
np.testing.assert_array_equal(photon_array.pupil_v, 0.0)
# Add time stamps
photon_array.time = 0.0
assert photon_array.hasAllocatedWavelengths()
assert photon_array.hasAllocatedAngles()
assert photon_array.hasAllocatedPupil()
assert photon_array.hasAllocatedTimes()
np.testing.assert_array_equal(photon_array.dxdz, 0.23)
np.testing.assert_array_equal(photon_array.dydz, 0.88)
np.testing.assert_array_equal(photon_array.wavelength, 912)
np.testing.assert_array_equal(photon_array.pupil_u, 6.0)
np.testing.assert_array_equal(photon_array.pupil_v, 0.0)
np.testing.assert_array_equal(photon_array.time, 0.0)
# Check rescaling the total flux
flux = photon_array.flux.sum()
np.testing.assert_almost_equal(photon_array.getTotalFlux(), flux)
photon_array.scaleFlux(17)
np.testing.assert_almost_equal(photon_array.getTotalFlux(), 17*flux)
photon_array.setTotalFlux(199)
np.testing.assert_almost_equal(photon_array.getTotalFlux(), 199)
photon_array.scaleFlux(-1.7)
np.testing.assert_almost_equal(photon_array.getTotalFlux(), -1.7*199)
photon_array.setTotalFlux(-199)
np.testing.assert_almost_equal(photon_array.getTotalFlux(), -199)
# Check rescaling the positions
x = photon_array.x.copy()
y = photon_array.y.copy()
photon_array.scaleXY(1.9)
np.testing.assert_array_almost_equal(photon_array.x, 1.9*x)
np.testing.assert_array_almost_equal(photon_array.y, 1.9*y)
# Check ways to assign to photons
pa1 = galsim.PhotonArray(50)
pa1.x = photon_array.x[:50]
if is_jax_galsim():
pa1.y = photon_array.y[:50]
pa1.flux = photon_array.flux[:50]
else:
for i in range(50):
pa1.y[i] = photon_array.y[i]
pa1.flux[0:50] = photon_array.flux[:50]
pa1.dxdz = photon_array.dxdz[:50]
pa1.dydz = photon_array.dydz[:50]
pa1.wavelength = photon_array.wavelength[:50]
pa1.pupil_u = photon_array.pupil_u[:50]
pa1.pupil_v = photon_array.pupil_v[:50]
pa1.time = photon_array.time[:50]
np.testing.assert_array_almost_equal(pa1.x, photon_array.x[:50])
np.testing.assert_array_almost_equal(pa1.y, photon_array.y[:50])
np.testing.assert_array_almost_equal(pa1.flux, photon_array.flux[:50])
np.testing.assert_array_almost_equal(pa1.dxdz, photon_array.dxdz[:50])
np.testing.assert_array_almost_equal(pa1.dydz, photon_array.dydz[:50])
np.testing.assert_array_almost_equal(pa1.wavelength, photon_array.wavelength[:50])
np.testing.assert_array_almost_equal(pa1.pupil_u, photon_array.pupil_u[:50])
np.testing.assert_array_almost_equal(pa1.pupil_v, photon_array.pupil_v[:50])
np.testing.assert_array_almost_equal(pa1.time, photon_array.time[:50])
# Check copyFrom
pa2 = galsim.PhotonArray(100)
pa2.copyFrom(pa1, slice(0,50))
pa2.copyFrom(pa1, target_indices=slice(50,100), source_indices=slice(49,None,-1))
np.testing.assert_array_equal(pa2.x[:50], pa1.x)
np.testing.assert_array_equal(pa2.y[:50], pa1.y)
np.testing.assert_array_equal(pa2.flux[:50], pa1.flux)
np.testing.assert_array_equal(pa2.dxdz[:50], pa1.dxdz)
np.testing.assert_array_equal(pa2.dydz[:50], pa1.dydz)
np.testing.assert_array_equal(pa2.wavelength[:50], pa1.wavelength)
np.testing.assert_array_equal(pa2.pupil_u[:50], pa1.pupil_u)
np.testing.assert_array_equal(pa2.pupil_v[:50], pa1.pupil_v)
np.testing.assert_array_equal(pa2.time[:50], pa1.time)
np.testing.assert_array_equal(pa2.x[50:], pa1.x[::-1])
np.testing.assert_array_equal(pa2.y[50:], pa1.y[::-1])
np.testing.assert_array_equal(pa2.flux[50:], pa1.flux[::-1])
np.testing.assert_array_equal(pa2.dxdz[50:], pa1.dxdz[::-1])
np.testing.assert_array_equal(pa2.dydz[50:], pa1.dydz[::-1])
np.testing.assert_array_equal(pa2.wavelength[50:], pa1.wavelength[::-1])
np.testing.assert_array_equal(pa2.pupil_u[50:], pa1.pupil_u[::-1])
np.testing.assert_array_equal(pa2.pupil_v[50:], pa1.pupil_v[::-1])
np.testing.assert_array_equal(pa2.time[50:], pa1.time[::-1])
# Can copy a single photon if desired.
pa2.copyFrom(pa1, 17, 20)
assert pa2.flux[17] == pa1.flux[20]
assert pa2.x[17] == pa1.x[20]
assert pa2.time[17] == pa1.time[20]
# Can choose not to copy flux
if is_jax_galsim():
pa2._flux = pa2._flux.at[27].set(-1)
else:
pa2.flux[27] = -1
pa2.copyFrom(pa1, 27, 10, do_flux=False)
assert pa2.flux[27] != pa1.flux[10]
assert pa2.x[27] == pa1.x[10]
assert pa2.time[27] == pa1.time[10]
# ... or positions
pa2.copyFrom(pa1, 37, 8, do_xy=False)
assert pa2.flux[37] == pa1.flux[8]
assert pa2.x[37] != pa1.x[8]
assert pa2.y[37] != pa1.y[8]
assert pa2.time[37] == pa1.time[8]
# ... or the other arrays
if is_jax_galsim():
pa2._dxdz = pa2._dxdz.at[47].set(-1)
pa2._dydz = pa2._dydz.at[47].set(-1)
pa2._wave = pa2._wave.at[47].set(-1)
pa2._pupil_u = pa2._pupil_u.at[47].set(-1)
pa2._pupil_v = pa2._pupil_v.at[47].set(-1)
pa2._time = pa2._time.at[47].set(-1)
else:
pa2.dxdz[47] = pa2.dydz[47] = pa2.wavelength[47] = -1
pa2.pupil_u[47] = pa2.pupil_v[47] = pa2.time[47] = -1
pa2.copyFrom(pa1, 47, 18, do_other=False)
assert pa2.flux[47] == pa1.flux[18]
assert pa2.x[47] == pa1.x[18]
assert pa2.y[47] == pa1.y[18]
assert pa2.dxdz[47] != pa1.dxdz[18]
assert pa2.dydz[47] != pa1.dydz[18]
assert pa2.wavelength[47] != pa1.wavelength[18]
assert pa2.pupil_u[47] != pa1.pupil_u[18]
assert pa2.pupil_v[47] != pa1.pupil_v[18]
assert pa2.time[47] != pa1.time[18]
# Can also use complicated numpy expressions for indexing.
nleft = np.sum(pa1.x < 0)
pa2.copyFrom(pa1, slice(nleft), (pa1.x<0))
assert np.all(pa2.x[:nleft] < 0)
np.testing.assert_array_equal(pa2.x[:nleft], pa1.x[pa1.x<0])
np.testing.assert_array_equal(pa2.y[:nleft], pa1.y[pa1.x<0])
pa2.copyFrom(pa1, slice(nleft,50), np.where(pa1.x>=0))
assert np.all(pa2.x[nleft:50] > 0)
np.testing.assert_array_equal(pa2.x[nleft:50], pa1.x[pa1.x>=0])
np.testing.assert_array_equal(pa2.y[nleft:50], pa1.y[pa1.x>=0])
# Error if indices are invalid
assert_raises(ValueError, pa2.copyFrom, pa1, slice(50,None), slice(50,None))
assert_raises(ValueError, pa2.copyFrom, pa1, 100, 0)
assert_raises(ValueError, pa2.copyFrom, pa1, 0, slice(None))
assert_raises(ValueError, pa2.copyFrom, pa1)
assert_raises(ValueError, pa2.copyFrom, pa1, slice(None), pa1.x<0)
assert_raises(ValueError, pa2.copyFrom, pa1, slice(None), np.where(pa1.x<0))
# Test some trivial usage of makeFromImage
zero = galsim.Image(4,4,init_value=0)
photons = galsim.PhotonArray.makeFromImage(zero)
print('photons = ',photons)
assert len(photons) == 16
np.testing.assert_array_equal(photons.flux, 0.)
ones = galsim.Image(4,4,init_value=1)
photons = galsim.PhotonArray.makeFromImage(ones)
print('photons = ',photons)
assert len(photons) == 16
np.testing.assert_array_almost_equal(photons.flux, 1.)
tens = galsim.Image(4,4,init_value=8)
photons = galsim.PhotonArray.makeFromImage(tens, max_flux=5.)
print('photons = ',photons)
assert len(photons) == 32
np.testing.assert_array_almost_equal(photons.flux, 4.)
assert_raises(ValueError, galsim.PhotonArray.makeFromImage, zero, max_flux=0.)
assert_raises(ValueError, galsim.PhotonArray.makeFromImage, zero, max_flux=-2)
# Check some other errors
undef = galsim.Image()
assert_raises(galsim.GalSimUndefinedBoundsError, pa2.addTo, undef)
# Check picklability again with non-zero values for everything
check_pickle(photon_array)
@timer
def test_convolve():
nphotons = 1000000
obj = galsim.Gaussian(flux=1.7, sigma=2.3)
rng = galsim.UniformDeviate(1234)
pa1 = obj.shoot(nphotons, rng)
rng2 = rng.duplicate() # Save this state.
pa2 = obj.shoot(nphotons, rng)
# Check that convolve is deterministic
conv_x = pa1.x + pa2.x
conv_y = pa1.y + pa2.y
conv_flux = pa1.flux * pa2.flux * nphotons
np.testing.assert_allclose(np.sum(pa1.flux), 1.7)
np.testing.assert_allclose(np.sum(pa2.flux), 1.7)
np.testing.assert_allclose(np.sum(conv_flux), 1.7*1.7)
np.testing.assert_allclose(np.sum(pa1.x**2)/nphotons, 2.3**2, rtol=0.01)
np.testing.assert_allclose(np.sum(pa2.x**2)/nphotons, 2.3**2, rtol=0.01)
np.testing.assert_allclose(np.sum(conv_x**2)/nphotons, 2.*2.3**2, rtol=0.01)
np.testing.assert_allclose(np.sum(pa1.y**2)/nphotons, 2.3**2, rtol=0.01)
np.testing.assert_allclose(np.sum(pa2.y**2)/nphotons, 2.3**2, rtol=0.01)
np.testing.assert_allclose(np.sum(conv_y**2)/nphotons, 2.*2.3**2, rtol=0.01)
pa3 = galsim.PhotonArray(nphotons)
pa3.copyFrom(pa1) # copy from pa1
pa3.convolve(pa2)
np.testing.assert_allclose(pa3.x, conv_x)
np.testing.assert_allclose(pa3.y, conv_y)
np.testing.assert_allclose(pa3.flux, conv_flux)
# Can also effect the convolution by treating the psf as a PhotonOp
pa3.copyFrom(pa1)
obj.applyTo(pa3, rng=rng2)
np.testing.assert_allclose(pa3.x, conv_x)
np.testing.assert_allclose(pa3.y, conv_y)
np.testing.assert_allclose(pa3.flux, conv_flux)
# Error to have different lengths
pa4 = galsim.PhotonArray(50, pa1.x[:50], pa1.y[:50], pa1.flux[:50])
assert_raises(galsim.GalSimError, pa1.convolve, pa4)
# Check propagation of dxdz, dydz, wavelength, pupil_u, pupil_v
for attr, checkFn in zip(
['dxdz', 'dydz', 'wavelength', 'pupil_u', 'pupil_v', 'time'],
['hasAllocatedAngles', 'hasAllocatedAngles',
'hasAllocatedWavelengths', 'hasAllocatedPupil', 'hasAllocatedPupil',
'hasAllocatedTimes']
):
pa1 = obj.shoot(nphotons, rng)
pa2 = obj.shoot(nphotons, rng)
assert not getattr(pa1, checkFn)()
assert not getattr(pa1, checkFn)()
data = np.linspace(-0.1, 0.1, nphotons)
setattr(pa1, attr, data)
assert getattr(pa1, checkFn)()
assert not getattr(pa2, checkFn)()
pa1.convolve(pa2)
assert getattr(pa1, checkFn)()
assert not getattr(pa2, checkFn)()
np.testing.assert_array_equal(getattr(pa1, attr), data)
pa2.convolve(pa1)
assert getattr(pa1, checkFn)()
assert getattr(pa2, checkFn)()
np.testing.assert_array_equal(getattr(pa2, attr), data)
# both have data now...
pa1.convolve(pa2)
np.testing.assert_array_equal(getattr(pa1, attr), data)
np.testing.assert_array_equal(getattr(pa2, attr), data)
# If the second one has different data, the first takes precedence.
setattr(pa2, attr, data * 2)
pa1.convolve(pa2)
np.testing.assert_array_equal(getattr(pa1, attr), data)
np.testing.assert_array_equal(getattr(pa2, attr), 2*data)
@timer
def test_wavelength_sampler():
nphotons = 1000
obj = galsim.Exponential(flux=1.7, scale_radius=2.3)
rng = galsim.UniformDeviate(1234)
photon_array = obj.shoot(nphotons, rng)
sed = galsim.SED(os.path.join(sedpath, 'CWW_E_ext.sed'), 'A', 'flambda').thin()
bandpass = galsim.Bandpass(os.path.join(bppath, 'LSST_r.dat'), 'nm').thin()
sampler = galsim.WavelengthSampler(sed, bandpass)
sampler.applyTo(photon_array, rng=rng)
# Note: the underlying functionality of the sampleWavelengths function is tested
# in test_sed.py. So here we are really just testing that the wrapper class is
# properly writing to the photon_array.wavelengths array.
assert photon_array.hasAllocatedWavelengths()
assert not photon_array.hasAllocatedAngles()
check_pickle(sampler)
print('mean wavelength = ',np.mean(photon_array.wavelength))
print('min wavelength = ',np.min(photon_array.wavelength))
print('max wavelength = ',np.max(photon_array.wavelength))
assert np.min(photon_array.wavelength) > bandpass.blue_limit
assert np.max(photon_array.wavelength) < bandpass.red_limit
# This is a regression test based on the value at commit 134a119
np.testing.assert_allclose(np.mean(photon_array.wavelength), 622.755128, rtol=1.e-4)
# If we use a flat SED (in photons/nm), then the mean sampled wavelength should very closely
# match the bandpass effective wavelength.
photon_array2 = galsim.PhotonArray(100000)
sed2 = galsim.SED('1', 'nm', 'fphotons')
sampler2 = galsim.WavelengthSampler(sed2, bandpass)
sampler2.applyTo(photon_array2, rng=rng)
np.testing.assert_allclose(np.mean(photon_array2.wavelength),
bandpass.effective_wavelength,
rtol=0, atol=0.2, # 2 Angstrom accuracy is pretty good
err_msg="Mean sampled wavelength not close to effective_wavelength")
# If the photon array already has wavelengths set, then it proceeds, but gives a warning.
with assert_warns(galsim.GalSimWarning):
sampler2.applyTo(photon_array2, rng=rng)
np.testing.assert_allclose(np.mean(photon_array2.wavelength),
bandpass.effective_wavelength, rtol=0, atol=0.2)
# Test that using this as a surface op works properly.
# First do the shooting and clipping manually.
im1 = galsim.Image(64,64,scale=1)
im1.setCenter(0,0)
photon_array.flux[photon_array.wavelength < 600] = 0.
photon_array.addTo(im1)
# Make a dummy surface op that clips any photons with lambda < 600
class Clip600:
def applyTo(self, photon_array, local_wcs=None, rng=None):
photon_array.flux[photon_array.wavelength < 600] = 0.
# Use (a new) sampler and clip600 as photon_ops in drawImage
im2 = galsim.Image(64,64,scale=1)
im2.setCenter(0,0)
clip600 = Clip600()
rng2 = galsim.BaseDeviate(1234)
sampler2 = galsim.WavelengthSampler(sed, bandpass)
obj.drawImage(im2, method='phot', n_photons=nphotons, use_true_center=False,
photon_ops=[sampler2,clip600], rng=rng2, save_photons=True)
print('sum = ',im1.array.sum(),im2.array.sum())
np.testing.assert_array_equal(im1.array, im2.array)
# Equivalent version just getting photons back
rng2.seed(1234)
photons = obj.makePhot(n_photons=nphotons, photon_ops=[sampler2,clip600], rng=rng2)
print('phot.x = ',photons.x)
print('im2.photons.x = ',im2.photons.x)
assert photons == im2.photons
# Base class is invalid to try to use.
op = galsim.PhotonOp()
with assert_raises(NotImplementedError):
op.applyTo(photon_array)
@timer
def test_photon_angles():
"""Test the photon_array function
"""
# Make a photon array
seed = 12345
ud = galsim.UniformDeviate(seed)
gal = galsim.Sersic(n=4, half_light_radius=1)
photon_array = gal.shoot(100000, ud)
# Add the directions (N.B. using the same seed as for generating the photon array
# above. The fact that it is the same does not matter here; the testing routine
# only needs to have a definite seed value so the consistency of the results with
# expectations can be evaluated precisely
fratio = 1.2
obscuration = 0.2
# rng can be None, an existing BaseDeviate, or an integer
for rng in [ None, ud, 12345 ]:
assigner = galsim.FRatioAngles(fratio, obscuration)
assigner.applyTo(photon_array, rng=rng)
check_pickle(assigner)
dxdz = photon_array.dxdz
dydz = photon_array.dydz
phi = np.arctan2(dydz,dxdz)
tantheta = np.sqrt(np.square(dxdz) + np.square(dydz))
sintheta = np.sin(np.arctan(tantheta))
# Check that the values are within the ranges expected
# (The test on phi really can't fail, because it is only testing the range of the
# arctan2 function.)
np.testing.assert_array_less(-phi, np.pi, "Azimuth angles outside possible range")
np.testing.assert_array_less(phi, np.pi, "Azimuth angles outside possible range")
fov_angle = np.arctan(0.5 / fratio)
obscuration_angle = obscuration * fov_angle
np.testing.assert_array_less(-sintheta, -np.sin(obscuration_angle),
"Inclination angles outside possible range")
np.testing.assert_array_less(sintheta, np.sin(fov_angle),
"Inclination angles outside possible range")
# Compare these slopes with the expected distributions (uniform in azimuth
# over all azimiths and uniform in sin(inclination) over the range of
# allowed inclinations
# Only test this for the last one, so we make sure we have a deterministic result.
# (The above tests should be reliable even for the default rng.)
phi_histo, phi_bins = np.histogram(phi, bins=100)
sintheta_histo, sintheta_bins = np.histogram(sintheta, bins=100)
phi_ref = float(np.sum(phi_histo))/phi_histo.size
sintheta_ref = float(np.sum(sintheta_histo))/sintheta_histo.size
chisqr_phi = np.sum(np.square(phi_histo - phi_ref)/phi_ref) / phi_histo.size
chisqr_sintheta = np.sum(np.square(sintheta_histo - sintheta_ref) /
sintheta_ref) / sintheta_histo.size
print('chisqr_phi = ',chisqr_phi)
print('chisqr_sintheta = ',chisqr_sintheta)
assert 0.9 < chisqr_phi < 1.1, "Distribution in azimuth is not nearly uniform"
assert 0.9 < chisqr_sintheta < 1.1, "Distribution in sin(inclination) is not nearly uniform"
# Try some invalid inputs
assert_raises(ValueError, galsim.FRatioAngles, fratio=-0.3)
assert_raises(ValueError, galsim.FRatioAngles, fratio=1.2, obscuration=-0.3)
assert_raises(ValueError, galsim.FRatioAngles, fratio=1.2, obscuration=1.0)
assert_raises(ValueError, galsim.FRatioAngles, fratio=1.2, obscuration=1.9)
@timer
def test_photon_io():
"""Test the ability to read and write photons to a file
"""
nphotons = 1000
obj = galsim.Exponential(flux=1.7, scale_radius=2.3)
rng = galsim.UniformDeviate(1234)
image = obj.drawImage(method='phot', n_photons=nphotons, save_photons=True, rng=rng)
photons = image.photons
assert photons.size() == len(photons) == nphotons
with assert_raises(galsim.GalSimIncompatibleValuesError):
obj.drawImage(method='phot', n_photons=nphotons, save_photons=True, maxN=1.e5)
file_name = 'output/photons1.dat'
photons.write(file_name)
photons1 = galsim.PhotonArray.read(file_name)
assert photons1.size() == nphotons
assert not photons1.hasAllocatedWavelengths()
assert not photons1.hasAllocatedAngles()
assert not photons1.hasAllocatedPupil()
assert not photons1.hasAllocatedTimes()
np.testing.assert_array_equal(photons1.x, photons.x)
np.testing.assert_array_equal(photons1.y, photons.y)
np.testing.assert_array_equal(photons1.flux, photons.flux)
sed = galsim.SED(os.path.join(sedpath, 'CWW_E_ext.sed'), 'nm', 'flambda').thin()
bandpass = galsim.Bandpass(os.path.join(bppath, 'LSST_r.dat'), 'nm').thin()
wave_sampler = galsim.WavelengthSampler(sed, bandpass)
angle_sampler = galsim.FRatioAngles(1.3, 0.3)
ops = [ wave_sampler, angle_sampler ]
for op in ops:
op.applyTo(photons, rng=rng)
# Directly inject some pupil coordinates and time stamps
photons.pupil_u = np.linspace(0, 1, nphotons)
photons.pupil_v = np.linspace(1, 2, nphotons)
photons.time = np.linspace(0, 30, nphotons)
file_name = 'output/photons2.dat'
photons.write(file_name)
photons2 = galsim.PhotonArray.read(file_name)
assert photons2.size() == nphotons
assert photons2.hasAllocatedWavelengths()
assert photons2.hasAllocatedAngles()
assert photons2.hasAllocatedPupil()
assert photons2.hasAllocatedTimes()
np.testing.assert_array_equal(photons2.x, photons.x)
np.testing.assert_array_equal(photons2.y, photons.y)
np.testing.assert_array_equal(photons2.flux, photons.flux)
np.testing.assert_array_equal(photons2.dxdz, photons.dxdz)
np.testing.assert_array_equal(photons2.dydz, photons.dydz)
np.testing.assert_array_equal(photons2.wavelength, photons.wavelength)
np.testing.assert_array_equal(photons.pupil_u, photons.pupil_u)
np.testing.assert_array_equal(photons.pupil_v, photons.pupil_v)
np.testing.assert_array_equal(photons.time, photons.time)
@timer
def test_dcr():
"""Test the dcr surface op
"""
# This tests that implementing DCR with the surface op is equivalent to using
# ChromaticAtmosphere.
# We use fairly extreme choices for the parameters to make the comparison easier, so
# we can still get good discrimination of any errors with only 10^6 photons.
zenith_angle = 45 * galsim.degrees # Larger angle has larger DCR.
parallactic_angle = 129 * galsim.degrees # Something random, not near 0 or 180
pixel_scale = 0.03 # Small pixel scale means shifts are many pixels, rather than a fraction.
alpha = -1.2 # The normal alpha is -0.2, so this is exaggerates the effect.
bandpass = galsim.Bandpass('LSST_r.dat', 'nm')
base_wavelength = bandpass.effective_wavelength
base_wavelength += 500 # This exaggerates the effects fairly substantially.
sed = galsim.SED('CWW_E_ext.sed', wave_type='ang', flux_type='flambda')
flux = 1.e6
fwhm = 0.3
base_PSF = galsim.Kolmogorov(fwhm=fwhm)
# Use ChromaticAtmosphere
# Note, somewhat gratuitous check that ImageI works with dtype=int in config below.
im1 = galsim.ImageI(50, 50, scale=pixel_scale)
star = galsim.DeltaFunction() * sed
star = star.withFlux(flux, bandpass=bandpass)
chrom_PSF = galsim.ChromaticAtmosphere(base_PSF,
base_wavelength=base_wavelength,
zenith_angle=zenith_angle,
parallactic_angle=parallactic_angle,
alpha=alpha)
chrom = galsim.Convolve(star, chrom_PSF)
chrom.drawImage(bandpass, image=im1)
# Repeat with config
config = {
'psf': { 'type': 'ChromaticAtmosphere',
'base_profile': { 'type': 'Kolmogorov', 'fwhm': fwhm },
'base_wavelength': base_wavelength,
'zenith_angle': zenith_angle,
'parallactic_angle': parallactic_angle,
'alpha': alpha
},
'gal': { 'type': 'DeltaFunction', 'flux': flux, 'sed': sed },
'image': { 'xsize': 50, 'ysize': 50, 'pixel_scale': pixel_scale,
'bandpass': bandpass,
'random_seed': 31415,
'dtype': int,
},
}
im1c = galsim.config.BuildImage(config)
assert im1c == im1
# Use PhotonDCR
im2 = galsim.ImageI(50, 50, scale=pixel_scale)
dcr = galsim.PhotonDCR(base_wavelength=base_wavelength,
zenith_angle=zenith_angle,
parallactic_angle=parallactic_angle,
alpha=alpha)
achrom = base_PSF.withFlux(flux)
# Because we'll be comparing to config version, get the rng the way it will do it.
rng = galsim.BaseDeviate(galsim.BaseDeviate(31415).raw()+1)
wave_sampler = galsim.WavelengthSampler(sed, bandpass)
photon_ops = [wave_sampler, dcr]
achrom.drawImage(image=im2, method='phot', rng=rng, photon_ops=photon_ops)
check_pickle(dcr)
# Repeat with config
config = {
'psf': { 'type': 'Kolmogorov', 'fwhm': fwhm },
'gal': { 'type': 'DeltaFunction', 'flux': flux },
'image': { 'xsize': 50, 'ysize': 50, 'pixel_scale': pixel_scale,
'bandpass': bandpass,
'random_seed': 31415,
'dtype': 'np.int32',
},
'stamp': {
'draw_method': 'phot',
'photon_ops': [
{ 'type': 'WavelengthSampler',
'sed': sed },
{ 'type': 'PhotonDCR',
'base_wavelength': base_wavelength,
'zenith_angle': zenith_angle,
'parallactic_angle': parallactic_angle,
'alpha': alpha
}
],
},
}
im2c = galsim.config.BuildImage(config)
assert im2c == im2
# Make sure it's ok if sky_pos is set. (This used to be a bug.)
config['sky_pos'] = galsim.CelestialCoord(15 * galsim.degrees, -25 * galsim.degrees)
galsim.config.RemoveCurrent(config)
im2d = galsim.config.BuildImage(config)
assert im2d == im2
# Should work with fft, but not quite match (because of inexact photon locations).
im3 = galsim.ImageF(50, 50, scale=pixel_scale)
achrom.drawImage(image=im3, method='fft', rng=rng, photon_ops=photon_ops)
printval(im3, im2, show=False)
np.testing.assert_allclose(im3.array, im2.array, atol=0.1 * np.max(im2.array),
err_msg="PhotonDCR on fft image didn't match phot image")
# Moments come out less than 1% different.
res2 = im2.FindAdaptiveMom()
res3 = im3.FindAdaptiveMom()
np.testing.assert_allclose(res3.moments_amp, res2.moments_amp, rtol=1.e-2)
np.testing.assert_allclose(res3.moments_sigma, res2.moments_sigma, rtol=1.e-2)
np.testing.assert_allclose(res3.observed_shape.e1, res2.observed_shape.e1, atol=1.e-2)
np.testing.assert_allclose(res3.observed_shape.e2, res2.observed_shape.e2, atol=1.e-2)
np.testing.assert_allclose(res3.moments_centroid.x, res2.moments_centroid.x, rtol=1.e-2)
np.testing.assert_allclose(res3.moments_centroid.y, res2.moments_centroid.y, rtol=1.e-2)
# Repeat with maxN < flux
# Note: Because of the different way this generates the random positions, it's not identical
# to the above run without maxN. Both runs are equally valid realizations of photon
# positions corresponding to the FFT image. But not the same realization.
achrom.drawImage(image=im3, method='auto', rng=rng, photon_ops=photon_ops, maxN=10**4)
printval(im3, im2, show=False)
np.testing.assert_allclose(im3.array, im2.array, atol=0.2 * np.max(im2.array),
err_msg="PhotonDCR on fft image with maxN didn't match phot image")
res3 = im3.FindAdaptiveMom()
np.testing.assert_allclose(res3.moments_amp, res2.moments_amp, rtol=1.e-2)
np.testing.assert_allclose(res3.moments_sigma, res2.moments_sigma, rtol=1.e-2)
np.testing.assert_allclose(res3.observed_shape.e1, res2.observed_shape.e1, atol=1.e-2)
np.testing.assert_allclose(res3.observed_shape.e2, res2.observed_shape.e2, atol=1.e-2)
np.testing.assert_allclose(res3.moments_centroid.x, res2.moments_centroid.x, rtol=1.e-2)
np.testing.assert_allclose(res3.moments_centroid.y, res2.moments_centroid.y, rtol=1.e-2)
# Compare ChromaticAtmosphere image with PhotonDCR image.
printval(im2, im1, show=False)
# tolerace for photon shooting is ~sqrt(flux) = 1.e3
np.testing.assert_allclose(im2.array, im1.array, atol=1.e3,
err_msg="PhotonDCR didn't match ChromaticAtmosphere")
# Use ChromaticAtmosphere in photon_ops
im3 = galsim.ImageI(50, 50, scale=pixel_scale)
photon_ops = [chrom_PSF]
star.drawImage(bandpass, image=im3, method='phot', rng=rng, photon_ops=photon_ops)
printval(im3, im1, show=False)
np.testing.assert_allclose(im3.array, im1.array, atol=1.e3,
err_msg="ChromaticAtmosphere in photon_ops didn't match")
# Repeat with thinned bandpass and SED to check that thin still works well.
im3 = galsim.ImageI(50, 50, scale=pixel_scale)
thin = 0.1 # Even higher also works. But this is probably enough.
thin_bandpass = bandpass.thin(thin)
thin_sed = sed.thin(thin)
print('len bp = %d => %d'%(len(bandpass.wave_list), len(thin_bandpass.wave_list)))
print('len sed = %d => %d'%(len(sed.wave_list), len(thin_sed.wave_list)))
wave_sampler = galsim.WavelengthSampler(thin_sed, thin_bandpass)
photon_ops = [wave_sampler, dcr]
achrom.drawImage(image=im3, method='phot', rng=rng, photon_ops=photon_ops)
printval(im3, im1, show=False)
np.testing.assert_allclose(im3.array, im1.array, atol=1.e3,
err_msg="thinning factor %f led to 1.e-4 level inaccuracy"%thin)
# Check scale_unit
im4 = galsim.ImageI(50, 50, scale=pixel_scale/60)
wave_sampler = galsim.WavelengthSampler(sed, bandpass)
dcr = galsim.PhotonDCR(base_wavelength=base_wavelength,
zenith_angle=zenith_angle,
parallactic_angle=parallactic_angle,
scale_unit='arcmin',
alpha=alpha)
photon_ops = [wave_sampler, dcr]
rng = galsim.BaseDeviate(galsim.BaseDeviate(31415).raw()+1)
achrom.dilate(1./60).drawImage(image=im4, method='phot', rng=rng, photon_ops=photon_ops)
printval(im4, im1, show=False)
np.testing.assert_allclose(im4.array, im1.array, atol=1.e3,
err_msg="PhotonDCR with scale_unit=arcmin, didn't match")
galsim.config.RemoveCurrent(config)
del config['stamp']['photon_ops'][1]['_get']
config['stamp']['photon_ops'][1]['scale_unit'] = 'arcmin'
config['image']['pixel_scale'] = pixel_scale/60
config['psf']['fwhm'] = fwhm/60
im4c = galsim.config.BuildImage(config)
assert im4c == im4
# Check some other valid options
# alpha = 0 means don't do any size scaling.
# obj_coord, HA and latitude are another option for setting the angles
# pressure, temp, and water pressure are settable.
# Also use a non-trivial WCS.
wcs = galsim.FitsWCS('des_data/DECam_00154912_12_header.fits')
image = galsim.Image(50, 50, wcs=wcs)
bandpass = galsim.Bandpass('LSST_r.dat', wave_type='nm').thin(0.1)
base_wavelength = bandpass.effective_wavelength
lsst_lat = galsim.Angle.from_dms('-30:14:23.76')
lsst_long = galsim.Angle.from_dms('-70:44:34.67')
local_sidereal_time = 3.14 * galsim.hours # Not pi. This is the time for this observation.
im5 = galsim.ImageI(50, 50, wcs=wcs)
obj_coord = wcs.toWorld(im5.true_center)
base_PSF = galsim.Kolmogorov(fwhm=0.9)
achrom = base_PSF.withFlux(flux)
dcr = galsim.PhotonDCR(base_wavelength=base_wavelength,
obj_coord=obj_coord,
HA=local_sidereal_time-obj_coord.ra,
latitude=lsst_lat,
pressure=72, # default is 69.328
temperature=290, # default is 293.15
H2O_pressure=0.9) # default is 1.067
#alpha=0) # default is 0, so don't need to set it.
wave_sampler = galsim.WavelengthSampler(sed, bandpass)
photon_ops = [wave_sampler, dcr]
rng = galsim.BaseDeviate(galsim.BaseDeviate(31415).raw()+1)
achrom.drawImage(image=im5, method='phot', rng=rng, photon_ops=photon_ops)
check_pickle(dcr)
galsim.config.RemoveCurrent(config)
config['psf']['fwhm'] = 0.9
config['image'] = {
'xsize': 50,
'ysize': 50,
'wcs': { 'type': 'Fits', 'file_name': 'des_data/DECam_00154912_12_header.fits' },
'bandpass': bandpass,
'random_seed': 31415,
'dtype': 'np.int32',
'world_pos': obj_coord,
}
config['stamp']['photon_ops'][1] = {
'type': 'PhotonDCR',
'base_wavelength': base_wavelength,
'HA': local_sidereal_time-obj_coord.ra,
'latitude': '-30:14:23.76 deg',
'pressure': 72*u.kPa,
'temperature': '290 K',
'H2O_pressure': '$900*u.Pa',
}
im5c = galsim.config.BuildImage(config)
assert im5c == im5
# Also one using zenith_coord = (lst, lat)
config['stamp']['photon_ops'][1] = {
'type': 'PhotonDCR',
'base_wavelength': base_wavelength,
'zenith_coord': {
'type': 'RADec',
'ra': local_sidereal_time,
'dec': lsst_lat,
},
'pressure': 72,
'temperature': 290,
'H2O_pressure': 0.9,
}
im5d = galsim.config.BuildImage(config)
assert im5d == im5
im6 = galsim.ImageI(50, 50, wcs=wcs)
star = galsim.DeltaFunction() * sed
star = star.withFlux(flux, bandpass=bandpass)
chrom_PSF = galsim.ChromaticAtmosphere(base_PSF,
base_wavelength=bandpass.effective_wavelength,
obj_coord=obj_coord,
HA=local_sidereal_time-obj_coord.ra,
latitude=lsst_lat,
pressure=72,
temperature=290,
H2O_pressure=0.9,
alpha=0)
chrom = galsim.Convolve(star, chrom_PSF)
chrom.drawImage(bandpass, image=im6)
printval(im5, im6, show=False)
np.testing.assert_allclose(im5.array, im6.array, atol=1.e3,
err_msg="PhotonDCR with alpha=0 didn't match")
# Use ChromaticAtmosphere in photon_ops
im7 = galsim.ImageI(50, 50, wcs=wcs)
photon_ops = [chrom_PSF]
star.drawImage(bandpass, image=im7, method='phot', rng=rng, photon_ops=photon_ops)
printval(im7, im6, show=False)
np.testing.assert_allclose(im7.array, im6.array, atol=1.e3,
err_msg="ChromaticAtmosphere in photon_ops didn't match")
# ChromaticAtmosphere in photon_ops is almost trivially equal to base_psf and dcr in photon_ops.
im8 = galsim.ImageI(50, 50, wcs=wcs)
photon_ops = [base_PSF, dcr]
star.drawImage(bandpass, image=im8, method='phot', rng=rng, photon_ops=photon_ops)
printval(im8, im6, show=False)
np.testing.assert_allclose(im8.array, im6.array, atol=1.e3,
err_msg="base_psf + dcr in photon_ops didn't match")
# Including the wavelength sampler with chromatic drawing is not necessary, but is allowed.
# (Mostly in case someone wants to do something a little different w.r.t. wavelength sampling.
photon_ops = [wave_sampler, base_PSF, dcr]
star.drawImage(bandpass, image=im8, method='phot', rng=rng, photon_ops=photon_ops)
printval(im8, im6, show=False)
np.testing.assert_allclose(im8.array, im6.array, atol=1.e3,
err_msg="wave_sampler,base_psf,dcr in photon_ops didn't match")
# Also check invalid parameters
zenith_coord = galsim.CelestialCoord(13.54 * galsim.hours, lsst_lat)
assert_raises(TypeError, galsim.PhotonDCR,
zenith_angle=zenith_angle,
parallactic_angle=parallactic_angle) # base_wavelength is required
assert_raises(TypeError, galsim.PhotonDCR,
base_wavelength=500,
parallactic_angle=parallactic_angle) # zenith_angle (somehow) is required
assert_raises(TypeError, galsim.PhotonDCR, 500,
zenith_angle=34.4,
parallactic_angle=parallactic_angle) # zenith_angle must be Angle
assert_raises(TypeError, galsim.PhotonDCR, 500,
zenith_angle=zenith_angle,
parallactic_angle=34.5) # parallactic_angle must be Angle
assert_raises(TypeError, galsim.PhotonDCR, 500,
obj_coord=obj_coord,
latitude=lsst_lat) # Missing HA
assert_raises(TypeError, galsim.PhotonDCR, 500,
obj_coord=obj_coord,
HA=local_sidereal_time-obj_coord.ra) # Missing latitude
assert_raises(TypeError, galsim.PhotonDCR, 500,
obj_coord=obj_coord) # Need either zenith_coord, or (HA,lat)
assert_raises(TypeError, galsim.PhotonDCR, 500,
obj_coord=obj_coord,
zenith_coord=zenith_coord,
HA=local_sidereal_time-obj_coord.ra) # Can't have both HA and zenith_coord
assert_raises(TypeError, galsim.PhotonDCR, 500,
obj_coord=obj_coord,
zenith_coord=zenith_coord,
latitude=lsst_lat) # Can't have both lat and zenith_coord
assert_raises(TypeError, galsim.PhotonDCR, 500,
zenith_angle=zenith_angle,
parallactic_angle=parallactic_angle,
H20_pressure=1.) # invalid (misspelled)
assert_raises(ValueError, galsim.PhotonDCR, 500,
zenith_angle=zenith_angle,
parallactic_angle=parallactic_angle,
scale_unit='inches') # invalid scale_unit
photons = galsim.PhotonArray(2, flux=1)
assert_raises(galsim.GalSimError, dcr.applyTo, photons) # Requires wavelengths to be set
assert_raises(galsim.GalSimError, chrom_PSF.applyTo, photons) # Requires wavelengths to be set
photons = galsim.PhotonArray(2, flux=1, wavelength=500)
assert_raises(TypeError, dcr.applyTo, photons) # Requires local_wcs
# Invalid to use dcr without some way of setting wavelengths.
assert_raises(galsim.GalSimError, achrom.drawImage, im2, method='phot', photon_ops=[dcr])
@unittest.skipIf(no_astroplan, 'Unable to import astroplan')
@timer
def test_dcr_angles():
"""Check the DCR angle calculations by comparing to astroplan's calculations of the same.
"""
# Note: test_chromatic.py and test_sed.py both also test aspects of the dcr module, so
# this particular test could belong in either of them too. But I (MJ) put it here, since
# I wrote it in conjunction with the tests of PhotonDCR to try to make sure that code
# is working properly.
import astropy.time
# Set up an observation date, time, location, coordinate
# These are arbitrary, so ripped from astroplan's docs
# https://media.readthedocs.org/pdf/astroplan/latest/astroplan.pdf
subaru = astroplan.Observer.at_site('subaru')
time = astropy.time.Time('2015-06-16 12:00:00')
# Stars that are visible from the north in summer time.