import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import folium
from folium import IFrame
from branca.colormap import linear
from branca.colormap import LinearColormap
import numpy as np
# Map Interactive Popup Format
html_template = """
Local Time: {time}
Methane: {ch4:.2f} ppb
"""
# Load the data from the file
file_path = './1241-1247UTC_Mar28_2024_CH4_lat_lon_unixTime.txt' # Update the path
#file_path = './1241-1250UTC_Mar28_2024_CH4_lat_lon_unixTime.txt' # Update the path
#file_path = './1352-1433UTC_Aug1_2024_CH4_lat_lon_unixTime.txt' # Update the path
#file_path = './1352-1545UTC_Aug1_2024_CH4_lat_lon_unixTime.txt'
figure_path = './'
data = pd.read_csv(file_path, delim_whitespace=True, header=0)
# Filter out invalid methane concentration values
data = data[data['CH4LicormatchIphonePPB'] > 0]
data["datetime"] = pd.to_datetime(data['unix_time'],unit='s') # Convert unix time to UTC
data['local_datetime'] = data['datetime'] - pd.Timedelta(hours=5) # Convert UTC to local time (Summer Central Time)
# Ensure the datetime is formatted to display up to seconds
data['local_datetime'] = data['local_datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
m = folium.Map(location=[np.mean(data['lat']), np.mean(data['lon'])], zoom_start=15, tiles=None)
# Add a tile layer (try different ones if 'OpenStreetMap' doesn't work)
folium.TileLayer('OpenStreetMap',name='Street View').add_to(m)
folium.TileLayer('Esri.WorldImagery',name='Satellite View').add_to(m)
vmin = 2100
vmax = 2600
color_scale = LinearColormap(['blue', 'cyan', 'yellow', 'red'], vmin=vmin, vmax=vmax)
# Adding a color bar
color_scale.caption = 'Methane (ppb)'
color_scale.add_to(m)
# Add markers
for lat, lon, ch4, local_time in zip(data['lat'], data['lon'], data['CH4LicormatchIphonePPB'], data['local_datetime']):
if ch4 > 0: # Assuming negative values are invalid
html = html_template.format(time=local_time, ch4=ch4)
iframe = IFrame(html, width=260, height=40) # width and height of the IFrame itself
popup = folium.Popup(iframe, max_width=200)
folium.CircleMarker(
location=[lat, lon],
radius=3.8432,
color=color_scale(ch4),
fill=True,
fill_color=color_scale(ch4),
fill_opacity=0.7,
popup=popup
).add_to(m)
folium.LayerControl().add_to(m)
# CSS to adjust the caption font size and tick label size
CSS = """
"""
# Inject CSS into the map
m.get_root().header.add_child(folium.Element(CSS))
# Save and show the map
m.save(figure_path+'mathane_map_Mar28_2024Pampa.html')
#m.save(figure_path+'mathane_map_full.html')