Skip to content

dimensions

Functions to create and manipulate dimensions for netCDF files.

as_naive_timestamp

as_naive_timestamp(
    d: Union[datetime, Timestamp]
) -> Timestamp

Convert a date-time object to a naive timestamp.

Source code in src/efts_io/dimensions.py
76
77
78
79
80
81
82
83
84
85
def as_naive_timestamp(d: Union[datetime, pd.Timestamp]) -> pd.Timestamp:
    """Convert a date-time object to a naive timestamp."""
    return pd.Timestamp(
        year=d.year,
        month=d.month,
        day=d.day,
        hour=d.hour,
        minute=d.minute,
        second=d.second,
    )

check_is_utc

check_is_utc(d: Any) -> bool

Check that a date-time is in the UTC time zone.

Source code in src/efts_io/dimensions.py
38
39
40
41
42
43
44
45
46
def check_is_utc(d: Any) -> bool:
    """Check that a date-time is in the UTC time zone."""
    a = pd.Timestamp(d)
    if a.tz is None:
        return True  # ?
    z = a.tz
    from datetime import timezone

    return z == timezone.utc

create_netcdf_time_axis

create_netcdf_time_axis(
    d: Any,
    time_step: str = "hours since",
    tzoffset: Optional[str] = None,
) -> str

Create a time axis unit known to work for netCDF.

Source code in src/efts_io/dimensions.py
67
68
69
70
71
72
73
def create_netcdf_time_axis(d: Any, time_step: str = "hours since", tzoffset: Optional[str] = None) -> str:
    """Create a time axis unit known to work for netCDF."""
    if tzoffset is None:
        if not check_is_utc(d):
            raise ValueError("date time must have UTC or GMT as time zone")
        tzoffset = "+0000"
    return " ".join([time_step, iso_date_time_str(as_naive_timestamp(d)), tzoffset])

create_time_info

create_time_info(
    start: Any,
    n: int,
    time_step: str = "hours since",
    time_step_delta: int = 1,
    tzoffset: Optional[str] = None,
) -> Dict[str, Any]

Helper function to create the definition of the time dimension for use in a netCDF file.

Source code in src/efts_io/dimensions.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def create_time_info(
    start: Any,
    n: int,
    time_step: str = "hours since",
    time_step_delta: int = 1,
    tzoffset: Optional[str] = None,
) -> Dict[str, Any]:
    """Helper function to create the definition of the time dimension for use in a netCDF file."""
    return {
        UNITS_ATTR_KEY: create_netcdf_time_axis(
            d=start,
            time_step=time_step,
            tzoffset=tzoffset,
        ),
        "values": np.arange(0, n) * time_step_delta,
    }

iso_date_time_str

iso_date_time_str(t: Any) -> str

Convert a date-time object to a string in ISO format, using space as separator.

Source code in src/efts_io/dimensions.py
21
22
23
def iso_date_time_str(t: Any) -> str:
    """Convert a date-time object to a string in ISO format, using space as separator."""
    return pd.Timestamp(t).isoformat(" ")