Skip to content

Add visualization options to goes_timelapse function #1242

Closed
@SerafiniJose

Description

Description

Add one parameter to goes_timelapse function so that the output visualization bands could be changed.
Now only true color is available,
bands = ["CMI_C02", "CMI_GREEN", "CMI_C01"],
it will be useful to have false color also because fires are more visible using these bands
bands = ["CMI_C05", "CMI_C03", "CMI_GREEN"].

Source code

def goes_timelapse(
roi=None,
out_gif=None,
start_date="2021-10-24T14:00:00",
end_date="2021-10-25T01:00:00",
data="GOES-17",
scan="full_disk",
dimensions=768,
framesPerSecond=10,
date_format="YYYY-MM-dd HH:mm",
xy=("3%", "3%"),
text_sequence=None,
font_type="arial.ttf",
font_size=20,
font_color="#ffffff",
add_progress_bar=True,
progress_bar_color="white",
progress_bar_height=5,
loop=0,
crs=None,
overlay_data=None,
overlay_color="black",
overlay_width=1,
overlay_opacity=1.0,
mp4=False,
fading=False,
vis = "TrueColor"
**kwargs,
):
"""Create a timelapse of GOES data. The code is adapted from Justin Braaten's code: https://code.earthengine.google.com/57245f2d3d04233765c42fb5ef19c1f4.
Credits to Justin Braaten. See also https://jstnbraaten.medium.com/goes-in-earth-engine-53fbc8783c16
Args:
out_gif (str): The file path to save the gif.
start_date (str, optional): The start date of the time series. Defaults to "2021-10-24T14:00:00".
end_date (str, optional): The end date of the time series. Defaults to "2021-10-25T01:00:00".
data (str, optional): The GOES satellite data to use. Defaults to "GOES-17".
scan (str, optional): The GOES scan to use. Defaults to "full_disk".
roi (ee.Geometry, optional): The region of interest. Defaults to None.
dimensions (int, optional): a number or pair of numbers in format WIDTHxHEIGHT) Maximum dimensions of the thumbnail to render, in pixels. If only one number is passed, it is used as the maximum, and the other dimension is computed by proportional scaling. Defaults to 768.
frames_per_second (int, optional): Animation speed. Defaults to 10.
date_format (str, optional): The date format to use. Defaults to "YYYY-MM-dd HH:mm".
xy (tuple, optional): Top left corner of the text. It can be formatted like this: (10, 10) or ('15%', '25%'). Defaults to None.
text_sequence (int, str, list, optional): Text to be drawn. It can be an integer number, a string, or a list of strings. Defaults to None.
font_type (str, optional): Font type. Defaults to "arial.ttf".
font_size (int, optional): Font size. Defaults to 20.
font_color (str, optional): Font color. It can be a string (e.g., 'red'), rgb tuple (e.g., (255, 127, 0)), or hex code (e.g., '#ff00ff'). Defaults to '#000000'.
add_progress_bar (bool, optional): Whether to add a progress bar at the bottom of the GIF. Defaults to True.
progress_bar_color (str, optional): Color for the progress bar. Defaults to 'white'.
progress_bar_height (int, optional): Height of the progress bar. Defaults to 5. loop (int, optional): controls how many times the animation repeats. The default, 1, means that the animation will play once and then stop (displaying the last frame). A value of 0 means that the animation will repeat forever. Defaults to 0.
crs (str, optional): The coordinate reference system to use, e.g., "EPSG:3857". Defaults to None.
overlay_data (int, str, list, optional): Administrative boundary to be drawn on the timelapse. Defaults to None.
overlay_color (str, optional): Color for the overlay data. Can be any color name or hex color code. Defaults to 'black'.
overlay_width (int, optional): Line width of the overlay. Defaults to 1.
overlay_opacity (float, optional): Opacity of the overlay. Defaults to 1.0.
mp4 (bool, optional): Whether to save the animation as an mp4 file. Defaults to False.
fading (int | bool, optional): If True, add fading effect to the timelapse. Defaults to False, no fading. To add fading effect, set it to True (1 second fading duration) or to an integer value (fading duration).
vis (str, optional): Wheter to use TrueColor or FalseColor band combination to generate GOES timelapse. Defaults to TrueColor.
Raises:
Exception: Raise exception.
"""

try:

    if "region" in kwargs:
        roi = kwargs["region"]

    if out_gif is None:
        out_gif = os.path.abspath(f"goes_{random_string(3)}.gif")

    if vis == "TrueColor":
        bands = ["CMI_C02", "CMI_GREEN", "CMI_C01"]
    elif vis == "FalseColor":
        bands = ["CMI_C05", "CMI_C03", "CMI_GREEN"]

    visParams = {
        "bands": bands,
        "min": 0,
        "max": 0.8,
    }
    col = goes_timeseries(start_date, end_date, data, scan, roi)
    col = col.select(bands).map(
        lambda img: img.visualize(**visParams).set(
            {
                "system:time_start": img.get("system:time_start"),
            }
        )
    )
    if overlay_data is not None:
        col = add_overlay(
            col, overlay_data, overlay_color, overlay_width, overlay_opacity
        )

    if roi is None:
        roi = ee.Geometry.Polygon(
            [
                [
                    [-159.5954, 60.4088],
                    [-159.5954, 24.5178],
                    [-114.2438, 24.5178],
                    [-114.2438, 60.4088],
                ]
            ],
            None,
            False,
        )

    if crs is None:
        crs = col.first().projection()

    videoParams = {
        "bands": ["vis-red", "vis-green", "vis-blue"],
        "min": 0,
        "max": 255,
        "dimensions": dimensions,
        "framesPerSecond": framesPerSecond,
        "region": roi,
        "crs": crs,
    }

    if text_sequence is None:
        text_sequence = image_dates(col, date_format=date_format).getInfo()

    download_ee_video(col, videoParams, out_gif)

    if os.path.exists(out_gif):

        add_text_to_gif(
            out_gif,
            out_gif,
            xy,
            text_sequence,
            font_type,
            font_size,
            font_color,
            add_progress_bar,
            progress_bar_color,
            progress_bar_height,
            duration=1000 / framesPerSecond,
            loop=loop,
        )

        try:
            reduce_gif_size(out_gif)

            if isinstance(fading, bool):
                fading = int(fading)
            if fading > 0:
                gif_fading(out_gif, out_gif, duration=fading, verbose=False)

        except Exception as _:
            pass

        if mp4:
            out_mp4 = out_gif.replace(".gif", ".mp4")
            gif_to_mp4(out_gif, out_mp4)

        return out_gif

except Exception as e:
    raise Exception(e)

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions