Skip to content

Commit 4c586e3

Browse files
committed
dev
1 parent d6904b0 commit 4c586e3

File tree

3 files changed

+183
-63
lines changed

3 files changed

+183
-63
lines changed

cf/read_write/read.py

Lines changed: 105 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from urllib.parse import urlparse
77

88
import cfdm
9-
from cfdm.read_write.exceptions import UnknownFileFormatError
9+
from cfdm.read_write.exceptions import UnknownFileFormatError as FileTypeError
1010
from cfdm.read_write.netcdf import NetCDFRead
1111

1212
from ..aggregate import aggregate as cf_aggregate
@@ -183,7 +183,7 @@ class read(cfdm.read):
183183
are read. Sub-directories are not read unless the
184184
*recursive* parameter is True. If any directories contain
185185
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
187187
True.
188188
189189
As a special case, if the `cdl_string` parameter is set to
@@ -199,18 +199,23 @@ class read(cfdm.read):
199199
200200
{{read warnings: `bool`, optional}}
201201
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}}
207203
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
214219
215220
cdl_string: `bool`, optional
216221
If True and the format to read is CDL, read a string
@@ -420,6 +425,12 @@ class read(cfdm.read):
420425
chunks: deprecated at version NEXTVERSION
421426
Use the *dask_chunks* parameter instead.
422427
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+
423434
:Returns:
424435
425436
`FieldList` or `DomainList`
@@ -475,12 +486,12 @@ def __new__(
475486
external=None,
476487
verbose=None,
477488
warnings=False,
478-
ignore_read_error=False,
489+
ignore_unknown_type=False,
479490
aggregate=True,
480491
nfields=None,
481492
squeeze=False,
482493
unsqueeze=False,
483-
fmt=None,
494+
file_type=None,
484495
cdl_string=False,
485496
select=None,
486497
extra=None,
@@ -505,6 +516,8 @@ def __new__(
505516
storage_options=None,
506517
cache=True,
507518
chunks="auto",
519+
ignore_read_error=False,
520+
fmt=None,
508521
):
509522
"""Read field or domain constructs from a dataset."""
510523
if field:
@@ -556,6 +569,24 @@ def __new__(
556569
removed_at="5.0.0",
557570
) # pragma: no cover
558571

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+
559590
cls.netcdf = NetCDFRead(cls.implementation)
560591
cls.um = UMRead(cls.implementation)
561592

@@ -566,8 +597,8 @@ def __new__(
566597
info = cfdm.is_log_level_info(logger)
567598

568599
# 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":
571602
if info:
572603
logger.info(
573604
"It is not necessary to set the cf.read fmt as "
@@ -601,16 +632,16 @@ def __new__(
601632

602633
aggregate_options["copy"] = False
603634

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"))
614645

615646
# ------------------------------------------------------------
616647
# Parse the 'um' keyword parameter
@@ -643,7 +674,7 @@ def __new__(
643674
# Glob files on disk
644675
files2 = glob(file_glob)
645676

646-
if not files2 and not ignore_read_error:
677+
if not files2 and not ignore_unknown_type:
647678
open(file_glob, "rb")
648679

649680
files3 = []
@@ -673,12 +704,14 @@ def __new__(
673704
# ----------------------------------------------------
674705
# Read the file
675706
# ----------------------------------------------------
676-
fmts = fmt.copy()
707+
file_types = file_type.copy()
708+
ftype = None
709+
file_contents = None
677710

678711
# Record unknown file format errors
679712
file_format_errors = []
680-
681-
if fmts.intersection(("netCDF", "CDL")):
713+
print ('---------', file_types)
714+
if file_types.intersection(("netCDF", "CDL")):
682715
try:
683716
file_contents = super().__new__(
684717
cls,
@@ -701,24 +734,29 @@ def __new__(
701734
to_memory=to_memory,
702735
squeeze=squeeze,
703736
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,
706739
)
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"))
710745
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:
713748
# 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)
721758
try:
759+
print ('9999')
722760
file_contents = cls.um.read(
723761
filename,
724762
um_version=um.get("version"),
@@ -732,30 +770,40 @@ def __new__(
732770
squeeze=squeeze,
733771
unsqueeze=unsqueeze,
734772
domain=domain,
773+
# ignore_unknown_type=ignore_unknown_type,
735774
)
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)
739782
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')
742787
# 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"
748794

749795
if file_format_errors:
796+
print ('rrrr',file_format_errors, file_contents)
750797
error = "\n".join(map(str, file_format_errors))
751-
raise UnknownFileFormatError(f"\n{error}")
798+
raise FileTypeError(f"\n{error}")
752799

753800
if domain:
754801
file_contents = DomainList(file_contents)
755802

756803
file_contents = FieldList(file_contents)
757804

758-
ftypes.add(ftype)
805+
if ftype:
806+
ftypes.add(ftype)
759807

760808
# --------------------------------------------------------
761809
# Select matching fields (only from netCDF files at

cf/read_write/um/umread.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
from ...umread_lib.umfile import File
3030
from ...units import Units
3131

32-
# import numpy as np
33-
34-
3532
logger = logging.getLogger(__name__)
3633

3734
_cached_runid = {}
@@ -3393,6 +3390,7 @@ def read(
33933390
squeeze=False,
33943391
unsqueeze=False,
33953392
domain=False,
3393+
ignore_unknown_type=False,
33963394
):
33973395
"""Read fields from a PP file or UM fields file.
33983396
@@ -3526,7 +3524,13 @@ def read(
35263524
else:
35273525
byte_ordering = None
35283526

3527+
# try:
35293528
f = self.file_open(filename, parse=True)
3529+
# except UnknownFileFormatError:
3530+
# if not ignore_unknown_type:
3531+
# raise
3532+
#
3533+
# return []
35303534

35313535
info = is_log_level_info(logger)
35323536

@@ -3598,7 +3602,7 @@ def _open_um_file(
35983602
pass
35993603

36003604
raise UnknownFileFormatError(
3601-
f"Can't open {filename} as a PP or UM dataset"
3605+
f"Can't interpret {filename} as a PP or UM dataset"
36023606
)
36033607

36043608
self._um_file = f

0 commit comments

Comments
 (0)