Skip to content

Commit 52b9836

Browse files
psf validation (with 4th-order moments) enabled: merge star cat, convert pix
1 parent 3b2448e commit 52b9836

File tree

4 files changed

+102
-21
lines changed

4 files changed

+102
-21
lines changed

docs/source/pipeline_canfar.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,17 @@ create_final_cat.py -m final_cat_$patch.hdf5 -i . -p $patch/cfis/final_cat.param
122122

123123
# Star catalogue
124124
combine_runs.bash -p $psf -c psf
125+
126+
## The following are not necessary, only to get merged catalogue with non-converted quantities
125127
shapepipe_run -c $SP_CONFIG/config_Ms_$psf.ini
126128
shapepipe_run -c $SP_CONFIG/config_Pl_$psf.ini
127129

128130
# With fourth-order moment, additional psf interpol validation runs
129131
combine_runs.bash -p $psf -c psf_val
130132

133+
## The following are not necessary, only to get merged catalogue with non-converted quantities
134+
shapepipe_run -c $SP_CONFIG/config_Msval_$psf.ini
135+
131136

132137
# Convert star cat to WCS
133138
## Convert all input validation psf files and create directories par patch

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ include-package-data = true
5858
"scripts/sh/curl_canfar_local.sh",
5959
"scripts/sh/functions.sh",
6060
"scripts/sh/stats_jobs_canfar.sh",
61-
"scripts/sh/combine_runs.sh",
61+
"scripts/sh/combine_runs.bash",
6262
"scripts/python/update_runs_log_file.py",
6363
"scripts/python/summary_run.py",
6464
"scripts/python/summary_params_pre_v2.py",
6565
"scripts/python/link_to_exp_for_tile.py",
66+
"scripts/python/convert_psf_pix2world.py",
6667
]

scripts/python/convert_psf_pix2world.py

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,94 @@
1616
from 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

53102
class 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,

src/shapepipe/modules/merge_starcat_package/merge_starcat.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ def process(self):
554554
555555
"""
556556
x, y, ra, dec = [], [], [], []
557-
g1_psf, g2_psf, size_psf = [], [], []
558-
g1, g2, size = [], [], []
557+
g1_psf, g2_psf, size_psf, m41_psf, m42_psf = [], [], [], [], []
558+
g1, g2, size, m41, m42 = [], [], [], [], []
559559
flag_psf, flag_star = [], []
560560
mag, snr, psfex_acc = [], [], []
561561
ccd_nb = []
@@ -584,8 +584,12 @@ def process(self):
584584
g1_psf += list(data_j["E1_PSF_HSM"])
585585
g2_psf += list(data_j["E2_PSF_HSM"])
586586
size_psf += list(data_j["SIGMA_PSF_HSM"] ** 2)
587+
m41_psf += list(data_j["M_4_PSF_1"])
588+
m42_psf += list(data_j["M_4_PSF_1"])
587589
g1 += list(data_j["E1_STAR_HSM"])
588590
g2 += list(data_j["E2_STAR_HSM"])
591+
m41 += list(data_j["M_4_STAR_1"])
592+
m42 += list(data_j["M_4_STAR_2"])
589593
size += list(data_j["SIGMA_STAR_HSM"] ** 2)
590594

591595
# flags
@@ -633,9 +637,13 @@ def process(self):
633637
"E1_PSF_HSM": g1_psf,
634638
"E2_PSF_HSM": g2_psf,
635639
"SIGMA_PSF_HSM": np.sqrt(size_psf),
640+
"M_4_PSF_1": m41_psf,
641+
"M_4_PSF_2": m42_psf,
636642
"E1_STAR_HSM": g1,
637643
"E2_STAR_HSM": g2,
638644
"SIGMA_STAR_HSM": np.sqrt(size),
645+
"M_4_STAR_1": m41,
646+
"M_4_STAR_2": m42,
639647
"FLAG_PSF_HSM": flag_psf,
640648
"FLAG_STAR_HSM": flag_star,
641649
"MAG": mag,

0 commit comments

Comments
 (0)