Update app.py
Browse files
app.py
CHANGED
|
@@ -73,14 +73,13 @@ sdo_obj = ["HMIB","HMIBC","HMIIC","HMIIF","HMID","HMII","HMI171",
|
|
| 73 |
"211193171","211193171n","211193171rg",
|
| 74 |
"094335193","304211171"]
|
| 75 |
sdo_size= [256,512,1024,2048,4096]
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
css="""
|
| 78 |
-
.gallery-container {
|
| 79 |
-
max-height: 80vh;
|
| 80 |
-
overflow-y: auto;
|
| 81 |
-
border: 1px solid #e0e0e0;
|
| 82 |
-
padding: 10px;
|
| 83 |
-
}
|
| 84 |
.img_box{
|
| 85 |
display: flex;
|
| 86 |
flex-direction: column;
|
|
@@ -139,7 +138,8 @@ def load_json(url1="",url2="",url3="",url4="",url5="",url6="",url7="",url8=""):
|
|
| 139 |
feed1 = requests.get(get_url)
|
| 140 |
return None, feed1.text
|
| 141 |
elif get_url.endswith(".png") or get_url.endswith(".gif") or get_url.endswith(".jpg"):
|
| 142 |
-
html_out=f
|
|
|
|
| 143 |
return html_out, None
|
| 144 |
return None,None
|
| 145 |
|
|
@@ -160,7 +160,7 @@ def make_tree(url1="",url2="",url3="",url4="",url5="",url6="",url7="",url8=""):
|
|
| 160 |
return None
|
| 161 |
|
| 162 |
def get_images():
|
| 163 |
-
html_out="<div class='img_box'>"
|
| 164 |
get_url=f'{main_directory}images/geospace/'
|
| 165 |
feed1 = requests.get(get_url)
|
| 166 |
spl = feed1.text.split("href=")
|
|
@@ -176,11 +176,12 @@ def get_images():
|
|
| 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
|
| 180 |
|
| 181 |
def make_animation_new(gif_box=[], fps=10, loop=True):
|
| 182 |
if gif_box:
|
| 183 |
gif_box.sort(reverse=False)
|
|
|
|
| 184 |
try:
|
| 185 |
frames = []
|
| 186 |
for i,ea in enumerate(gif_box):
|
|
@@ -200,19 +201,30 @@ def make_animation_new(gif_box=[], fps=10, loop=True):
|
|
| 200 |
gif_base64 = base64.b64encode(gif_bytes).decode('utf-8')
|
| 201 |
gif_data_url = f"data:image/gif;base64,{gif_base64}"
|
| 202 |
|
| 203 |
-
html_out=f
|
|
|
|
|
|
|
| 204 |
os.remove(temp_gif_path)
|
| 205 |
for frame_file in frames:
|
| 206 |
os.remove(frame_file)
|
| 207 |
-
return
|
| 208 |
|
| 209 |
except Exception as e:
|
|
|
|
| 210 |
return f"<div>An error occurred: {e}</div>"
|
| 211 |
else:
|
| 212 |
return "<div>No images were provided to create an animation.</div>"
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
def make_nasa_soho_videos():
|
| 215 |
-
html_out="<div class='img_box_soho'>"
|
| 216 |
for ea in nasa_soho_mp4:
|
| 217 |
file_name=ea['source']
|
| 218 |
html_out+=f'''
|
|
@@ -221,7 +233,7 @@ def make_nasa_soho_videos():
|
|
| 221 |
Your browser does not support the video tag.
|
| 222 |
</video>'''
|
| 223 |
html_out+='</div>'
|
| 224 |
-
return
|
| 225 |
|
| 226 |
def make_nasa_soho_images(inp_src: list):
|
| 227 |
html_out=""
|
|
@@ -231,11 +243,11 @@ def make_nasa_soho_images(inp_src: list):
|
|
| 231 |
return html_out
|
| 232 |
|
| 233 |
def make_nasa_soho_image_trigger():
|
| 234 |
-
html_in="<div class='img_box_soho'>"
|
| 235 |
html_in+= make_nasa_soho_images(nasa_images)
|
| 236 |
html_in+= make_nasa_soho_images(sdo_aia_latest)
|
| 237 |
html_in+="</div>"
|
| 238 |
-
return
|
| 239 |
|
| 240 |
def generate_sdo_urls(date1_str: str, date2_str: str) -> list[str]:
|
| 241 |
url_box = []
|
|
@@ -258,7 +270,7 @@ def generate_sdo_urls(date1_str: str, date2_str: str) -> list[str]:
|
|
| 258 |
return url_box
|
| 259 |
|
| 260 |
def nasa_sdo_images(obj,size,date1,date2):
|
| 261 |
-
html_in="<div class='img_box_soho'>"
|
| 262 |
sdo_urls = generate_sdo_urls(date1, date2)
|
| 263 |
|
| 264 |
if not sdo_urls:
|
|
@@ -279,45 +291,55 @@ def nasa_sdo_images(obj,size,date1,date2):
|
|
| 279 |
feed1 = requests.get(link)
|
| 280 |
feed1.raise_for_status()
|
| 281 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 282 |
continue
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
soup = bs4.BeautifulSoup(feed1.content, 'html.parser')
|
|
|
|
| 288 |
|
| 289 |
-
for element in
|
| 290 |
href=element.get('href')
|
| 291 |
if href and href.endswith('.jpg'):
|
| 292 |
parts = href.split('_')
|
| 293 |
if len(parts) < 4:
|
| 294 |
continue
|
| 295 |
|
| 296 |
-
image_time_str
|
|
|
|
|
|
|
| 297 |
|
| 298 |
time_match = False
|
| 299 |
if is_first_day and is_last_day:
|
| 300 |
-
if start_time_str <= image_time_str <= end_time_str:
|
|
|
|
| 301 |
elif is_first_day:
|
| 302 |
-
if image_time_str >= start_time_str:
|
|
|
|
| 303 |
elif is_last_day:
|
| 304 |
-
if image_time_str <= end_time_str:
|
| 305 |
-
|
|
|
|
| 306 |
time_match = True
|
| 307 |
|
| 308 |
-
if time_match
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
html_in += "</div>"
|
| 314 |
-
|
| 315 |
if not out_box:
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
return final_html, out_box
|
| 321 |
|
| 322 |
def get_concat_h_cut(in1, in2):
|
| 323 |
uid=uuid.uuid4()
|
|
@@ -333,7 +355,10 @@ def get_concat_v_cut(in1, in2, theme='dark'):
|
|
| 333 |
uid=uuid.uuid4()
|
| 334 |
im1=Image.open(in1)
|
| 335 |
im2=Image.open(in2)
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
| 337 |
dst = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height),color=color)
|
| 338 |
dst.paste(im1, (0, 0))
|
| 339 |
dst.paste(im2, (0, im1.height))
|
|
@@ -341,44 +366,46 @@ def get_concat_v_cut(in1, in2, theme='dark'):
|
|
| 341 |
return f"v_{uid}.jpg"
|
| 342 |
|
| 343 |
def nasa_sdo_composite(obj,size,date1,date2,ret_num):
|
| 344 |
-
html_in="<div class='img_box_soho'>"
|
| 345 |
-
in_year
|
|
|
|
|
|
|
| 346 |
sdo_source_format = f"https://sdo.gsfc.nasa.gov/assets/img/browse/{in_year}/{in_month}/{in_day}/"
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
size_list = sdo_size if "All" in size else size
|
| 355 |
-
obj_list = sdo_obj if "All" in obj else obj
|
| 356 |
-
|
| 357 |
link_box=[]
|
| 358 |
soup = bs4.BeautifulSoup(feed1.content, 'html.parser')
|
| 359 |
-
|
| 360 |
-
|
|
|
|
|
|
|
| 361 |
href=element.get('href')
|
| 362 |
-
if href
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
|
|
|
| 370 |
link_box.sort(reverse=True)
|
| 371 |
out_box=[]
|
| 372 |
-
for
|
| 373 |
-
if
|
| 374 |
out_link=f'{sdo_source_format}{ea}'
|
| 375 |
out_box.append(out_link)
|
| 376 |
-
|
| 377 |
-
|
| 378 |
html_in+="</div>"
|
| 379 |
-
return
|
| 380 |
|
| 381 |
def run():
|
|
|
|
| 382 |
im_html=get_images()
|
| 383 |
return im_html
|
| 384 |
|
|
@@ -390,48 +417,55 @@ def get_date(year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2):
|
|
| 390 |
date2 = end_dt.strftime('%Y%m%d%H%M')
|
| 391 |
return date1, date2
|
| 392 |
except ValueError as e:
|
|
|
|
| 393 |
return None, None
|
| 394 |
|
|
|
|
| 395 |
def comp_combine(inp_ims: list,comp_col,comp_row,resize=False):
|
| 396 |
im_box=[]
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
if not inp_ims:
|
| 401 |
-
|
| 402 |
-
for i,im_url in enumerate(inp_ims):
|
| 403 |
try:
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
for
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
def comp_1(comp_list=comp_list,col=3,row=4,resize=[640,450]):
|
| 437 |
outp=comp_combine(comp_list,col,row,resize)
|
|
@@ -444,7 +478,7 @@ def get_time():
|
|
| 444 |
return (gr.update(value=html_out), now.year, now.month, now.day, now.hour, now.minute,
|
| 445 |
now.year, now.month, now.day, now.hour, now.minute)
|
| 446 |
|
| 447 |
-
with gr.Blocks(
|
| 448 |
url_list=gr.State([])
|
| 449 |
now = datetime.datetime.utcnow()
|
| 450 |
|
|
@@ -473,18 +507,18 @@ with gr.Blocks(css=css) as app:
|
|
| 473 |
with gr.Tab("NASA"):
|
| 474 |
with gr.Tab("SDO Images"):
|
| 475 |
with gr.Row():
|
| 476 |
-
gr.Markdown("<h5>From:")
|
| 477 |
-
year1=gr.Number(label="Year",minimum=2010,maximum=now.year,precision=0,value=now.year)
|
| 478 |
-
month1=gr.Number(label="Month",minimum=1,maximum=12,precision=0,value=now.month)
|
| 479 |
-
day1=gr.Number(label="Day",minimum=1,maximum=31,precision=0,value=now.day)
|
| 480 |
-
hour1=gr.Number(label="Hour",minimum=0,maximum=23,precision=0,value=now.hour)
|
| 481 |
-
minute1=gr.Number(label="Minute",minimum=0,maximum=59,precision=0,value=now.minute)
|
| 482 |
-
gr.Markdown("<h5>To:")
|
| 483 |
-
year2=gr.Number(label="Year",minimum=2010,maximum=now.year,precision=0,value=now.year)
|
| 484 |
-
month2=gr.Number(label="Month",minimum=1,maximum=12,precision=0,value=now.month)
|
| 485 |
-
day2=gr.Number(label="Day",minimum=1,maximum=31,precision=0,value=now.day)
|
| 486 |
-
hour2=gr.Number(label="Hour",minimum=0,maximum=23,precision=0,value=now.hour)
|
| 487 |
-
minute2=gr.Number(label="Minute",minimum=0,maximum=59,precision=0,value=now.minute)
|
| 488 |
|
| 489 |
with gr.Row():
|
| 490 |
sdo_drop1=gr.Dropdown(label="Instrument", multiselect=True, choices=["All"]+sdo_obj, value="0094")
|
|
@@ -499,6 +533,7 @@ with gr.Blocks(css=css) as app:
|
|
| 499 |
comp_combine_btn=gr.Button("Combine")
|
| 500 |
sdo_comp_btn=gr.Button("Load")
|
| 501 |
img_out=gr.Image()
|
|
|
|
| 502 |
sdo_comp_html=gr.HTML()
|
| 503 |
image_comp_list=gr.Textbox(visible=True)
|
| 504 |
with gr.Tab("Images"):
|
|
@@ -508,6 +543,7 @@ with gr.Blocks(css=css) as app:
|
|
| 508 |
loop_checkbox = gr.Checkbox(label="Loop Animation", value=True)
|
| 509 |
sdo_gif=gr.Button("Make GIF")
|
| 510 |
sdo_gif_html=gr.HTML()
|
|
|
|
| 511 |
sdo_html=gr.HTML()
|
| 512 |
|
| 513 |
date1=gr.Textbox(visible=False)
|
|
@@ -521,7 +557,6 @@ with gr.Blocks(css=css) as app:
|
|
| 521 |
|
| 522 |
app.load(get_time,None,[time_html,year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2]).then(make_tree,None,drop1)
|
| 523 |
|
| 524 |
-
sdo_gif.click(make_animation_new,[url_list, fps_slider, loop_checkbox],sdo_gif_html)
|
| 525 |
first_comp_btn.click(comp_1,None,first_comp_img)
|
| 526 |
comp_combine_btn.click(comp_combine,[image_comp_list,comp_col,comp_row],img_out)
|
| 527 |
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])
|
|
@@ -541,4 +576,4 @@ with gr.Blocks(css=css) as app:
|
|
| 541 |
|
| 542 |
first_btn.click(run,None,[html_im])
|
| 543 |
|
| 544 |
-
app.queue(default_concurrency_limit=10).launch()
|
|
|
|
| 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;
|
|
|
|
| 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 |
|
|
|
|
| 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=")
|
|
|
|
| 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):
|
|
|
|
| 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'''
|
|
|
|
| 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=""
|
|
|
|
| 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 = []
|
|
|
|
| 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:
|
|
|
|
| 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()
|
|
|
|
| 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))
|
|
|
|
| 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 |
|
|
|
|
| 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)
|
|
|
|
| 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 |
|
|
|
|
| 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")
|
|
|
|
| 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"):
|
|
|
|
| 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)
|
|
|
|
| 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])
|
|
|
|
| 576 |
|
| 577 |
first_btn.click(run,None,[html_im])
|
| 578 |
|
| 579 |
+
app.queue(default_concurrency_limit=10).launch()```
|