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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import moviepy.video.io.ImageSequenceClip as ic
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import bs4
|
| 9 |
+
import datetime
|
| 10 |
+
import urllib.request
|
| 11 |
+
import uuid
|
| 12 |
+
import base64
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
main_directory = "https://services.swpc.noaa.gov/"
|
| 16 |
+
sdo_source = "https://sdo.gsfc.nasa.gov/assets/img/browse/"
|
| 17 |
+
sdo_source_format = "https://sdo.gsfc.nasa.gov/assets/img/browse/YEAR/MONTH/DAY/DATE_IDENT_SIZE_TOOL.jpg"
|
| 18 |
+
|
| 19 |
+
comp_list=[
|
| 20 |
+
"https://services.swpc.noaa.gov/images/geospace/geospace_1_day.png",
|
| 21 |
+
"https://services.swpc.noaa.gov/images/ace-epam-24-hour.gif",
|
| 22 |
+
"https://services.swpc.noaa.gov/images/ace-epam-e-24-hour.gif",
|
| 23 |
+
"https://services.swpc.noaa.gov/images/ace-epam-p-24-hour.gif",
|
| 24 |
+
"https://services.swpc.noaa.gov/images/ace-mag-24-hour.gif",
|
| 25 |
+
"https://services.swpc.noaa.gov/images/ace-mag-swepam-24-hour.gif",
|
| 26 |
+
"https://services.swpc.noaa.gov/images/ace-sis-24-hour.gif",
|
| 27 |
+
"https://services.swpc.noaa.gov/images/boulder-magnetometer.png",
|
| 28 |
+
"https://services.swpc.noaa.gov/images/seaesrt-time-series-270.png",
|
| 29 |
+
"https://services.swpc.noaa.gov/images/station-k-index.png",
|
| 30 |
+
"https://services.swpc.noaa.gov/images/notifications-timeline.png",
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
sdo_aia_latest=[
|
| 34 |
+
{"name":"Fe18 94A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_94.gif"},
|
| 35 |
+
{"name":"Fe20 131A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_131.gif"},
|
| 36 |
+
{"name":"Fe9/10 171A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_171.gif"},
|
| 37 |
+
{"name":"Fe12 193A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_193.gif"},
|
| 38 |
+
{"name":"Fe14 211A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_211.gif"},
|
| 39 |
+
{"name":"He2 304A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_304.gif"},
|
| 40 |
+
{"name":"Fe16 335A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_335.gif"},
|
| 41 |
+
{"name":"cont+C4 1600A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_1600.gif"},
|
| 42 |
+
{"name":"continuum 1700A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_1700.gif"},
|
| 43 |
+
{"name":"continuum 4500A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_4500.gif"},
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
nasa_images=[
|
| 47 |
+
{"name":"EIT 171A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_171/512/latest.jpg"},
|
| 48 |
+
{"name":"EIT 195A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_195/512/latest.jpg"},
|
| 49 |
+
{"name":"EIT 284A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_284/512/latest.jpg"},
|
| 50 |
+
{"name":"EIT 304A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_304/512/latest.jpg"},
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
nasa_soho_gifs=[
|
| 54 |
+
{"name":"EIT 304A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_304small.gif"},
|
| 55 |
+
{"name":"EIT 195A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_195small.gif"},
|
| 56 |
+
{"name":"EIT 171A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_171small.gif"},
|
| 57 |
+
{"name":"EIT 284A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_284small.gif"},
|
| 58 |
+
]
|
| 59 |
+
nasa_soho_mp4=[
|
| 60 |
+
{"name":"EIT 171A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_171small.mp4"},
|
| 61 |
+
{"name":"EIT 195A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_195small.mp4"},
|
| 62 |
+
{"name":"EIT 284A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_284small.mp4"},
|
| 63 |
+
{"name":"EIT 304A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_304small.mp4"},
|
| 64 |
+
{"name":"LASCO C2", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_c2small.mp4"},
|
| 65 |
+
{"name":"LASCO C3", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_c3small.mp4"},
|
| 66 |
+
{"name":"SDO/HMI Continuum", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_hmi_igr-512.mp4"},
|
| 67 |
+
{"name":"SDO/HMI Magnetogram", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_hmi_mag-512.mp4"},
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
sdo_obj = ["HMIB","HMIBC","HMIIC","HMIIF","HMID","HMII","HMI171",
|
| 71 |
+
"0094","0131","0171","0193","0211",
|
| 72 |
+
"0304","0335","1600","1700","4500",
|
| 73 |
+
"211193171","211193171n","211193171rg",
|
| 74 |
+
"094335193","304211171"]
|
| 75 |
+
sdo_size= [256,512,1024,2048,4096]
|
| 76 |
+
month_dict={"01":31,"02":28,"03":31,"04":30,"05":31,"06":30,"07":31,"08":31,"09":30,"10":31,"11":30,"12":31}
|
| 77 |
+
html = """
|
| 78 |
+
<div>
|
| 79 |
+
PAGE_LINK
|
| 80 |
+
</div>
|
| 81 |
+
"""
|
| 82 |
+
css="""
|
| 83 |
+
.img_box{
|
| 84 |
+
display: flex;
|
| 85 |
+
flex-direction: column;
|
| 86 |
+
flex-flow: unset;
|
| 87 |
+
flex-wrap: wrap;
|
| 88 |
+
justify-content: space-around;
|
| 89 |
+
}
|
| 90 |
+
.img_class{
|
| 91 |
+
background: #ffffff;
|
| 92 |
+
max-width: 48%;
|
| 93 |
+
font-family: monospace;
|
| 94 |
+
border-top: #9300ff;
|
| 95 |
+
border-style: inset;
|
| 96 |
+
margin-top: 5px;
|
| 97 |
+
}
|
| 98 |
+
.img_class_raw{
|
| 99 |
+
background: #ffffff;
|
| 100 |
+
width: 100%;
|
| 101 |
+
font-family: monospace;
|
| 102 |
+
border-top: #9300ff;
|
| 103 |
+
border-style: inset;
|
| 104 |
+
margin-top: 5px;
|
| 105 |
+
display:flex;
|
| 106 |
+
flex-direction:column;
|
| 107 |
+
}
|
| 108 |
+
.img_box_soho{
|
| 109 |
+
display: flex;
|
| 110 |
+
flex-direction: row;
|
| 111 |
+
flex-wrap: wrap;
|
| 112 |
+
justify-content: space-between;
|
| 113 |
+
}
|
| 114 |
+
.img_class_soho{
|
| 115 |
+
background: #ffffff;
|
| 116 |
+
font-family: monospace;
|
| 117 |
+
border-top: #9300ff;
|
| 118 |
+
border-style: inset;
|
| 119 |
+
margin-top: 5px;
|
| 120 |
+
max-width: 48%;
|
| 121 |
+
}
|
| 122 |
+
.img_class_sdo{
|
| 123 |
+
background: #ffffff;
|
| 124 |
+
font-family: monospace;
|
| 125 |
+
border-top: #9300ff;
|
| 126 |
+
border-style: inset;
|
| 127 |
+
margin-top: 5px;
|
| 128 |
+
max-width: 25%;
|
| 129 |
+
font-size: small;
|
| 130 |
+
}
|
| 131 |
+
"""
|
| 132 |
+
def load_json(url1="",url2="",url3="",url4="",url5="",url6="",url7="",url8=""):
|
| 133 |
+
get_url=f'{main_directory}{url1}{url2}{url3}{url4}{url5}{url6}{url7}{url8}'
|
| 134 |
+
get_url=get_url.split("None")[0]
|
| 135 |
+
get_url=get_url.split("[]")[0]
|
| 136 |
+
|
| 137 |
+
if get_url.endswith('.json'):
|
| 138 |
+
feed1 = requests.get(get_url)
|
| 139 |
+
return None, feed1.text
|
| 140 |
+
elif get_url.endswith(".png") or get_url.endswith(".gif") or get_url.endswith(".jpg"):
|
| 141 |
+
html_out=f"<style>{css}</style><div>"
|
| 142 |
+
html_out+=f'<div class="img_class_raw"><a href="{get_url}" target="_blank">{get_url}</a><br><img src={get_url}></div></div>'
|
| 143 |
+
return html_out, None
|
| 144 |
+
return None,None
|
| 145 |
+
|
| 146 |
+
def make_tree(url1="",url2="",url3="",url4="",url5="",url6="",url7="",url8=""):
|
| 147 |
+
link_box=[]
|
| 148 |
+
html_out=""
|
| 149 |
+
get_url=f'{main_directory}{url1}{url2}{url3}{url4}{url5}{url6}{url7}{url8}'
|
| 150 |
+
if not get_url.endswith('.json'):
|
| 151 |
+
feed1 = requests.get(get_url)
|
| 152 |
+
spl = feed1.text.split("href=")
|
| 153 |
+
for line in spl:
|
| 154 |
+
spl2 = line.split(">")[0]
|
| 155 |
+
if spl2.endswith('/"') or spl2.endswith('.json"') or spl2.endswith('.png"') or spl2.endswith('.gif"') or spl2.endswith('.jpg"'):
|
| 156 |
+
fin=line.split(">")[0].strip('""')
|
| 157 |
+
link_box.append(fin)
|
| 158 |
+
return gr.update(choices=[l for l in link_box],interactive=True)
|
| 159 |
+
else:
|
| 160 |
+
return None
|
| 161 |
+
|
| 162 |
+
def get_images():
|
| 163 |
+
html_out=f"<style>{css}</style><div class='img_box'>"
|
| 164 |
+
get_url=f'{main_directory}images/geospace/'
|
| 165 |
+
feed1 = requests.get(get_url)
|
| 166 |
+
spl = feed1.text.split("href=")
|
| 167 |
+
for line in spl:
|
| 168 |
+
spl2 = line.split(">")[0].strip('""')
|
| 169 |
+
if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
|
| 170 |
+
html_out+=f'<div class="img_class"><a href="{get_url}{spl2}" target="_blank">{spl2}</a><br><img src={get_url}{spl2}></div>'
|
| 171 |
+
get_url2=f'{main_directory}images/'
|
| 172 |
+
feed2 = requests.get(get_url2)
|
| 173 |
+
spl = feed2.text.split("href=")
|
| 174 |
+
for line2 in spl:
|
| 175 |
+
spl2 = line2.split(">")[0].strip('""')
|
| 176 |
+
if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
|
| 177 |
+
html_out+=f'<div class="img_class"><a href="{get_url2}{spl2}" target="_blank">{spl2}</a><br><img src={get_url2}{spl2}></div>'
|
| 178 |
+
html_out+="</div>"
|
| 179 |
+
return html_out
|
| 180 |
+
|
| 181 |
+
def make_animation_new(gif_box=[], fps=10, loop=True):
|
| 182 |
+
if gif_box:
|
| 183 |
+
gif_box.sort(reverse=False)
|
| 184 |
+
print("Making GIF...")
|
| 185 |
+
try:
|
| 186 |
+
frames = []
|
| 187 |
+
for i,ea in enumerate(gif_box):
|
| 188 |
+
urllib.request.urlretrieve(ea,f'tmp{i}.png')
|
| 189 |
+
frames.append(f'tmp{i}.png')
|
| 190 |
+
|
| 191 |
+
uid=uuid.uuid4()
|
| 192 |
+
clip = ic.ImageSequenceClip(frames, fps=fps)
|
| 193 |
+
|
| 194 |
+
temp_gif_path = f"temp_{uid}.gif"
|
| 195 |
+
loop_val = 0 if loop else 1
|
| 196 |
+
clip.write_gif(temp_gif_path, fps=fps, loop=loop_val)
|
| 197 |
+
|
| 198 |
+
with open(temp_gif_path, 'rb') as gif_file:
|
| 199 |
+
gif_bytes = gif_file.read()
|
| 200 |
+
|
| 201 |
+
gif_base64 = base64.b64encode(gif_bytes).decode('utf-8')
|
| 202 |
+
gif_data_url = f"data:image/gif;base64,{gif_base64}"
|
| 203 |
+
|
| 204 |
+
html_out=f"<style>{css}</style><div>"
|
| 205 |
+
html_out+=f'<div class="img_class"><img src="{gif_data_url}"></div>'
|
| 206 |
+
html_out+="</div>"
|
| 207 |
+
os.remove(temp_gif_path)
|
| 208 |
+
for frame_file in frames:
|
| 209 |
+
os.remove(frame_file)
|
| 210 |
+
return html_out
|
| 211 |
+
|
| 212 |
+
except Exception as e:
|
| 213 |
+
print(f"ERROR HAPPENED: {e}")
|
| 214 |
+
return f"<div>An error occurred: {e}</div>"
|
| 215 |
+
else:
|
| 216 |
+
return "<div>No images were provided to create an animation.</div>"
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
def make_html(inp_files):
|
| 220 |
+
html_out=f"<style>{css}</style><div class='img_box'>"
|
| 221 |
+
for ea in inp_files:
|
| 222 |
+
html_out+=f'<div class="img_class"><img src="file={ea}"></div>'
|
| 223 |
+
html_out+='</div>'
|
| 224 |
+
return html_out
|
| 225 |
+
|
| 226 |
+
def make_nasa_soho_videos():
|
| 227 |
+
html_out=f"<style>{css}</style><div class='img_box_soho'>"
|
| 228 |
+
for ea in nasa_soho_mp4:
|
| 229 |
+
file_name=ea['source']
|
| 230 |
+
html_out+=f'''
|
| 231 |
+
<video width="50%" height="320" controls>
|
| 232 |
+
<source src="{file_name}" type="video/mp4">
|
| 233 |
+
Your browser does not support the video tag.
|
| 234 |
+
</video>'''
|
| 235 |
+
html_out+='</div>'
|
| 236 |
+
return html_out
|
| 237 |
+
|
| 238 |
+
def make_nasa_soho_images(inp_src: list):
|
| 239 |
+
html_out=""
|
| 240 |
+
for ea in inp_src:
|
| 241 |
+
file_name=ea['source']
|
| 242 |
+
html_out+=f'<div class="img_class_soho"><img src="{file_name}"></div>'
|
| 243 |
+
return html_out
|
| 244 |
+
|
| 245 |
+
def make_nasa_soho_image_trigger():
|
| 246 |
+
html_in=f"<style>{css}</style><div class='img_box_soho'>"
|
| 247 |
+
html_in+= make_nasa_soho_images(nasa_images)
|
| 248 |
+
html_in+= make_nasa_soho_images(sdo_aia_latest)
|
| 249 |
+
html_in+="</div>"
|
| 250 |
+
return html_in
|
| 251 |
+
|
| 252 |
+
def generate_sdo_urls(date1_str: str, date2_str: str) -> list[str]:
|
| 253 |
+
url_box = []
|
| 254 |
+
base_url = "https://sdo.gsfc.nasa.gov/assets/img/browse"
|
| 255 |
+
try:
|
| 256 |
+
start_date = datetime.datetime.strptime(date1_str[:8], "%Y%m%d").date()
|
| 257 |
+
end_date = datetime.datetime.strptime(date2_str[:8], "%Y%m%d").date()
|
| 258 |
+
except ValueError:
|
| 259 |
+
return []
|
| 260 |
+
if start_date > end_date:
|
| 261 |
+
return []
|
| 262 |
+
current_date = start_date
|
| 263 |
+
while current_date <= end_date:
|
| 264 |
+
year_str = f"{current_date.year:04}"
|
| 265 |
+
month_str = f"{current_date.month:02}"
|
| 266 |
+
day_str = f"{current_date.day:02}"
|
| 267 |
+
out_url = f"{base_url}/{year_str}/{month_str}/{day_str}/"
|
| 268 |
+
url_box.append(out_url)
|
| 269 |
+
current_date += datetime.timedelta(days=1)
|
| 270 |
+
return url_box
|
| 271 |
+
|
| 272 |
+
def nasa_sdo_images(obj,size,date1,date2):
|
| 273 |
+
html_in=f"<style>{css}</style><div class='img_box_soho'>"
|
| 274 |
+
sdo_urls = generate_sdo_urls(date1, date2)
|
| 275 |
+
|
| 276 |
+
if not sdo_urls:
|
| 277 |
+
return "<div>Invalid date range or format. Please ensure the start date is not after the end date.</div>", []
|
| 278 |
+
|
| 279 |
+
out_box=[]
|
| 280 |
+
start_day_str = date1[:8]
|
| 281 |
+
end_day_str = date2[:8]
|
| 282 |
+
start_time_str = date1[8:12]
|
| 283 |
+
end_time_str = date2[8:12]
|
| 284 |
+
|
| 285 |
+
for link in sdo_urls:
|
| 286 |
+
current_day_str = "".join(link.split('/')[-4:-1])
|
| 287 |
+
is_first_day = (current_day_str == start_day_str)
|
| 288 |
+
is_last_day = (current_day_str == end_day_str)
|
| 289 |
+
|
| 290 |
+
try:
|
| 291 |
+
feed1 = requests.get(link)
|
| 292 |
+
feed1.raise_for_status()
|
| 293 |
+
except requests.exceptions.RequestException as e:
|
| 294 |
+
print(f"Could not fetch {link}: {e}")
|
| 295 |
+
continue
|
| 296 |
+
|
| 297 |
+
if "All" in size:
|
| 298 |
+
size_list = sdo_size
|
| 299 |
+
else:
|
| 300 |
+
size_list = size
|
| 301 |
+
if "All" in obj:
|
| 302 |
+
obj_list = sdo_obj
|
| 303 |
+
else:
|
| 304 |
+
obj_list = obj
|
| 305 |
+
|
| 306 |
+
soup = bs4.BeautifulSoup(feed1.content, 'html.parser')
|
| 307 |
+
anchor_elements = soup.find_all('a')
|
| 308 |
+
|
| 309 |
+
for element in anchor_elements:
|
| 310 |
+
href=element.get('href')
|
| 311 |
+
if href and href.endswith('.jpg'):
|
| 312 |
+
parts = href.split('_')
|
| 313 |
+
if len(parts) < 4:
|
| 314 |
+
continue
|
| 315 |
+
|
| 316 |
+
image_time_str = parts[1]
|
| 317 |
+
image_size_str = parts[2]
|
| 318 |
+
image_obj_str = parts[3].split('.jpg')[0]
|
| 319 |
+
|
| 320 |
+
time_match = False
|
| 321 |
+
if is_first_day and is_last_day:
|
| 322 |
+
if start_time_str <= image_time_str <= end_time_str:
|
| 323 |
+
time_match = True
|
| 324 |
+
elif is_first_day:
|
| 325 |
+
if image_time_str >= start_time_str:
|
| 326 |
+
time_match = True
|
| 327 |
+
elif is_last_day:
|
| 328 |
+
if image_time_str <= end_time_str:
|
| 329 |
+
time_match = True
|
| 330 |
+
else: # Middle day
|
| 331 |
+
time_match = True
|
| 332 |
+
|
| 333 |
+
if time_match:
|
| 334 |
+
if image_obj_str in obj_list and int(image_size_str) in size_list:
|
| 335 |
+
full_url = f'{link}{href}'
|
| 336 |
+
out_box.append(full_url)
|
| 337 |
+
html_in+=f'<div class="img_class_sdo"><a href="{full_url}" target="_blank">{href}</a><br><img src={full_url}></div>'
|
| 338 |
+
html_in += "</div>"
|
| 339 |
+
if not out_box:
|
| 340 |
+
html_in = "<div>No images found for the specified criteria.</div>"
|
| 341 |
+
return html_in, out_box
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
def get_concat_h_cut(in1, in2):
|
| 345 |
+
uid=uuid.uuid4()
|
| 346 |
+
im1=Image.open(in1)
|
| 347 |
+
im2=Image.open(in2)
|
| 348 |
+
dst = Image.new('RGB', (im1.width + im2.width, min(im1.height, im2.height)))
|
| 349 |
+
dst.paste(im1, (0, 0))
|
| 350 |
+
dst.paste(im2, (im1.width, 0))
|
| 351 |
+
dst.save(f"h_{uid}.jpg")
|
| 352 |
+
return f"h_{uid}.jpg"
|
| 353 |
+
|
| 354 |
+
def get_concat_v_cut(in1, in2, theme='dark'):
|
| 355 |
+
uid=uuid.uuid4()
|
| 356 |
+
im1=Image.open(in1)
|
| 357 |
+
im2=Image.open(in2)
|
| 358 |
+
if theme=='dark':
|
| 359 |
+
color=(31,41,55)
|
| 360 |
+
if theme=='light':
|
| 361 |
+
color=(255,255,255)
|
| 362 |
+
dst = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height),color=color)
|
| 363 |
+
dst.paste(im1, (0, 0))
|
| 364 |
+
dst.paste(im2, (0, im1.height))
|
| 365 |
+
dst.save(f"v_{uid}.jpg")
|
| 366 |
+
return f"v_{uid}.jpg"
|
| 367 |
+
|
| 368 |
+
def nasa_sdo_composite(obj,size,date1,date2,ret_num):
|
| 369 |
+
html_in=f"<style>{css}</style><div class='img_box_soho'>"
|
| 370 |
+
in_year=f"{date2[0:4]}"
|
| 371 |
+
in_month=f"{date2[4:6]}"
|
| 372 |
+
in_day=f"{date2[6:8]}"
|
| 373 |
+
sdo_source_format = f"https://sdo.gsfc.nasa.gov/assets/img/browse/{in_year}/{in_month}/{in_day}/"
|
| 374 |
+
get_url=sdo_source_format
|
| 375 |
+
feed1 = requests.get(get_url)
|
| 376 |
+
if "All" in size:
|
| 377 |
+
size = sdo_size
|
| 378 |
+
if "All" in obj:
|
| 379 |
+
obj = sdo_obj
|
| 380 |
+
link_box=[]
|
| 381 |
+
soup = bs4.BeautifulSoup(feed1.content, 'html.parser')
|
| 382 |
+
anchor_elements = soup.find_all('a')
|
| 383 |
+
cnt=1
|
| 384 |
+
max_cnt=ret_num
|
| 385 |
+
for element in anchor_elements:
|
| 386 |
+
href=element.get('href')
|
| 387 |
+
if href.endswith('.jpg'):
|
| 388 |
+
for o in obj:
|
| 389 |
+
for s in size:
|
| 390 |
+
ls=href.split("_")
|
| 391 |
+
if ls[1][0:4]>=date1[8:13]:
|
| 392 |
+
src_obj=ls[3].split('.jpg')[0]
|
| 393 |
+
if src_obj == o:
|
| 394 |
+
if int(ls[2]) == int(s):
|
| 395 |
+
link_box.append(href)
|
| 396 |
+
link_box.sort(reverse=True)
|
| 397 |
+
out_box=[]
|
| 398 |
+
for ea in link_box:
|
| 399 |
+
if cnt<=max_cnt:
|
| 400 |
+
out_link=f'{sdo_source_format}{ea}'
|
| 401 |
+
out_box.append(out_link)
|
| 402 |
+
cnt+=1
|
| 403 |
+
html_in+=f'<div class="img_class_sdo"><a href="{sdo_source_format}{ea}" target="_blank">{ea}</a><br><img src={sdo_source_format}{ea}></div>'
|
| 404 |
+
html_in+="</div>"
|
| 405 |
+
return html_in, out_box
|
| 406 |
+
|
| 407 |
+
def run():
|
| 408 |
+
out=make_tree()
|
| 409 |
+
im_html=get_images()
|
| 410 |
+
return im_html
|
| 411 |
+
|
| 412 |
+
def get_date(year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2):
|
| 413 |
+
try:
|
| 414 |
+
start_dt = datetime.datetime(int(year1), int(month1), int(day1), int(hour1), int(minute1))
|
| 415 |
+
end_dt = datetime.datetime(int(year2), int(month2), int(day2), int(hour2), int(minute2))
|
| 416 |
+
date1 = start_dt.strftime('%Y%m%d%H%M')
|
| 417 |
+
date2 = end_dt.strftime('%Y%m%d%H%M')
|
| 418 |
+
return date1, date2
|
| 419 |
+
except ValueError as e:
|
| 420 |
+
print(f"Error creating date: {e}")
|
| 421 |
+
return None, None
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
def comp_combine(inp_ims: list,comp_col,comp_row,resize=False):
|
| 425 |
+
im_box=[]
|
| 426 |
+
v_box=[]
|
| 427 |
+
rows=comp_row
|
| 428 |
+
cols=comp_col
|
| 429 |
+
if not type(inp_ims)==type([]):
|
| 430 |
+
try:
|
| 431 |
+
inp_ims=eval(inp_ims)
|
| 432 |
+
except:
|
| 433 |
+
return None
|
| 434 |
+
if not inp_ims:
|
| 435 |
+
return None
|
| 436 |
+
|
| 437 |
+
for i,im in enumerate(inp_ims):
|
| 438 |
+
urllib.request.urlretrieve(im, f"tmp-{i}.jpg")
|
| 439 |
+
if resize:
|
| 440 |
+
pil_im = Image.open(f'tmp-{i}.jpg')
|
| 441 |
+
pil_im = pil_im.resize((int(resize[0]),int(resize[1])),resample=Image.Resampling.LANCZOS).convert('RGB')
|
| 442 |
+
pil_im.save(f'tmp-{i}.jpg')
|
| 443 |
+
im_box.append(f"tmp-{i}.jpg")
|
| 444 |
+
im_cnt=len(im_box)
|
| 445 |
+
cnt=1
|
| 446 |
+
for rr in range(rows):
|
| 447 |
+
if cnt > im_cnt: break
|
| 448 |
+
col_start=[im_box[cnt-1]]
|
| 449 |
+
for cc in range(cols-1):
|
| 450 |
+
try:
|
| 451 |
+
if cnt >= im_cnt: break
|
| 452 |
+
col_out=get_concat_h_cut(col_start[0],im_box[cnt])
|
| 453 |
+
col_start[0]=col_out
|
| 454 |
+
cnt+=1
|
| 455 |
+
except Exception as e:
|
| 456 |
+
pass
|
| 457 |
+
v_box.append(col_start[0])
|
| 458 |
+
if not v_box:
|
| 459 |
+
return None
|
| 460 |
+
|
| 461 |
+
v_cnt=1
|
| 462 |
+
v_box_cnt=len(v_box)
|
| 463 |
+
v_start=[v_box[0]]
|
| 464 |
+
for vv in range(v_box_cnt-1):
|
| 465 |
+
v_out=get_concat_v_cut(v_start[0],v_box[v_cnt])
|
| 466 |
+
v_start[0]=v_out
|
| 467 |
+
v_cnt+=1
|
| 468 |
+
return v_start[0]
|
| 469 |
+
|
| 470 |
+
def comp_1(comp_list=comp_list,col=3,row=4,resize=[640,450]):
|
| 471 |
+
outp=comp_combine(comp_list,col,row,resize)
|
| 472 |
+
return (outp)
|
| 473 |
+
|
| 474 |
+
def get_time():
|
| 475 |
+
now = datetime.datetime.utcnow()
|
| 476 |
+
datetimestamp_str = now.strftime('%Y-%m-%d %H:%M:%S')
|
| 477 |
+
html_out = f"""<div style='font-size:xxx-large;'>Current: {datetimestamp_str} UTC</div>"""
|
| 478 |
+
return (gr.update(value=html_out), now.year, now.month, now.day, now.hour, now.minute,
|
| 479 |
+
now.year, now.month, now.day, now.hour, now.minute)
|
| 480 |
+
|
| 481 |
+
with gr.Blocks() as app:
|
| 482 |
+
url_list=gr.State([])
|
| 483 |
+
now = datetime.datetime.utcnow()
|
| 484 |
+
|
| 485 |
+
time_html=gr.HTML()
|
| 486 |
+
with gr.Tab("NOAA"):
|
| 487 |
+
with gr.Tab("Compilation"):
|
| 488 |
+
first_comp_btn=gr.Button("Load")
|
| 489 |
+
first_comp_img=gr.Image()
|
| 490 |
+
with gr.Tab("Images"):
|
| 491 |
+
first_btn=gr.Button("Load")
|
| 492 |
+
html_im=gr.HTML()
|
| 493 |
+
with gr.Tab("Raw"):
|
| 494 |
+
with gr.Row():
|
| 495 |
+
drop1=gr.Dropdown()
|
| 496 |
+
drop2=gr.Dropdown()
|
| 497 |
+
drop3=gr.Dropdown()
|
| 498 |
+
drop4=gr.Dropdown()
|
| 499 |
+
with gr.Row():
|
| 500 |
+
drop5=gr.Dropdown()
|
| 501 |
+
drop6=gr.Dropdown()
|
| 502 |
+
drop7=gr.Dropdown()
|
| 503 |
+
drop8=gr.Dropdown()
|
| 504 |
+
load_btn=gr.Button("Load")
|
| 505 |
+
html_raw=gr.HTML()
|
| 506 |
+
links=gr.JSON()
|
| 507 |
+
with gr.Tab("NASA"):
|
| 508 |
+
with gr.Tab("SDO Images"):
|
| 509 |
+
with gr.Row():
|
| 510 |
+
gr.Markdown("<h5 style='font-size:xx-large;'>From:")
|
| 511 |
+
year1=gr.Number(label="Year",minimum=2010,maximum=now.year,precision=0,value=now.year,min_width=10)
|
| 512 |
+
month1=gr.Number(label="Month",minimum=1,maximum=12,precision=0,value=now.month,min_width=10)
|
| 513 |
+
day1=gr.Number(label="Day",minimum=1,maximum=31,precision=0,value=now.day,min_width=10)
|
| 514 |
+
hour1=gr.Number(label="Hour",minimum=0,maximum=23,precision=0,value=now.hour,min_width=10)
|
| 515 |
+
minute1=gr.Number(label="Minute",minimum=0,maximum=59,precision=0,value=now.minute,min_width=10)
|
| 516 |
+
gr.Markdown("<h5 style='font-size:xx-large;'>To:")
|
| 517 |
+
year2=gr.Number(label="Year",minimum=2010,maximum=now.year,precision=0,value=now.year,min_width=10)
|
| 518 |
+
month2=gr.Number(label="Month",minimum=1,maximum=12,precision=0,value=now.month,min_width=10)
|
| 519 |
+
day2=gr.Number(label="Day",minimum=1,maximum=31,precision=0,value=now.day,min_width=10)
|
| 520 |
+
hour2=gr.Number(label="Hour",minimum=0,maximum=23,precision=0,value=now.hour,min_width=10)
|
| 521 |
+
minute2=gr.Number(label="Minute",minimum=0,maximum=59,precision=0,value=now.minute,min_width=10)
|
| 522 |
+
|
| 523 |
+
with gr.Row():
|
| 524 |
+
sdo_drop1=gr.Dropdown(label="Instrument", multiselect=True, choices=["All"]+sdo_obj, value="0094")
|
| 525 |
+
sdo_drop2=gr.Dropdown(label="Size", multiselect=True, choices=["All"]+sdo_size,value=512)
|
| 526 |
+
|
| 527 |
+
with gr.Tab("Composite"):
|
| 528 |
+
with gr.Row():
|
| 529 |
+
comp_num=gr.Number(label="Return",precision=0,value=4,interactive=True)
|
| 530 |
+
comp_col=gr.Number(label="Columns",precision=0,value=2,interactive=True)
|
| 531 |
+
comp_row=gr.Number(label="Rows",precision=0,value=2,interactive=True)
|
| 532 |
+
with gr.Row():
|
| 533 |
+
comp_combine_btn=gr.Button("Combine")
|
| 534 |
+
sdo_comp_btn=gr.Button("Load")
|
| 535 |
+
img_out=gr.Image()
|
| 536 |
+
comp_combine_html=gr.HTML()
|
| 537 |
+
sdo_comp_html=gr.HTML()
|
| 538 |
+
image_comp_list=gr.Textbox(visible=True)
|
| 539 |
+
with gr.Tab("Images"):
|
| 540 |
+
sdo_btn=gr.Button("Load")
|
| 541 |
+
with gr.Row():
|
| 542 |
+
fps_slider = gr.Slider(minimum=1, maximum=60, value=10, step=1, label="Animation FPS")
|
| 543 |
+
loop_checkbox = gr.Checkbox(label="Loop Animation", value=True)
|
| 544 |
+
sdo_gif=gr.Button("Make GIF")
|
| 545 |
+
sdo_gif_html=gr.HTML()
|
| 546 |
+
sdo_gif.click(make_animation_new,[url_list, fps_slider, loop_checkbox],sdo_gif_html)
|
| 547 |
+
sdo_html=gr.HTML()
|
| 548 |
+
|
| 549 |
+
date1=gr.Textbox(visible=False)
|
| 550 |
+
date2=gr.Textbox(visible=False)
|
| 551 |
+
with gr.Tab("SOHO Images"):
|
| 552 |
+
soho_btn=gr.Button("Load")
|
| 553 |
+
html_nasa=gr.HTML()
|
| 554 |
+
with gr.Tab("SOHO Video"):
|
| 555 |
+
soho_vid_btn=gr.Button("Load")
|
| 556 |
+
html_vid_nasa=gr.HTML()
|
| 557 |
+
|
| 558 |
+
app.load(get_time,None,[time_html,year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2]).then(make_tree,None,drop1)
|
| 559 |
+
|
| 560 |
+
first_comp_btn.click(comp_1,None,first_comp_img)
|
| 561 |
+
comp_combine_btn.click(comp_combine,[image_comp_list,comp_col,comp_row],img_out)
|
| 562 |
+
sdo_comp_btn.click(get_date,[year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2],[date1,date2]).then(nasa_sdo_composite,[sdo_drop1,sdo_drop2,date1,date2,comp_num],[sdo_comp_html,image_comp_list])
|
| 563 |
+
|
| 564 |
+
sdo_btn.click(get_date,[year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2],[date1,date2]).then(nasa_sdo_images,[sdo_drop1,sdo_drop2,date1,date2],[sdo_html,url_list])
|
| 565 |
+
soho_btn.click(make_nasa_soho_image_trigger,None,html_nasa)
|
| 566 |
+
soho_vid_btn.click(make_nasa_soho_videos,None,html_vid_nasa)
|
| 567 |
+
load_btn.click(load_json,[drop1,drop2,drop3,drop4,drop5,drop6,drop7,drop8],[html_raw,links])
|
| 568 |
+
|
| 569 |
+
drop1.change(make_tree,drop1,[drop2])
|
| 570 |
+
drop2.change(make_tree,[drop1,drop2],[drop3])
|
| 571 |
+
drop3.change(make_tree,[drop1,drop2,drop3],[drop4])
|
| 572 |
+
drop4.change(make_tree,[drop1,drop2,drop3,drop4],[drop5])
|
| 573 |
+
drop5.change(make_tree,[drop1,drop2,drop3,drop4,drop5],[drop6])
|
| 574 |
+
drop6.change(make_tree,[drop1,drop2,drop3,drop4,drop5,drop6],[drop7])
|
| 575 |
+
drop7.change(make_tree,[drop1,drop2,drop3,drop4,drop5,drop6,drop7],[drop8])
|
| 576 |
+
|
| 577 |
+
first_btn.click(run,None,[html_im])
|
| 578 |
+
|
| 579 |
+
app.queue(default_concurrency_limit=10).launch()```
|