6
6
from urllib .parse import urlparse
7
7
8
8
import cfdm
9
- from cfdm .read_write .exceptions import UnknownFileFormatError as FileTypeError
9
+ from cfdm .read_write .exceptions import FileTypeError
10
10
from cfdm .read_write .netcdf import NetCDFRead
11
11
12
12
from ..aggregate import aggregate as cf_aggregate
@@ -199,20 +199,16 @@ class read(cfdm.read):
199
199
200
200
{{read warnings: `bool`, optional}}
201
201
202
- {{read ignore_unknown_type: `bool`, optional}}
203
-
204
- .. versionadded:: NEXTVERSION
205
-
206
202
{{read file_type: (sequence of) `str`, optional}}
207
203
208
- Valid files types are:
204
+ Valid file types are:
209
205
210
206
============ ============================================
211
- *file_type* Description
207
+ file type Description
212
208
============ ============================================
213
- ``'netCDF'`` Binary netCDF-3 or netCDF-4 file
214
- ``'CDL'`` Text CDL representation of a netCDF file
215
- ``'UM'`` UM fields file or PP file
209
+ ``'netCDF'`` Binary netCDF-3 or netCDF-4 files
210
+ ``'CDL'`` Text CDL representations of netCDF files
211
+ ``'UM'`` UM fields files or PP files
216
212
============ ============================================
217
213
218
214
.. versionadded:: NEXTVERSION
@@ -429,7 +425,7 @@ class read(cfdm.read):
429
425
Use the *file_type* parameter instead.
430
426
431
427
ignore_read_error: deprecated at version NEXTVERSION
432
- Use the *ignore_unknown_type * parameter instead.
428
+ Use the *file_type * parameter instead.
433
429
434
430
:Returns:
435
431
@@ -486,7 +482,6 @@ def __new__(
486
482
external = None ,
487
483
verbose = None ,
488
484
warnings = False ,
489
- ignore_unknown_type = False ,
490
485
aggregate = True ,
491
486
nfields = None ,
492
487
squeeze = False ,
@@ -582,48 +577,25 @@ def __new__(
582
577
_DEPRECATION_ERROR_FUNCTION_KWARGS (
583
578
"cf.read" ,
584
579
{"ignore_read_error" : ignore_read_error },
585
- "Use keyword 'ignore_unknown_type ' instead." ,
580
+ "Use keyword 'file_type ' instead." ,
586
581
version = "NEXTVERSION" ,
587
582
removed_at = "5.0.0" ,
588
583
) # pragma: no cover
589
584
585
+ info = cfdm .is_log_level_info (logger )
586
+
590
587
cls .netcdf = NetCDFRead (cls .implementation )
591
588
cls .um = UMRead (cls .implementation )
592
589
593
- # Parse select
590
+ # ------------------------------------------------------------
591
+ # Parse the 'select' keyword parameter
592
+ # ------------------------------------------------------------
594
593
if isinstance (select , (str , Query , Pattern )):
595
594
select = (select ,)
596
595
597
- info = cfdm .is_log_level_info (logger )
598
-
599
- # Manage input parameters where contradictions are possible:
600
- if cdl_string and file_type :
601
- if file_type == "CDL" :
602
- if info :
603
- logger .info (
604
- "It is not necessary to set the cf.read fmt as "
605
- "'CDL' when cdl_string is True, since that implies "
606
- "CDL is the format."
607
- ) # pragma: no cover
608
- else :
609
- raise ValueError (
610
- "cdl_string can only be True when the format is CDL, "
611
- "though fmt is ignored in that case so there is no "
612
- "need to set it."
613
- )
614
-
615
- if follow_symlinks and not recursive :
616
- raise ValueError (
617
- f"Can't set follow_symlinks={ follow_symlinks !r} "
618
- f"when recursive={ recursive !r} "
619
- )
620
-
621
- # Initialise the output list of fields/domains
622
- if domain :
623
- out = DomainList ()
624
- else :
625
- out = FieldList ()
626
-
596
+ # ------------------------------------------------------------
597
+ # Parse the 'aggregate' keyword parameter
598
+ # ------------------------------------------------------------
627
599
if isinstance (aggregate , dict ):
628
600
aggregate_options = aggregate .copy ()
629
601
aggregate = True
@@ -632,23 +604,45 @@ def __new__(
632
604
633
605
aggregate_options ["copy" ] = False
634
606
635
- ## ------------------------------------------------------------
636
- ## Parse the 'fmt ' keyword parameter
637
- ## ------------------------------------------------------------
638
- #if file_type:
639
- # if isinstance(file_type, str):
640
- # file_type = (file_type,)
641
- #
642
- # file_type = set (file_type)
643
- #else:
644
- # file_type = set(("netCDF", "CDL", "UM") )
607
+ # ------------------------------------------------------------
608
+ # Parse the 'file_type ' keyword parameter
609
+ # ------------------------------------------------------------
610
+ netCDF_file_types = set (( "netCDF" , "CDL" ))
611
+ UM_file_types = set (( "UM" ,))
612
+ if file_type is not None :
613
+ if isinstance ( file_type , str ):
614
+ file_type = (file_type , )
615
+
616
+ file_type = set (file_type )
645
617
646
618
# ------------------------------------------------------------
647
619
# Parse the 'um' keyword parameter
648
620
# ------------------------------------------------------------
649
621
if not um :
650
622
um = {}
651
623
624
+ # ------------------------------------------------------------
625
+ # Parse the 'cdl_string' keyword parameter
626
+ # ------------------------------------------------------------
627
+ if cdl_string and file_type is not None :
628
+ raise ValueError ("Can't set file_type when cdl_string=True" )
629
+
630
+ # ------------------------------------------------------------
631
+ # Parse the 'follow_symlinks' and 'recursive' keyword
632
+ # parameters
633
+ # ------------------------------------------------------------
634
+ if follow_symlinks and not recursive :
635
+ raise ValueError (
636
+ f"Can't set follow_symlinks={ follow_symlinks !r} "
637
+ f"when recursive={ recursive !r} "
638
+ )
639
+
640
+ # Initialise the output list of fields/domains
641
+ if domain :
642
+ out = DomainList ()
643
+ else :
644
+ out = FieldList ()
645
+
652
646
# Count the number of fields (in all files) and the number of
653
647
# files
654
648
field_counter = - 1
@@ -661,6 +655,7 @@ def __new__(
661
655
files = [
662
656
NetCDFRead .string_to_cdl (cdl_string ) for cdl_string in files
663
657
]
658
+ file_type = set (("CDL" ,))
664
659
665
660
for file_glob in flat (files ):
666
661
# Expand variables
@@ -674,8 +669,9 @@ def __new__(
674
669
# Glob files on disk
675
670
files2 = glob (file_glob )
676
671
677
- if not files2 and not ignore_unknown_type :
678
- open (file_glob , "rb" )
672
+ if not files2 :
673
+ # Trigger a FileNotFoundError error
674
+ open (file_glob )
679
675
680
676
files3 = []
681
677
for x in files2 :
@@ -694,7 +690,7 @@ def __new__(
694
690
695
691
files2 = files3
696
692
697
- # How each file was read, as netCDF, or UM, etc.
693
+ # The types of all of the input files
698
694
ftypes = set ()
699
695
700
696
for filename in files2 :
@@ -704,14 +700,19 @@ def __new__(
704
700
# ----------------------------------------------------
705
701
# Read the file
706
702
# ----------------------------------------------------
707
- file_types = file_type .copy ()
703
+ file_contents = []
704
+
705
+ # The type of this file
708
706
ftype = None
709
- file_contents = None
710
707
711
- # Record unknown file format errors
708
+ # Record file type errors
712
709
file_format_errors = []
713
- print ('---------' , file_types )
714
- if file_types .intersection (("netCDF" , "CDL" )):
710
+
711
+ if ftype is None and (
712
+ file_type is None
713
+ or file_type .intersection (netCDF_file_types )
714
+ ):
715
+ # Try to read as netCDF
715
716
try :
716
717
file_contents = super ().__new__ (
717
718
cls ,
@@ -735,28 +736,19 @@ def __new__(
735
736
squeeze = squeeze ,
736
737
unsqueeze = unsqueeze ,
737
738
file_type = file_type ,
738
- # ignore_unknown_type=ignore_unknown_type,
739
739
)
740
740
except FileTypeError as error :
741
741
if file_type is None :
742
742
file_format_errors .append (error )
743
-
744
- file_types .difference_update (("netCDF" , "CDL" ))
745
743
else :
746
744
file_format_errors = []
747
- # if file_contents or not ignore_unknown_type:
748
- # Zero or more fields/domains were
749
- # successfully read. Set 'file_types' to
750
- # an empty set so that no other file
751
- # formats are attempted.
752
- file_types = set ()
753
745
ftype = "netCDF"
754
-
755
- print ('here yyy' ,file_types , file_contents , file_format_errors )
756
- if file_types .intersection (("UM" ,)):
757
- print ('UM' , filename )
746
+
747
+ if ftype is None and (
748
+ file_type is None or file_type .intersection (UM_file_types )
749
+ ):
750
+ # Try to read as UM
758
751
try :
759
- print ('9999' )
760
752
file_contents = cls .um .read (
761
753
filename ,
762
754
um_version = um .get ("version" ),
@@ -770,30 +762,16 @@ def __new__(
770
762
squeeze = squeeze ,
771
763
unsqueeze = unsqueeze ,
772
764
domain = domain ,
773
- # ignore_unknown_type=ignore_unknown_type ,
765
+ file_type = file_type ,
774
766
)
775
767
except FileTypeError as error :
776
768
if file_type is None :
777
769
file_format_errors .append (error )
778
-
779
- # print (1111111)
780
- file_types .difference_update (("UM" ,))
781
- # file_format_errors.append(error)
782
770
else :
783
- print (1111155511 , file_contents )
784
- # file_format_errors = []
785
- # if file_contents or not ignore_unknown_type:
786
- # print ('bon')
787
- # Zero or more fields/domains were
788
- # successfully read. Set 'file_types' to
789
- # an empty set so that no other file
790
- # formats are attempted.
791
771
file_format_errors = []
792
- file_types = set ()
793
772
ftype = "UM"
794
773
795
774
if file_format_errors :
796
- print ('rrrr' ,file_format_errors , file_contents )
797
775
error = "\n " .join (map (str , file_format_errors ))
798
776
raise FileTypeError (f"\n { error } " )
799
777
@@ -805,28 +783,19 @@ def __new__(
805
783
if ftype :
806
784
ftypes .add (ftype )
807
785
808
- # --------------------------------------------------------
809
- # Select matching fields (only from netCDF files at
810
- # this stage - we'll do UM fields later)
811
- # --------------------------------------------------------
786
+ # Select matching fields (only for netCDF files at
787
+ # this stage - we'll other it for other file types
788
+ # later)
812
789
if select and ftype == "netCDF" :
813
790
file_contents = file_contents .select_by_identity (* select )
814
791
815
- # --------------------------------------------------------
816
- # Add this file's contents to that already read from other
817
- # files
818
- # --------------------------------------------------------
792
+ # Add this file's contents to that already read from
793
+ # other files
819
794
out .extend (file_contents )
820
795
821
796
field_counter = len (out )
822
797
file_counter += 1
823
798
824
- if info :
825
- logger .info (
826
- f"Read { field_counter } field{ cls ._plural (field_counter )} "
827
- f"from { file_counter } file{ cls ._plural (file_counter )} "
828
- ) # pragma: no cover
829
-
830
799
# ----------------------------------------------------------------
831
800
# Aggregate the output fields/domains
832
801
# ----------------------------------------------------------------
@@ -863,12 +832,18 @@ def __new__(
863
832
del f ._custom ["standard_name" ]
864
833
865
834
# ----------------------------------------------------------------
866
- # Select matching fields from UM/PP fields (post setting of
835
+ # Select matching fields from UM files (post setting of their
867
836
# standard names)
868
837
# ----------------------------------------------------------------
869
838
if select and "UM" in ftypes :
870
839
out = out .select_by_identity (* select )
871
840
841
+ if info :
842
+ logger .info (
843
+ f"Read { field_counter } field{ cls ._plural (field_counter )} "
844
+ f"from { file_counter } file{ cls ._plural (file_counter )} "
845
+ ) # pragma: no cover
846
+
872
847
if nfields is not None and len (out ) != nfields :
873
848
raise ValueError (
874
849
f"{ nfields } field{ cls ._plural (nfields )} requested but "
0 commit comments