2323
2424import numpy
2525from astropy .convolution import Box1DKernel , convolve # type: ignore[import-untyped]
26- from astropy .io .fits import BinTableHDU , FITS_rec , HDUList # type: ignore[import-untyped]
26+ from astropy .io .fits import BinTableHDU , FITS_rec # type: ignore[import-untyped]
2727from astropy .io .fits import open as FITS
2828from numpy import mean , median , ndarray , sqrt , std
2929from plotly .graph_objects import Figure , Scatter # type: ignore[import-untyped]
4141 from dash .dependencies import Input , Output , State
4242
4343
44- def fetch (url : str , speclink : str , auth : None | tuple [str , str ] = None ) -> bytes :
44+ def fetch (url : str , auth : None | tuple [str , str ] = None , speclink : str = "" ) -> bytes :
4545 try :
4646 rv = request ("GET" , url , auth = auth )
4747 except ChunkedEncodingError : # Connection broken: IncompleteRead
4848 rv = request ("GET" , url , auth = auth )
4949 except ConnectionError as e : # ConnectTimeout | SSLError
5050 print (e )
5151 sleep (1 )
52- return fetch (url , auth )
53- if (rv .status_code != 404 ):
54- print (rv .status_code , url )
55- if speclink != "" : print (speclink )
52+ return fetch (url , auth , speclink )
53+ if (rv .status_code != 404 ):
54+ print (rv .status_code , url )
55+ if speclink : print (" " , speclink )
5656 rv .raise_for_status () # HTTPError
5757 return rv .content
5858def unzstd (f : str ) -> bytes :
@@ -73,11 +73,11 @@ def unzstd(f: str) -> bytes:
7373 if isfile (bhm_data_local ):
7474 try :
7575 lcl_data = parse_json (unzstd (bhm_data_local ))
76- rmt_meta = parse_json (fetch (remote + basename (bhm_meta_local ), "" ))
76+ rmt_meta = parse_json (fetch (remote + basename (bhm_meta_local )))
7777 if lcl_data ["hdr" ]["date" ] >= rmt_meta ["date" ]: break # already latest
7878 except HTTPError : break # skip update
7979 except : print_exc ()
80- try : write (bhm_data_local , fetch (remote + basename (bhm_data_local ), "" ))
80+ try : write (bhm_data_local , fetch (remote + basename (bhm_data_local )))
8181 except HTTPError : sleep (1 ) # retry after delay
8282
8383metadata : dict [str , list | dict | str | int ] = lcl_data ["hdr" ]
@@ -127,7 +127,7 @@ def get(hdu: FITS_rec, col: str, default: None = None):
127127
128128@lru_cache (64 )
129129def cached_fetch (url : str , speclink : str ) -> bytes :
130- return fetch (url , speclink , (username , password ) if url .startswith ("https://data.sdss5.org/sas/sdsswork/" ) else None )
130+ return fetch (url , (username , password ) if url .startswith ("https://data.sdss5.org/sas/sdsswork/" ) else None , speclink )
131131def locked_fetch (url : str , speclink : str ) -> bytes :
132132 with fetch_queue [url ]: r = cached_fetch (url , speclink )
133133 return r
@@ -173,7 +173,7 @@ def SDSSV_fetch(username: str, password: str, field: int | str, mjd: int, obj: i
173173 url , speclink = sdss_sas_fits (field , mjd , obj , branch ) # speclink added PBH 2025-11-06
174174
175175 numpy .seterr (divide = "ignore" ) # Python does not comply with IEEE 754 :(
176- fits = cast ( HDUList , FITS (IOBuffer (locked_fetch (url ,speclink ) ))) # prevent duplicated requests
176+ fits = FITS (IOBuffer (locked_fetch (url , speclink ))) # prevent duplicated requests
177177 hdu2 = fits ["COADD" ] if "COADD" in fits else fits [1 ]
178178 hdu3 = fits ["SPALL" ] if "SPALL" in fits else fits [2 ] # SPECOBJ
179179 assert isa (hdu2 , BinTableHDU ) and isa (hdu2 .data , FITS_rec )
@@ -338,7 +338,11 @@ def legend(meta: Meta, base: str = "") -> str:
338338 for fieldmjd in catalogs .get (f"{ cat } " , [0 ])[1 :]: # {13'FIELD,5'MJD}
339339 fid , mjd = divmod (abs (fieldmjd ), 10 ** 5 )
340340 if field != "all" and int (field ) != fid : continue
341- dat = SDSSV_fetch (username , password , fid , mjd , cat )
341+ try :
342+ dat = SDSSV_fetch (username , password , fid , mjd , cat )
343+ except Exception as e :
344+ if str (e ): print (e ) if isa (e , HTTPError ) else print_exc ()
345+ continue
342346 meta = Meta (dat [0 ])
343347 data .append (Data (meta , wave = dat [1 ], flux = dat [2 ], errs = dat [3 ], name = legend (meta )))
344348 if mjd not in mjd_list : mjd_list .append (mjd )
0 commit comments