Skip to content

Commit

Permalink
nansencenter#525: history is cut by gdal CreateCopy. This needs to be…
Browse files Browse the repository at this point in the history
… overridden.
  • Loading branch information
mortenwh committed Jan 5, 2023
1 parent 54f17ab commit cfc5a28
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions nansat/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,29 @@ def export(self, filename='', bands=None, rm_metadata=None, add_geolocation=True
# Rename variable names to get rid of the band numbers
self.rename_variables(filename)
# Rename attributes to get rid of "GDAL_" added by gdal
self.rename_attributes(filename)
self.correct_attributes(filename, history=self.vrt.dataset.GetMetadata()['history'])

self.logger.debug('Export - OK!')

@staticmethod
def rename_attributes(filename):
def correct_attributes(filename, history=None):
""" Rename global attributes to get rid of the "GDAL_"-string
added by gdal.
added by gdal, remove attributes added by gdal that are
already present in the Nansat object, and correct the history
attribute (the latter may be reduced in length because
gdal.GetDriverByName(driver).CreateCopy limits the string
lenght to 161 characters).
"""
GDAL = "GDAL_"
del_attrs = []
rename_attrs = []
# Open new file to edit attribute names
ds = Dataset(filename, 'r+')
""" The netcdf driver adds the Conventions attribute with
value CF-1.5. This may be wrong, so it is better to use the
Conventions metadata from the Nansat object. Other attributes
added by gdal that are already present in Nansat, should also
be deleted."""
value CF-1.5. This in most cases wrong, so it is better to use
the Conventions metadata from the Nansat object. Other
attributes added by gdal that are already present in Nansat,
should also be deleted."""
for attr in ds.ncattrs():
if GDAL in attr:
if attr.replace(GDAL, "") in ds.ncattrs():
Expand All @@ -163,6 +167,9 @@ def rename_attributes(filename):
# Rename attributes:
for attr in rename_attrs:
ds.renameAttribute(attr, attr.replace(GDAL, ""))
# Correct the history
if history is not None:
ds.history = history
ds.close()

@staticmethod
Expand Down

0 comments on commit cfc5a28

Please sign in to comment.