Beispiel #1
0
 def _meta(cls, hdrfile):
     """ Get some metadata from header file """
     hdr = File2List(hdrfile)
     meta = {}
     meta['proj'] = (
         'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984", SPHEROID["WGS_1984",6378137.0,298.257223563]],' +
         'PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]')
     meta['size'] = [int(hdr[23]), int(hdr[24])]
     lat = [float(hdr[12]), float(hdr[14])]
     lat = [min(lat), max(lat)]
     meta['lat'] = lat
     lon = [float(hdr[13]), float(hdr[15])]
     lon = [min(lon), max(lon)]
     meta['lon'] = lon
     meta['res'] = [(lon[1] - lon[0]) / (meta['size'][0] - 1), (lat[1] - lat[0]) / (meta['size'][1] - 1)]
     meta['envihdr'] = [
         'ENVI', 'samples = %s' % meta['size'][0], 'lines = %s' % meta['size'][1],
         'bands = 1', 'header offset = 0', 'file type = ENVI Standard', 'data type = 12',
         'interleave = bsq', 'sensor type = Unknown', 'byte order = 0',
         'coordinate system string = ' + meta['proj'],
         'data ignore value = 0',
         'map info = {Geographic Lat/Lon, 1, 1, %s, %s, %s, %s}'
         % (lon[0], lat[1], meta['res'][0], meta['res'][1])]
     meta['CF'] = float(hdr[21])
     return meta
Beispiel #2
0
 def datafiles(self):
     """ Get list of readable datafiles from asset (multiple filenames if tar or hdf file) """
     path = os.path.dirname(self.filename)
     indexfile = os.path.join(path, self.filename + '.index')
     if os.path.exists(indexfile):
         datafiles = File2List(indexfile)
         if len(datafiles) > 0:
             return datafiles
     try:
         if tarfile.is_tarfile(self.filename):
             tfile = tarfile.open(self.filename)
             tfile = tarfile.open(self.filename)
             datafiles = tfile.getnames()
         else:
             # Try subdatasets
             fh = gdal.Open(self.filename)
             sds = fh.GetSubDatasets()
             datafiles = [s[0] for s in sds]
         if len(datafiles) > 0:
             List2File(datafiles, indexfile)
             return datafiles
         else:
             return [self.filename]
     except:
         raise Exception('Problem accessing asset(s) in %s' % self.filename)
Beispiel #3
0
    def datafiles(self):
        indexfile = self.filename + '.index'

        if os.path.exists(indexfile):
            datafiles = File2List(indexfile)
        else:
            gdalfile = gdal.Open(self.filename)
            subdatasets = gdalfile.GetSubDatasets()
            datafiles = [s[0] for s in subdatasets]
            List2File(datafiles, indexfile)
        return datafiles
Beispiel #4
0
    def _proc_meta(self):
        """ Get some metadata from header file """
        # add asset specific hdr file line number keys to local namespace
        l = self._assets[self.asset]['hdr_lines']
        ###############
        ###############
        datafiles = self.datafiles()
        for f in datafiles:
            if f[-3:] == 'hdr':
                hdrfile = f
            if f[-4:] == 'date':
                datefile = f
                self.rootname = f[:-5]
        hdrfile = super(sarAsset, self).extract((hdrfile, ))[0]
        hdr = File2List(hdrfile)
        RemoveFiles((hdrfile, ))
        ##### LOAD META
        meta = {}
        meta['proj'] = (
            'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984", SPHEROID["WGS_1984",6378137.0,298.257223563]],'
            + 'PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]')

        meta['size'] = [int(hdr[l['rows']]), int(hdr[l['cols']])]
        lat = sorted([float(hdr[l['ul_lat']]), float(hdr[l['lr_lat']])])
        meta['lat'] = lat
        lon = sorted([float(hdr[l['ul_lon']]), float(hdr[l['lr_lon']])])
        meta['lon'] = lon
        meta['res'] = [(lon[1] - lon[0]) / (meta['size'][0] - 1),
                       (lat[1] - lat[0]) / (meta['size'][1] - 1)]
        meta['envihdr'] = [
            'ENVI',
            'samples = %s' % meta['size'][0],
            'lines = %s' % meta['size'][1], 'bands = 1', 'header offset = 0',
            'file type = ENVI Standard', 'data type = 12', 'interleave = bsq',
            'sensor type = Unknown', 'byte order = 0',
            'coordinate system string = ' + meta['proj'],
            'data ignore value = 0',
            'map info = {Geographic Lat/Lon, 1, 1, %s, %s, %s, %s}' %
            (lon[0], lat[1], meta['res'][0], meta['res'][1])
        ]
        meta['CF'] = float(hdr[l['cal_factor']])

        # N.B.: self._meta_dict isn't complete, but is complete enough to open
        #       via _jaxa_opener.  Only need to get date to complete the it.
        self._meta_dict = meta

        dateimg = self._jaxa_opener(datefile)
        dateimg.SetNoData(0)
        datevals = numpy.unique(dateimg.Read())
        dateimg = None
        RemoveFiles((datefile, ), ['.hdr', '.aux.xml'])
        dates = [(self._sensors[self.sensor]['startdate'] +
                  datetime.timedelta(days=int(d))) for d in datevals if d != 0]
        if not dates:
            raise Exception('%s: no valid dates' % self.bname)
        date = min(dates)
        self._meta_dict['min_date'] = date
        #VerboseOut('Date from image: %s' % str(date),3)
        # If year provided check
        #if fname[7] == 'Y' and fname[8:10] != '00':
        #    ydate = datetime.datetime.strptime(fname[8:10], '%y')
        #    if date.year != ydate.year:
        #        raise Exception('%s: Date %s outside of expected year (%s)' % (fname, str(date),str(ydate)))
        # If widebeam check cycle dates
        if self.asset == 'alos1' and self.is_cycle:
            cycledates = self._assets[self.asset]['cycledates']
            cdate = datetime.datetime.strptime(cycledates[self.cyid],
                                               '%d-%b-%y').date()
            delta = (date - cdate).days
            utils.verbose_out(
                '{}: {} days different between datearray and cycledate'.format(
                    self.bname, delta))
            if not (0 <= delta <= 45):
                raise Exception('%s: Date %s outside of cycle range (%s)' %
                                (self.bname, str(date), str(cdate)))