Coverage for examples/rerun.py: 0%

41 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-07 01:55 -0700

1from lsst.pex.config import Config, ConfigurableField 

2from lsst.pipe.base import CmdLineTask 

3from lsst.meas.deblender import SourceDeblendTask 

4import lsst.afw.table as afwTable 

5 

6''' 

7This script allows one to quickly load an image and run just the 

8deblender on it, without any of the calibration steps before that. 

9 

10That is, it reads the calexp and pre-deblending sources, then runs the 

11deblender and writes the outputs to self-contained FITS files. 

12''' 

13 

14 

15class MyConfig(Config): 

16 deblend = ConfigurableField(target=SourceDeblendTask, doc="Deblender") 

17 

18 

19class MyTask(CmdLineTask): 

20 _DefaultName = "my" 

21 ConfigClass = MyConfig 

22 

23 def _getConfigName(self): 

24 return None 

25 

26 def __init__(self, *args, **kwargs): 

27 super(MyTask, self).__init__(*args, **kwargs) 

28 # self.makeSubtask("deblend", schema=self.schema) 

29 

30 def run(self, dataRef): 

31 calexp = dataRef.get("calexp") 

32 psf = calexp.getPsf() 

33 sources = dataRef.get("src") 

34 

35 mapper = afwTable.SchemaMapper(sources.getSchema()) 

36 # map all the existing fields 

37 mapper.addMinimalSchema(sources.getSchema(), True) 

38 schema = mapper.getOutputSchema() 

39 

40 # It would be better for the schema-populating code to not be in 

41 # the SourceDeblendTask constructor! 

42 self.makeSubtask("deblend", schema=schema) 

43 

44 outsources = afwTable.SourceCatalog(schema) 

45 outsources.reserve(2 * len(sources)) 

46 outsources.extend(sources, mapper=mapper) 

47 sources = outsources 

48 print(len(sources), 'sources before deblending') 

49 

50 self.deblend.run(calexp, sources) 

51 print(len(sources), 'sources after deblending') 

52 

53 fn = 'deblended.fits' 

54 print('Writing sources...') 

55 sources.writeFits(fn) 

56 print('Wrote sources to', fn) 

57 

58 fn = 'calexp.fits' 

59 calexp.writeFits(fn) 

60 print('Wrote calexp to', fn) 

61 

62 fn = 'psf.fits' 

63 psf.writeFits(fn) 

64 print('Wrote PSF to', fn) 

65 

66 

67if __name__ == "__main__": 

68 MyTask.parseAndRun()