Last active
June 13, 2023 19:06
-
-
Save shijithpk/d72724a2605266793651330e601ee717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "23a56085-5632-4ff1-ad7a-97a4fc5bfb8d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"! pip install --user --quiet owslib" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "8fdda757-45a3-4665-b689-4664db25cea6", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"! pip install --user --quiet scikit-image" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "feeef91f-8ad6-4fc8-af66-993485f8df0d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"! pip install --user --quiet pillow" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "0bd1bd59-b237-4626-babd-3e99517b4b81", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# # authorisation functions file from https://gitlab.eumetsat.int/eumetlab/data-services/authorisation_functions/\n", | |
"\n", | |
"# import requests\n", | |
"# import re\n", | |
"\n", | |
"# register for an account at EUMETSAT here -- https://eoportal.eumetsat.int/userMgmt/register.faces\n", | |
"# after that you'll need a consumer secret and consumer key. Instructions on how to get that are at https://eumetsatspace.atlassian.net/wiki/spaces/DSEV/pages/375652353/API+Authentication\n", | |
"# Put the consumer secret and consumer key in the credentials.json you'll be downloading below\n", | |
"\n", | |
"# url_1 = 'https://gitlab.eumetsat.int/eumetlab/data-services/authorisation_functions/-/raw/master/credentials.json'\n", | |
"# url_2 = 'https://gitlab.eumetsat.int/eumetlab/data-services/authorisation_functions/-/raw/master/authorisation_functions.py'\n", | |
"\n", | |
"# for url in [url_1,url_2]:\n", | |
"# myfile = requests.get(url)\n", | |
"# pattern = r'.*\\/(.*)'\n", | |
"# match = re.search(pattern, url)\n", | |
"# file_name = match.group(1)\n", | |
"# open(file_name, 'wb').write(myfile.content)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "ace6c3e7-4b3e-40d6-a5ca-51246471e13d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# code taken from https://eumetsatspace.atlassian.net/wiki/spaces/DSEV/pages/375783427/Jupyter+Notebook+Using+the+EUMETView+WMS+API\n", | |
"\n", | |
"import os, sys\n", | |
"import warnings\n", | |
"# from IPython.core.display import HTML\n", | |
"# from IPython.display import Image\n", | |
"from owslib.wms import WebMapService \n", | |
"from owslib.util import Authentication\n", | |
"from skimage import io\n", | |
"import requests\n", | |
"\n", | |
"from PIL import Image, ImageDraw, ImageFont\n", | |
"import io\n", | |
"import time\n", | |
"from dateutil import parser\n", | |
"\n", | |
"sys.path.append(os.path.dirname(os.getcwd()))\n", | |
"import authorisation_functions as auth\n", | |
"\n", | |
"# turn off warnings (suppresses output from certification verification when verify=false)\n", | |
"warnings.simplefilter(\"ignore\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "b79d486f-26ec-488c-87cb-6610545b727b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Successfully retrieved credentials....\n", | |
"Access token retrieved: 66ac99ba-142d-38b0-ae45-dfaf8d80d8e3\n" | |
] | |
} | |
], | |
"source": [ | |
"credentials = auth.import_credentials('credentials.json')\n", | |
"access_token = auth.generate_token(consumer_key=credentials['consumer_key'], consumer_secret=credentials['consumer_secret'])\n", | |
"print('Access token retrieved: ' + access_token)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "8b6f3d15-ac76-40b9-a4c5-192112385852", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"service_url = 'https://view.eumetsat.int/geoserver/ows?'\n", | |
"wms = WebMapService(service_url, auth=Authentication(verify=False))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "c005f05a-0c0a-4435-af0d-03bd3d2fdd28", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# https://view.eumetsat.int/geoserver/ows?service=WMS&request=GetMap&version=1.3.0&\n", | |
"#layers=msg_iodc:ir108&styles=&format=image/jpeg&crs=EPSG:4326&bbox=-77,-35.5,77,118.5&width=800&height=800\n", | |
"\n", | |
"target_layer = 'msg_iodc:ir108'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "366379d9-c942-4b58-bc4a-a5d4129055dd", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# mkdir images" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "c0edec59-fb3f-4964-9599-57c7066a79c6", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from datetime import datetime, timedelta\n", | |
"\n", | |
"time_list = []\n", | |
"\n", | |
"start_time = datetime(2023, 6, 4, 17, 30) # have taken time from zoom earth , this is in UTC\n", | |
"# start_time = datetime(2023, 6, 11, 15, 0) #just for testing\n", | |
"end_time = datetime.now()\n", | |
"delta = timedelta(minutes=15)\n", | |
"\n", | |
"current_time = start_time\n", | |
"while current_time <= end_time:\n", | |
" time_list.append(current_time.isoformat() + 'Z') # + '+00:00'\n", | |
" current_time += delta" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"id": "5941b46b-8de5-4ac0-b178-d31000caf724", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"format_option = 'image/png'\n", | |
"\n", | |
"x_min = 54.54429120906749 #51.597703\n", | |
"x_max = 86.03923534038681 #93.917039\n", | |
"y_min = 0.0 #-1.262013\n", | |
"y_max = 30.119686 #32.408900\n", | |
"\n", | |
"\n", | |
"for time_point in time_list:\n", | |
" time.sleep(0.5)\n", | |
" \n", | |
" payload = {\n", | |
" 'access_token': access_token,\n", | |
" 'layers' : [target_layer,'backgrounds:ne_10m_coastline'],\n", | |
" 'styles' : ['','whitelines'],\n", | |
" 'format' : format_option,\n", | |
" 'crs' : 'EPSG:4326',\n", | |
" 'bbox' : (x_min, y_min, x_max, y_max),\n", | |
" 'time': time_point,\n", | |
" 'transparent': True, # or should it be 'true'?\n", | |
" 'size' : (1080,1080), # this will have to be in the right aspect ratio \n", | |
" }\n", | |
"\n", | |
" wms = WebMapService(service_url, auth=Authentication(verify=False))\n", | |
" \n", | |
" try:\n", | |
" img = wms.getmap(**payload)\n", | |
" except:\n", | |
" continue\n", | |
"\n", | |
" image_name = 'images/raw/' + time_point + '.png'\n", | |
" \n", | |
" with open(image_name, 'wb') as f:\n", | |
" f.write(img.read())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"id": "6e67ad03-b702-41c1-9589-6494762efef2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from PIL import Image, ImageDraw, ImageFont\n", | |
"\n", | |
"import pytz\n", | |
"\n", | |
"for time_point in time_list:\n", | |
" \n", | |
" image_name = 'images/raw/' + time_point + '.png'\n", | |
" \n", | |
" try:\n", | |
" img_pil = Image.open(image_name).convert(\"RGBA\")\n", | |
" except:\n", | |
" continue \n", | |
"\n", | |
" \n", | |
" width, height = img_pil.size\n", | |
" draw = ImageDraw.Draw(img_pil) #draw = ImageDraw.Draw(img_pil,\"RGBA\")\n", | |
"\n", | |
" # put time image was taken # convert to indian time\n", | |
" readable_string = parser.isoparse(time_point).astimezone(pytz.timezone('Asia/Kolkata')).strftime(\"%B %d, %Y at %I:%M %p IST\")\n", | |
" \n", | |
" padding = 10\n", | |
" font_time_size = 20\n", | |
" font_time = ImageFont.truetype('font/Lato-Bold.ttf', font_time_size)\n", | |
" textwidth_time, textheight_time = draw.textsize(readable_string, font_time)\n", | |
" x_time = (width - textwidth_time)//2\n", | |
" y_time = height - (4 * textheight_time) \n", | |
" draw.rectangle([x_time - padding, y_time - padding, \n", | |
" x_time + textwidth_time + padding, y_time + textheight_time + padding], fill=\"black\")\n", | |
" draw.text((x_time, y_time), readable_string, font=font_time)\n", | |
" \n", | |
" \n", | |
" text_title = \"CYCLONE BIPARJOY'S PATH TO GUJARAT\"\n", | |
" font_title_size = 45 \n", | |
" font_title = ImageFont.truetype('font/Lato-Black.ttf', font_title_size)\n", | |
" textwidth_title, textheight_title = draw.textsize(text_title, font_title)\n", | |
" padding_title = textheight_title//6\n", | |
" x_title = padding_title * 8\n", | |
" y_title = padding_title * 7\n", | |
" draw.rectangle([x_title - padding_title, \n", | |
" y_title - padding_title, \n", | |
" x_title + textwidth_title + padding_title, \n", | |
" y_title + textheight_title + padding_title], fill=\"black\")\n", | |
" draw.text((x_title, y_title-(1.1*padding_title)), text_title, font=font_title) #, anchor='mm'\n", | |
" \n", | |
" \n", | |
" text_source = 'Source: EUMETSAT satellite Meteosat-9'\n", | |
" font_source_size = 16\n", | |
" font_source = ImageFont.truetype('font/Lato-Regular.ttf', font_source_size)\n", | |
" textwidth_source, textheight_source = draw.textsize(text_source, font_source)\n", | |
" padding_source = textheight_source//3\n", | |
" x_source = padding_source * 2\n", | |
" y_source = height - (5 * padding_source)\n", | |
" draw.rectangle([x_source - padding_source, y_source - 0.5*padding_source, \n", | |
" x_source + textwidth_source + padding_source, \n", | |
" y_source + textheight_source + padding_source], fill=\"black\")\n", | |
" draw.text((x_source, y_source), text_source, font=font_source, \n", | |
" # anchor='lm'\n", | |
" )\n", | |
"\n", | |
" \n", | |
" text_credit = 'Graphic: Shijith Kunhitty'\n", | |
" font_credit_size = 16\n", | |
" font_credit = ImageFont.truetype('font/Lato-Regular.ttf', font_credit_size)\n", | |
" textwidth_credit, textheight_credit = draw.textsize(text_credit, font_credit)\n", | |
" padding_credit = textheight_credit//3\n", | |
" x_credit = width - textwidth_credit - (padding_credit * 2)\n", | |
" y_credit = height - (5 * padding_credit)\n", | |
" draw.rectangle([x_credit - padding_credit, y_credit - padding_credit, \n", | |
" x_credit + textwidth_credit + padding_credit, \n", | |
" y_credit + textheight_credit + padding_credit], fill=\"black\")\n", | |
" draw.text((x_credit, y_credit), text_credit, font=font_credit, \n", | |
" # anchor='lm'\n", | |
" )\n", | |
" \n", | |
" \n", | |
" # overlay logo in top right\n", | |
" logo = Image.open('round-favicon.png')\n", | |
" side_length = 80\n", | |
" logo_resized = logo.resize((side_length,side_length), resample=Image.LANCZOS)\n", | |
" x_logo = int(width - (1.25 * side_length))\n", | |
" y_logo = side_length//4\n", | |
" img_pil.paste(logo_resized,(x_logo,y_logo), logo_resized) \n", | |
" \n", | |
" image_name_edited = 'images/edited/' + time_point + '.png'\n", | |
" \n", | |
" img_pil.save(image_name_edited)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "16cb4620-a9ca-4d1f-83ad-7c5105af3e69", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Note: you may need to restart the kernel to use updated packages.\n" | |
] | |
} | |
], | |
"source": [ | |
"pip install --user --quiet imageio-ffmpeg" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"id": "4ec07474-3b9d-4fa5-b2bc-2883f88f1707", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1080, 1080) to (1088, 1088) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).\n" | |
] | |
} | |
], | |
"source": [ | |
"import imageio\n", | |
"import os\n", | |
"\n", | |
"image_read_list = []\n", | |
"\n", | |
"for time_point in time_list:\n", | |
" image_name = 'images/edited/' + time_point + '.png'\n", | |
" try:\n", | |
" image_read_list.append(imageio.imread(image_name))\n", | |
" except:\n", | |
" continue\n", | |
"\n", | |
"fps = 18\n", | |
"duration = 1/fps\n", | |
"\n", | |
"# imageio.mimsave('biparjoy_path_gif_v2.gif', image_read_list, duration=duration)\n", | |
"\n", | |
"# might need to install this -- pip install imageio-ffmpeg for line below to work\n", | |
"imageio.mimwrite('biparjoy_v4_1088x1088.mp4', image_read_list, fps=fps)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "56b69e76-8042-4cbc-8d2d-6c9283eef93d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.10.6 64-bit", | |
"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.6" | |
}, | |
"vscode": { | |
"interpreter": { | |
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment