VIIRSdownloader/VIIRSdownloader.py

131 lines
5.2 KiB
Python
Raw Normal View History

2022-07-26 14:09:34 +00:00
# -*- coding: utf-8 -*-
"""
2022-07-28 15:06:10 +00:00
Downloads the actual active fire csv from
2022-07-28 13:18:17 +00:00
https://firms.modaps.eosdis.nasa.gov/data/active_fire/suomi-npp-viirs-c2/csv/SUOMI_VIIRS_C2_Europe_24h.csv
2022-07-26 14:09:34 +00:00
and displays it on a map.
2022-07-28 15:06:10 +00:00
Description of data displayed:
2022-07-28 13:18:17 +00:00
https://www.earthdata.nasa.gov/learn/find-data/near-real-time/firms/vnp14imgtdlnrt#ed-viirs-375m-attributes
2022-07-28 15:06:10 +00:00
@author: faraway
2022-07-26 14:09:34 +00:00
"""
import sys
2022-07-28 13:18:17 +00:00
import argparse
import re
2022-07-26 14:09:34 +00:00
import os.path
2022-07-28 15:06:10 +00:00
import pickle
2022-07-26 14:09:34 +00:00
import datetime as dt
2022-07-28 15:06:10 +00:00
import webbrowser
import requests
2022-07-26 14:09:34 +00:00
import pandas as pd
import folium
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
2022-07-28 15:06:10 +00:00
def check_file_format(args_inputfile):
pattern = re.compile(r'local_VIIRS_data-\d{4}-\d{2}-\d{2}.csv')
2022-07-28 13:18:17 +00:00
if not re.match(pattern, args_inputfile):
raise argparse.ArgumentTypeError
return args_inputfile
2022-07-28 15:06:10 +00:00
def get_address(data_frame, date):
2022-07-28 13:18:17 +00:00
address_file = 'adressdump-' + date
if not os.path.exists(address_file):
address_list = []
2022-07-26 14:09:34 +00:00
geolocator = Nominatim(user_agent='blub browser')
reverse = RateLimiter(geolocator.reverse, min_delay_seconds=5)
for r in data_frame.iterrows():
location = (r[1]['latitude'], r[1]['longitude'])
2022-07-28 13:18:17 +00:00
address_list.append(reverse(location, language='en'))
with open(address_file, 'wb') as file:
pickle.dump(address_list, file)
2022-07-26 14:09:34 +00:00
else:
2022-07-28 13:18:17 +00:00
with open(address_file, 'rb') as file:
address_list = pickle.load(file)
return address_list
2022-07-26 14:09:34 +00:00
2022-07-28 15:06:10 +00:00
def render_map(in_ukraine, address_list, date):
2022-07-26 14:09:34 +00:00
src = ('https://gibs.earthdata.nasa.gov/wmts-webmerc/VIIRS_SNPP_CorrectedReflectance_BandsM11-I2-I1/' +
'default/{time}/GoogleMapsCompatible_Level9/{z}/{y}/{x}.jpg')
2022-07-28 15:06:10 +00:00
if not in_ukraine.empty:
fire_map = folium.Map(location=in_ukraine[['latitude', 'longitude']].mean(
).to_list(), zoom_start=4, max_bounds=True, crs='EPSG3857')
for r in in_ukraine.iterrows():
location = (r[1]['latitude'], r[1]['longitude'])
folium.Marker(location=location, tooltip=(
r[1].to_string())).add_to(fire_map)
if address_list:
for address in address_list:
if address is not None:
location = (address.raw['lat'], address.raw['lon'])
folium.CircleMarker(location=location,
tooltip=(
str(address.raw['display_name'])),
radius=10).add_to(fire_map)
else:
fire_map = folium.Map(location=[(44.184598 + 52.3797464)/2, (22.137059 + 40.2275801)/2],
zoom_start=4, max_bounds=True, crs='EPSG3857')
folium.raster_layers.TileLayer(
tiles=src,
subdomains='abc',
name='VIIRS CorrectedReflectance_BandsM11-I2-I1',
attr='NASA VIIRS',
overlay=True,
layer='VIIRS_SNPP_CorrectedReflectance_BandsM11-I2-I1',
tileMatrixSet='GoogleMapsCompatible_Level9',
time=str(dt.date.fromisoformat(date) - dt.timedelta(days=1)),
tileSize=256,
).add_to(fire_map)
folium.LayerControl().add_to(fire_map)
return fire_map
def main(args):
bounding = [44.184598, 52.3797464, 22.137059, 40.2275801]
2022-07-28 13:18:17 +00:00
if args.inputfile:
local_file = args.inputfile
2022-07-28 15:06:10 +00:00
date = re.search(r'\d{4}-\d{2}-\d{2}', args.inputfile).group(0)
2022-07-28 13:18:17 +00:00
else:
2022-07-28 15:06:10 +00:00
date = str(dt.date.today())
2022-07-28 13:18:17 +00:00
local_file = 'local_VIIRS_data-' + date + '.csv'
2022-07-28 15:06:10 +00:00
if not os.path.exists(local_file):
try:
remote_url = r'https://firms.modaps.eosdis.nasa.gov/data/active_fire/suomi-npp-viirs-c2/csv/SUOMI_VIIRS_C2_Europe_24h.csv'
data = requests.get(remote_url, allow_redirects=True)
2022-07-28 13:18:17 +00:00
try:
with open(local_file, 'wb') as file:
file.write(data.content)
2022-07-28 15:06:10 +00:00
except OSError as e:
raise SystemExit(e)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
2022-07-28 13:18:17 +00:00
html_file = local_file.split('.')[0] + '.html'
2022-07-26 14:09:34 +00:00
data_frame = pd.read_csv(local_file)
data_frame = data_frame.astype({'confidence': 'string'})
2022-07-28 15:06:10 +00:00
in_ukraine = data_frame[data_frame['latitude'].between(bounding[0], bounding[1])
& data_frame['longitude'].between(bounding[2],
bounding[3])
& (data_frame['confidence'].str.contains('nominal')
| data_frame['confidence'].str.contains('high'))]
2022-07-28 13:18:17 +00:00
if not args.noreversegeolocation:
2022-07-28 15:06:10 +00:00
address_list = get_address(in_ukraine, date)
2022-07-28 13:18:17 +00:00
else:
address_list = []
2022-07-28 15:06:10 +00:00
fire_map = render_map(in_ukraine, address_list, date)
fire_map.save(html_file)
webbrowser.open_new(html_file)
2022-07-26 14:09:34 +00:00
sys.exit(0)
if __name__ == '__main__':
2022-07-28 13:18:17 +00:00
parser = argparse.ArgumentParser()
parser.add_argument(
2022-07-28 15:06:10 +00:00
'-i', '--inputfile', type=check_file_format,
help='Specify the cvs file containing the satellite data to be displayed. INPUTFILE must match local_VIIRS_data-YYYY-MM-DD.csv')
2022-07-28 13:18:17 +00:00
parser.add_argument('-nr', '--noreversegeolocation', action='store_true',
help='Disable reverse geolocation')
args = parser.parse_args()
main(args)