Exemplo n.º 1
0
Arquivo: core.py Projeto: aniucd/gips
 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)
Exemplo n.º 2
0
 def datafiles(self):
     datafiles = super(prismAsset, self).datafiles()
     datafiles = filter(lambda x: x.lower().endswith('.bil'), datafiles)
     if len(datafiles) > 0:
         indexfile = self.filename + '.index'
         utils.verbose_out('indexfile: {}'.format(indexfile), 3)
         List2File(datafiles, indexfile)
         return datafiles
Exemplo n.º 3
0
    def __init__(self, bandnum, wvlen1, wvlen2, dtime, lat, lon, profile=False):
        self.lat = lat
        self.lon = lon
        self.datetime = dtime
        seconds = (dtime.second + dtime.microsecond / 1000000.) / 3600.
        self.dtime = self.datetime.hour + self.datetime.minute / 60.0 + seconds
        self.julianday = (dtime - datetime.datetime(dtime.year, 1, 1)).days + 1

        self.model = atmospheric_model(self.julianday, lat)

        #fout = open('atm.txt','w')
        #fout.write('{:>5}{:>20}{:>20}\n'.format('Band','%T','Radiance'))

        tmpdir = tempfile.mkdtemp()
        pwd = os.getcwd()
        os.chdir(tmpdir)

        # Create link to MODTRAN data dir
        if not os.path.lexists('DATA'):
            os.symlink(self._datadir, 'DATA')

        if profile:
            mprofile = merraData.profile(lon, lat, dtime)
            pressure = mprofile['pressure']
            temp = mprofile['temp']
            humidity = mprofile['humidity']
            ozone = mprofile['ozone']
            self.atmprofile = []
            for i in range(0, len(pressure)):
                c2c1 = self.card2c1(P=pressure[i], T=temp[i], H2O=humidity[i], O3=ozone[i])
                self.atmprofile.append(c2c1)
        else:
            self.atmprofile = None

        # Generate MODTRAN input files

        # Determine if radiance or transmittance mode
        rootnames = self.addband(bandnum, wvlen1, wvlen2)
        List2File(rootnames, 'mod5root.in')

        try:
            # run output and get results
            modout = commands.getstatusoutput('modtran')
            #VerboseOut("MODTRAN Output:", 4)
            #[VerboseOut(m, 4) for m in modout]
            self.output = self.readoutput(bandnum)
            verbose_out('MODTRAN Output: %s' % ' '.join([str(s) for s in self.output]), 4)
        except:
            verbose_out(modout, 4)
            raise

        # Change back to original directory
        os.chdir(pwd)

        #print 'MODTRAN dir: ', tmpdir
        # Remove directory
        shutil.rmtree(tmpdir)
Exemplo n.º 4
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
Exemplo n.º 5
0
 def extract(self, filenames=[], path=None):
     """ Extract filenames from asset and create ENVI header files """
     files = super(sarAsset, self).extract(filenames, path=path)
     meta = self.get_meta_dict()
     datafiles = {}
     for f in files:
         bname = os.path.basename(f)
         if f[-3:] != 'hdr':
             bandname = bname[len(self.rootname) + 1:]
             envihdr = copy.deepcopy(meta['envihdr'])
             if bandname in ['mask', 'linci']:
                 envihdr[6] = 'data type = 1'
             envihdr.append('band names={%s}' % bandname)
             List2File(envihdr, f + '.hdr')
         else:
             bandname = 'hdr'
         datafiles[bandname] = f
     return datafiles
Exemplo n.º 6
0
    def __init__(self, filename):
        """ Inspect a single file and get some basic info """
        super(sarAsset, self).__init__(filename)

        bname = os.path.basename(filename)
        self.tile = bname[10:17]
        self.sensor = bname[-9:-8] + bname[-15:-12]

        datafiles = self.datafiles()
        for f in datafiles:
            if f[-3:] == 'hdr':
                hdrfile = f
            if f[-4:] == 'date':
                datefile = f
                rootname = f[:-5]

        # unique to sarData (TODO - is this still used later?)
        self.hdrfile = hdrfile

        # Check if inspecting a file in the repository
        path = os.path.dirname(filename)
        if self.Repository.path() in path:
            date = datetime.datetime.strptime(os.path.basename(path), self.Repository._datedir).date()
            dates = date
            #VerboseOut('Date from repository = '+str(dates),4)
        else:
            # extract header and date image
            tfile = tarfile.open(filename)
            tfile.extract(hdrfile, path)
            hdrfile = os.path.join(path, hdrfile)
            os.chmod(hdrfile, 0664)
            meta = self._meta(hdrfile)
            tfile.extract(datefile, path)
            datefile = os.path.join(path, datefile)
            os.chmod(datefile, 0664)
            # Write ENVI header for date image
            List2File(meta['envihdr'], datefile + '.hdr')
            dateimg = gippy.GeoImage(datefile)
            dateimg.SetNoData(0)
            datevals = numpy.unique(dateimg.Read())
            dates = [self._launchdate[self.sensor[0]] + datetime.timedelta(days=int(d)) for d in datevals if d != 0]
            if not dates:
                RemoveFiles([hdrfile, datefile], ['.hdr', '.aux.xml'])
                raise Exception('%s: no valid dates' % bname)
            date = min(dates)
            dateimg = None
            RemoveFiles([hdrfile, datefile], ['.hdr', '.aux.xml'])
            #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 bname[7] == 'C':
                cdate = datetime.datetime.strptime(self._cycledates[int(bname[8:10])], '%d-%b-%y').date()
                if not (cdate <= date <= (cdate + datetime.timedelta(days=45))):
                    raise Exception('%s: Date %s outside of cycle range (%s)' % (bname, str(date), str(cdate)))
            #VerboseOut('%s: inspect %s' % (fname,datetime.datetime.now()-start), 4)
        self.date = dates
        self.rootname = rootname