88import pandas as pd
99from datetime import datetime
1010from urllib .parse import urljoin
11-
11+ from functools import lru_cache
1212from monitor_texts import MonitoringAppTexts
1313from monitor_dates import MonitoringAppDates
1414
2626start_date = datetime (int (sdate [0 :4 ]), int (sdate [4 :6 ]), int (sdate [6 :8 ]), int (sdate [8 :10 ]))
2727end_date = datetime (int (edate [0 :4 ]), int (edate [4 :6 ]), int (edate [6 :8 ]), int (edate [8 :10 ]))
2828
29- datas = [d .strftime ('%Y%m%d%H' ) for d in pd .date_range (start_date , end_date , freq = '6h' )][::- 1 ]
30-
31- variaveis = [
32- "10_metre_u-wind_component" ,
33- "10_metre_v-wind_component" ,
29+ # força 00UTC
30+ start_date = start_date .replace (hour = 0 )
31+ end_date = end_date .replace (hour = 0 )
32+
33+ datas = [d .strftime ('%Y%m%d%H' ) for d in pd .date_range (start_date , end_date , freq = 'D' )][::- 1 ]
34+
35+ #variaveis = [
36+ # "10_metre_u-wind_component",
37+ # "10_metre_v-wind_component",
38+ # "Geopotential_height",
39+ # "Meridional_wind_v"
40+ #]
41+
42+ variaveis = [
43+ "Zonal_wind_u" ,
44+ "Meridional_wind_v" ,
45+ "Omega" ,
46+ "Stream_function" ,
47+ "Velocity_potential" ,
3448 "Geopotential_height" ,
35- "Meridional_wind_v"
49+ "Absolute_temperature" ,
50+ "Specific_humidity" ,
51+ "Vertical_dist_total_cloud_cover" ,
52+ "Time_mean_surface_relative_humidity"
3653]
3754
38- niveis = ["925" , "850" , "500" ]
55+ #niveis = ["925", "850", "500"]
56+ niveis = ["1000" , "925" , "850" , "500" , "250" , "20" , "10" , "3" ]
3957tempos = [str (i ) for i in range (0 , 73 , 6 )]
4058
4159data = pn .widgets .Select (name = "Date" , options = datas , width = 245 )
@@ -71,15 +89,32 @@ def img_smna(d, v, l, t):
7189 return pn .pane .Markdown ("Selecione parâmetros" )
7290
7391 url = urljoin (BASE_URL , f"SMNA/{ d } /{ v } /{ l } /{ t } .jpg" )
74- return pn .pane .JPG (f"{ url } ?t={ int (time .time ())} " , width = 800 )
92+ img = fetch_image_or_fallback (url )
93+ return pn .pane .JPG (io .BytesIO (img ), width = 800 )
7594
7695@pn .depends (data , var , lev , tmp )
7796def img_bam (d , v , l , t ):
7897 if not (d and v and l and t ):
7998 return pn .pane .Markdown ("Selecione parâmetros" )
8099
81100 url = urljoin (BASE_URL , f"BAM/{ d } /{ v } /{ l } /{ t } .jpg" )
82- return pn .pane .JPG (f"{ url } ?t={ int (time .time ())} " , width = 800 )
101+ img = fetch_image_or_fallback (url )
102+ return pn .pane .JPG (io .BytesIO (img ), width = 800 )
103+
104+ @lru_cache (maxsize = 128 )
105+ def fetch_image_or_fallback (url , fallback_path = "https://dataserver.cptec.inpe.br/dataserver_dimnt/das/carlos.bastarz/SMNAMonitoringApp/anls_imgs/imgs/image_not_available.jpg" ):
106+ try :
107+ r = requests .get (url , timeout = 5 )
108+ r .raise_for_status ()
109+ return r .content
110+ except Exception :
111+ return get_fallback_image (fallback_path )
112+
113+ @lru_cache (maxsize = 1 )
114+ def get_fallback_image (url ):
115+ r = requests .get (url , timeout = 5 )
116+ r .raise_for_status ()
117+ return r .content
83118
84119download_smna = pn .widgets .FileDownload (
85120 icon = "download" ,
@@ -149,4 +184,4 @@ def LayoutMainAnl():
149184 plots ,
150185 monitor_warning_bottom_main ,
151186 sizing_mode = "stretch_both"
152- )
187+ )
0 commit comments