1010import structlog
1111from astropy import units
1212from astropy .io import fits
13- from pydantic import BaseModel
13+ from pydantic import BaseModel , TypeAdapter
1414
1515from tilemaker .metadata .core import DataConfiguration
1616from tilemaker .metadata .definitions import Band , FITSLayerProvider , Layer , Map , MapGroup
17+ from tilemaker .metadata .sources import Source , SourceGroup
1718
1819# Define the hits unit
1920hits = units .def_unit ("hits" , units .dimensionless_unscaled )
@@ -25,14 +26,30 @@ def generate(
2526) -> DataConfiguration :
2627 map_files = [x for x in filenames if "fits" in x .name ]
2728 map_group = map_group_from_fits (map_files , force_auto_contrast = force_auto_contrast )
29+ source_files = [x for x in filenames if "json" in x .name ]
30+ source_groups = [source_group_from_json (x ) for x in source_files ]
2831
29- return DataConfiguration (map_groups = [map_group ], boxes = [], source_groups = [])
32+ return DataConfiguration (
33+ map_groups = [map_group ], boxes = [], source_groups = source_groups
34+ )
3035
3136
3237def filename_to_id (filename : str | Path ) -> str :
3338 return md5 (str (filename ).encode ("utf-8" )).hexdigest ()[:6 ]
3439
3540
41+ def source_group_from_json (source_file : Path ) -> SourceGroup :
42+ with open (source_file , "r" ) as handle :
43+ SourceListTypeAdapter = TypeAdapter (list [Source ])
44+ return SourceGroup (
45+ source_group_id = filename_to_id (source_file ),
46+ name = source_file .name ,
47+ description = "Source group read from file" ,
48+ sources = SourceListTypeAdapter .validate_json (handle .read ()),
49+ grant = None ,
50+ )
51+
52+
3653def map_group_from_fits (
3754 filenames : list [Path ],
3855 force_auto_contrast : bool = False ,
0 commit comments