def createAuxFile(frame, filename):
    import math
    import array
    import datetime
    from iscesys.DateTimeUtil.DateTimeUtil import DateTimeUtil as DTU
    prf = frame.getInstrument().getPulseRepetitionFrequency()
    senStart = frame.getSensingStart()
    numPulses = int(
        math.ceil(
            DTU.timeDeltaToSeconds(frame.getSensingStop() - senStart) * prf))
    # the aux files has two entries per line. day of the year and microseconds
    #in the day
    musec0 = (senStart.hour * 3600 + senStart.minute * 60 +
              senStart.second) * 10**6
    musec0 += senStart.microsecond
    maxMusec = (24 * 3600) * 10**6
    day0 = (datetime.datetime(senStart.year, senStart.month, senStart.day) -
            datetime.datetime(senStart.year, 1, 1)).days + 1
    outputArray = array.array('d', [0] * 2 * numPulses)
    frame.auxFile = filename
    fp = open(frame.auxFile, 'wb')
    j = -1
    for i1 in range(numPulses):
        j += 1
        musec = round((j / prf) * 10**6) + musec0
        if musec >= maxMusec:
            day0 += 1
            musec0 = musec % maxMusec
            musec = musec0
            j = 0
        outputArray[2 * i1] = day0
        outputArray[2 * i1 + 1] = musec

    outputArray.tofile(fp)
    fp.close()
    def _populateFrame(self, file):
        rft = file['S01']['B001'].attrs['Range First Times'][0]
        slantRange = rft * Const.c / 2.0
        sensingStart = self._parseNanoSecondTimeStamp(
            file.attrs['Scene Sensing Start UTC'])
        sensingStop = self._parseNanoSecondTimeStamp(
            file.attrs['Scene Sensing Stop UTC'])
        centerTime = DTUtil.timeDeltaToSeconds(sensingStop -
                                               sensingStart) / 2.0
        sensingMid = sensingStart + datetime.timedelta(
            microseconds=int(centerTime * 1e6))

        self.frame.setStartingRange(slantRange)
        self.frame.setPassDirection(file.attrs['Orbit Direction'])
        self.frame.setOrbitNumber(file.attrs['Orbit Number'])
        self.frame.setProcessingFacility(file.attrs['Processing Centre'])
        self.frame.setProcessingSoftwareVersion(
            file.attrs['L0 Software Version'])
        self.frame.setPolarization(file['S01'].attrs['Polarisation'])
        self.frame.setNumberOfLines(file['S01']['B001'].shape[0])
        self.frame.setNumberOfSamples(file['S01']['B001'].shape[1])
        self.frame.setSensingStart(sensingStart)
        self.frame.setSensingMid(sensingMid)
        self.frame.setSensingStop(sensingStop)

        rangePixelSize = self.frame.getInstrument().getRangePixelSize()
        farRange = slantRange + self.frame.getNumberOfSamples() * rangePixelSize
        self.frame.setFarRange(farRange)
Beispiel #3
0
    def _populateFrame(self):
        """Populate the scene object with metadata"""
        numberOfLines = self._imageryFileData['numLines']
        numberOfSamples = self._imageryFileData['numSamples']
        pri = self._imageryFileData['pri']
        startingRange = Const.c * float(
            self._imageryFileData['timeToFirstSample']) * 1.0e-9 / 2.0
        rangeSampleSpacing = Const.c / (
            2 * self._imageryFileData['rangeSamplingRate'])
        farRange = startingRange + numberOfSamples * rangeSampleSpacing
        first_line_utc = datetime.datetime.strptime(
            self._imageryFileData['FIRST_LINE_TIME'], '%d-%b-%Y %H:%M:%S.%f')
        center_line_utc = datetime.datetime.strptime(
            self._imageryFileData['FIRST_LINE_TIME'], '%d-%b-%Y %H:%M:%S.%f')
        last_line_utc = datetime.datetime.strptime(
            self._imageryFileData['LAST_LINE_TIME'], '%d-%b-%Y %H:%M:%S.%f')
        centerTime = DTUtil.timeDeltaToSeconds(last_line_utc -
                                               first_line_utc) / 2.0
        center_line_utc = center_line_utc + datetime.timedelta(
            microseconds=int(centerTime * 1e6))

        self.frame.setStartingRange(startingRange)
        self.frame.setFarRange(farRange)
        self.frame.setProcessingFacility(self._imageryFileData['PROC_CENTER'])
        self.frame.setProcessingSystem(self._imageryFileData['SOFTWARE_VER'])
        self.frame.setTrackNumber(int(self._imageryFileData['REL_ORBIT']))
        self.frame.setOrbitNumber(int(self._imageryFileData['ABS_ORBIT']))
        self.frame.setPolarization(self._imageryFileData['MDS1_TX_RX_POLAR'])
        self.frame.setNumberOfSamples(numberOfSamples)
        self.frame.setNumberOfLines(numberOfLines)
        self.frame.setSensingStart(first_line_utc)
        self.frame.setSensingMid(center_line_utc)
        self.frame.setSensingStop(last_line_utc)
Beispiel #4
0
    def _populateFrame(self):
        """Populate the scene object with metadata"""
        numberOfLines = self._imageFileData['length'] # updated the value after findSwst and extractIQ
        numberOfSamples = self._imageFileData['width']# updated value after findSwst and extractIQ
        frame = 0  # ERS in Envisat format header does not contain frame number!
        pulseInterval = (self._imageFileData['pri_counter']+2.0) * self.constants['SEC_PER_PRI_COUNT']
        rangeSamplingRate = self.constants['rangeSamplingRate']
        rangePixelSize = Const.c/(2.0*rangeSamplingRate)
        startingRange = (9*pulseInterval + self._imageFileData['minSwst']*4/rangeSamplingRate-self.constants['delayTime'])*Const.c/2.0
        farRange = startingRange + self._imageFileData['width']*rangePixelSize

        first_line_utc = datetime.datetime.strptime(self._imageFileData['SENSING_START'], '%d-%b-%Y %H:%M:%S.%f')
        mid_line_utc = datetime.datetime.strptime(self._imageFileData['SENSING_START'], '%d-%b-%Y %H:%M:%S.%f')
        last_line_utc = datetime.datetime.strptime(self._imageFileData['SENSING_STOP'], '%d-%b-%Y %H:%M:%S.%f')
        centerTime = DTU.timeDeltaToSeconds(last_line_utc-first_line_utc)/2.0
        mid_line_utc = mid_line_utc + datetime.timedelta(microseconds=int(centerTime*1e6))
    
        print("Debug Print: Frame UTC start, mid, end times:  %s %s %s" % (first_line_utc,mid_line_utc,last_line_utc))

        self.frame.setFrameNumber(frame)
        self.frame.setProcessingFacility(self._imageFileData['PROC_CENTER'])
        self.frame.setProcessingSystem(self._imageFileData['SOFTWARE_VER'])
        self.frame.setTrackNumber(int(self._imageFileData['REL_ORBIT']))
        self.frame.setOrbitNumber(int(self._imageFileData['ABS_ORBIT']))
        self.frame.setPolarization(self.constants['polarization'])
        self.frame.setNumberOfSamples(numberOfSamples)
        self.frame.setNumberOfLines(numberOfLines)
        self.frame.setStartingRange(startingRange)
        self.frame.setFarRange(farRange)
        self.frame.setSensingStart(first_line_utc)
        self.frame.setSensingMid(mid_line_utc)
        self.frame.setSensingStop(last_line_utc)
    def extendOrbit(self, orbit):
        '''
        Input orbit must be WGS-84.
        '''

        deltaT = DTUtil.timeDeltaToSeconds(orbit.maxTime - orbit.minTime) / 2.0
        midTime = orbit.minTime + datetime.timedelta(microseconds=int(deltaT *
                                                                      1e6))

        #pegCoord is an Util.geo coordinate object
        pegCoord, hdg = self.getPegAndHeading(orbit, midTime)

        ####Sch orbit w.r.t mid point of orbit
        schOrb = self.getSCHOrbit(orbit, pegCoord, hdg)

        ####Extend the SCH orbits
        self.extendSCHOrbit(schOrb)

        ####Convert the SCH orbit back to WGS84 orbits
        extOrb = self.getXYZOrbit(schOrb, pegCoord, hdg)

        ####Statistics on the transforms if needed
        #diffOrbits(orbit, extOrb, skip=self._newPoints)

        return extOrb
Beispiel #6
0
    def extractImage(self):
        import array
        import math

        self.parse()
        try:
            out = open(self.output, 'wb')
        except:
            raise Exception('Cannot open output file: %s' % (self.output))

        self.imageFile.extractImage(output=out)
        out.close()

        rawImage = isceobj.createSlcImage()
        rawImage.setByteOrder('l')
        rawImage.setAccessMode('read')
        rawImage.setFilename(self.output)
        rawImage.setWidth(self.imageFile.width)
        rawImage.setXmin(0)
        rawImage.setXmax(self.imageFile.width)
        self.frame.setImage(rawImage)

        prf = self.frame.getInstrument().getPulseRepetitionFrequency()
        senStart = self.frame.getSensingStart()
        numPulses = int(
            math.ceil(
                DTU.timeDeltaToSeconds(self.frame.getSensingStop() - senStart)
                * prf))
        musec0 = (senStart.hour * 3600 + senStart.minute * 60 +
                  senStart.second) * 10**6 + senStart.microsecond
        maxMusec = (
            24 *
            3600) * 10**6  #use it to check if we went across  a day. very rare
        day0 = (datetime.datetime(senStart.year, senStart.month, senStart.day)
                - datetime.datetime(senStart.year, 1, 1)).days + 1
        outputArray = array.array('d', [0] * 2 * numPulses)
        self.frame.auxFile = self.output + '.aux'
        fp = open(self.frame.auxFile, 'wb')
        j = -1
        for i1 in range(numPulses):
            j += 1
            musec = round((j / prf) * 10**6) + musec0
            if musec >= maxMusec:
                day0 += 1
                musec0 = musec % maxMusec
                musec = musec0
                j = 0
            outputArray[2 * i1] = day0
            outputArray[2 * i1 + 1] = musec

        outputArray.tofile(fp)
        fp.close()
Beispiel #7
0
    def _populateFrame(self, file):
        size = file['Frame'].shape
        start = DTU.parseIsoDateTime(file['Frame'].attrs['Sensing Start'])
        stop = DTU.parseIsoDateTime(file['Frame'].attrs['Sensing Stop'])
        deltaT = DTU.timeDeltaToSeconds(stop - start)
        mid = start + datetime.timedelta(microseconds=int(deltaT / 2.0 * 1e6))
        startingRange = file['Frame'].attrs['Starting Range']
        rangePixelSize = file['Instrument'].attrs['Range Pixel Size']
        farRange = startingRange + size[1] * rangePixelSize

        self.frame.setStartingRange(file['Frame'].attrs['Starting Range'])
        self.frame.setFarRange(farRange)
        self.frame.setNumberOfLines(size[0])
        self.frame.setNumberOfSamples(2 * size[1])
        self.frame.setSensingStart(start)
        self.frame.setSensingMid(mid)
        self.frame.setSensingStop(stop)
Beispiel #8
0
    def _populateFrame(self):
        """Populate the scene object with metadata"""
        # Decode some code words, and calculate some parameters
        numberOfLines = None
        for dataSet in self._imageryFileData['dataSets']:
            if (dataSet['DS_NAME'] == 'ASAR_SOURCE_PACKETS'):
                numberOfLines = int(dataSet['NUM_DSR'])

        numberOfSamples = self._imageryFileData['numberOfSamples']
        pri = self._imageryFileData['priCodeword'] / self._instrumentFileData[
            'sampleRate']
        windowStartTime = self._imageryFileData[
            'windowStartTimeCodeword'] / self._instrumentFileData['sampleRate']
        rangeSampleSpacing = Const.c / (2 *
                                        self._instrumentFileData['sampleRate'])
        index = self._imageryFileData['antennaBeamSetNumber'] - 1
        startingRange = (self._instrumentFileData['r_values'][index] * pri +
                         windowStartTime) * Const.c / 2.0
        farRange = startingRange + numberOfSamples * rangeSampleSpacing
        rangeBias = self._instrumentFileData['rangeGateBias'] * Const.c / 2
        # The %b in the next lines strptime read the abbreviated month of the year by locale and could
        # present a problem for people with a different locale set.
        first_line_utc = datetime.datetime.strptime(
            self._imageryFileData['SENSING_START'], '%d-%b-%Y %H:%M:%S.%f')
        center_line_utc = datetime.datetime.strptime(
            self._imageryFileData['SENSING_START'], '%d-%b-%Y %H:%M:%S.%f')
        last_line_utc = datetime.datetime.strptime(
            self._imageryFileData['SENSING_STOP'], '%d-%b-%Y %H:%M:%S.%f')
        centerTime = DTUtil.timeDeltaToSeconds(last_line_utc -
                                               first_line_utc) / 2.0
        center_line_utc = center_line_utc + datetime.timedelta(
            microseconds=int(centerTime * 1e6))

        self.frame.setStartingRange(startingRange - rangeBias)
        self.frame.setFarRange(farRange - rangeBias)
        self.frame.setProcessingFacility(self._imageryFileData['PROC_CENTER'])
        self.frame.setProcessingSystem(self._imageryFileData['SOFTWARE_VER'])
        self.frame.setTrackNumber(int(self._imageryFileData['REL_ORBIT']))
        self.frame.setOrbitNumber(int(self._imageryFileData['ABS_ORBIT']))
        self.frame.setPolarization(self._imageryFileData['TX_RX_POLAR'])
        self.frame.setNumberOfSamples(numberOfSamples)
        self.frame.setNumberOfLines(numberOfLines)
        self.frame.setSensingStart(first_line_utc)
        self.frame.setSensingMid(center_line_utc)
        self.frame.setSensingStop(last_line_utc)
Beispiel #9
0
    def createTrack(self, output):
        import os
        from operator import itemgetter
        from isceobj import Constants as CN
        from ctypes import cdll, c_char_p, c_int, c_ubyte, byref
        lib = cdll.LoadLibrary(os.path.dirname(__file__) + '/concatenate.so')
        # Perhaps we should check to see if Xmin is 0, if it is not, strip off the header
        self.logger.info(
            "Adjusting Sampling Window Start Times for all Frames")
        # Iterate over each frame object, and calculate the number of samples with which to pad it on the left and right
        outputs = []
        totalWidth = 0
        auxList = []
        for frame in self._frames:
            # Calculate the amount of padding
            thisNearRange = frame.getStartingRange()
            thisFarRange = frame.getFarRange()
            left_pad = int(
                round((thisNearRange - self._nearRange) *
                      frame.getInstrument().getRangeSamplingRate() /
                      (CN.SPEED_OF_LIGHT / 2.0))) * 2
            right_pad = int(
                round((self._farRange - thisFarRange) *
                      frame.getInstrument().getRangeSamplingRate() /
                      (CN.SPEED_OF_LIGHT / 2.0))) * 2
            width = frame.getImage().getXmax()
            if width - int(width) != 0:
                raise ValueError("frame Xmax is not an integer")
            else:
                width = int(width)

            input = frame.getImage().getFilename()
            #            tempOutput = os.path.basename(os.tmpnam()) # Some temporary filename
            with tempfile.NamedTemporaryFile(dir='.') as f:
                tempOutput = f.name

            pad_value = int(frame.getInstrument().getInPhaseValue())

            if totalWidth < left_pad + width + right_pad:
                totalWidth = left_pad + width + right_pad
            # Resample this frame with swst_resample
            input_c = c_char_p(bytes(input, 'utf-8'))
            output_c = c_char_p(bytes(tempOutput, 'utf-8'))
            width_c = c_int(width)
            left_pad_c = c_int(left_pad)
            right_pad_c = c_int(right_pad)
            pad_value_c = c_ubyte(pad_value)
            lib.swst_resample(input_c, output_c, byref(width_c),
                              byref(left_pad_c), byref(right_pad_c),
                              byref(pad_value_c))
            outputs.append(tempOutput)
            auxList.append(frame.auxFile)

        #this step construct the aux file withe the pulsetime info for the all set of frames
        self.createAuxFile(auxList, output + '.aux')
        # This assumes that all of the frames to be concatenated are sampled at the same PRI
        prf = self._frames[0].getInstrument().getPulseRepetitionFrequency()
        # Calculate the starting output line of each scene
        i = 0
        lineSort = []
        # the listSort has 2 elements: a line start number which is the position of that specific frame
        # computed from acquisition time and the  corresponding file name
        for frame in self._frames:
            startLine = int(
                round(
                    DTU.timeDeltaToSeconds(frame.getSensingStart() -
                                           self._startTime) * prf))
            lineSort.append([startLine, outputs[i]])
            i += 1

        sortedList = sorted(
            lineSort,
            key=itemgetter(0))  # sort by line number i.e. acquisition time
        startLines, outputs = self.reAdjustStartLine(sortedList, totalWidth)

        self.logger.info("Concatenating Frames along Track")
        # this is a hack since the length of the file could be actually different from the one computed using start and stop time. it only matters the last frame added
        import os

        fileSize = os.path.getsize(outputs[-1])

        numLines = fileSize // totalWidth + startLines[-1]
        totalLines_c = c_int(numLines)
        # Next, call frame_concatenate
        width_c = c_int(
            totalWidth
        )  # Width of each frame (with the padding added in swst_resample)
        numberOfFrames_c = c_int(len(self._frames))
        inputs_c = (c_char_p * len(outputs))(
        )  # These are the inputs to frame_concatenate, but the outputs from swst_resample
        for kk in range(len(outputs)):
            inputs_c[kk] = bytes(outputs[kk], 'utf-8')
        output_c = c_char_p(bytes(output, 'utf-8'))
        startLines_c = (c_int * len(startLines))()
        startLines_c[:] = startLines
        lib.frame_concatenate(output_c, byref(width_c), byref(totalLines_c),
                              byref(numberOfFrames_c), inputs_c, startLines_c)

        # Clean up the temporary output files from swst_resample
        for file in outputs:
            os.unlink(file)

        orbitNum = self._frames[0].getOrbitNumber()
        first_line_utc = self._startTime
        last_line_utc = self._stopTime
        centerTime = DTU.timeDeltaToSeconds(last_line_utc -
                                            first_line_utc) / 2.0
        center_line_utc = first_line_utc + datetime.timedelta(
            microseconds=int(centerTime * 1e6))
        procFac = self._frames[0].getProcessingFacility()
        procSys = self._frames[0].getProcessingSystem()
        procSoft = self._frames[0].getProcessingSoftwareVersion()
        pol = self._frames[0].getPolarization()
        xmin = self._frames[0].getImage().getXmin()

        self._frame.setOrbitNumber(orbitNum)
        self._frame.setSensingStart(first_line_utc)
        self._frame.setSensingMid(center_line_utc)
        self._frame.setSensingStop(last_line_utc)
        self._frame.setStartingRange(self._nearRange)
        self._frame.setFarRange(self._farRange)
        self._frame.setProcessingFacility(procFac)
        self._frame.setProcessingSystem(procSys)
        self._frame.setProcessingSoftwareVersion(procSoft)
        self._frame.setPolarization(pol)
        self._frame.setNumberOfLines(numLines)
        self._frame.setNumberOfSamples(width)
        # add image to frame
        rawImage = isceobj.createRawImage()
        rawImage.setByteOrder('l')
        rawImage.setFilename(output)
        rawImage.setAccessMode('r')
        rawImage.setWidth(totalWidth)
        rawImage.setXmax(totalWidth)
        rawImage.setXmin(xmin)
        self._frame.setImage(rawImage)
Beispiel #10
0
    def extractImage(self):
        import array
        import math
        # just in case there was only one image and it was passed as a str instead of a list with only one element
        if(isinstance(self._imageFileList,str)):
            self._imageFileList = [self._imageFileList]
            self._leaderFileList = [self._leaderFileList]
        if(len(self._imageFileList) != len(self._leaderFileList)):
            self.logger.error("Number of leader files different from number of image files.")
            raise Exception

        self.frameList = []

        for i in range(len(self._imageFileList)):
            appendStr = "_" + str(i)
            #if only one file don't change the name
            if(len(self._imageFileList) == 1):
                appendStr = ''

            self.frame = Frame()
            self.frame.configure()

            self._leaderFile = self._leaderFileList[i]
            self._imageFile = self._imageFileList[i]
            self.leaderFile = LeaderFile(file=self._leaderFile)
            self.leaderFile.parse()

            self.imageFile = ImageFile(self)

            try:
                outputNow = self.output + appendStr
                out = open(outputNow,'wb')
            except IOError as strerr:
                self.logger.error("IOError: %s" % strerr)
                return

            self.imageFile.extractImage(output=out)
            out.close()

            rawImage = isceobj.createRawImage()
            rawImage.setByteOrder('l')
            rawImage.setAccessMode('read')
            rawImage.setFilename(outputNow)
            rawImage.setWidth(self.imageFile.width)
            rawImage.setXmin(0)
            rawImage.setXmax(self.imageFile.width)
            self.frame.setImage(rawImage)
            self.populateMetadata()
            self.frameList.append(self.frame)
            #jng Howard Z at this point adjusts the sampling starting time for imagery generated from CRDC_SARDPF facility.
            # for now create the orbit aux file based in starting time and prf
            prf = self.frame.getInstrument().getPulseRepetitionFrequency()
            senStart = self.frame.getSensingStart()
            numPulses = int(math.ceil(DTU.timeDeltaToSeconds(self.frame.getSensingStop()-senStart)*prf))
            # the aux files has two entries per line. day of the year and microseconds in the day
            musec0 = (senStart.hour*3600 + senStart.minute*60 + senStart.second)*10**6 + senStart.microsecond
            maxMusec = (24*3600)*10**6#use it to check if we went across  a day. very rare
            day0 = (datetime.datetime(senStart.year,senStart.month,senStart.day) - datetime.datetime(senStart.year,1,1)).days + 1
            outputArray  = array.array('d',[0]*2*numPulses)
            self.frame.auxFile = outputNow + '.aux'
            fp = open(self.frame.auxFile,'wb')
            j = -1
            for i1 in range(numPulses):
                j += 1
                musec = round((j/prf)*10**6) + musec0
                if musec >= maxMusec:
                    day0 += 1
                    musec0 = musec%maxMusec
                    musec = musec0
                    j = 0
                outputArray[2*i1] = day0
                outputArray[2*i1+1] = musec

            outputArray.tofile(fp)
            fp.close()

        tk = Track()
        if(len(self._imageFileList) > 1):
            self.frame = tk.combineFrames(self.output,self.frameList)

            for i in range(len(self._imageFileList)):
                try:
                    os.remove(self.output + "_" + str(i))
                except OSError:
                    print("Error. Cannot remove temporary file",self.output + "_" + str(i))
                    raise OSError
    def populateMetadata(self):
        """
            Create metadata objects from the metadata files
        """
        mission = self.product.sourceAttributes.satellite
        swath = self.product.sourceAttributes.radarParameters.beams
        frequency = self.product.sourceAttributes.radarParameters.radarCenterFrequency
        orig_prf = self.product.sourceAttributes.radarParameters.prf  # original PRF not necessarily effective PRF
        rangePixelSize = self.product.imageAttributes.rasterAttributes.sampledPixelSpacing
        rangeSamplingRate = Const.c / (2 * rangePixelSize)
        pulseLength = self.product.sourceAttributes.radarParameters.pulseLengths[
            0]
        pulseBandwidth = self.product.sourceAttributes.radarParameters.pulseBandwidths[
            0]
        polarization = self.product.sourceAttributes.radarParameters.polarizations
        lookSide = lookMap[self.product.sourceAttributes.radarParameters.
                           antennaPointing.upper()]
        facility = self.product.imageGenerationParameters.generalProcessingInformation._processingFacility
        version = self.product.imageGenerationParameters.generalProcessingInformation.softwareVersion
        lines = self.product.imageAttributes.rasterAttributes.numberOfLines
        samples = self.product.imageAttributes.rasterAttributes.numberOfSamplesPerLine
        startingRange = self.product.imageGenerationParameters.slantRangeToGroundRange.slantRangeTimeToFirstRangeSample * (
            Const.c / 2)
        incidenceAngle = (self.product.imageGenerationParameters.
                          sarProcessingInformation.incidenceAngleNearRange +
                          self.product.imageGenerationParameters.
                          sarProcessingInformation.incidenceAngleFarRange) / 2
        # some RS2 scenes have oversampled SLC images because processed azimuth bandwidth larger than PRF EJF 2015/08/15
        azimuthPixelSize = self.product.imageAttributes.rasterAttributes.sampledLineSpacing  # ground spacing in meters
        totalProcessedAzimuthBandwidth = self.product.imageGenerationParameters.sarProcessingInformation.totalProcessedAzimuthBandwidth
        prf = orig_prf * np.ceil(
            totalProcessedAzimuthBandwidth / orig_prf
        )  # effective PRF can be double original, suggested by Piyush
        print("effective PRF %f, original PRF %f" % (prf, orig_prf))

        lineFlip = (self.product.imageAttributes.rasterAttributes.
                    lineTimeOrdering.upper() == 'DECREASING')

        if lineFlip:
            dataStopTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeFirstLine
            dataStartTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeLastLine
        else:
            dataStartTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeFirstLine
            dataStopTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeLastLine

        passDirection = self.product.sourceAttributes.orbitAndAttitude.orbitInformation.passDirection
        height = self.product.imageGenerationParameters.sarProcessingInformation._satelliteHeight

        ####Populate platform
        platform = self.frame.getInstrument().getPlatform()
        platform.setPlanet(Planet(pname="Earth"))
        platform.setMission(mission)
        platform.setPointingDirection(lookSide)
        platform.setAntennaLength(15.0)

        ####Populate instrument
        instrument = self.frame.getInstrument()
        instrument.setRadarFrequency(frequency)
        instrument.setPulseRepetitionFrequency(prf)
        instrument.setPulseLength(pulseLength)
        instrument.setChirpSlope(pulseBandwidth / pulseLength)
        instrument.setIncidenceAngle(incidenceAngle)
        #self.frame.getInstrument().setRangeBias(0)
        instrument.setRangePixelSize(rangePixelSize)
        instrument.setRangeSamplingRate(rangeSamplingRate)
        instrument.setBeamNumber(swath)
        instrument.setPulseLength(pulseLength)

        #Populate Frame
        #self.frame.setSatelliteHeight(height)
        self.frame.setSensingStart(dataStartTime)
        self.frame.setSensingStop(dataStopTime)
        diffTime = DTUtil.timeDeltaToSeconds(dataStopTime -
                                             dataStartTime) / 2.0
        sensingMid = dataStartTime + datetime.timedelta(
            microseconds=int(diffTime * 1e6))
        self.frame.setSensingMid(sensingMid)
        self.frame.setPassDirection(passDirection)
        self.frame.setPolarization(polarization)
        self.frame.setStartingRange(startingRange)
        self.frame.setFarRange(startingRange + (samples - 1) * rangePixelSize)
        self.frame.setNumberOfLines(lines)
        self.frame.setNumberOfSamples(samples)
        self.frame.setProcessingFacility(facility)
        self.frame.setProcessingSoftwareVersion(version)

        # Initialize orbit objects
        # Read into temp orbit first.
        # Radarsat 2 needs orbit extensions.
        tempOrbit = Orbit()

        self.frame.getOrbit().setOrbitSource(
            'Header: ' + self.product.sourceAttributes.orbitAndAttitude.
            orbitInformation.orbitDataFile)
        self.frame.setPassDirection(passDirection)
        stateVectors = self.product.sourceAttributes.orbitAndAttitude.orbitInformation.stateVectors
        for i in range(len(stateVectors)):
            position = [
                stateVectors[i].xPosition, stateVectors[i].yPosition,
                stateVectors[i].zPosition
            ]
            velocity = [
                stateVectors[i].xVelocity, stateVectors[i].yVelocity,
                stateVectors[i].zVelocity
            ]
            vec = StateVector()
            vec.setTime(stateVectors[i].timeStamp)
            vec.setPosition(position)
            vec.setVelocity(velocity)
            tempOrbit.addStateVector(vec)

        planet = self.frame.instrument.platform.planet
        orbExt = OrbitExtender(planet=planet)
        orbExt.configure()
        newOrb = orbExt.extendOrbit(tempOrbit)

        for sv in newOrb:
            self.frame.getOrbit().addStateVector(sv)

# save the Doppler centroid coefficients, converting units from product.xml file
# units in the file are quadratic coefficients in Hz, Hz/sec, and Hz/(sec^2)
# ISCE expects Hz, Hz/(range sample), Hz((range sample)^2
# note that RS2 Doppler values are estimated at time dc.dopplerCentroidReferenceTime,
# so the values might need to be adjusted for ISCE usage
# added EJF 2015/08/17
        dc = self.product.imageGenerationParameters.dopplerCentroid
        poly = dc.dopplerCentroidCoefficients
        # need to convert units
        poly[1] = poly[1] / rangeSamplingRate
        poly[2] = poly[2] / rangeSamplingRate**2
        self.doppler_coeff = poly

        # similarly save Doppler azimuth fm rate values, converting units
        # units in the file are quadratic coefficients in Hz, Hz/sec, and Hz/(sec^2)
        # Guessing that ISCE expects Hz, Hz/(range sample), Hz((range sample)^2
        # note that RS2 Doppler values are estimated at time dc.dopplerRateReferenceTime,
        # so the values might need to be adjusted for ISCE usage
        # added EJF 2015/08/17
        dr = self.product.imageGenerationParameters.dopplerRateValues
        fmpoly = dr.dopplerRateValuesCoefficients
        # need to convert units
        fmpoly[1] = fmpoly[1] / rangeSamplingRate
        fmpoly[2] = fmpoly[2] / rangeSamplingRate**2
        self.azfmrate_coeff = fmpoly
    def populateMetadata(self):
        """
            Create metadata objects from the metadata files
        """
        ####Set each parameter one - by - one
        mission = self.grab_from_xml('adsHeader/missionId')
        swath = self.grab_from_xml('adsHeader/swath')
        polarization = self.grab_from_xml('adsHeader/polarisation')

        frequency = float(
            self.grab_from_xml(
                'generalAnnotation/productInformation/radarFrequency'))
        passDirection = self.grab_from_xml(
            'generalAnnotation/productInformation/pass')

        rangePixelSize = float(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/rangePixelSpacing'))
        azimuthPixelSize = float(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/azimuthPixelSpacing'))
        rangeSamplingRate = Const.c / (2.0 * rangePixelSize)
        prf = 1.0 / float(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/azimuthTimeInterval'))

        lines = int(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/numberOfLines'))
        samples = int(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/numberOfSamples'))

        startingRange = float(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/slantRangeTime')
        ) * Const.c / 2.0
        incidenceAngle = float(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/incidenceAngleMidSwath'))
        dataStartTime = self.convertToDateTime(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/productFirstLineUtcTime'))
        dataStopTime = self.convertToDateTime(
            self.grab_from_xml(
                'imageAnnotation/imageInformation/productLastLineUtcTime'))

        pulseLength = float(
            self.grab_from_xml(
                'generalAnnotation/downlinkInformationList/downlinkInformation/downlinkValues/txPulseLength'
            ))
        chirpSlope = float(
            self.grab_from_xml(
                'generalAnnotation/downlinkInformationList/downlinkInformation/downlinkValues/txPulseRampRate'
            ))
        pulseBandwidth = pulseLength * chirpSlope

        ####Sentinel is always right looking
        lookSide = -1
        facility = 'EU'
        version = '1.0'

        #        height = self.product.imageGenerationParameters.sarProcessingInformation._satelliteHeight

        ####Populate platform
        platform = self.frame.getInstrument().getPlatform()
        platform.setPlanet(Planet(pname="Earth"))
        platform.setMission(mission)
        platform.setPointingDirection(lookSide)
        platform.setAntennaLength(2 * azimuthPixelSize)

        ####Populate instrument
        instrument = self.frame.getInstrument()
        instrument.setRadarFrequency(frequency)
        instrument.setPulseRepetitionFrequency(prf)
        instrument.setPulseLength(pulseLength)
        instrument.setChirpSlope(pulseBandwidth / pulseLength)
        instrument.setIncidenceAngle(incidenceAngle)
        #self.frame.getInstrument().setRangeBias(0)
        instrument.setRangePixelSize(rangePixelSize)
        instrument.setRangeSamplingRate(rangeSamplingRate)
        instrument.setBeamNumber(swath)
        instrument.setPulseLength(pulseLength)

        #Populate Frame
        #self.frame.setSatelliteHeight(height)
        self.frame.setSensingStart(dataStartTime)
        self.frame.setSensingStop(dataStopTime)
        diffTime = DTUtil.timeDeltaToSeconds(dataStopTime -
                                             dataStartTime) / 2.0
        sensingMid = dataStartTime + datetime.timedelta(
            microseconds=int(diffTime * 1e6))
        self.frame.setSensingMid(sensingMid)
        self.frame.setPassDirection(passDirection)
        self.frame.setPolarization(polarization)
        self.frame.setStartingRange(startingRange)
        self.frame.setFarRange(startingRange + (samples - 1) * rangePixelSize)
        self.frame.setNumberOfLines(lines)
        self.frame.setNumberOfSamples(samples)
        self.frame.setProcessingFacility(facility)
        self.frame.setProcessingSoftwareVersion(version)

        self.frame.setPassDirection(passDirection)

        ResOrbFlag = self.extractResOrbit()
        if ResOrbFlag == 0:
            print(
                "cannot find POD Restituted Orbit, using orbit coming along with SLC"
            )
            self.extractOrbit()
 def testTimeDeltaToSeconds(self):
     ans = 29 * 60.0 + 15
     td = self.dt2 - self.dt1
     numSeconds = DateTimeUtil.timeDeltaToSeconds(td)
     self.assertAlmostEquals(numSeconds, ans, 5)
    def extractImage(self):
        import array
        import math

        self.frameList = []

        for i in range(len(self._imageFileList)):
            appendStr = "_" + str(i)  #intermediate raw files suffix
            if (len(self._imageFileList) == 1):
                appendStr = ''  #if only one file don't change the name

            outputNow = self.output + appendStr
            auxImage = isceobj.createImage()  # unused
            widthAux = 2  # unused
            auxName = outputNow + '.aux'

            self.imageFile = self._imageFileList[i]
            self.frame = Frame()
            self.frame.configure()

            self.frame.auxFile = auxName  #add the auxFile as part of the frame and diring the stitching create also a combined aux file # HB: added from Envisat.py
            imageFileParser = ImageFile(fileName=self.imageFile)
            self._imageFileData = imageFileParser.parse(
            )  # parse image and get swst values and new width

            try:
                outputNow = self.output + appendStr
                out = open(outputNow, 'wb')
            except IOError as strerr:
                self.logger.error("IOError: %s" % strerr)
                return

            imageFileParser.extractIQ(
                output=out)  # IMPORTANT for ERS Envisat-type data
            out.close()

            self.populateMetadata(
            )  # populate Platform, Instrument, Frame, and Orbit
            self._populateImage(outputNow)
            self.frameList.append(self.frame)

            ### Below: create a aux file
            # for now create the orbit aux file based in starting time and prf
            prf = self.frame.getInstrument().getPulseRepetitionFrequency()
            senStart = self.frame.getSensingStart()
            numPulses = int(
                math.ceil(
                    DTU.timeDeltaToSeconds(self.frame.getSensingStop() -
                                           senStart) * prf))
            # the aux files has two entries per line. day of the year and microseconds in the day
            musec0 = (senStart.hour * 3600 + senStart.minute * 60 +
                      senStart.second) * 10**6 + senStart.microsecond
            maxMusec = (
                24 * 3600
            ) * 10**6  # use it to check if we went across a day. very rare
            day0 = (datetime.datetime(senStart.year, senStart.month,
                                      senStart.day) -
                    datetime.datetime(senStart.year, 1, 1)).days + 1
            outputArray = array.array('d', [0] * 2 * numPulses)
            self.frame.auxFile = outputNow + '.aux'
            fp = open(self.frame.auxFile, 'wb')
            j = -1
            for i1 in range(numPulses):
                j += 1
                musec = round((j / prf) * 10**6) + musec0
                if musec >= maxMusec:
                    day0 += 1
                    musec0 = musec % maxMusec
                    musec = musec0
                    j = 0
                outputArray[2 * i1] = day0
                outputArray[2 * i1 + 1] = musec

            outputArray.tofile(fp)
            fp.close()

        ## refactor this with __init__.tkfunc
        tk = Track()
        if (len(self._imageFileList) > 1):
            self.frame = tk.combineFrames(self.output, self.frameList)

            for i in range(len(self._imageFileList)):
                try:
                    os.remove(self.output + "_" + str(i))
                except OSError:
                    print("Error. Cannot remove temporary file",
                          self.output + "_" + str(i))
                    raise OSError
Beispiel #15
0
    def populateMetadata(self):
        """
            Create metadata objects from the metadata files
        """
        mission = self.product.sourceAttributes.satellite
        swath = self.product.sourceAttributes.radarParameters.beams
        frequency = self.product.sourceAttributes.radarParameters.radarCenterFrequency
        prf = self.product.sourceAttributes.radarParameters.prf        
        rangePixelSize = self.product.imageAttributes.rasterAttributes.sampledPixelSpacing
        rangeSamplingRate = Const.c/(2*rangePixelSize)        
        pulseLength = self.product.sourceAttributes.radarParameters.pulseLengths[0]
        pulseBandwidth = self.product.sourceAttributes.radarParameters.pulseBandwidths[0]
        polarization = self.product.sourceAttributes.radarParameters.polarizations
        lookSide = lookMap[self.product.sourceAttributes.radarParameters.antennaPointing.upper()]
        facility = self.product.imageGenerationParameters.generalProcessingInformation._processingFacility
        version = self.product.imageGenerationParameters.generalProcessingInformation.softwareVersion
        lines = self.product.imageAttributes.rasterAttributes.numberOfLines
        samples = self.product.imageAttributes.rasterAttributes.numberOfSamplesPerLine
        startingRange = self.product.imageGenerationParameters.slantRangeToGroundRange.slantRangeTimeToFirstRangeSample * (Const.c/2)
        incidenceAngle = (self.product.imageGenerationParameters.sarProcessingInformation.incidenceAngleNearRange + self.product.imageGenerationParameters.sarProcessingInformation.incidenceAngleFarRange)/2

        lineFlip =  (self.product.imageAttributes.rasterAttributes.lineTimeOrdering.upper() == 'DECREASING')

        if lineFlip:
            dataStopTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeFirstLine
            dataStartTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeLastLine
        else:
            dataStartTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeFirstLine
            dataStopTime = self.product.imageGenerationParameters.sarProcessingInformation.zeroDopplerTimeLastLine

        passDirection = self.product.sourceAttributes.orbitAndAttitude.orbitInformation.passDirection
        height = self.product.imageGenerationParameters.sarProcessingInformation._satelliteHeight

        ####Populate platform
        platform = self.frame.getInstrument().getPlatform()
        platform.setPlanet(Planet("Earth"))
        platform.setMission(mission)
        platform.setPointingDirection(lookSide)
        platform.setAntennaLength(15.0)

        ####Populate instrument
        instrument = self.frame.getInstrument()
        instrument.setRadarFrequency(frequency)
        instrument.setPulseRepetitionFrequency(prf)
        instrument.setPulseLength(pulseLength)
        instrument.setChirpSlope(pulseBandwidth/pulseLength)
        instrument.setIncidenceAngle(incidenceAngle)
        #self.frame.getInstrument().setRangeBias(0)
        instrument.setRangePixelSize(rangePixelSize)
        instrument.setRangeSamplingRate(rangeSamplingRate)
        instrument.setBeamNumber(swath)
        instrument.setPulseLength(pulseLength)


        #Populate Frame
        #self.frame.setSatelliteHeight(height)
        self.frame.setSensingStart(dataStartTime)
        self.frame.setSensingStop(dataStopTime)
        diffTime = DTUtil.timeDeltaToSeconds(dataStopTime - dataStartTime)/2.0
        sensingMid = dataStartTime + datetime.timedelta(microseconds=int(diffTime*1e6))
        self.frame.setSensingMid(sensingMid)
        self.frame.setPassDirection(passDirection)
        self.frame.setPolarization(polarization) 
        self.frame.setStartingRange(startingRange)
        self.frame.setFarRange(startingRange + (samples-1)*rangePixelSize)
        self.frame.setNumberOfLines(lines)
        self.frame.setNumberOfSamples(samples)
        self.frame.setProcessingFacility(facility)
        self.frame.setProcessingSoftwareVersion(version)
        
        # Initialize orbit objects
        # Read into temp orbit first. 
        # Radarsat 2 needs orbit extensions.
        tempOrbit = Orbit()

        self.frame.getOrbit().setOrbitSource('Header: ' + self.product.sourceAttributes.orbitAndAttitude.orbitInformation.orbitDataFile)
        self.frame.setPassDirection(passDirection)
        stateVectors = self.product.sourceAttributes.orbitAndAttitude.orbitInformation.stateVectors
        for i in range(len(stateVectors)):
            position = [stateVectors[i].xPosition, stateVectors[i].yPosition, stateVectors[i].zPosition]
            velocity = [stateVectors[i].xVelocity, stateVectors[i].yVelocity, stateVectors[i].zVelocity]
            vec = StateVector()            
            vec.setTime(stateVectors[i].timeStamp)
            vec.setPosition(position)
            vec.setVelocity(velocity)
            tempOrbit.addStateVector(vec)

        planet = self.frame.instrument.platform.planet
        orbExt = OrbitExtender(planet=planet)
        orbExt.configure()
        newOrb = orbExt.extendOrbit(tempOrbit)

        for sv in newOrb:
            self.frame.getOrbit().addStateVector(sv)