forked from WorldWideTelescope/wwt-hips-list-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_wtml.py
executable file
·80 lines (60 loc) · 2.31 KB
/
create_wtml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /usr/bin/env python
#
# Copyright 2021 the .NET Foundation
# Licensed under the MIT License
"""
Combine ``datasets.yml`` and ``hierarchy.yml`` to create ``hips.wtml``.
"""
import yaml
from wwt_data_formats import write_xml_doc
from wwt_data_formats.enums import Bandpass, DataSetType, ProjectionType
from wwt_data_formats.folder import Folder
from wwt_data_formats.imageset import ImageSet
def realize_imageset(info: dict):
imgset = ImageSet()
if info["bandpass"] != "Uncategorized":
imgset.band_pass = Bandpass[info["bandpass"].upper()]
imgset.base_degrees_per_tile = 180
imgset.credits = info["credits"]
imgset.credits_url = info["credits_url"]
if info["type"] != "Uncategorized":
imgset.data_set_type = DataSetType[info["type"].upper()]
# Note that "description" is referenced in docs but not actually available
# in the app, so it's a useful place for us to stash some helpful metadata:
imgset.description = f"HiPS List ID: {info['_id']}"
imgset.file_type = info["file_type"]
imgset.mean_radius = 1
imgset.name = info["_name"]
imgset.projection = ProjectionType.HEALPIX
imgset.quad_tree_map = "0123"
imgset.reference_frame = "Sky"
imgset.thumbnail_url = info["thumbnail_url"]
imgset.tile_levels = int(info["tile_levels"])
imgset.width_factor = 1
imgset.url = info["url"]
return imgset
def realize_folder(info: dict, imagesets: dict):
f = Folder(name=info["_name"])
# TODO: FolderType
f.group = "Explorer"
f.searchable = True
for item in info["children"]:
if isinstance(item, dict):
f.children.append(realize_folder(item, imagesets))
else:
f.children.append(imagesets[item])
return f
def entrypoint():
imagesets_by_id = {}
with open("datasets.yml", "rt", encoding="utf-8") as f:
for info in yaml.load_all(f, yaml.SafeLoader):
imagesets_by_id[info["_id"]] = realize_imageset(info)
with open("hierarchy.yml", "rt", encoding="utf-8") as f:
hier = yaml.load(f, yaml.SafeLoader)
folder = realize_folder(hier, imagesets_by_id)
with open("hips.wtml", "wt", encoding="utf-8") as f:
write_xml_doc(
folder.to_xml(), indent=True, dest_stream=f, dest_wants_bytes=False
)
if __name__ == "__main__":
entrypoint()