6
6
from urllib .parse import urlparse
7
7
8
8
import cfdm
9
- from cfdm .read_write .exceptions import UnknownFileFormatError
9
+ from cfdm .read_write .exceptions import UnknownFileFormatError as FileTypeError
10
10
from cfdm .read_write .netcdf import NetCDFRead
11
11
12
12
from ..aggregate import aggregate as cf_aggregate
@@ -183,7 +183,7 @@ class read(cfdm.read):
183
183
are read. Sub-directories are not read unless the
184
184
*recursive* parameter is True. If any directories contain
185
185
files that are not valid datasets then an exception will
186
- be raised, unless the *ignore_read_error * parameter is
186
+ be raised, unless the *ignore_unknown_type * parameter is
187
187
True.
188
188
189
189
As a special case, if the `cdl_string` parameter is set to
@@ -199,18 +199,23 @@ class read(cfdm.read):
199
199
200
200
{{read warnings: `bool`, optional}}
201
201
202
- ignore_read_error: `bool`, optional
203
- If True then ignore any file which raises an IOError
204
- whilst being read, as would be the case for an empty file,
205
- unknown file format, etc. By default the IOError is
206
- raised.
202
+ {{read ignore_unknown_type: `bool`, optional}}
207
203
208
- fmt: `str`, optional
209
- Only read files of the given format, ignoring all other
210
- files. Valid formats are ``'NETCDF'`` for CF-netCDF files,
211
- ``'CFA'`` for CFA-netCDF files, ``'UM'`` for PP or UM
212
- fields files, and ``'CDL'`` for CDL text files. By default
213
- files of any of these formats are read.
204
+ .. versionadded:: NEXTVERSION
205
+
206
+ {{read file_type: (sequence of) `str`, optional}}
207
+
208
+ Valid files types are:
209
+
210
+ ============ ============================================
211
+ *file_type* Description
212
+ ============ ============================================
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
216
+ ============ ============================================
217
+
218
+ .. versionadded:: NEXTVERSION
214
219
215
220
cdl_string: `bool`, optional
216
221
If True and the format to read is CDL, read a string
@@ -420,6 +425,12 @@ class read(cfdm.read):
420
425
chunks: deprecated at version NEXTVERSION
421
426
Use the *dask_chunks* parameter instead.
422
427
428
+ fmt: deprecated at version NEXTVERSION
429
+ Use the *file_type* parameter instead.
430
+
431
+ ignore_read_error: deprecated at version NEXTVERSION
432
+ Use the *ignore_unknown_type* parameter instead.
433
+
423
434
:Returns:
424
435
425
436
`FieldList` or `DomainList`
@@ -475,12 +486,12 @@ def __new__(
475
486
external = None ,
476
487
verbose = None ,
477
488
warnings = False ,
478
- ignore_read_error = False ,
489
+ ignore_unknown_type = False ,
479
490
aggregate = True ,
480
491
nfields = None ,
481
492
squeeze = False ,
482
493
unsqueeze = False ,
483
- fmt = None ,
494
+ file_type = None ,
484
495
cdl_string = False ,
485
496
select = None ,
486
497
extra = None ,
@@ -505,6 +516,8 @@ def __new__(
505
516
storage_options = None ,
506
517
cache = True ,
507
518
chunks = "auto" ,
519
+ ignore_read_error = False ,
520
+ fmt = None ,
508
521
):
509
522
"""Read field or domain constructs from a dataset."""
510
523
if field :
@@ -556,6 +569,24 @@ def __new__(
556
569
removed_at = "5.0.0" ,
557
570
) # pragma: no cover
558
571
572
+ if fmt is not None :
573
+ _DEPRECATION_ERROR_FUNCTION_KWARGS (
574
+ "cf.read" ,
575
+ {"fmt" : fmt },
576
+ "Use keyword 'file_type' instead." ,
577
+ version = "NEXTVERSION" ,
578
+ removed_at = "5.0.0" ,
579
+ ) # pragma: no cover
580
+
581
+ if ignore_read_error :
582
+ _DEPRECATION_ERROR_FUNCTION_KWARGS (
583
+ "cf.read" ,
584
+ {"ignore_read_error" : ignore_read_error },
585
+ "Use keyword 'ignore_unknown_type' instead." ,
586
+ version = "NEXTVERSION" ,
587
+ removed_at = "5.0.0" ,
588
+ ) # pragma: no cover
589
+
559
590
cls .netcdf = NetCDFRead (cls .implementation )
560
591
cls .um = UMRead (cls .implementation )
561
592
@@ -566,8 +597,8 @@ def __new__(
566
597
info = cfdm .is_log_level_info (logger )
567
598
568
599
# Manage input parameters where contradictions are possible:
569
- if cdl_string and fmt :
570
- if fmt == "CDL" :
600
+ if cdl_string and file_type :
601
+ if file_type == "CDL" :
571
602
if info :
572
603
logger .info (
573
604
"It is not necessary to set the cf.read fmt as "
@@ -601,16 +632,16 @@ def __new__(
601
632
602
633
aggregate_options ["copy" ] = False
603
634
604
- # ------------------------------------------------------------
605
- # Parse the 'fmt' keyword parameter
606
- # ------------------------------------------------------------
607
- if fmt :
608
- if isinstance (fmt , str ):
609
- fmt = (fmt ,)
610
-
611
- fmt = set (fmt )
612
- else :
613
- fmt = set (("netCDF" , "CDL" , "UM" ))
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"))
614
645
615
646
# ------------------------------------------------------------
616
647
# Parse the 'um' keyword parameter
@@ -643,7 +674,7 @@ def __new__(
643
674
# Glob files on disk
644
675
files2 = glob (file_glob )
645
676
646
- if not files2 and not ignore_read_error :
677
+ if not files2 and not ignore_unknown_type :
647
678
open (file_glob , "rb" )
648
679
649
680
files3 = []
@@ -673,12 +704,14 @@ def __new__(
673
704
# ----------------------------------------------------
674
705
# Read the file
675
706
# ----------------------------------------------------
676
- fmts = fmt .copy ()
707
+ file_types = file_type .copy ()
708
+ ftype = None
709
+ file_contents = None
677
710
678
711
# Record unknown file format errors
679
712
file_format_errors = []
680
-
681
- if fmts .intersection (("netCDF" , "CDL" )):
713
+ print ( '---------' , file_types )
714
+ if file_types .intersection (("netCDF" , "CDL" )):
682
715
try :
683
716
file_contents = super ().__new__ (
684
717
cls ,
@@ -701,24 +734,29 @@ def __new__(
701
734
to_memory = to_memory ,
702
735
squeeze = squeeze ,
703
736
unsqueeze = unsqueeze ,
704
- file_type = fmt ,
705
- ignore_unknown_format = ignore_read_error ,
737
+ file_type = file_type ,
738
+ # ignore_unknown_type=ignore_unknown_type ,
706
739
)
707
- except UnknownFileFormatError as error :
708
- fmts .difference_update (("netCDF" , "CDL" ))
709
- file_format_errors .append (error )
740
+ except FileTypeError as error :
741
+ if file_type is None :
742
+ file_format_errors .append (error )
743
+
744
+ file_types .difference_update (("netCDF" , "CDL" ))
710
745
else :
711
- file_format_errors = ()
712
- if file_contents or not ignore_read_error :
746
+ file_format_errors = []
747
+ # if file_contents or not ignore_unknown_type :
713
748
# Zero or more fields/domains were
714
- # successfully read. Set 'fmts' to an
715
- # empty set so that no other file formats
716
- # are attempted.
717
- fmts = set ()
718
- ftype = "netCDF"
719
-
720
- if fmts .intersection (("UM" ,)):
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
+ ftype = "netCDF"
754
+
755
+ print ('here yyy' ,file_types , file_contents , file_format_errors )
756
+ if file_types .intersection (("UM" ,)):
757
+ print ('UM' , filename )
721
758
try :
759
+ print ('9999' )
722
760
file_contents = cls .um .read (
723
761
filename ,
724
762
um_version = um .get ("version" ),
@@ -732,30 +770,40 @@ def __new__(
732
770
squeeze = squeeze ,
733
771
unsqueeze = unsqueeze ,
734
772
domain = domain ,
773
+ # ignore_unknown_type=ignore_unknown_type,
735
774
)
736
- except UnknownFileFormatError as error :
737
- fmts .difference_update (("UM" ,))
738
- file_format_errors .append (error )
775
+ except FileTypeError as error :
776
+ if file_type is None :
777
+ file_format_errors .append (error )
778
+
779
+ # print (1111111)
780
+ file_types .difference_update (("UM" ,))
781
+ # file_format_errors.append(error)
739
782
else :
740
- file_format_errors = ()
741
- if file_contents or not ignore_read_error :
783
+ print (1111155511 , file_contents )
784
+ # file_format_errors = []
785
+ # if file_contents or not ignore_unknown_type:
786
+ # print ('bon')
742
787
# Zero or more fields/domains were
743
- # successfully read. Set 'fmts' to an
744
- # empty set so that no other file formats
745
- # are attempted.
746
- fmts = set ()
747
- ftype = "UM"
788
+ # successfully read. Set 'file_types' to
789
+ # an empty set so that no other file
790
+ # formats are attempted.
791
+ file_format_errors = []
792
+ file_types = set ()
793
+ ftype = "UM"
748
794
749
795
if file_format_errors :
796
+ print ('rrrr' ,file_format_errors , file_contents )
750
797
error = "\n " .join (map (str , file_format_errors ))
751
- raise UnknownFileFormatError (f"\n { error } " )
798
+ raise FileTypeError (f"\n { error } " )
752
799
753
800
if domain :
754
801
file_contents = DomainList (file_contents )
755
802
756
803
file_contents = FieldList (file_contents )
757
804
758
- ftypes .add (ftype )
805
+ if ftype :
806
+ ftypes .add (ftype )
759
807
760
808
# --------------------------------------------------------
761
809
# Select matching fields (only from netCDF files at
0 commit comments