Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of obs_base. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22from lsst.daf.butler import Butler, DatasetType 

23from lsst.pipe.tasks.makeDiscreteSkyMap import MakeDiscreteSkyMapTask, MakeDiscreteSkyMapConfig 

24from lsst.obs.base.utils import getInstrument 

25 

26 

27def makeDiscreteSkyMap(repo, config_file, collections, instrument, 

28 out_collection='skymaps', skymap_id='discrete'): 

29 """Implements the command line interface `butler make-discrete-skymap` subcommand, 

30 should only be called by command line tools and unit test code that tests 

31 this function. 

32 

33 Constructs a skymap from calibrated exposure in the butler repository 

34 

35 Parameters 

36 ---------- 

37 repo : `str` 

38 URI to the location to read the repo. 

39 config_file : `str` or `None` 

40 Path to a config file that contains overrides to the skymap config. 

41 collections : `list` [`str`] 

42 An expression specifying the collections to be searched (in order) when 

43 reading datasets, and optionally dataset type restrictions on them. 

44 At least one collection must be specified. This is the collection 

45 with the calibrated exposures. 

46 instrument : `str` 

47 The name or fully-qualified class name of an instrument. 

48 out_collection : `str`, optional 

49 The name of the collection to save the skymap to. Default is 'skymaps'. 

50 skymap_id : `str`, optional 

51 The identifier of the skymap to save. Default is 'discrete'. 

52 """ 

53 butler = Butler(repo, collections=collections, writeable=True, run=out_collection) 

54 instr = getInstrument(instrument, butler.registry) 

55 config = MakeDiscreteSkyMapConfig() 

56 instr.applyConfigOverrides(MakeDiscreteSkyMapTask._DefaultName, config) 

57 

58 if config_file is not None: 

59 config.load(config_file) 

60 skymap_name = config.coaddName + "Coadd_skyMap" 

61 oldSkyMap = None 

62 if config.doAppend: 

63 if out_collection in collections: 

64 raise ValueError(f"Cannot overwrite dataset. If appending, specify an output " 

65 f"collection not in the input collections.") 

66 dataId = {'skymap': skymap_id} 

67 try: 

68 oldSkyMap = butler.get(skymap_name, collections=collections, dataId=dataId) 

69 except LookupError as e: 

70 msg = (f"Could not find seed skymap for {skymap_name} with dataId {dataId} " 

71 f"in collections {collections} but doAppend is {config.doAppend}. Aborting...") 

72 raise LookupError(msg, *e.args[1:]) 

73 

74 datasets = butler.registry.queryDatasets('calexp', collections=collections) 

75 wcs_md_tuple_list = [(butler.getDirect('calexp.metadata', ref), butler.getDirect('calexp.wcs', ref)) 

76 for ref in datasets] 

77 task = MakeDiscreteSkyMapTask(config=config) 

78 result = task.run(wcs_md_tuple_list, oldSkyMap) 

79 skymap_dataset_type = DatasetType(skymap_name, dimensions=["skymap", ], 

80 universe=butler.registry.dimensions, 

81 storageClass="SkyMap") 

82 butler.registry.registerDatasetType(skymap_dataset_type) 

83 if config.doAppend: 

84 # By definition if appending the dataset has already been registered 

85 result.skyMap.register(skymap_id, butler.registry) 

86 butler.put(result.skyMap, skymap_name, dataId={'skymap': skymap_id})