@@ -600,14 +600,6 @@ def __new__(
600
600
601
601
aggregate_options ["copy" ] = False
602
602
603
- # Parse the extra parameter
604
- if extra is None :
605
- extra = ()
606
- elif isinstance (extra , str ):
607
- extra = (extra ,)
608
-
609
- ftypes = set ()
610
-
611
603
# Count the number of fields (in all files) and the number of
612
604
# files
613
605
field_counter = - 1
@@ -657,39 +649,20 @@ def __new__(
657
649
if info :
658
650
logger .info (f"File: { filename } " ) # pragma: no cover
659
651
660
- if um :
661
- ftype = "UM"
662
- else :
663
- try :
664
- ftype = cls .file_format (filename )
665
- except Exception as error :
666
- if not ignore_read_error :
667
- raise ValueError (error )
668
-
669
- logger .warning (f"WARNING: { error } " ) # pragma: no cover
670
- continue
671
-
672
- if domain and ftype == "UM" :
673
- raise ValueError (
674
- f"Can't read PP/UM file { filename } into domain "
675
- "constructs"
676
- )
677
-
678
- ftypes .add (ftype )
679
-
680
652
# --------------------------------------------------------
681
653
# Read the file
682
654
# --------------------------------------------------------
655
+ ftypes = [None ]
683
656
file_contents = cls ._read_a_file (
684
657
filename ,
685
- ftype = ftype ,
658
+ ftypes = ftypes ,
686
659
external = external ,
687
660
ignore_read_error = ignore_read_error ,
688
661
verbose = verbose ,
689
662
warnings = warnings ,
690
663
aggregate = aggregate ,
691
664
aggregate_options = aggregate_options ,
692
- selected_fmt = fmt ,
665
+ fmt = fmt ,
693
666
um = um ,
694
667
extra = extra ,
695
668
height_at_top_of_model = height_at_top_of_model ,
@@ -711,9 +684,10 @@ def __new__(
711
684
)
712
685
713
686
# --------------------------------------------------------
714
- # Select matching fields (not from UM files, yet)
687
+ # Select matching fields (only from netCDF files at
688
+ # this stage - we'll do UM fields later)
715
689
# --------------------------------------------------------
716
- if select and ftype != "UM " :
690
+ if select and ftypes [ - 1 ] == "netCDF " :
717
691
file_contents = file_contents .select_by_identity (* select )
718
692
719
693
# --------------------------------------------------------
@@ -787,14 +761,14 @@ def _plural(n): # pragma: no cover
787
761
def _read_a_file (
788
762
cls ,
789
763
filename ,
790
- ftype = None ,
764
+ ftypes = None ,
791
765
aggregate = True ,
792
766
aggregate_options = None ,
793
767
ignore_read_error = False ,
794
768
verbose = None ,
795
769
warnings = False ,
796
770
external = None ,
797
- selected_fmt = None ,
771
+ fmt = None ,
798
772
um = None ,
799
773
extra = None ,
800
774
height_at_top_of_model = None ,
@@ -821,7 +795,7 @@ def _read_a_file(
821
795
filename: `str`
822
796
See `cf.read` for details.
823
797
824
- ftype : `str`
798
+ ftypes : `str` TODOCFA
825
799
The file format to interpret the file. Recognised formats are
826
800
``'netCDF'``, ``'CDL'``, ``'UM'`` and ``'PP'``.
827
801
@@ -885,70 +859,22 @@ def _read_a_file(
885
859
The field or domain constructs in the dataset.
886
860
887
861
"""
888
- if aggregate_options is None :
889
- aggregate_options = {}
890
-
891
- # Find this file's type
892
- fmt = None
893
- word_size = None
894
- endian = None
895
- height_at_top_of_model = None
896
- umversion = 405
897
-
898
- if um :
899
- fmt = um .get ("fmt" )
900
- word_size = um .get ("word_size" )
901
- endian = um .get ("endian" )
902
- umversion = um .get ("version" , umversion )
903
- height_at_top_of_model = um .get ("height_at_top_of_model" )
904
-
905
- if fmt is not None :
906
- fmt = fmt .upper ()
907
-
908
- if umversion is not None :
909
- umversion = float (str (umversion ).replace ("." , "0" , 1 ))
862
+ if fmt :
863
+ if isinstance (fmt , str ):
864
+ fmt = (fmt ,)
910
865
911
- # extra_read_vars = {
912
- # "fmt": selected_fmt,
913
- # "ignore_read_error": ignore_read_error,
914
- # }
866
+ fmt = set (fmt )
915
867
916
- # ----------------------------------------------------------------
917
- # Still here? Read the file into fields or domains.
918
- # ----------------------------------------------------------------
919
- # originally_cdl = ftype == "CDL"
920
- # if originally_cdl:
921
- # # Create a temporary netCDF file from input CDL
922
- # ftype = "netCDF"
923
- # cdl_filename = filename
924
- # filename = cls.netcdf.cdl_to_netcdf(filename)
925
- # extra_read_vars["fmt"] = "NETCDF"
926
- #
927
- # if not cls.netcdf.is_netcdf_file(filename):
928
- # error_msg = (
929
- # f"Can't determine format of file {filename} generated "
930
- # f"from CDL file {cdl_filename}"
931
- # )
932
- # if ignore_read_error:
933
- # logger.warning(error_msg) # pragma: no cover
934
- # return FieldList()
935
- # else:
936
- # raise IOError(error_msg)
937
-
938
- if ftype in ("netCDF" , "CDL" ): # and extra_read_vars["fmt"] in (
939
- # None,
940
- # "NETCDF",
941
- # "CDL",
942
- # "CFA",
943
- # ):
868
+ errors = []
869
+
870
+ try :
944
871
out = super ().__new__ (
945
872
cls ,
946
873
filename ,
947
874
external = external ,
948
875
extra = extra ,
949
876
verbose = verbose ,
950
877
warnings = warnings ,
951
- # extra_read_vars=extra_read_vars,
952
878
mask = mask ,
953
879
unpack = unpack ,
954
880
warn_valid = warn_valid ,
@@ -963,79 +889,61 @@ def _read_a_file(
963
889
to_memory = to_memory ,
964
890
squeeze = squeeze ,
965
891
unsqueeze = unsqueeze ,
966
- )
967
- elif ftype == "UM" : # and extra_read_vars["fmt"] in (None, "UM"):
968
- if domain :
969
- raise ValueError (
970
- "Can't set domain=True when reading UM or PP datasets"
971
- )
972
-
973
- out = cls .um .read (
974
- filename ,
975
- um_version = umversion ,
976
- verbose = verbose ,
977
- set_standard_name = False ,
978
- height_at_top_of_model = height_at_top_of_model ,
979
892
fmt = fmt ,
980
- word_size = word_size ,
981
- endian = endian ,
982
- select = select ,
983
- squeeze = squeeze ,
984
- unsqueeze = unsqueeze ,
893
+ ignore_unknown_format = ignore_read_error ,
985
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
+ else :
904
+ if out or not ignore_read_error :
905
+ ftypes .append ("netCDF" )
906
+ else :
907
+ # Set to None to indicate that we should try other
908
+ # file formats
909
+ out = None
986
910
987
- # PP fields are aggregated intrafile prior to interfile
988
- # aggregation
989
- if aggregate :
990
- # For PP fields, the default is strict_units=False
991
- if "strict_units" not in aggregate_options :
992
- aggregate_options ["relaxed_units" ] = True
911
+ if out is None :
912
+ if not um :
913
+ um = {}
993
914
915
+ try :
916
+ out = cls .um .read (
917
+ filename ,
918
+ um_version = um .get ("version" ),
919
+ verbose = verbose ,
920
+ set_standard_name = False ,
921
+ height_at_top_of_model = height_at_top_of_model ,
922
+ fmt = um .get ("fmt" ),
923
+ word_size = um .get ("word_size" ),
924
+ endian = um .get ("endian" ),
925
+ select = select ,
926
+ squeeze = squeeze ,
927
+ unsqueeze = unsqueeze ,
928
+ domain = domain ,
929
+ )
930
+ except Exception as error :
931
+ errors .append (error )
932
+ errors = '\n ' .join (map (str , errors ))
933
+ raise RuntimeError (f"\n { errors } " )
934
+ else :
935
+ if out or not ignore_read_error :
936
+ ftypes .append ("UM" )
937
+
938
+ # UM fields are aggregated intrafile prior to
939
+ # interfile aggregation
940
+ if aggregate :
941
+ # Set defaults specific to UM fields
942
+ if "strict_units" not in aggregate_options :
943
+ aggregate_options ["relaxed_units" ] = True
944
+
994
945
# Return the fields/domains
995
946
if domain :
996
947
return DomainList (out )
997
948
998
949
return FieldList (out )
999
-
1000
- @classmethod
1001
- def file_format (cls , filename ):
1002
- """Return the file format.
1003
-
1004
- :Parameters:
1005
-
1006
- filename: `str`
1007
- The file name.
1008
-
1009
- :Returns:
1010
-
1011
- `str`
1012
- The format type of the file. One of ``'netCDF'``, ``'UM'``
1013
- or ``'CDL'``.
1014
-
1015
- **Examples**
1016
-
1017
- >>> r.file_format(filename)
1018
- 'netCDF'
1019
-
1020
- """
1021
- # ----------------------------------------------------------------
1022
- # NetCDF
1023
- # ----------------------------------------------------------------
1024
- fmt = cls .netcdf .file_format (filename )
1025
- if fmt :
1026
- return fmt
1027
-
1028
- # ----------------------------------------------------------------
1029
- # PP or FF
1030
- # ----------------------------------------------------------------
1031
- if cls .um .is_um_file (filename ):
1032
- return "UM"
1033
-
1034
- # # ----------------------------------------------------------------
1035
- # # CDL
1036
- # # ----------------------------------------------------------------
1037
- # if cls.netcdf.is_cdl_file(filename):
1038
- # return "CDL"
1039
-
1040
- # Still here?
1041
- raise IOError (f"Can't determine format of file { filename } " )
0 commit comments