Coverage for src/rechunk_data/__init__.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-30 09:58 +0000

1"""CLI for rechunking netcdf data.""" 

2 

3import argparse 

4import logging 

5from pathlib import Path 

6from typing import List, Optional 

7from ._rechunk import ( 

8 rechunk_netcdf_file, 

9 rechunk_dataset, 

10 logger, 

11) 

12 

13__version__ = "2310.0.1" 

14PROGRAM_NAME = "rechunk-data" 

15 

16 

17def parse_args(argv: Optional[List[str]]) -> argparse.Namespace: 

18 """Parse arguments for the command line parser.""" 

19 

20 parser = argparse.ArgumentParser( 

21 prog=PROGRAM_NAME, 

22 description=( 

23 "Rechunk input netcdf data to optimal chunk-size." 

24 " approx. 126 MB per chunk" 

25 ), 

26 formatter_class=argparse.ArgumentDefaultsHelpFormatter, 

27 ) 

28 parser.add_argument( 

29 "input", 

30 type=Path, 

31 help=( 

32 "Input file/directory. If a directory is given " 

33 "all ``.nc`` files in all sub directories will be " 

34 "processed" 

35 ), 

36 ) 

37 parser.add_argument( 

38 "--output", 

39 type=Path, 

40 help=( 

41 "Output file/directory of the chunked netcdf " 

42 "file(s). Note: If ``input`` is a directory output should be a" 

43 " directory. If None given (default) the ``input`` is overidden." 

44 ), 

45 default=None, 

46 ) 

47 parser.add_argument( 

48 "--netcdf-engine", 

49 help=("The netcdf engine used to create the new netcdf file."), 

50 choices=("h5netcdf", "netcdf4"), 

51 default="netcdf4", 

52 type=str, 

53 ) 

54 parser.add_argument( 

55 "--skip-cf-convention", 

56 help="Do not assume assume data variables follow CF conventions.", 

57 action="store_true", 

58 default=False, 

59 ) 

60 parser.add_argument( 

61 "-v", 

62 action="count", 

63 default=0, 

64 help="Increase verbosity", 

65 ) 

66 parser.add_argument( 

67 "-V", 

68 "--version", 

69 action="version", 

70 version=f"%(prog)s {__version__}", 

71 ) 

72 args = parser.parse_args(argv) 

73 logger.setLevel(max(logging.ERROR - (10 + args.v * 10), 10)) 

74 return args 

75 

76 

77def cli(argv: Optional[List[str]] = None) -> None: 

78 """Command line interface calling the rechunking method.""" 

79 args = parse_args(argv) 

80 rechunk_netcdf_file( 

81 args.input, 

82 args.output, 

83 engine=args.netcdf_engine, 

84 decode_cf=args.skip_cf_convention is False, 

85 )