1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

""" 

:Author: Daniel Mohr 

:Email: daniel.mohr@dlr.de 

:Date: 2021-02-15 (last change). 

:License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007. 

""" 

 

import tempfile 

import time 

 

 

def check_netcdf_file(file, output_format='human_readable'): 

""" 

:Author: Daniel Mohr 

:Email: daniel.mohr@dlr.de 

:Date: 2021-02-15 (last change). 

 

Checks the give file for the format netCDF. 

It uses the CF Checker: https://github.com/cedadev/cf-checker 

 

:param file: file to analyse 

""" 

# pylint: disable=no-member,bad-option-value,import-outside-toplevel 

import cfchecker.cfchecks 

import netCDF4 

result = dict() 

result['error'] = 0 

result['log'] = [] 

checker_name = 'pydabu (netcdf check)' 

result[checker_name] = dict() 

rootgrp = netCDF4.Dataset(file, "r") 

convention = None 

33 ↛ 35line 33 didn't jump to line 35, because the condition on line 33 was never true if hasattr(rootgrp, 'Conventions'): 

# Climate and Forecasts (CF) Metadata Convention is used 

convention = rootgrp.Conventions 

36 ↛ 39line 36 didn't jump to line 39, because the condition on line 36 was never false if hasattr(rootgrp, 'file_format'): 

# should be the same as data_model 

result[checker_name]['file_format'] = rootgrp.file_format 

39 ↛ 44line 39 didn't jump to line 44, because the condition on line 39 was never false if hasattr(rootgrp, 'data_model'): 

# data_model describes the netCDF data model version: 

# NETCDF3_CLASSIC, NETCDF4, NETCDF4_CLASSIC, NETCDF3_64BIT_OFFSET or 

# NETCDF3_64BIT_DATA 

result[checker_name]['data_model'] = rootgrp.data_model 

44 ↛ 46line 44 didn't jump to line 46, because the condition on line 44 was never false if hasattr(rootgrp, 'disk_format'): 

result[checker_name]['disk_format'] = rootgrp.disk_format 

rootgrp.close() 

result[checker_name]['created'] = time.time() 

48 ↛ 50line 48 didn't jump to line 50, because the condition on line 48 was never true if convention is not None: 

# check for Climate and Forecasts (CF) Metadata Convention 

inst = cfchecker.cfchecks.CFChecker( 

uploader=None, 

useFileName='yes', 

badc=None, 

coards=None, 

cfRegionNamesXML=cfchecker.cfchecks.REGIONNAMES, 

cfStandardNamesXML=cfchecker.cfchecks.STANDARDNAME, 

cfAreaTypesXML=cfchecker.cfchecks.AREATYPES, 

cacheDir=tempfile.gettempdir(), 

cacheTables=False, # True for many files 

cacheTime=24*3600, # 1 day as hard coded in cfchecker.cfchecks 

version=convention, 

debug=False, 

silent=True) 

inst.checker(file) 

totals = inst.get_total_counts() 

checker_name = 'CF Checker Version ' + cfchecker.cfchecks.__version__ 

result[checker_name] = dict() 

result[checker_name]['created'] = time.time() 

result[checker_name]['error'] = totals["FATAL"] + totals["ERROR"] 

result[checker_name]['warning'] = totals["WARN"] 

if output_format != 'human_readable': 

result[checker_name]['result'] = inst.all_results 

return result