{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import geopandas as gpd\n",
    "from tqdm.auto import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from coal_emissions_monitoring.satellite_imagery import (\n",
    "    create_aoi_for_plants,\n",
    "    get_image_metadata_for_plants,\n",
    "    get_image_from_cog\n",
    ")\n",
    "from coal_emissions_monitoring.data_viz import view_satellite_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/google/labeled_geospatial_data.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.is_powered_on.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get unique combinations of lat/lon\n",
    "unique_coords = df[[\"lat\", \"lon\"]].drop_duplicates().reset_index(drop=True)\n",
    "unique_coords.reset_index(inplace=True)\n",
    "unique_coords.set_index([\"lat\", \"lon\"], inplace=True)\n",
    "unique_coords = unique_coords[\"index\"].to_dict()\n",
    "unique_coords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set an epsg code for each unique lat/lon\n",
    "df[\"facility_id\"] = df.apply(\n",
    "    lambda x: unique_coords[(x[\"lat\"], x[\"lon\"])], axis=1\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.facility_id.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.rename(columns={\"lat\": \"latitude\", \"lon\": \"longitude\"}, inplace=True)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.rename(columns={\"timestamp\": \"ts\"}, inplace=True)\n",
    "df.ts = pd.to_datetime(df.ts)\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude), crs=\"EPSG:4326\")\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = create_aoi_for_plants(gdf)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.geometry.explore()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# image_metadata_df = get_image_metadata_for_plants(\n",
    "#     gdf,\n",
    "#     start_date=gdf.ts.min(),\n",
    "#     end_date=gdf.ts.max(),\n",
    "#     max_cloud_cover_prct=50,\n",
    "# )\n",
    "image_metadata_df = pd.read_csv(\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/google/image_metadata.csv\")\n",
    "image_metadata_df.ts = pd.to_datetime(image_metadata_df.ts)\n",
    "image_metadata_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# filter the image metadata to match the day of each row of gdf\n",
    "image_metadata_df[\"date\"] = image_metadata_df.ts.dt.date\n",
    "gdf[\"date\"] = gdf.ts.dt.date\n",
    "image_metadata_df = image_metadata_df.merge(\n",
    "    gdf[[\"facility_id\", \"date\"]], on=[\"facility_id\", \"date\"]\n",
    ")\n",
    "image_metadata_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_metadata_df.to_csv(\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/google/image_metadata.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.merge(\n",
    "    image_metadata_df.drop(columns=[\"ts\"]),\n",
    "    on=[\"facility_id\", \"date\"]\n",
    ").to_csv(\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/google/all_urls_dataset.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gdf.merge(\n",
    "    image_metadata_df[[\"facility_id\", \"date\", \"cloud_cover\", \"visual\"]],\n",
    "    on=[\"facility_id\", \"date\"]\n",
    ")\n",
    "gdf.rename(columns={\"visual\": \"cog_url\"}, inplace=True)\n",
    "gdf.drop(columns=[\"date\"], inplace=True)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.sort_values(by=[\"facility_id\", \"ts\"], inplace=True)\n",
    "gdf.to_csv(\"/Users/adminuser/GitHub/ccai-ss23-ai-monitoring-tutorial/data/google/final_dataset.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "row = gdf.iloc[0]\n",
    "image = get_image_from_cog(\n",
    "    cog_url=row.cog_url,\n",
    "    geometry=row.geometry,\n",
    "    size=64,\n",
    ")\n",
    "image.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "view_satellite_image(image)"
   ]
  },
  {
   "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.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
