Coverage for src/ufig/plugins/run_sextractor.py: 89%

57 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-12 19:08 +0000

1# Copyright (c) 2014 ETH Zurich, Institute of Astronomy, Lukas Gamper 

2# <lukas.gamper@usystems.ch> 

3""" 

4Created on Jun 3, 2014 

5@author: Lukas Gamper 

6 

7""" 

8 

9import contextlib 

10import os 

11import subprocess 

12import tempfile 

13 

14from ivy.plugin.base_plugin import BasePlugin 

15from pkg_resources import resource_filename 

16 

17import ufig 

18from ufig import io_util, sysmaps_util 

19from ufig.plugins.write_image import get_seeing_value 

20 

21 

22def get_sextractor_cmd(ctx): 

23 par = ctx.parameters 

24 

25 catalog_name_short = par.sextractor_catalog_name.rsplit(".", 1)[0] 

26 checkimages_names = [] 

27 checkimages = par.sextractor_checkimages 

28 for suffix in par.sextractor_checkimages_suffixes: 

29 checkimages_names += [catalog_name_short + suffix] 

30 if len(checkimages_names) == 0: 

31 checkimages_names = ["NONE"] 

32 checkimages = ["NONE"] 

33 

34 if not os.path.isabs(par.sextractor_config): 

35 par.sextractor_config = resource_filename( 

36 ufig.__name__, "res/sextractor/" + par.sextractor_config 

37 ) 

38 if not os.path.isabs(par.sextractor_params): 

39 par.sextractor_params = resource_filename( 

40 ufig.__name__, "res/sextractor/" + par.sextractor_params 

41 ) 

42 if not os.path.isabs(par.sextractor_filter): 

43 par.sextractor_filter = resource_filename( 

44 ufig.__name__, "res/sextractor/" + par.sextractor_filter 

45 ) 

46 if not os.path.isabs(par.sextractor_nnw): 

47 par.sextractor_nnw = resource_filename( 

48 ufig.__name__, "res/sextractor/" + par.sextractor_nnw 

49 ) 

50 

51 seeing = get_seeing_value(ctx) 

52 

53 cmd = [ 

54 par.sextractor_binary, 

55 par.image_name, 

56 "-c", 

57 par.sextractor_config, 

58 "-SEEING_FWHM", 

59 str(seeing * par.pixscale), # Seeing value in arcsec 

60 "-SATUR_KEY", 

61 "NONE", 

62 "-SATUR_LEVEL", 

63 str(par.saturation_level), 

64 "-MAG_ZEROPOINT", 

65 str(par.magzero), 

66 "-GAIN_KEY", 

67 "NONE", 

68 "-GAIN", 

69 str(par.n_exp * par.gain), 

70 "-PIXEL_SCALE", 

71 str(par.pixscale), 

72 "-STARNNW_NAME", 

73 par.sextractor_nnw, 

74 "-FILTER_NAME", 

75 par.sextractor_filter, 

76 "-PARAMETERS_NAME", 

77 par.sextractor_params, 

78 "-CATALOG_NAME", 

79 par.sextractor_catalog_name, 

80 "-CHECKIMAGE_TYPE", 

81 ",".join(checkimages), 

82 "-CHECKIMAGE_NAME", 

83 ",".join(checkimages_names), 

84 "-CATALOG_TYPE", 

85 "FITS_LDAC", 

86 "-VERBOSE_TYPE", 

87 "QUIET", 

88 ] 

89 

90 if par.weight_type != "NONE": 

91 path = io_util.get_abs_path(par.filepath_weight_fits, par.maps_remote_dir) 

92 cmd += [ 

93 "-WEIGHT_IMAGE", 

94 path, 

95 "-WEIGHT_TYPE", 

96 par.weight_type, 

97 "-WEIGHT_GAIN", 

98 par.weight_gain, 

99 ] 

100 

101 return cmd, checkimages_names 

102 

103 

104class Plugin(BasePlugin): 

105 """ 

106 Executes sextractor by spawning a subprocess. 

107 

108 :param image_name of image 

109 :param sextractor_binary: path to sextractor binary 

110 :param sextractor_config: c 

111 :param saturation_level: SATUR_LEVEL 

112 :param magzero_point: MAG_ZEROPOINT 

113 :param gain: GAIN 

114 :param sextractor_nnw: STARNNW_NAME 

115 :param sextractor_filter: FILTER_NAME 

116 :param sextractor_params: PARAMETERS_NAME 

117 :param sextractor_catalog_name: CATALOG_NAME 

118 :param sextractor_checkimages: CHECKIMAGE_TYPE 

119 :param sextractor_checkimages_suffixes: Suffixes to construct CHECKIMAGE_NAME from 

120 sextractor_catalog_name 

121 """ 

122 

123 def __call__(self): 

124 par = self.ctx.parameters 

125 

126 if self.ctx.parameters.sextractor_use_temp: 126 ↛ 133line 126 didn't jump to line 133 because the condition on line 126 was always true

127 catalog_name = os.path.join( 

128 tempfile.mkdtemp("sextractor"), 

129 par.sextractor_catalog_name.split("/")[-1], 

130 ) 

131 par.sextractor_catalog_name = catalog_name 

132 

133 if par.weight_type != "NONE": 133 ↛ 140line 133 didn't jump to line 140 because the condition on line 133 was never true

134 # par.filepath_weight_fits, weight_image_converted = \ 

135 # io_util.handle_sextractor_weight_image(par.weight_image, 

136 # par.tempdir_weight_fits, 

137 # overwrite=True, 

138 # root_path=par.maps_remote_dir) 

139 

140 overwrite_photo = True 

141 remove_photo = True 

142 ( 

143 filepath_photometry_weight, 

144 filepath_detection_weight, 

145 ) = sysmaps_util.write_temp_sextractor_weights( 

146 par, 

147 dirpath_temp=par.tempdir_weight_fits, 

148 overwrite_photo=overwrite_photo, 

149 ) 

150 par.filepath_weight_fits = filepath_photometry_weight 

151 

152 else: 

153 remove_photo = False 

154 

155 cmd, checkimages_names = get_sextractor_cmd(self.ctx) 

156 

157 if self.ctx.parameters.overwrite: 157 ↛ 165line 157 didn't jump to line 165 because the condition on line 157 was always true

158 with contextlib.suppress(OSError): 

159 os.remove(par.sextractor_catalog_name) 

160 

161 for name in checkimages_names: 

162 with contextlib.suppress(OSError): 

163 os.remove(name) 

164 

165 subprocess.check_call(cmd, stderr=subprocess.STDOUT) 

166 

167 if remove_photo and os.path.isfile(filepath_photometry_weight): 167 ↛ 168line 167 didn't jump to line 168 because the condition on line 167 was never true

168 os.remove(par.filepath_weight_fits) 

169 

170 def __str__(self): 

171 return "run sextractor"