Skip to content

Croissant

CroissantConf

Bases: BaseModel

Croissant mapping config model.

Parameters:

Name Type Description Default
croissant_version str

Croissant version.

'1.0'
conformsTo str

Croissant conformance link

'http://mlcommons.org/croissant/1.0'
Source code in stac_generator/plugins/mappings/croissant.py
78
79
80
81
82
83
84
85
86
87
class CroissantConf(BaseModel):
    """Croissant mapping config model."""

    croissant_version: str = Field(
        default="1.0",
        description="Croissant version.",
    )
    conformsTo: str = Field(
        default="http://mlcommons.org/croissant/1.0", description="Croissant conformance link"
    )

CroissantMapping

Bases: BaseMapping

Map metadata into Croissant.

Mapping Name**: croissant_mapping

Description:

Example Configuration:

.. code-block:: yaml

    - name: croissant_mapping
      conf:
        croissant_version: 1.0.0
Source code in stac_generator/plugins/mappings/croissant.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class CroissantMapping(BaseMapping):
    """Map metadata into Croissant.

    Mapping Name**: ``croissant_mapping``

    Description:


    Example Configuration:

        .. code-block:: yaml

            - name: croissant_mapping
              conf:
                croissant_version: 1.0.0

    """

    config_class = CroissantConf

    def datetime_field(self, date_str: str) -> str:
        dt = parser.parse(date_str)
        return dt.strftime("%Y-%m-%dT%H:%M:%SZ")

    def _get_parent_from_nested_dict(self, dct, path):
        """
        Return parent of nested path keys in dct and return it.
        """
        current_dct = dct
        for key in path[:-1]:
            current_dct = current_dct[key]

        return current_dct

    def croissant_record(self, body: dict) -> dict:
        fixed = {
            "croissant_version": self.conf.croissant_version,
            "id": body.pop("id"),
        }

        output = fixed | CROISSANT_FIXED_PROPERTIES | {"@type": "sc:Dataset"} | body

        # If mapper file is imported, then use mappings
        get_parent = self._get_parent_from_nested_dict
        if scm:
            print("Applying STAC-to-Croissant mappings.")
            delimiter = getattr(scm, "delimiter", "|")

            print("Overriding fields")
            for key, value in getattr(scm, "overrides", {}):
                key_path = key.split(delimiter)
                get_parent(output, key_path)[key_path[-1]] = value

            print("Mapping fields")
            for key, value in getattr(scm, "mapper", {}).items():
                key_path = key.split(delimiter)
                value_path = value.split(delimiter)
                parent = get_parent(output, key_path)
                content = copy.deepcopy(parent[key_path[-1]])
                del parent[key_path[-1]]

                current = output
                for v_key in value_path[:-1]:
                    current[v_key] = {}
                    current = current[v_key]

                current[value_path[-1]] = content

            print("Removing fields")
            for key in getattr(scm, "removals", []):
                key_path = key.split(delimiter)
                get_parent(output, key_path).pop(key_path[-1], None)

        print("Mapped STAC content to Croissant.")
        return output

    def run(
        self,
        body: dict,
        recipe: Recipe,
        **kwargs,
    ) -> dict:
        return self.croissant_record(body)