6
6
from urllib .parse import urlparse
7
7
8
8
import cfdm
9
+ from cfdm .read_write .exceptions import UnknownFileFormatError
9
10
from cfdm .read_write .netcdf import NetCDFRead
10
11
11
12
from ..aggregate import aggregate as cf_aggregate
@@ -864,51 +865,52 @@ def _read_a_file(
864
865
fmt = (fmt ,)
865
866
866
867
fmt = set (fmt )
867
-
868
- errors = []
869
-
870
- try :
871
- out = super ().__new__ (
872
- cls ,
873
- filename ,
874
- external = external ,
875
- extra = extra ,
876
- verbose = verbose ,
877
- warnings = warnings ,
878
- mask = mask ,
879
- unpack = unpack ,
880
- warn_valid = warn_valid ,
881
- domain = domain ,
882
- storage_options = storage_options ,
883
- netcdf_backend = netcdf_backend ,
884
- dask_chunks = dask_chunks ,
885
- store_hdf5_chunks = store_hdf5_chunks ,
886
- cache = cache ,
887
- cfa = cfa ,
888
- cfa_write = cfa_write ,
889
- to_memory = to_memory ,
890
- squeeze = squeeze ,
891
- unsqueeze = unsqueeze ,
892
- fmt = fmt ,
893
- ignore_unknown_format = ignore_read_error ,
894
- )
895
- except RuntimeError as error :
896
- if fmt is None or fmt .intersection (("UM" ,)):
897
- # Set to None to indicate that we should try other
898
- # file formats
899
- errors .append (error )
900
- out = None
901
- else :
902
- raise
903
868
else :
904
- if out or not ignore_read_error :
905
- ftypes .append ("netCDF" )
869
+ fmt = set (("netCDF" , "CDL" , "UM" ))
870
+
871
+ file_format_errors = []
872
+
873
+ out = None
874
+ if fmt .intersection (("netCDF" , "CDL" )):
875
+ try :
876
+ out = super ().__new__ (
877
+ cls ,
878
+ filename ,
879
+ external = external ,
880
+ extra = extra ,
881
+ verbose = verbose ,
882
+ warnings = warnings ,
883
+ mask = mask ,
884
+ unpack = unpack ,
885
+ warn_valid = warn_valid ,
886
+ domain = domain ,
887
+ storage_options = storage_options ,
888
+ netcdf_backend = netcdf_backend ,
889
+ dask_chunks = dask_chunks ,
890
+ store_hdf5_chunks = store_hdf5_chunks ,
891
+ cache = cache ,
892
+ cfa = cfa ,
893
+ cfa_write = cfa_write ,
894
+ to_memory = to_memory ,
895
+ squeeze = squeeze ,
896
+ unsqueeze = unsqueeze ,
897
+ fmt = fmt ,
898
+ ignore_unknown_format = ignore_read_error ,
899
+ )
900
+ except UnknownFileFormatError as error :
901
+ fmt .difference_update (("netCDF" , "CDL" ))
902
+ if fmt :
903
+ file_format_errors .append (error )
904
+ else :
905
+ raise
906
906
else :
907
- # Set to None to indicate that we should try other
908
- # file formats
909
- out = None
907
+ if out or not ignore_read_error :
908
+ # Zero or more fields/domains were successfully read
909
+ fmt = set ()
910
+ file_format_errors = ()
911
+ ftypes .append ("netCDF" )
910
912
911
- if out is None :
913
+ if fmt . intersection (( "UM" ,)) :
912
914
if not um :
913
915
um = {}
914
916
@@ -927,21 +929,25 @@ def _read_a_file(
927
929
unsqueeze = unsqueeze ,
928
930
domain = domain ,
929
931
)
930
- except Exception as error :
931
- errors .append (error )
932
- errors = '\n ' .join (map (str , errors ))
933
- raise RuntimeError (f"\n { errors } " )
932
+ except UnknownFileFormatError as error :
933
+ fmt .difference_update (("UM" ,))
934
+ file_format_errors .append (error )
934
935
else :
935
936
if out or not ignore_read_error :
937
+ file_format_errors = ()
936
938
ftypes .append ("UM" )
937
-
939
+
938
940
# UM fields are aggregated intrafile prior to
939
941
# interfile aggregation
940
942
if aggregate :
941
943
# Set defaults specific to UM fields
942
944
if "strict_units" not in aggregate_options :
943
945
aggregate_options ["relaxed_units" ] = True
944
-
946
+
947
+ if file_format_errors :
948
+ file_format_errors = "\n " .join (map (str , file_format_errors ))
949
+ raise UnknownFileFormatError (f"\n { file_format_errors } " )
950
+
945
951
# Return the fields/domains
946
952
if domain :
947
953
return DomainList (out )
0 commit comments