@@ -479,9 +479,29 @@ def find_idx(self, overwrite: bool = False) -> tuple[Optional[Union[Path, str]],
479479 if not overwrite :
480480 local_grib = self .get_localFilePath ()
481481 for suffix in self .IDX_SUFFIX :
482- local_idx = local_grib .with_suffix (suffix )
482+ # There is no easy way to know if the index suffix needs to be appended or overwrite the grib suffix.
483+ # Hence there is a need to do various checks to verify if the index file exists locally or not.
484+
485+ # This is the case of GFS, where the grib file ends with ".grb2" and the index file is ".grb2.inv"
486+ # The index suffix needs to overwrite the grib file suffix, now ending in ".grb2.inv"
487+ if suffix .startswith (local_grib .suffix ):
488+ local_idx = local_grib .with_suffix (suffix )
489+
490+ # This is the case of GFS, where the grib file ends with ".f000" and the index file is ".idx"
491+ # The index suffix needs to be appended to the grib file suffix, now ending in ".f000.idx"
492+ else :
493+ local_idx = local_grib .with_suffix (local_grib .suffix + suffix )
494+
483495 if local_idx .exists () and not self .overwrite :
484496 return (local_idx , "local" )
497+
498+ # If the index file does not exists locally, we will need to try another variation of the index file name.
499+ # This is the case of IFS, where the grib file ends with ".grib2" and the index file is ".index"
500+ # The index suffix needs to overwrite the grib file suffix, now ending in ".index"
501+ else :
502+ local_idx = local_grib .with_suffix (suffix )
503+ if local_idx .exists () and not self .overwrite :
504+ return (local_idx , "local" )
485505
486506 # If priority list is set, we want to search SOURCES in that
487507 # priority order. If priority is None, then search all SOURCES
@@ -740,9 +760,34 @@ def index_as_dataframe(self) -> pd.DataFrame:
740760 # eccodes keywords explained here:
741761 # https://confluence.ecmwf.int/display/UDOC/Identification+keywords
742762
743- r = requests .get (self .idx )
744- idxs = [json .loads (x ) for x in r .text .split ("\n " ) if x ]
745- r .close ()
763+ if self .idx_source in ["local" ]:
764+ with open (self .idx , "r" ) as file :
765+ read_this_idx = StringIO (file .read ())
766+ else :
767+ print (f"Downloading inventory file from { self .idx = } " )
768+ response = requests .get (self .idx )
769+ if response .status_code != 200 :
770+ response .raise_for_status ()
771+ response .close ()
772+ raise ValueError (
773+ f"\n Cant open index file { self .idx } \n "
774+ f"Download the full file first (with `H.download()`).\n "
775+ f"You will need to remake the Herbie object (H = `Herbie()`)\n "
776+ f"or delete this cached property: `del H.index_as_dataframe()`"
777+ )
778+
779+ read_this_idx = StringIO (response .text )
780+ response .close ()
781+
782+ index_filepath = self .get_localIndexFilePath ()
783+ os .makedirs (os .path .dirname (index_filepath ), exist_ok = True )
784+
785+ with open (index_filepath , "w" ) as file :
786+ file .write (read_this_idx .read ())
787+ # reset the cursor to the beggining of the StringIO
788+ read_this_idx .seek (0 )
789+
790+ idxs = [json .loads (x ) for x in read_this_idx .getvalue ().split ("\n " ) if x ]
746791 df = pd .DataFrame (idxs )
747792
748793 # Format the DataFrame
0 commit comments