5 Program to extract a band (Variable) from one OBPG netCDF4 L2 file and create
6 a new file containing just that band.
8 The program is also meant to serve as an example of how OBPG files in netCDF4
9 format can be accessed and manipulated.
12 http://schubert.atmos.colostate.edu/~cslocum/netcdf_example.html
23 __version__ =
'1.0.1_2016-10-18'
27 Copies the attribute named in attr from in_group to out_group.
29 The checks for unicode data are needed because netCDF stores its data in
30 Unicode. However, setncattr will prepend "string" to the attributes if
31 they are unicode values. A long discussion of this appears at:
32 https://github.com/Unidata/netcdf4-python/pull/389
34 attr_val = in_group.getncattr(attr)
35 if isinstance(attr, unicode):
37 if isinstance(attr_val, unicode):
38 attr_val =
str(attr_val)
39 out_group.setncattr(attr, attr_val)
43 Copies the netCDF4 Variable held in src_var from the src_grp Group to the
47 var_type = src_var[1].datatype
48 new_var = dest_grp.createVariable(var_name, var_type,
49 dimensions=src_var[1].dimensions)
50 var_attrs = src_var[1].ncattrs()
51 for var_attr
in var_attrs:
53 dest_grp.variables[var_name][:] =\
54 src_grp.variables[var_name][:]
59 Creates a Group in out_grp using cur_grp_name as the name of the created
60 Group. Then recursively does the same for any subgroups of in_grp. If
61 select_var is not None, then only that Variable will be copied into the new
62 Group; otherwise, all Variables are copied.
65 new_grp = out_parent_grp.createGroup(in_grp_name)
67 grp_variables = in_parent_grp.groups[in_grp_name].variables
69 var_to_copy = grp_variables[select_var]
71 in_parent_grp.groups[in_grp_name], new_grp)
73 for var_item
in grp_variables.items():
79 attrs = in_parent_grp[in_grp_name].ncattrs()
83 subgroups = in_parent_grp[in_grp_name].groups
84 if len(subgroups) > 0:
88 except AttributeError:
89 print (traceback.format_exc())
93 Returns the command line arguments: band to extract, input filename, and
96 parser = argparse.ArgumentParser(description=\
97 'Extracts a band from a netCDF4 OBPG file.')
98 parser.add_argument(
'--version', action=
'version',
99 version=
'%(prog)s ' + __version__)
100 parser.add_argument(
'band', type=str, help=
'band to be extracted')
101 parser.add_argument(
'input_file', type=str, help=
'path to the input file')
102 parser.add_argument(
'output_file', type=str, help=
'path to the output file')
103 args = parser.parse_args()
104 arg_list = [args.band, args.input_file, args.output_file]
109 Primary driver of the program; get command line arguments, check the files
110 specified and kick off the processing
117 if os.path.exists(out_file):
118 err_msg =
'Error! A file named {0} already exists!'.format(out_file)
121 if os.path.exists(in_file):
122 with nc.Dataset(in_file)
as in_dataset:
123 if band
in in_dataset.groups[
'geophysical_data'].variables:
124 with nc.Dataset(out_file,
'w')
as out_dataset:
126 [
'date_created',
'history',
129 cmd_line =
' '.join(sys.argv)
130 orig_history = in_dataset.getncattr(
'history')
131 if isinstance(orig_history, unicode):
132 orig_history =
str(orig_history)
133 history_str =
' ; '.join([orig_history, cmd_line])
134 creation_date_str = time.strftime(
'%Y-%m-%dT%H:%M:%S.000Z',
136 out_dataset.setncattr(
'history', history_str)
137 out_dataset.setncattr(
'product_name', sys.argv[3])
138 out_dataset.setncattr(
'date_created', creation_date_str)
140 err_msg =
'Error! Cannot locate band {0} in {1}.'.\
141 format(band, in_file)
144 err_msg =
'Could NOT find {0}.'.format(in_file)
149 Copy the top level dimensions, Variables, and Attributes from in_dataset
150 to out_dataset. Copy Groups other than the 'geophysical_data' group. In the
151 'geophysical_data' Group, call copy_band to copy only the Variable named
154 for dimension
in in_dataset.dimensions:
155 out_dataset.createDimension(dimension,
156 len(in_dataset.dimensions[dimension]))
157 for var
in in_dataset.variables:
159 global_attrs = in_dataset.ncattrs()
161 for attr
in global_attrs:
162 if not attr
in attrs_to_skip:
165 for attr
in global_attrs:
167 top_groups = in_dataset.groups
168 for grp
in top_groups:
169 if grp ==
'geophysical_data':
175 if __name__ ==
'__main__':