File size: 12,872 Bytes
511891d
 
 
9ddd211
7748011
6b9f87a
dec627f
979fb77
d997d39
ce783e0
f2edad2
511891d
979fb77
 
368e63a
 
 
 
 
 
 
 
 
 
 
 
 
 
3263bf0
 
 
 
 
 
 
e75e7f7
64d19ba
 
 
 
 
e75e7f7
 
 
 
 
ee40bed
 
 
 
 
e75e7f7
64d19ba
 
 
6f2a024
 
0b73570
6f2a024
 
bd24d05
 
 
 
36788d8
bd24d05
3263bf0
 
bd24d05
 
49f5e2d
bd24d05
 
 
 
 
 
49f5e2d
 
 
 
 
 
 
4507214
 
49f5e2d
bf2d25c
 
 
 
e75e7f7
bf2d25c
 
 
 
 
 
 
9b5aa1e
bf2d25c
bd24d05
288aecc
 
5b2b36d
bd24d05
56000e1
 
 
bd24d05
c721296
7839cc0
bd24d05
 
 
49f5e2d
bd24d05
 
 
67949b6
25ec869
6f2a024
0d0db28
5b2b36d
c721296
7839cc0
 
 
 
 
e4e5afb
7839cc0
 
 
 
c721296
 
bd24d05
01f064c
3263bf0
7748011
01f064c
 
 
eea38a1
121c044
a582447
753b7e4
07eb494
 
7748011
 
 
 
 
 
 
753b7e4
7748011
 
01f064c
 
7748011
 
 
 
 
6330d91
7748011
6e5ae84
 
7748011
 
aea9da6
7748011
 
 
6def092
 
aea9da6
7748011
77b6da3
05a6079
 
 
 
 
 
 
67462d9
4246286
7fffb8e
6b9f87a
f7f6a9c
6330d91
05a6079
 
4246286
 
 
 
 
 
f2f8f1a
0171a9f
4246286
7748011
3263bf0
bf2d25c
e75e7f7
64d19ba
e75e7f7
5167993
e75e7f7
 
 
 
64d19ba
 
3263bf0
368e63a
7b97a9d
ffa0f93
368e63a
3263bf0
68174a3
ffa0f93
77cde0f
3263bf0
368e63a
ffa0f93
 
 
368e63a
68174a3
ffa0f93
368e63a
979fb77
5ed2d1f
 
 
 
 
979fb77
9af5251
 
 
979fb77
 
 
6c7b674
979fb77
 
 
 
f5d6bd9
7385d6f
979fb77
 
 
8d86a71
0307b0c
5ed2d1f
 
 
 
 
 
7385d6f
e25a33b
0b73570
cb3b6d4
01f064c
 
 
e8ad872
bb25cdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
979fb77
5ed2d1f
 
 
 
 
 
 
 
979fb77
16e3e48
68174a3
64d19ba
 
68174a3
 
 
01f064c
64d19ba
e25a33b
368e63a
68174a3
64d19ba
7748011
4246286
01f064c
288aecc
 
 
01f064c
 
a003977
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import gradio as gr
import requests
import json
import os
from PIL import Image
import moviepy.video.io.ImageSequenceClip as ic
from pathlib import Path
import bs4

import urllib.request 
import uuid
main_directory = "https://services.swpc.noaa.gov/"
sdo_source = "https://sdo.gsfc.nasa.gov/assets/img/browse/"
sdo_source_format = "https://sdo.gsfc.nasa.gov/assets/img/browse/YEAR/MONTH/DAY/DATE_IDENT_SIZE_TOOL.jpg"
sdo_aia_latest=[
    {"name":"Fe18 94A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_94.gif"},
    {"name":"Fe20 131A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_131.gif"},
    {"name":"Fe9/10 171A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_171.gif"},
    {"name":"Fe12 193A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_193.gif"},
    
    {"name":"Fe14 211A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_211.gif"},
    {"name":"He2 304A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_304.gif"},
    {"name":"Fe16 335A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_335.gif"},
    {"name":"cont+C4 1600A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_1600.gif"},
    {"name":"continuum 1700A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_1700.gif"},
    {"name":"continuum 4500A", "source":"https://umbra.nascom.nasa.gov/images/latest_aia_4500.gif"},
]

nasa_images=[
    {"name":"EIT 171A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_171/512/latest.jpg"},
    {"name":"EIT 195A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_195/512/latest.jpg"},
    {"name":"EIT 284A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_284/512/latest.jpg"},
    {"name":"EIT 304A", "source":"https://soho.nascom.nasa.gov/data/realtime/eit_304/512/latest.jpg"},
]

nasa_soho_gifs=[
    {"name":"EIT 304A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_304small.gif"},
    {"name":"EIT 195A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_195small.gif"},
    {"name":"EIT 171A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_171small.gif"},
    {"name":"EIT 284A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_284small.gif"},
]    
nasa_soho_mp4=[
    {"name":"EIT 171A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_171small.mp4"},
    {"name":"EIT 195A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_195small.mp4"},
    {"name":"EIT 284A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_284small.mp4"},
    {"name":"EIT 304A", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_eit_304small.mp4"},
    
    {"name":"LASCO C2", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_c2small.mp4"},
    {"name":"LASCO C3", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_c3small.mp4"},
    {"name":"SDO/HMI Continuum", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_hmi_igr-512.mp4"},
    {"name":"SDO/HMI Magnetogram", "source":"https://soho.nascom.nasa.gov/data/LATEST/current_hmi_mag-512.mp4"},
]  



html = """
<div>
PAGE_LINK
</div>
"""
css="""
.img_box{
    display: flex;
    flex-direction: column;
    flex-flow: unset;
    flex-wrap: wrap;
    justify-content: space-around;

}
.img_class{
    background: #ffffff;
    max-width: 48%;
    font-family: monospace;
    border-top: #9300ff;
    border-style: inset;
    margin-top: 5px;
}
.img_class_raw{
    background: #ffffff;
    width: 100%;
    font-family: monospace;
    border-top: #9300ff;
    border-style: inset;
    margin-top: 5px;
    display:flex;
    flex-direction:column;
}
.img_box_soho{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
}
.img_class_soho{
    background: #ffffff;
    font-family: monospace;
    border-top: #9300ff;
    border-style: inset;
    margin-top: 5px;
    max-width: 48%;
}
"""
def load_json(url1="",url2="",url3="",url4=""):
    get_url=f'{main_directory}{url1}{url2}{url3}{url4}'
    print(f'{get_url}')
    get_url=get_url.split("None")[0]
    print(f'{get_url}')
    get_url=get_url.split("[]")[0]
    print(f'{get_url}')
    
    if get_url.endswith('.json'):
        feed1 = requests.get(get_url)
        return None, feed1.text
    elif get_url.endswith(".png") or get_url.endswith(".gif") or get_url.endswith(".jpg"):
        html_out=f"<style>{css}</style><div>"
        html_out+=f'<div class="img_class_raw"><a href="{get_url}" target="_blank">{get_url}</a><br><img src={get_url}></div></div>'
        return html_out, None
    return None,None
    
def make_tree(url1="",url2="",url3="",url4=""):
    link_box=[]
    html_out=""
    get_url=f'{main_directory}{url1}{url2}{url3}{url4}'
    print(f'######### :: {get_url}')
    if not get_url.endswith('.json'):
        feed1 = requests.get(get_url)
        spl = feed1.text.split("href=")
        for line in spl:
            spl2 = line.split(">")[0]
            print(spl2)
            if spl2.endswith('/"') or spl2.endswith('.json"') or spl2.endswith('.png"') or spl2.endswith('.gif"') or spl2.endswith('.jpg"'):
                fin=line.split(">")[0].strip('""')
                link_box.append(fin)
                #html_out=html_out+html.replace("PAGE_LINK",fin)
        return gr.update(choices=[l for l in link_box],interactive=True)
    else:
        return None

def get_images():
    html_out=f"<style>{css}</style><div class='img_box'>"    
    get_url=f'{main_directory}images/geospace/'
    feed1 = requests.get(get_url)
    spl = feed1.text.split("href=")
    for line in spl:
        spl2 = line.split(">")[0].strip('""')
        if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
            print(spl2)
            html_out+=f'<div class="img_class"><a href="{get_url}{spl2}" target="_blank">{spl2}</a><br><img src={get_url}{spl2}></div>'
        else:
            print(spl2)
    get_url2=f'{main_directory}images/'
    feed2 = requests.get(get_url2)
    spl = feed2.text.split("href=")
    for line2 in spl:
        spl2 = line2.split(">")[0].strip('""')
        if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
            print(spl2)
            html_out+=f'<div class="img_class"><a href="{get_url2}{spl2}" target="_blank">{spl2}</a><br><img src={get_url2}{spl2}></div>'
        else:
            print(spl2)            
    html_out+="</div>"
    return html_out

def make_animation():
    get_url=f'{main_directory}images/animations/'
    feed1 = requests.get(get_url)
    spl = feed1.text.split("href=")
    files_out=[]
    for line in spl:
        gif_box=[]

        spl2 = line.split(">")[0].strip('""')
        if spl2.endswith("/"):
            feed2 = requests.get(f'{get_url}{spl2}')
            spl3 = feed2.text.split("href=")
            for line2 in spl3:
                spl3 = line2.split(">")[0].strip('""')
                print("$$$$$$$$$$$$$$")
                print(spl3)
                if spl3.endswith(".png") or spl3.endswith(".gif") or spl3.endswith(".jpg"):
                    gif_box.append(f'{get_url}{spl2}{spl3}')
            if gif_box:
                try:
                    #frames = [Image.open(image) for image in glob.glob(f"{frame_folder}/*.JPG")]
                    frames = []
                    for i,ea in enumerate(gif_box):
                        print("############")
                        print(ea)
                        urllib.request.urlretrieve(ea,f'tmp{i}.png')                     
                        frames.append(f'tmp{i}.png')
                    fps=60
                    uid=uuid.uuid4()
                    clip = ic.ImageSequenceClip(frames, fps = fps) 
                    clip.to_gif(f"{uid}.gif",fps=fps) 
                    files_out.append(f'{uid}.gif')
                except Exception as e:
                    print(e)
    return files_out

def make_html(inp_files):
    html_out=f"<style>{css}</style><div class='img_box'>"
    for ea in inp_files:
        print(ea)
        html_out+=f'<div class="img_class"><img src="file={ea}"></div>'
    html_out+='</div>'
    return html_out

def make_nasa_soho_videos():
    html_out=f"<style>{css}</style><div class='img_box_soho'>"
    for ea in nasa_soho_mp4:
        file_name=ea['source']
        html_out+=f'''
        <video width="50%" height="320" controls>
          <source src="{file_name}" type="video/mp4">
            Your browser does not support the video tag.
        </video>'''
        #html_out+=f'<div class="img_class_soho"><img src="file={file_name}"></div>'
    html_out+='</div>'
    return html_out
    
def make_nasa_soho_images(inp_src: list):
    html_out=""
    #html_out=f"<style>{css}</style><div class='img_box_soho'>"
    for ea in inp_src:
        file_name=ea['source']
        html_out+=f'<div class="img_class_soho"><img src="{file_name}"></div>'
    #html_out+='</div>'
    #html_out+=html_in
    return html_out    
def make_nasa_soho_image_trigger():
    html_in=f"<style>{css}</style><div class='img_box_soho'>"
    
    html_in+= make_nasa_soho_images(nasa_images)    
    html_in+= make_nasa_soho_images(sdo_aia_latest)     
    #html_in+= make_nasa_soho_videos()
    html_in+="</div>"
    return html_in

def nasa_sdo_images(size,obj):
    if size=="All":
        size1=9999
    else:
        size1=size
    html_out=""
    in_year="2024"
    in_month="03"
    in_day="24"
    file_name="DATE_IDENT_SIZE_TOOL.jpg"
    sdo_source_format = f"https://sdo.gsfc.nasa.gov/assets/img/browse/{in_year}/{in_month}/{in_day}/"
    get_url=sdo_source_format
    print(get_url)
    feed1 = requests.get(get_url)

    link_box=[]
    # Parse the HTML content using BeautifulSoup
    soup = bs4.BeautifulSoup(feed1.content, 'html.parser')
    #print(soup)
    anchor_elements = soup.find_all('a')
    for element in anchor_elements:
        href=element.get('href')
        if href.endswith('.jpg'):
            ls=href.split("_")
            src_obj=ls[3].split('.jpg')[0]
            if src_obj == obj or obj=="All":
                if int(ls[2]) == int(size1) or if size=="All":
                    link_json={'year':ls[0][0:4],'month':ls[0][4:6],'day':ls[0][6:8],'dump':ls[1],'size':ls[2],'obj':src_obj}
                    #print(href)
                    link_box.append(link_json)
        #html_out+=f'<div class="img_class"><a href="{get_url}{spl2}" target="_blank">{spl2}</a><br><img src={get_url}{spl2}></div>'
    return link_box
def run():
    out=make_tree()
    im_html=get_images()
    return out, im_html

with gr.Blocks() as app:
    with gr.Tab("NOAA"):
        with gr.Tab("Images"):
            html_im=gr.HTML()
        with gr.Tab("Animations"):
            anim_btn=gr.Button()
            anim_out=gr.HTML() 
            file_out=gr.Files()
        with gr.Tab("Raw"):
            with gr.Row():
                drop1=gr.Dropdown()
                drop2=gr.Dropdown()
                drop3=gr.Dropdown()
                drop4=gr.Dropdown()
            load_btn=gr.Button("Load")
            html_raw=gr.HTML()
            links=gr.JSON()
    with gr.Tab("NASA"):
        with gr.Tab("SDO Images"):
            with gr.Row():
                sdo_drop1=gr.Dropdown(label="Instrument", choices=["All","HMIB","HMIBC","HMIIC","HMIIF","HMID","HMII","HMI171",
                                                                   "0094","0131","0171","0193","0211",
                                                                   "0304","0335","1600","1700","4500",
                                                                   "211193171","211193171n","211193171rg",
                                                                   "094335193","304211171"], value="All")
                sdo_drop2=gr.Dropdown(label="Size", choices=[256,512,1024,2048,4096,"All"],value=512)
                
            sdo_btn=gr.Button("Load")
            sdo_json=gr.JSON()
        with gr.Tab("SOHO Images"):
            soho_btn=gr.Button("Load")
            html_nasa=gr.HTML()
        with gr.Tab("SOHO Video"):
            soho_vid_btn=gr.Button("Load")
            html_vid_nasa=gr.HTML()

    ###### NASA Images ##########
    sdo_btn.click(nasa_sdo_images,None,sdo_json)
    soho_btn.click(make_nasa_soho_image_trigger,None,html_nasa)
    soho_vid_btn.click(make_nasa_soho_videos,None,html_vid_nasa)
    ###### NOAA Images ##########
    load_btn.click(load_json,[drop1,drop2,drop3,drop4],[html_raw,links])
    anim_btn.click(make_animation,None,[file_out]).then(make_html,file_out,anim_out)
    ####### Raw ############
    drop1.change(make_tree,drop1,[drop2])
    drop2.change(make_tree,[drop1,drop2],[drop3])
    drop3.change(make_tree,[drop1,drop2,drop3],[drop4])
    ######################
    app.load(run,None,[drop1,html_im])
app.queue(default_concurrency_limit=10).launch()