1616from cs_util import logging
1717
1818
19- def transform_shape (mom_list , jac ):
19+ def transform_shape (mom_list , jac , rescale = False ):
2020 """Transform Shape.
2121
2222 Transform shape (ellipticity and size) using a Jacobian.
2323
2424 Parameters
2525 ----------
2626 mom_list : list
27- input moment measurements; each list element contains
28- first and second ellipticity component and size
27+ input " moment" measurements; each list element contains
28+ first and second ellipticity components and size
2929 jac : galsim.JacobianWCS
3030 Jacobian transformation matrix information
31+ rescale : bool, optional
32+ rescale ellipticity to modulus if ``True``, to prevent
33+ values outside [-1; 1]; default is ``False``
3134
3235 Returns
3336 -------
3437 list
3538 transformed shape parameters, which are
36- first and second ellipticity component and size
39+ first and second ellipticity components and size
3740
3841 """
3942 scale , shear , theta , flip = jac .getDecomposition ()
4043
4144 sig_tmp = mom_list [2 ] * scale
42- shape = galsim .Shear (g1 = mom_list [0 ], g2 = mom_list [1 ])
45+
46+ if rescale :
47+ modulus = np .sqrt (mom_list [0 ] ** 2 + mom_list [1 ] ** 2 ) * 1.1
48+ else :
49+ modulus = 1
50+
51+ shape = galsim .Shear (g1 = mom_list [0 ]/ modulus , g2 = mom_list [1 ]/ modulus )
52+
53+ if flip :
54+ print (f"g1 sign change, flip = { flip } " )
55+ shape = galsim .Shear (g1 = - shape .g1 , g2 = shape .g2 )
56+
57+ shape = galsim .Shear (g = shape .g , beta = shape .beta + theta )
58+ shape = shear + shape
59+
60+ return shape .g1 * modulus , shape .g2 * modulus , sig_tmp
61+
62+
63+ def transform_shape_scale (mom_list , jac ):
64+ """Transform Shape.
65+
66+ Transform shape (ellipticity and size) using a Jacobian.
67+ Input "ellipticities" can be outside [-1, 1].
68+
69+ Parameters
70+ ----------
71+ mom_list : list
72+ input "moment" measurements; each list element contains
73+ first and second ellipticity components
74+ jac : galsim.JacobianWCS
75+ Jacobian transformation matrix information
76+
77+ Returns
78+ -------
79+ list
80+ transformed shape parameters, which are
81+ first and second "ellipticity" components
82+
83+ """
84+ _ , shear , theta , flip = jac .getDecomposition ()
85+
86+ # Scale to within [-1, 1]
87+ modulus = np .sqrt (mom_list [0 ] ** 2 + mom_list [1 ] ** 2 ) * 1.1
88+ shape = galsim .Shear (g1 = mom_list [0 ]/ modulus , g2 = mom_list [1 ]/ modulus )
89+
4390 if flip :
44- # The following output is not observed
45- print ("FLIP!" )
91+ print (f"g1 sign change, flip = { flip } " )
4692 shape = galsim .Shear (g1 = - shape .g1 , g2 = shape .g2 )
93+
4794 shape = galsim .Shear (g = shape .g , beta = shape .beta + theta )
4895 shape = shear + shape
4996
50- return shape .g1 , shape .g2 , sig_tmp
97+ # Scale back to original modulus
98+
99+ return shape .g1 * modulus , shape .g2 * modulus
51100
52101
53102class Loc2Glob (object ):
54103 r"""Change from local to global coordinates.
55104
56105 Class to pass from local coordinates to global coordinates under
57- CFIS (CFHT) MegaCam instrument. The geometrical informcation of the
106+ CFIS (CFHT) MegaCam instrument. The geometrical information of the
58107 instrument is encoded in this function.
59108
60109 Parameters
@@ -378,6 +427,7 @@ def params_default(self):
378427 self ._params = {
379428 "input_base_dir" : "." ,
380429 "output_base_dir" : "." ,
430+ "sub_dir_pattern" : "run_sp_combined_psf" ,
381431 "mode" : "merge" ,
382432 "patches" : "" ,
383433 "psf" : "psfex" ,
@@ -399,6 +449,7 @@ def params_default(self):
399449 + " <input_base_dir>/P<patch?>/output;"
400450 " default is {}"
401451 ),
452+ "sub_dir_pattern" : "run directory pattern, default is {}" ,
402453 "mode" : (
403454 "run mode, allowed are 'merge', 'test'; default is" + " '{}'"
404455 ),
@@ -415,10 +466,14 @@ def params_default(self):
415466 ("E1_PSF_HSM" , float ),
416467 ("E2_PSF_HSM" , float ),
417468 ("SIGMA_PSF_HSM" , float ),
469+ ("M_4_PSF_1" , float ),
470+ ("M_4_PSF_2" , float ),
418471 ("FLAG_PSF_HSM" , float ),
419472 ("E1_STAR_HSM" , float ),
420473 ("E2_STAR_HSM" , float ),
421474 ("SIGMA_STAR_HSM" , float ),
475+ ("M_4_STAR_1" , float ),
476+ ("M_4_STAR_2" , float ),
422477 ("FLAG_STAR_HSM" , float ),
423478 ("CCD_NB" , int ),
424479 ]
@@ -435,11 +490,8 @@ def update_params(self):
435490
436491 """
437492 if self ._params ["psf" ] == "psfex" :
438- #self._params["sub_dir_pattern"] = "run_sp_exp_202"
439- self ._params ["sub_dir_pattern" ] = "run_sp_combined_psf"
440493 self ._params ["sub_dir_psfint" ] = "psfex_interp_runner"
441494 elif self ._params ["psf" ] == "mccd" :
442- self ._params ["sub_dir_pattern" ] = "run_sp_exp_SxSePsf_202"
443495 self ._params ["sub_dir_psfint" ] = "mccd_fit_val_runner"
444496 self ._params ["sub_dir_setools" ] = "setools_runner/output/mask"
445497 else :
@@ -471,7 +523,7 @@ def run(self):
471523 print ("Running patch:" , patch )
472524
473525 patch_dir = f"{ self ._params ['input_base_dir' ]} /P{ patch } /output/"
474- subdirs = f"{ patch_dir } /{ self ._params ['sub_dir_pattern' ]} * "
526+ subdirs = f"{ patch_dir } /{ self ._params ['sub_dir_pattern' ]} "
475527 exp_run_dirs = glob .glob (subdirs )
476528 n_exp_runs = len (exp_run_dirs )
477529 print (
@@ -567,9 +619,13 @@ def transform_exposures(self, output_dir, patch, idx, exp_run_dir):
567619 new_e1_psf = np .zeros_like (psf_file [mod ])
568620 new_e2_psf = np .zeros_like (psf_file [mod ])
569621 new_sig_psf = np .zeros_like (psf_file [mod ])
622+ new_m41_psf = np .zeros_like (psf_file [mod ])
623+ new_m42_psf = np .zeros_like (psf_file [mod ])
570624 new_e1_star = np .zeros_like (psf_file [mod ])
571625 new_e2_star = np .zeros_like (psf_file [mod ])
572626 new_sig_star = np .zeros_like (psf_file [mod ])
627+ new_m41_star = np .zeros_like (psf_file [mod ])
628+ new_m42_star = np .zeros_like (psf_file [mod ])
573629
574630 if self ._params ["psf" ] == "psfex" :
575631 header = fits .Header .fromstring (
@@ -580,10 +636,6 @@ def transform_exposures(self, output_dir, patch, idx, exp_run_dir):
580636 k = 0
581637 for ind , obj in enumerate (psf_file ):
582638 try :
583- # jac = wcs.jacobian(world_pos=galsim.CelestialCoord(
584- # ra=obj["RA"]*galsim.degrees,
585- # dec=obj["DEC"]*galsim.degrees
586- # ))
587639 jac = wcs .jacobian (
588640 image_pos = galsim .PositionD (
589641 obj ["X" ],
@@ -592,6 +644,7 @@ def transform_exposures(self, output_dir, patch, idx, exp_run_dir):
592644 )
593645 except Exception :
594646 continue
647+
595648 g1_psf_tmp , g2_psf_tmp , sig_psf_tmp = transform_shape (
596649 [
597650 obj ["E1_PSF_HSM" ],
@@ -600,10 +653,14 @@ def transform_exposures(self, output_dir, patch, idx, exp_run_dir):
600653 ],
601654 jac ,
602655 )
603-
604656 new_e1_psf [ind ] = g1_psf_tmp
605657 new_e2_psf [ind ] = g2_psf_tmp
606658 new_sig_psf [ind ] = sig_psf_tmp
659+ new_m41_psf [ind ], new_m42_psf [ind ], _ = transform_shape (
660+ [obj ["M_4_PSF_1" ], obj ["M_4_PSF_2" ], 0 ],
661+ jac ,
662+ rescale = True ,
663+ )
607664
608665 g1_star_tmp , g2_star_tmp , sig_star_tmp = transform_shape (
609666 [
@@ -616,6 +673,12 @@ def transform_exposures(self, output_dir, patch, idx, exp_run_dir):
616673 new_e1_star [ind ] = g1_star_tmp
617674 new_e2_star [ind ] = g2_star_tmp
618675 new_sig_star [ind ] = sig_star_tmp
676+ new_m41_star [ind ], new_m42_star [ind ], _ = transform_shape (
677+ [obj ["M_4_STAR_1" ], obj ["M_4_STAR_2" ], 0 ],
678+ jac ,
679+ rescale = True ,
680+ )
681+
619682 k += 1
620683
621684 exp_cat = np .array (
@@ -631,10 +694,14 @@ def transform_exposures(self, output_dir, patch, idx, exp_run_dir):
631694 new_e1_psf ,
632695 new_e2_psf ,
633696 new_sig_psf ,
697+ new_m41_psf ,
698+ new_m42_psf ,
634699 psf_file ["FLAG_PSF_HSM" ],
635700 new_e1_star ,
636701 new_e2_star ,
637702 new_sig_star ,
703+ new_m41_star ,
704+ new_m42_star ,
638705 psf_file ["FLAG_STAR_HSM" ],
639706 np .ones_like (psf_file ["RA" ], dtype = int )
640707 * ccd_id ,
0 commit comments