File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import argparse
2+
3+ import yaml
4+
5+ parser = argparse .ArgumentParser ()
6+ parser .add_argument ('file_path' )
7+
8+ args = parser .parse_args ()
9+
10+ try :
11+ from netCDF4 import Dataset
12+ except ImportError :
13+ parser .error ('No module named "netCDF4" Use "pip install netCDF4" to install it.' )
14+
15+ definitions = []
16+ with Dataset (args .file_path ) as ds :
17+ for variable_name , variable in ds .variables .items ():
18+ definition = {
19+ 'specifier' : variable_name
20+ }
21+
22+ for attribute_name in ['long_name' , 'units' ]:
23+ if attribute_name in variable .ncattrs ():
24+ definition [attribute_name ] = variable .getncattr (attribute_name )
25+
26+ definitions .append (definition )
27+
28+ yaml_string = yaml .dump (definitions , sort_keys = False )
29+ yaml_string = yaml_string .replace ('\n - ' , '\n \n - ' )
30+
31+ print (yaml_string )
You can’t perform that action at this time.
0 commit comments