nansencenter / nansat

Scientist friendly Python toolbox for processing 2D satellite Earth observation data.
http://nansat.readthedocs.io
GNU General Public License v3.0
182 stars 66 forks source link

create_bands fails becaise metadata missed in src metadict #371

Open korvinos opened 6 years ago

korvinos commented 6 years ago

I am trying to write a mapper for an S1 IW data provided in NetCDF files. A part of code from the mapper:


    def __init__(self, filename, gdal_dataset, gdal_metadata, *args, **kwargs):
        # Check if an input file can be read with this mapper, else raise error
        Mapper.check_input(filename)

        gdal_metadata = VRT._remove_strings_in_metadata_keys(gdal_metadata, ['NC_GLOBAL#'])
        lon = gdal.Open(gdal_dataset.GetSubDatasets()[0][0]).ReadAsArray()
        lat = gdal.Open(gdal_dataset.GetSubDatasets()[1][0]).ReadAsArray()
        self._init_from_lonlat(lon, lat, metadata=gdal_metadata)
        metadict = map(lambda filename: self.create_metadict_pair(filename),
                                  enumerate(self._get_sub_filenames(gdal_dataset)))

        # create bands
        self.create_bands(metadict)

The code fails while create_bands with:


KeyError                                  Traceback (most recent call last)
<ipython-input-4-ba90b27f68f7> in <module>()
----> 1 n = Nansat(src)

/home/vagrant/miniconda/lib/python2.7/site-packages/nansat/nansat.pyc in __init__(self, filename, mapper, log_level, **kwargs)
    151         self._init_empty(filename, log_level)
    152         # Create VRT object with mapping of variables
--> 153         self.vrt = self._get_mapper(mapper, **kwargs)
    154 
    155     def __getitem__(self, band_id):

/home/vagrant/miniconda/lib/python2.7/site-packages/nansat/nansat.pyc in _get_mapper(self, mappername, **kwargs)
   1139                 # create a Mapper object and get VRT dataset from it
   1140                 try:
-> 1141                     tmp_vrt = nansatMappers[iMapper](self.filename, gdal_dataset, metadata, **kwargs)
   1142                     self.logger.info('Mapper %s - success!' % iMapper)
   1143                     self.mapper = iMapper.replace('mapper_', '')

/vagrant/shared/src/sar-doppler/nansat_mappers/mapper_sentinel1_iw.py in __init__(self, filename, gdal_dataset, gdal_metadata, *args, **kwargs)
     60 
     61         # create bands
---> 62         self.create_bands(metadict)
     63         # self._create_complex_bands(self._get_sub_filenames(gdal_dataset))
     64 

/home/vagrant/miniconda/lib/python2.7/site-packages/nansat/vrt.pyc in create_bands(self, metadata_dict)
    918             src = band_dict['src']
    919             dst = band_dict.get('dst', None)
--> 920             self.create_band(src, dst)
    921             self.logger.debug('Creating band - OK!')
    922         self.dataset.FlushCache()

/home/vagrant/miniconda/lib/python2.7/site-packages/nansat/vrt.pyc in create_band(self, src, dst)
    993 
    994         srcs = list(map(VRT._make_source_bands_xml, srcs))
--> 995         options = VRT._set_add_band_options(srcs, dst)
    996         dst['dataType'] = VRT._get_dst_band_data_type(srcs, dst)
    997         dst['name'], wkv = self._create_band_name(dst)

/home/vagrant/miniconda/lib/python2.7/site-packages/nansat/vrt.pyc in _set_add_band_options(srcs, dst)
   1726             options = ['subclass=VRTRawRasterBand',
   1727                        'SourceFilename=%s' % srcs[0]['SourceFilename'],
-> 1728                        'ImageOffset=%d' % srcs[0]['ImageOffset'],
   1729                        'PixelOffset=%d' % srcs[0]['PixelOffset'],
   1730                        'LineOffset=%d' % srcs[0]['LineOffset'],

KeyError: u'ImageOffset'

Thus, the code fails in _set_add_band_options because PixelFunction is not specified in a src metadict as well as certain metadata (ImageOffset).

Therefore, I am wondering, could I manually specify a PixelFunction or other metadata? Or maybe there is another, a better way to solve this issue?

akorosov commented 6 years ago

Can you add content of metadict here also?