4 This L2 aggregator script will:
22 for group_name, group
in topB.groups.items():
23 topA.createGroup(group_name)
26 for attr
in group.ncattrs():
27 topA[group_name].setncattr(attr, group.getncattr(attr))
30 if group_name ==
"geophysical_data":
34 print(
"copying variables for group", group_name,
":")
37 for name, variable
in group.variables.items():
41 if "_FillValue" in variable.ncattrs():
42 fill_value = variable.getncattr(
"_FillValue")
46 topA[group_name].createVariable(name, variable.datatype, variable.dimensions, fill_value=fill_value)
48 for attr
in variable.ncattrs():
49 if attr !=
"_FillValue":
50 topA[group_name].variables[name].setncattr(attr, variable.getncattr(attr))
52 topA[group_name].variables[name][:] = variable[:]
54 if len(group.groups) != 0:
58 def merge(idatasets, mergefile, products):
67 orootgrp = netCDF4.Dataset(pathlib.Path(mergefile),
"w", format=
"NETCDF4")
70 for name, dim
in idatasets[0].dimensions.items():
71 orootgrp.createDimension(name, len(dim)
if not dim.isunlimited()
else None)
74 for attr
in idatasets[0].ncattrs():
75 if attr ==
"product_name":
76 orootgrp.setncattr(attr, mergefile)
78 orootgrp.setncattr(attr, idatasets[0].getncattr(attr))
84 print(
"copying variables for group geophysical_data:")
88 geophysical_data = orootgrp[
"/geophysical_data"]
90 for name, ivariable
in ds[
'/geophysical_data'].variables.items():
92 if (name
not in geophysical_data.variables):
96 if "_FillValue" in ivariable.ncattrs():
97 fill_value = ivariable.getncattr(
"_FillValue")
100 ovariable = geophysical_data.createVariable(name, ivariable.datatype, ivariable.dimensions, fill_value=fill_value)
102 for attr
in ivariable.ncattrs():
104 if attr !=
"_FillValue":
105 ovariable.setncattr(attr, ivariable.getncattr(attr))
107 ovariable[:] = ivariable[:]
112 if __name__ ==
"__main__":
115 parser = argparse.ArgumentParser(
116 description=
'Merge Level 2 files')
117 parser.add_argument(
'--version', action=
'store_true', help=
'print program version')
118 parser.add_argument(
'-v',
'--verbose', help=
'print status messages', action=
'store_true')
119 parser.add_argument(
'ifile', help=
'Input text file specifying L2 files to be merged')
120 parser.add_argument(
'ofile', help=
'Output netcdf file which will contain merged L2 file data')
121 parser.add_argument(
'--product', type=str, help=
'product(s) to map; comma separated', default =
"None")
123 print(
"l2merge", __version__)
124 if len(sys.argv) == 1:
128 if(sys.argv[1] ==
"--version"):
131 args = parser.parse_args()
132 verbose = args.verbose
137 with open(args.ifile)
as infile:
139 idatasets.append(netCDF4.Dataset(line.strip(),
'r'))
141 print(
"Error when trying to open input file: ", e)
144 products = args.product.split(
",")
145 products.append(
"l2_flags")
147 print(
"Merging datasets...")
149 merge(idatasets, args.ofile, products)
150 print(
"Merging was successful")
151 except Exception
as e:
152 print(
"Unable to merge L2 files: ", e)