4 This L3 aggregator script will:
5 1. Take in L3 mapped files
21 for group_name, group
in topB.groups.items():
22 topA.createGroup(group_name)
25 for attr
in group.ncattrs():
26 topA[group_name].setncattr(attr, group.getncattr(attr))
29 print(
"copying variables for group", group_name,
":")
32 for name, variable
in group.variables.items():
36 if "_FillValue" in variable.ncattrs():
37 fill_value = variable.getncattr(
"_FillValue")
41 topA[group_name].createVariable(name, variable.datatype, variable.dimensions, fill_value=fill_value)
43 for attr
in variable.ncattrs():
44 if attr !=
"_FillValue":
45 topA[group_name].variables[name].setncattr(attr, variable.getncattr(attr))
47 topA[group_name].variables[name][:] = variable[:]
49 if len(group.groups) != 0:
54 def merge(idatasets,ofile, products):
62 orootgrp = nc.Dataset(ofile,
"w", format=
"NETCDF4")
64 for name, dim
in idatasets[0].dimensions.items():
65 orootgrp.createDimension(name, len(dim)
if not dim.isunlimited()
else None)
68 for attr
in idatasets[0].ncattrs():
69 if attr ==
"product_name":
70 orootgrp.setncattr(attr, ofile)
72 orootgrp.setncattr(attr, idatasets[0].getncattr(attr))
76 dims = orootgrp.dimensions
77 nlat : int = dims[
"lat"].size
78 nlon : int = dims[
"lon"].size
80 print(
"copying variables from the file roots:")
83 if dims[
"lat"].size != nlat:
84 print(f
"--Error--: latitude dimenstions mismatch for {ds.product_name}")
86 if dims[
"lon"].size != nlon:
87 print(f
"--Error--: longitude dimenstions mismatch for {ds.product_name}")
89 for name, ivariable
in ds.variables.items():
91 if (name
not in orootgrp.variables):
95 if "_FillValue" in ivariable.ncattrs():
96 fill_value = ivariable.getncattr(
"_FillValue")
99 ovariable = orootgrp.createVariable(name, ivariable.datatype, ivariable.dimensions, fill_value=fill_value)
101 for attr
in ivariable.ncattrs():
103 if attr !=
"_FillValue":
104 ovariable.setncattr(attr, ivariable.getncattr(attr))
106 ovariable[:] = ivariable[:]
111 if __name__ ==
"__main__":
113 parser = argparse.ArgumentParser(
114 description=
'Merge Level 3 mapped files')
115 parser.add_argument(
'--version', action=
'store_true', help=
'print program version')
116 parser.add_argument(
'-v',
'--verbose', help=
'print status messages', action=
'store_true')
117 parser.add_argument(
'ifile', help=
'Input files (comma separated list of filenames) or a ASCII file which contains paths of L3 netcdf4 files to be merged')
118 parser.add_argument(
'ofile', help=
'Output netcdf file which will contain merged L3 file data')
119 parser.add_argument(
'--product', type=str, help=
'product(s) to map; comma separated', default =
None)
121 print(
"l3mapmerge", __version__)
122 if len(sys.argv) == 1:
125 if(sys.argv[1] ==
"--version"):
128 args = parser.parse_args()
129 verbose = args.verbose
130 ifile : str = args.ifile
131 ifiles = ifile.split(sep=
',')
136 with open(args.ifile)
as infile:
138 idatasets.append(nc.Dataset(line.strip(),
'r'))
140 print(
"--Error-- when trying to open input file: ", e)
144 for ncfile
in ifiles:
145 idatasets.append(nc.Dataset(ncfile.strip(),
'r'))
147 print(
"--Error-- when trying to open input file: ", e)
150 print(
"No products were specified. Exiting ...")
152 products : list[str] = args.product.split(
",") + [
'lat',
'lon']
154 print(
"Merging datasets...")
155 merge(idatasets, args.ofile, products)