Commit 414102be authored by PIOLLE's avatar PIOLLE
Browse files

creates a new file is slicing is requested on the target

parent b5288c83
...@@ -55,8 +55,8 @@ def instantiate_features(srcproduct, ...@@ -55,8 +55,8 @@ def instantiate_features(srcproduct,
product file. product file.
targetmapperclass (:class:AbstractMapper): mapper class for the target targetmapperclass (:class:AbstractMapper): mapper class for the target
product file. product file.
srcslices (str, optional): slices for srcslices (, optional): slices for
targetslices (str, optional): full path to the target product file. targetslices (, optional):
srcargs (dict): additional input arguments for source mapper srcargs (dict): additional input arguments for source mapper
instantiation. instantiation.
""" """
...@@ -84,6 +84,8 @@ def resample(srcfeature, ...@@ -84,6 +84,8 @@ def resample(srcfeature,
filename=None, filename=None,
use_source_time=False, use_source_time=False,
mask=None, mask=None,
copy=False,
attrs=None,
**kwargs **kwargs
): ):
"""Resample source product on a target """Resample source product on a target
...@@ -104,8 +106,12 @@ def resample(srcfeature, ...@@ -104,8 +106,12 @@ def resample(srcfeature,
This is meant for instance to mask land data over which This is meant for instance to mask land data over which
interpolation may have been performed in the resampling interpolation may have been performed in the resampling
process. process.
copy (bool): if True, make a copy of the target feature and add
the resampled source fields into it.
""" """
if copy:
targetfeature = targetfeature.extract_subset()
# resampling # resampling
if fields is None: if fields is None:
resampled_fields = srcfeature.get_fieldnames() resampled_fields = srcfeature.get_fieldnames()
...@@ -126,7 +132,7 @@ def resample(srcfeature, ...@@ -126,7 +132,7 @@ def resample(srcfeature,
targetfeature, targetfeature,
fields=resampled_fields, fields=resampled_fields,
radius=radius, radius=radius,
new=True, new=not copy,
add_reference=True, add_reference=True,
**kwargs **kwargs
) )
...@@ -156,6 +162,7 @@ def resample(srcfeature, ...@@ -156,6 +162,7 @@ def resample(srcfeature,
if outfeature: if outfeature:
# set global attributes # set global attributes
globalattrs = {} globalattrs = {}
globalattrs.update(attrs)
timevals = srcfeature.get_times() timevals = srcfeature.get_times()
time0 = timevals.min() time0 = timevals.min()
time1 = timevals.max() time1 = timevals.max()
...@@ -163,6 +170,7 @@ def resample(srcfeature, ...@@ -163,6 +170,7 @@ def resample(srcfeature,
maxtime = num2date(time1, srcfeature.get_time_units()) maxtime = num2date(time1, srcfeature.get_time_units())
globalattrs['time_coverage_start'] = mintime.strftime('%Y%m%dT%H%M%S') globalattrs['time_coverage_start'] = mintime.strftime('%Y%m%dT%H%M%S')
globalattrs['time_coverage_stop'] = maxtime.strftime('%Y%m%dT%H%M%S') globalattrs['time_coverage_stop'] = maxtime.strftime('%Y%m%dT%H%M%S')
if method[0] == 'closest_neighbour': if method[0] == 'closest_neighbour':
globalattrs['spatial_colocation_radius_in_km'] = radius / 1000. globalattrs['spatial_colocation_radius_in_km'] = radius / 1000.
# set time # set time
...@@ -426,6 +434,17 @@ if __name__ == "__main__": ...@@ -426,6 +434,17 @@ if __name__ == "__main__":
mask_field = options.target_mask.split('/')[0] mask_field = options.target_mask.split('/')[0]
mask_value = int(options.target_mask.split('/')[1]) mask_value = int(options.target_mask.split('/')[1])
mask = (targetfeature.get_values(mask_field) != mask_value) mask = (targetfeature.get_values(mask_field) != mask_value)
# creates automatically a copy of target if slicing is required
copy = target_slices is not None
# output global attributes
attrs = {}
attrs['source_filename'] = srcfeature.get_mapper().get_basename()
attrs['target_filename'] = targetfeature.get_mapper().get_basename()
attrs['source_subset_slices'] = str(source_slices)
attrs['target_subset_slices'] = str(target_slices)
if srcfeature.__class__.__name__ == 'GridTimeSeries' and timestep is None: if srcfeature.__class__.__name__ == 'GridTimeSeries' and timestep is None:
for grid in srcfeature: for grid in srcfeature:
date = grid.get_datetimes()[0] date = grid.get_datetimes()[0]
...@@ -441,7 +460,8 @@ if __name__ == "__main__": ...@@ -441,7 +460,8 @@ if __name__ == "__main__":
outdir=output, outdir=output,
filename=filename, filename=filename,
use_source_time=use_source_time, use_source_time=use_source_time,
mask=mask mask=mask,
attrs=attrs
) )
else: else:
if srcfeature.__class__.__name__ == 'GridTimeSeries': if srcfeature.__class__.__name__ == 'GridTimeSeries':
...@@ -454,5 +474,7 @@ if __name__ == "__main__": ...@@ -454,5 +474,7 @@ if __name__ == "__main__":
method=method, method=method,
outdir=output, outdir=output,
use_source_time=use_source_time, use_source_time=use_source_time,
mask=mask mask=mask,
copy=copy,
attrs=attrs
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment