-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified export function to avoid change of attribute names, and added test about fill values #526
Changes from 20 commits
56996cc
7e5a1f6
fa71c83
9677223
fceb4b5
6d715b9
68c01b7
1a53f4b
f754a8a
d90e2e3
18fc477
365ec55
21e4334
0ca6093
d1be8e9
110789c
2c45363
d4d6495
f276d0e
a671dc8
54f17ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import tempfile | ||
import datetime | ||
import warnings | ||
import importlib | ||
|
||
from nansat.utils import gdal | ||
import numpy as np | ||
|
@@ -30,17 +31,22 @@ | |
|
||
from nansat.exceptions import NansatGDALError | ||
|
||
try: | ||
import xarray as xr | ||
except: | ||
warnings.warn("'xarray' needs to be installed for Exporter.xr_export to work.") | ||
|
||
Comment on lines
+34
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
|
||
class Exporter(object): | ||
"""Abstract class for export functions """ | ||
DEFAULT_INSTITUTE = 'NERSC' | ||
DEFAULT_SOURCE = 'satellite remote sensing' | ||
|
||
UNWANTED_METADATA = ['dataType', 'SourceFilename', 'SourceBand', '_Unsigned', 'FillValue', | ||
'time', '_FillValue', 'type', 'scale', 'offset'] | ||
'_FillValue', 'type', 'scale', 'offset', 'NETCDF_VARNAME'] | ||
|
||
def export(self, filename='', bands=None, rm_metadata=None, add_geolocation=True, | ||
driver='netCDF', options=None, hardcopy=False): | ||
driver='netCDF', options='FORMAT=NC4', hardcopy=False): | ||
"""Export Nansat object into netCDF or GTiff file | ||
|
||
Parameters | ||
|
@@ -57,7 +63,8 @@ def export(self, filename='', bands=None, rm_metadata=None, add_geolocation=True | |
add geolocation array datasets to exported file? | ||
driver : str | ||
Name of GDAL driver (format) | ||
options : str or list | ||
options : str or list (default: 'FORMAT=NC4' for NetCDF 4 | ||
file format) | ||
GDAL export options in format of: 'OPT=VAL', or | ||
['OPT1=VAL1', 'OP2='VAL2'] | ||
See also http://www.gdal.org/frmt_netcdf.html | ||
|
@@ -70,11 +77,6 @@ def export(self, filename='', bands=None, rm_metadata=None, add_geolocation=True | |
|
||
Notes | ||
------ | ||
If number of bands is more than one, serial numbers are added at the end of each band name. | ||
It is possible to fix it by changing line.4605 in GDAL/frmts/netcdf/netcdfdataset.cpp : | ||
'if( nBands > 1 ) sprintf(szBandName,"%s%d",tmpMetadata,iBand);' | ||
--> 'if( nBands > 1 ) sprintf(szBandName,"%s",tmpMetadata);' | ||
|
||
CreateCopy fails in case the band name has special characters, | ||
e.g. the slash in 'HH/VV'. | ||
|
||
|
@@ -116,14 +118,74 @@ def export(self, filename='', bands=None, rm_metadata=None, add_geolocation=True | |
add_gcps = export_vrt.prepare_export_netcdf() | ||
|
||
# Create output file using GDAL | ||
dataset = gdal.GetDriverByName(driver).CreateCopy(filename, export_vrt.dataset, options=options) | ||
dataset = gdal.GetDriverByName(driver).CreateCopy(filename, export_vrt.dataset, | ||
options=options) | ||
del dataset | ||
# add GCPs into netCDF file as separate float variables | ||
if add_gcps: | ||
Exporter._add_gcps(filename, export_vrt.dataset.GetGCPs()) | ||
|
||
if driver=='netCDF': | ||
# 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.logger.debug('Export - OK!') | ||
|
||
@staticmethod | ||
def rename_attributes(filename): | ||
""" Rename global attributes to get rid of the "GDAL_"-string | ||
added by gdal. | ||
""" | ||
GDAL = "GDAL_" | ||
del_attrs = [] | ||
rename_attrs = [] | ||
# Open new file to edit attribute names | ||
ds = Dataset(filename, 'r+') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use with |
||
for attr in ds.ncattrs(): | ||
if GDAL in attr: | ||
if attr.replace(GDAL, "") in ds.ncattrs(): | ||
# Mark for deletion | ||
del_attrs.append(attr) | ||
else: | ||
# Mark for renaming | ||
rename_attrs.append(attr) | ||
|
||
# Delete repeated attributes.. | ||
for attr in del_attrs: | ||
ds.delncattr(attr) | ||
# Rename attributes: | ||
for attr in rename_attrs: | ||
ds.renameAttribute(attr, attr.replace(GDAL, "")) | ||
ds.close() | ||
|
||
@staticmethod | ||
def rename_variables(filename): | ||
""" Rename variable names to reflect the name attribute of | ||
the variable's metadata. | ||
|
||
Parameters | ||
---------- | ||
filename : str | ||
NetCDF file name | ||
""" | ||
# Open new file to edit variable names | ||
ds = Dataset(filename, 'r+') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use with.. |
||
|
||
# Decide which variables to rename | ||
rename_vars = [] | ||
for var in ds.variables.keys(): | ||
if ('name' in ds.variables[var].ncattrs()) and ( | ||
var != ds.variables[var].getncattr('name')): | ||
rename_vars.append(var) | ||
|
||
# Rename selected variables | ||
for var in rename_vars: | ||
ds.renameVariable(var, ds.variables[var].getncattr('name')) | ||
|
||
ds.close() | ||
|
||
def export2thredds(self, | ||
filename, | ||
bands=None, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove