8 from os.path
import basename
15 from shutil
import copyfile
as cp
16 import xml.etree.ElementTree
as ET
26 north=None, south=None, west=None, east=None,
27 spixl=None, epixl=None, sline=None, eline=None,
30 self.
idir = pathlib.Path(idir)
31 self.
odir = pathlib.Path(odir)
59 print(
'Extracting', srcfile)
64 infile = netCDF4.Dataset(srcfile,
'r')
66 outfile = netCDF4.Dataset(dstfile,
'r+')
69 nadir_bin = np.dtype(
'int32').
type(infile.nadir_bin)
70 if (nadir_bin > self.
epixl):
71 outfile.nadir_bin = np.dtype(
'int32').
type(-1)
73 outfile.nadir_bin = np.dtype(
'int32').
type(nadir_bin - (self.
spixl + 1))
80 if 'extract_pixel_start' in infile.ncattrs():
81 outfile.extract_pixel_start = np.dtype(
'int32').
type(infile.extract_pixel_start + self.
spixl + 1)
82 outfile.extract_pixel_stop = np.dtype(
'int32').
type(infile.extract_pixel_stop + self.
epixl + 1)
83 outfile.extract_line_start = np.dtype(
'int32').
type(infile.extract_line_start + self.
sline + 1)
84 outfile.extract_line_stop = np.dtype(
'int32').
type(infile.extract_line_stop + self.
eline + 1)
86 outfile.extract_pixel_start = np.dtype(
'int32').
type(self.
spixl + 1)
87 outfile.extract_pixel_stop = np.dtype(
'int32').
type(self.
epixl + 1)
88 outfile.extract_line_start = np.dtype(
'int32').
type(self.
sline + 1)
89 outfile.extract_line_stop = np.dtype(
'int32').
type(self.
eline + 1)
97 infile = netCDF4.Dataset(srcfile,
'r')
99 outfile = netCDF4.Dataset(dstfile,
'r+')
103 if 'nadir_view_time' in infile.groups[
'bin_attributes'].variables:
105 infile_start_sec = infile.groups[
'bin_attributes'].variables[
'nadir_view_time'][0]
106 infile_end_sec = infile.groups[
'bin_attributes'].variables[
'nadir_view_time'][infile.dimensions[
'bins_along_track'].size - 1]
108 outfile_start_sec = outfile.groups[
'bin_attributes'].variables[
'nadir_view_time'][0]
109 outfile_end_sec = outfile.groups[
'bin_attributes'].variables[
'nadir_view_time'][outfile.dimensions[
'bins_along_track'].size - 1]
112 infile_start_time = infile.time_coverage_start
113 infile_end_time = infile.time_coverage_end
116 start_form = datetime.datetime.strptime(infile_start_time[0:20],
'%Y-%m-%dT%H:%M:%SZ')
117 end_form = datetime.datetime.strptime(infile_end_time[0:20],
'%Y-%m-%dT%H:%M:%SZ')
119 epoch = datetime.datetime.strptime(
'1970 01 01 00 00 00',
'%Y %m %d %H %M %S')
122 if 'time_nadir' in infile.groups[
'geolocation_data'].variables:
127 infile_start_sec: float = infile.groups[
'geolocation_data'].variables[
'time_nadir'][0]
128 infile_end_sec = infile.groups[
'geolocation_data'].variables[
'time_nadir'][infile.dimensions[
'bins_along_track'].size - 1]
130 outfile_start_sec = outfile.groups[
'geolocation_data'].variables[
'time_nadir'][0]
131 outfile_end_sec = outfile.groups[
'geolocation_data'].variables[
'time_nadir'][outfile.dimensions[
'bins_along_track'].size - 1]
134 infile_start_time = infile.time_coverage_start
135 infile_end_time = infile.time_coverage_end
138 start_form = datetime.datetime.strptime(infile_start_time[0:20],
'%Y-%m-%dT%H:%M:%S')
139 end_form = datetime.datetime.strptime(infile_end_time[0:20],
'%Y-%m-%dT%H:%M:%S')
141 epoch = datetime.datetime.strptime(
'1970 01 01 00 00 00',
'%Y %m %d %H %M %S')
144 diff_start = start_form - epoch
146 diff_end = end_form - epoch
149 diff_sec_start = diff_start.total_seconds()
151 diff_sec_end = diff_end.total_seconds()
154 diff_infile_outfile_start = outfile_start_sec - infile_start_sec
155 diff_infile_outfile_end = outfile_end_sec - infile_end_sec
158 outfile_tot_start_sec = diff_sec_start + diff_infile_outfile_start
159 outfile_tot_end_sec = diff_sec_end + diff_infile_outfile_end
162 outfile_start_time_since = time.gmtime(outfile_tot_start_sec)
163 outfile_end_time_since = time.gmtime(outfile_tot_end_sec)
166 ostart_y = outfile_start_time_since.tm_year
167 ostart_mon =
"{0:0=2d}".format(outfile_start_time_since.tm_mon)
168 ostart_d =
"{0:0=2d}".format(outfile_start_time_since.tm_mday)
169 ostart_h =
"{0:0=2d}".format(outfile_start_time_since.tm_hour)
170 ostart_min =
"{0:0=2d}".format(outfile_start_time_since.tm_min)
171 ostart_s =
"{0:0=2d}".format(outfile_start_time_since.tm_sec)
173 oend_y = outfile_end_time_since.tm_year
174 oend_mon =
"{0:0=2d}".format(outfile_end_time_since.tm_mon)
175 oend_d =
"{0:0=2d}".format(outfile_end_time_since.tm_mday)
176 oend_h =
"{0:0=2d}".format(outfile_end_time_since.tm_hour)
177 oend_min =
"{0:0=2d}".format(outfile_end_time_since.tm_min)
178 oend_s =
"{0:0=2d}".format(outfile_end_time_since.tm_sec)
181 outfile.time_coverage_start =
str(ostart_y) +
'-' +
str(ostart_mon) +
'-' +
str(ostart_d) +
'T' +
str(ostart_h) +
':' +
str(ostart_min) +
':' +
str(ostart_s) +
'Z'
182 outfile.time_coverage_end =
str(oend_y) +
'-' +
str(oend_mon) +
'-' +
str(oend_d) +
'T' +
str(oend_h) +
':' +
str(oend_min) +
':' +
str(oend_s) +
'Z'
189 lat = outfile.dimensions[
'bins_along_track'].size - 1
190 lon = outfile.dimensions[
'bins_across_track'].size - 1
192 latitude = outfile.groups[
'geolocation_data'].variables[
'latitude']
193 longitude = outfile.groups[
'geolocation_data'].variables[
'longitude']
195 lon_min = longitude[0, 0]
196 lon_max = longitude[lat, lon]
197 lat_min = latitude[0, 0]
198 lat_max = latitude[lat , lon]
201 lat_add = np.dtype(
'int32').
type((lat_max - lat_min) / 20)
203 lon_add = (lon_max - lon_min) / 2
211 for i
in range(0, lat - 1, np.dtype(
'int32').
type(lat/lat_add)):
218 lat_l =
list(reversed(lat_r))
224 lon_u = [lon, lon/2, 0]
226 lat_u = [lat, lat, lat]
228 lon_d =
list(reversed(lon_u))
233 for i
in range(len(lat_u)):
234 lat_values.append(np.dtype(
'float32').
type(lat_u[i]))
235 lon_values.append(lon_u[i])
237 for i
in range(len(lat_l)):
238 lat_values.append(lat_l[i])
239 lon_values.append(lon_l[i])
241 for i
in range(len(lat_d)):
242 lat_values.append(lat_d[i])
243 lon_values.append(lon_d[i])
245 for i
in range(len(lat_r)):
246 lat_values.append(lat_r[i])
247 lon_values.append(lon_r[i])
253 p_seq.append(np.dtype(
'int32').
type(i + 1))
255 outfile.groups[
'geolocation_data'].setncattr(
'gringpointlatitude', lat_values)
256 outfile.groups[
'geolocation_data'].setncattr(
'gringpointlongitude', lon_values)
257 outfile.groups[
'geolocation_data'].setncattr(
'gringpointsequence', p_seq)
263 print(
"north={} south={} west={} east={}".
271 pl.lonlat2pixline(zero=
False)
273 (pl.spixl, pl.epixl, pl.sline, pl.eline)
282 subset = {
'bins_along_track':[self.
spixl, self.
epixl],
283 'bins_across_track': [self.
sline, self.
eline]}
289 if __name__ ==
"__main__":
292 parser = argparse.ArgumentParser(
293 description=
'Extract specified area from OLCI Level 1C files.',
294 epilog=
'Specify either geographic limits or pixel/line ranges, not both.')
295 parser.add_argument(
'-v',
'--verbose', help=
'print status messages',
297 parser.add_argument(
'idir',
298 help=
'Level 1C input file path')
299 parser.add_argument(
'odir', nargs=
'?',
300 help=
'output file path')
302 group1 = parser.add_argument_group(
'geographic limits')
303 group1.add_argument(
'-n',
'--north', type=float, help=
'northernmost latitude')
304 group1.add_argument(
'-s',
'--south', type=float, help=
'southernmost latitude')
305 group1.add_argument(
'-w',
'--west', type=float, help=
'westernmost longitude')
306 group1.add_argument(
'-e',
'--east', type=float, help=
'easternmost longitude')
308 group2 = parser.add_argument_group(
'pixel/line ranges (1-based)')
309 group2.add_argument(
'--spixl', type=int, help=
'start pixel')
310 group2.add_argument(
'--epixl', type=int, help=
'end pixel')
311 group2.add_argument(
'--sline', type=int, help=
'start line')
312 group2.add_argument(
'--eline', type=int, help=
'end line')
314 if len(sys.argv) == 1:
317 args = parser.parse_args()
330 verbose=args.verbose)
334 goodlatlons =
None not in (this.north, this.south, this.west, this.east)
335 goodindices =
None not in (this.spixl, this.epixl, this.sline, this.eline)
336 if (goodlatlons
and goodindices):
337 print(
"ERROR: Specify either geographic limits or pixel/line ranges, not both.")
340 status = this.getpixlin()
341 if status
not in (0, 110):
342 print(
"No extract; lonlat2pixline status =", status)
347 print(
"ERROR: Specify all values for either geographic limits or pixel/line ranges.")