Skip to content

Metobs models

smhi.models.metobs_model

MetobsBaseModel

Bases: BaseModel

Source code in smhi/models/metobs_model.py
class MetobsBaseModel(BaseModel):
    key: Optional[str] = None
    updated: Optional[datetime] = None
    title: str
    summary: str
    link: List[MetobsLink]

    @field_validator("from_", "to", "updated", mode="before", check_fields=False)
    @classmethod
    def parse_datetime(cls, x: int) -> Optional[float]:
        """Pydantic V2 treats timestamps differently depending on value."""
        if x is None:
            return x
        return x / 1000 if abs(x) < 2e10 else x

parse_datetime(x) classmethod

Pydantic V2 treats timestamps differently depending on value.

Source code in smhi/models/metobs_model.py
@field_validator("from_", "to", "updated", mode="before", check_fields=False)
@classmethod
def parse_datetime(cls, x: int) -> Optional[float]:
    """Pydantic V2 treats timestamps differently depending on value."""
    if x is None:
        return x
    return x / 1000 if abs(x) < 2e10 else x

MetobsCategoryModel

Bases: MetobsBaseModel

Model used for versions.

Source code in smhi/models/metobs_model.py
class MetobsCategoryModel(MetobsBaseModel):
    """Model used for versions."""

    version: List[MetobsLinks]

    @property
    def data(self) -> List[MetobsLinks]:
        return self.version

MetobsDataModel

Bases: BaseModel

Data model.

Source code in smhi/models/metobs_model.py
class MetobsDataModel(BaseModel):
    """Data model."""

    model_config = ConfigDict(arbitrary_types_allowed=True)

    station: Optional[pd.DataFrame] = None
    parameter: Optional[pd.DataFrame] = None
    period: Optional[pd.DataFrame] = None
    stationdata: Optional[pd.DataFrame] = None

Bases: BaseModel

Source code in smhi/models/metobs_model.py
class MetobsLinks(BaseModel):
    key: Optional[str] = None
    updated: Optional[datetime] = None
    title: str
    summary: str
    link: List[MetobsLink]

    @field_validator("updated", mode="before")
    @classmethod
    def parse_datetime(cls, x: int) -> Optional[float]:
        """Pydantic V2 treats timestamps differently depending on value."""
        if x is None:
            return x
        return x / 1000 if abs(x) < 2e10 else x

parse_datetime(x) classmethod

Pydantic V2 treats timestamps differently depending on value.

Source code in smhi/models/metobs_model.py
@field_validator("updated", mode="before")
@classmethod
def parse_datetime(cls, x: int) -> Optional[float]:
    """Pydantic V2 treats timestamps differently depending on value."""
    if x is None:
        return x
    return x / 1000 if abs(x) < 2e10 else x

MetobsParameterModel

Bases: MetobsBaseModel

Model used for stations.

Source code in smhi/models/metobs_model.py
class MetobsParameterModel(MetobsBaseModel):
    """Model used for stations."""

    unit: str
    value_type: MetobsValue = Field(..., alias="valueType")
    station_set: List[MetobsLinks] = Field(..., alias="stationSet")
    station: List[MetobsStationLink]

    @field_validator("station")
    @classmethod
    def serialise_station_in_order(cls, station: List[MetobsStationLink]):
        return sorted(station, key=lambda x: int(x.id))

    @property
    def data(self) -> Tuple[Tuple[int, str], ...]:
        return tuple((x.id, x.name) for x in self.station)

MetobsPeriodModel

Bases: MetobsBaseModel

Model used for stations.

Source code in smhi/models/metobs_model.py
class MetobsPeriodModel(MetobsBaseModel):
    """Model used for stations."""

    from_: datetime = Field(..., alias="from")
    to: datetime
    data: List[MetobsLinks]

MetobsPosition

Bases: BaseModel

Source code in smhi/models/metobs_model.py
class MetobsPosition(BaseModel):
    from_: datetime = Field(..., alias="from")
    to: datetime
    height: float
    latitude: float
    longitude: float

    @field_validator("from_", "to", mode="before")
    @classmethod
    def parse_datetime(cls, x: int) -> Optional[float]:
        """Pydantic V2 treats timestamps differently depending on value."""
        if x is None:
            return x
        return x / 1000 if abs(x) < 2e10 else x

parse_datetime(x) classmethod

Pydantic V2 treats timestamps differently depending on value.

Source code in smhi/models/metobs_model.py
@field_validator("from_", "to", mode="before")
@classmethod
def parse_datetime(cls, x: int) -> Optional[float]:
    """Pydantic V2 treats timestamps differently depending on value."""
    if x is None:
        return x
    return x / 1000 if abs(x) < 2e10 else x

Bases: MetobsLinks

Source code in smhi/models/metobs_model.py
class MetobsStationLink(MetobsLinks):
    name: str
    owner: str
    owner_category: str = Field(..., alias="ownerCategory")
    measuring_stations: MetobsMeasuringStations = Field(..., alias="measuringStations")
    id: int
    height: float
    latitude: float
    longitude: float
    active: bool
    from_: Optional[datetime] = Field(default=None, alias="from")
    to: Optional[datetime] = None

    @field_validator("from_", "to", "updated", mode="before")
    @classmethod
    def parse_datetime(cls, x: int) -> Optional[float]:
        """Pydantic V2 treats timestamps differently depending on value."""
        if x is None:
            return x
        return x / 1000 if abs(x) < 2e10 else x

parse_datetime(x) classmethod

Pydantic V2 treats timestamps differently depending on value.

Source code in smhi/models/metobs_model.py
@field_validator("from_", "to", "updated", mode="before")
@classmethod
def parse_datetime(cls, x: int) -> Optional[float]:
    """Pydantic V2 treats timestamps differently depending on value."""
    if x is None:
        return x
    return x / 1000 if abs(x) < 2e10 else x

MetobsStationModel

Bases: MetobsBaseModel

Model used for periods.

Source code in smhi/models/metobs_model.py
class MetobsStationModel(MetobsBaseModel):
    """Model used for periods."""

    owner: Optional[str] = None
    owner_category: Optional[str] = Field(default=None, alias="ownerCategory")
    measuring_stations: Optional[MetobsMeasuringStations] = Field(
        default=None, alias="measuringStations"
    )
    active: Optional[bool] = None
    from_: Optional[datetime] = Field(default=None, alias="from")
    to: Optional[datetime] = None
    position: Optional[List[MetobsPosition]] = None
    period: List[MetobsLinks]

    @field_validator("period")
    @classmethod
    def serialise_period_in_order(cls, period: List[MetobsLinks]):
        return sorted(period, key=lambda x: METOBS_AVAILABLE_PERIODS[x.key])

    @property
    def data(self) -> Tuple[Optional[str], ...]:
        return tuple(x.key for x in self.period)

MetobsVersionModel

Bases: MetobsBaseModel

Model used for parameters.

Source code in smhi/models/metobs_model.py
class MetobsVersionModel(MetobsBaseModel):
    """Model used for parameters."""

    resource: List[MetobsGeoLinks]

    @field_validator("resource")
    @classmethod
    def serialise_resource_in_order(cls, resource: List[MetobsGeoLinks]):
        return sorted(resource, key=lambda x: int(x.key))

    @property
    def data(self) -> Tuple[MetobsVersionItem, ...]:
        return tuple(
            MetobsVersionItem(key=x.key, title=x.title, summary=x.summary, unit=x.unit)
            for x in self.resource
        )