{
    "cells": [
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Images download\n",
                "---\n",
                "\n",
                "Download all images before training models."
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Setup"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### Imports"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "from tqdm.auto import tqdm"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "from coal_emissions_monitoring.constants import ALL_BANDS\n",
                "from coal_emissions_monitoring.data_cleaning import get_final_dataset\n",
                "from coal_emissions_monitoring.satellite_imagery import fetch_image_path_from_cog"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Get final datase"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "df = get_final_dataset(\n",
                "    image_metadata_path=\"/home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/image_metadata.csv\",\n",
                "    campd_facilities_path=\"https://drive.google.com/file/d/1b-5BriZUiiv2r0wFLubccLQpd2xb5ysl/view?usp=share_link\",\n",
                "    campd_emissions_path=\"https://drive.google.com/file/d/1oxZXR7GDcSXwwVoIjp66iS179cFVA5dP/view?usp=share_link\",\n",
                "    cog_type=\"all\",\n",
                ")\n",
                "df"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Download images"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### TCI (True Color Image)"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "tqdm.pandas(desc=\"Downloading visual images\")\n",
                "df[\"local_image_path\"] = df.progress_apply(\n",
                "    lambda row: fetch_image_path_from_cog(\n",
                "        cog_url=row.cog_url,\n",
                "        geometry=row.geometry,\n",
                "        images_dir=\"/home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/images/visual/\",\n",
                "        download_missing_images=True,\n",
                "    ),\n",
                "    axis=1,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# compress all images into one file\n",
                "!tar -czvf /home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/images/visual_images.tar.gz /home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/images/visual"
            ]
        },
        {
            "attachments": {},
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "### All bands"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "tqdm.pandas(desc=\"Downloading all bands images\")\n",
                "df[\"local_image_all_bands_path\"] = df.progress_apply(\n",
                "    lambda row: fetch_image_path_from_cog(\n",
                "        cog_url=[row[band] for band in ALL_BANDS],\n",
                "        geometry=row.geometry,\n",
                "        cog_type=\"all\",\n",
                "        images_dir=\"/home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/images/all_bands/\",\n",
                "        download_missing_images=True,\n",
                "    ),\n",
                "    axis=1,\n",
                ")"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# compress all images into one file\n",
                "!tar -czvf /home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/images/all_bands_images.tar.gz /home/adminuser/ccai-ss23-ai-monitoring-tutorial/data/images/all_bands"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": []
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "ccai_ss23",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.10.10"
        },
        "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
