added comand line arguments

This commit is contained in:
faraway 2022-07-28 15:18:17 +02:00
parent ecb003bec3
commit d3bbd76fef

View File

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