{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "719279e8",
   "metadata": {},
   "source": [
    "### What this script does \n",
    "\n",
    "- Opens NASA Worldview for each date in a range and saves a screenshot (.png) per day.\n",
    "\n",
    "- Runs headless Chrome via Selenium; stores images in snapshots/.\n",
    "\n",
    "\n",
    "Edit before running/Update these lines:\n",
    "\n",
    " 1) Worldview URL (only if you want a different area/layers)\n",
    " \n",
    "base_url = (\"https://worldview.earthdata.nasa.gov/?v=...&t={}\")\n",
    "\n",
    " 2) Your local ChromeDriver path (required)\n",
    " \n",
    "chrome_driver_path = r\"C:\\path\\to\\your\\chromedriver.exe\"\n",
    "\n",
    " 3) Date range to capture\n",
    " \n",
    "start_date = datetime(2024, 3, 20)\n",
    "end_date   = datetime(2024, 3, 21)\n",
    "\n",
    " 4) (Optional) Headless mode: remove this line to see the browser\n",
    " \n",
    "chrome_options.add_argument(\"--headless\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56264226",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!pip install selenium\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df2914a8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "pip install -U selenium\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c117b5cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "from datetime import datetime, timedelta\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.common.exceptions import NoSuchElementException, TimeoutException"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc0bc03b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Base NASA Worldview URL template for satellite imagery\n",
    "# ⚠️ Edit this link only if you want to change the map region or layers.\n",
    "# You can generate a custom link by opening https://worldview.earthdata.nasa.gov/,\n",
    "# adjusting the area and layers, and copying the full URL (replace the date/time with {}).\n",
    "base_url = (\n",
    "    \"https://worldview.earthdata.nasa.gov/?\"\n",
    "    \"v=-20.32694246665797,39.378487105812205,26.013324746342217,60.346251241902785&\"\n",
    "    \"l=Reference_Labels_15m(hidden),Reference_Features_15m(hidden),Coastlines_15m,\"\n",
    "    \"BlueMarble_NextGeneration(hidden),VIIRS_NOAA20_CorrectedReflectance_TrueColor(hidden),\"\n",
    "    \"VIIRS_SNPP_CorrectedReflectance_TrueColor(hidden),MODIS_Aqua_CorrectedReflectance_TrueColor(hidden),\"\n",
    "    \"MODIS_Terra_CorrectedReflectance_TrueColor&lg=true&s=4.7507,52.639&t={}\"\n",
    ")\n",
    "\n",
    "# Path to your local ChromeDriver executable (required for Selenium)\n",
    "# ⚠️ Edit this path to match your local setup before running.\n",
    "# Example: chrome_driver_path = r\"D:\\Tools\\chromedriver\\chromedriver.exe\"\n",
    "chrome_driver_path = r\"C:\\path\\to\\your\\chromedriver.exe\"\n",
    "\n",
    "\n",
    "# Set up Chrome options\n",
    "chrome_options = Options()\n",
    "chrome_options.add_argument(\"--headless\")  # Optional: run in headless mode\n",
    "\n",
    "# Initialize ChromeDriver service\n",
    "service = Service(chrome_driver_path)\n",
    "\n",
    "# Initialize Chrome WebDriver with service and options\n",
    "driver = webdriver.Chrome(service=service, options=chrome_options)\n",
    "\n",
    "# Function to capture the snapshot\n",
    "def capture_snapshot(date):\n",
    "    formatted_date = date.strftime(\"%Y-%m-%d-T%H:%M:%SZ\")\n",
    "    url = base_url.format(formatted_date)\n",
    "    driver.get(url)\n",
    "    time.sleep(5)  # Wait for the page to load fully\n",
    "    \n",
    "   \n",
    "    # Take a screenshot\n",
    "    try:\n",
    "        if not os.path.exists('snapshots'):\n",
    "            os.makedirs('snapshots')\n",
    "        driver.save_screenshot(f\"snapshots/snapshot_{formatted_date}.png\")\n",
    "        print(f\"Snapshot taken for {formatted_date}\")\n",
    "    except Exception as e:\n",
    "        print(f\"Error saving screenshot for {formatted_date}: {e}\")\n",
    "\n",
    "# Set the date range\n",
    "start_date = datetime(2024, 3, 20)\n",
    "end_date = datetime(2024, 3, 21)  # Example: capture snapshots for the month of March\n",
    "\n",
    "# Iterate over each day in the date range\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    capture_snapshot(current_date)\n",
    "    current_date += timedelta(days=1)\n",
    "\n",
    "# Close the WebDriver\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7023a15e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "print(os.getcwd())\n",
    "try:\n",
    "    os.makedirs('test_directory')\n",
    "    print(\"Directory created successfully.\")\n",
    "except OSError as error:\n",
    "    print(f\"Error creating directory: {error}\")\n",
    "try:\n",
    "    with open('test_file.txt', 'w') as f:\n",
    "        f.write('Testing file write permissions.')\n",
    "    print(\"File saved successfully.\")\n",
    "except IOError as error:\n",
    "    print(f\"Error saving file: {error}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
