Beispiel #1
0
def build_key(iFile, oFile, oFileID):
    """Build a key file from a pds file"""
    #open the pds file
    pdsFile = open(iFile, 'rb')
    keyFile = open(oFile, 'wb')

    #read the file's header info to make the key's header
    headerHeader = array.array('I')
    headerHeader.fromfile(pdsFile, 3)
    global pdsSignature
    pdsSignature = headerHeader[0] >> 8
    pds_utils.pdsSignature = pdsSignature
    if pdsSignature != PDSSIGNATURE: headerHeader.byteswap()

    keyHeader = array.array('I')
    keyHeader.fromlist([2718281 * 256, oFileID])

    # now get the names of the streams
    numberOfRecordNameWords = array.array('I')
    numberOfRecordNameWords.fromfile(pdsFile, 1)
    if pdsSignature != PDSSIGNATURE: numberOfRecordNameWords.byteswap()
    nameWords = array.array('I')
    nameWords.fromfile(pdsFile, numberOfRecordNameWords[0])
    if pdsSignature != PDSSIGNATURE: nameWords.byteswap()

    #need number of names
    pdsFile.seek(4 * 4)
    nameChars = array.array('c')
    nameChars.fromfile(pdsFile, numberOfRecordNameWords[0] * 4)
    if pdsSignature != PDSSIGNATURE: nameChars.byteswap()
    #print name_list(nameChars)
    streamNamesFromPDS = pds_utils.name_list(nameChars)
    nRecordNames = len(streamNamesFromPDS)
    sortedStreamNames = list(streamNamesFromPDS)
    sortedStreamNames.sort()

    streamNameString = ""
    newStreamIndex2OldIndex = []
    oldStreamIndex2NewIndex = [0] * len(streamNamesFromPDS)
    for name in sortedStreamNames:
        newStreamIndex2OldIndex.append(streamNamesFromPDS.index(name))
        oldStreamIndex2NewIndex[
            newStreamIndex2OldIndex[-1]] = len(newStreamIndex2OldIndex) - 1
        streamNameString = streamNameString + name + "\0"
    streamNameString = streamNameString[:-1]
    while 0 != len(streamNameString) % 4:
        streamNameString = streamNameString + "\0"
    nameWords = array.array('I')
    nameWords.fromstring(streamNameString)

    #need to compute the length of the rest of the header
    keyHeaderLength = numberOfRecordNameWords[0] + 1 + 1 + 1 + 1
    keyHeader.fromlist([keyHeaderLength])
    keyHeader.fromlist([nRecordNames])
    keyHeader = keyHeader + numberOfRecordNameWords + nameWords
    #print keyHeader
    keyHeaderOffsetToNRecords = len(keyHeader) * 4
    keyHeader.fromlist([0, keyHeaderLength])
    keyHeader.tofile(keyFile)

    #grab the rest of the header
    restOfHeader = array.array('I')
    #the header size ignores the first 3 words in the event
    pdsFile.seek((headerHeader[2] + 2) * 4 - pdsFile.tell(), 1)
    restOfHeader.fromfile(pdsFile, 1)
    if pdsSignature != PDSSIGNATURE: restOfHeader.byteswap()
    if restOfHeader[0] != headerHeader[2]:
        raise "header inconsistent"

    #now loop over all the records
    recordsToSync = array.array('I')
    #print nRecordNames
    recordsToSync.fromlist([0xFFFFFFFFL] * nRecordNames)
    #print recordsToSync
    numRecord = 0
    numSyncValues = 0
    lastSyncValue = array.array('I')
    lastSyncValue.fromlist([0, 0, 0, 0])
    isFirstRecord = 1
    while 1:
        try:
            endReason = ""
            # the 1st through 5 words are the same between the key file and pds
            pdsRecordType = array.array('I')
            pdsRecordType.fromfile(pdsFile, 1)
            if pdsSignature != PDSSIGNATURE: pdsRecordType.byteswap()
            #translate the type from the index used in pds to key index
            pdsRecordType[0] = oldStreamIndex2NewIndex[pdsRecordType[0]]
            keyRecord = array.array('I')
            keyRecord.fromfile(pdsFile, 4)
            if pdsSignature != PDSSIGNATURE: keyRecord.byteswap()
            #print keyRecord
            if lastSyncValue != keyRecord:
                numSyncValues = numSyncValues + 1
                lastSyncValue = keyRecord
                if isFirstRecord:
                    isFirstRecord = 0
                else:
                    recordsToSync.tofile(keyFile)
                keyRecord.tofile(keyFile)
            recordsToSync[pdsRecordType[0]] = numRecord
            numRecord = numRecord + 1

            endReason = "bad Record: record size"
            recordDataLength = array.array('I')
            recordDataLength.fromfile(pdsFile, 1)
            if pdsSignature != PDSSIGNATURE: recordDataLength.byteswap()
            endReason = "bad Record: record (type " + str(
                keyRecord[0]) + ") (size " + str(recordDataLength[0]) + ")"

            pdsFile.seek(recordDataLength[0] * 4, 1)

        except EOFError:
            #print endReason
            break
    #pus out last record synchronization info
    recordsToSync.tofile(keyFile)

    keyFile.seek(keyHeaderOffsetToNRecords)
    nSyncValues = array.array('I')
    nSyncValues.fromlist([numSyncValues])
    nSyncValues.tofile(keyFile)
Beispiel #2
0
def buildLocationHeader(iPDSFileName, iFileID):
    """Build a PDS location header, from given pds file name and file id"""
    #    print "iPDSFileName",iPDSFileName

    pdsFile = open(iPDSFileName, 'rb')
    pdsFileID = int(iFileID)

    pdsHeader = array.array('I')
    pdsHeader.fromfile(pdsFile, 3)
    global pdsSignature
    pdsSignature = pdsHeader[0] >> 8
    pds_utils.PDSSIGNATURE = PDSSIGNATURE
    pds_utils.pdsSignature = pdsSignature
    if pdsSignature != PDSSIGNATURE:
        pdsHeader.byteswap()
        needToSwap = 1
    else:
        needToSwap = 0

    locationHeader = array.array('I')
    locationHeader.fromlist([2951413 * 256])
    #file id for master index
    locationHeader.append(0)
    #remaining number of words in of header
    locationHeader.append(0)
    #associated file list
    fileList = array.array('I')
    # for now only set the low word for the file ID
    fileList.fromlist([1, pdsFileID, 0])
    locationHeader = locationHeader + fileList

    # now get the names of the streams
    #  the location file and the PDS file use the same format for the
    #  stream names so just have to copy the info
    numberOfRecordNameWords = array.array('I')
    numberOfRecordNameWords.fromfile(pdsFile, 1)
    if pdsSignature != PDSSIGNATURE: numberOfRecordNameWords.byteswap()

    #need number of names
    pdsFile.seek(4 * 4)
    nameChars = array.array('c')
    nameChars.fromfile(pdsFile, numberOfRecordNameWords[0] * 4)
    if pdsSignature != PDSSIGNATURE: nameChars.byteswap()
    streamNames = pds_utils.name_list(nameChars)
    sortedStreamNames = list(streamNames)
    sortedStreamNames.sort()

    #build conversion from new stream index to old index
    newStreamIndex2OldIndex = []
    oldStreamIndex2NewIndex = [0] * len(streamNames)
    streamNameString = ""
    for name in sortedStreamNames:
        newStreamIndex2OldIndex.append(streamNames.index(name))
        oldStreamIndex2NewIndex[
            newStreamIndex2OldIndex[-1]] = len(newStreamIndex2OldIndex) - 1
        streamNameString = streamNameString + name + "\0"
    streamNameString = streamNameString[:-1]
    while 0 != len(streamNameString) % 4:
        streamNameString = streamNameString + "\0"
    nameWords = array.array('I')
    nameWords.fromstring(streamNameString)

    locationHeader = locationHeader + numberOfRecordNameWords + nameWords

    #print streamNames
    shProxyNames = pds_utils.name_list_from_file(pdsFile)
    #print shProxyNames
    #print len(shProxyNames)
    shNames = pds_utils.name_list_from_file(pdsFile)
    #print shNames

    #grab the rest of the header
    restOfHeader = array.array('I')
    #the header size ignores the first 3 words in the event
    restOfHeader.fromfile(pdsFile, pdsHeader[2] - pdsFile.tell() / 4 + 3)
    if pdsSignature != PDSSIGNATURE: restOfHeader.byteswap()
    #print restOfHeader
    if restOfHeader[-1] != pdsHeader[2]:
        raise "header inconsistent"

    #create the list of 'type' 'usage' 'production' tags for each stream
    def proxySort(a, b):
        #print a, b
        temp = cmp(a[0], b[0])
        if 0 == temp:
            temp = cmp(a[1], b[1])
            if 0 == temp:
                temp = cmp(a[2], b[2])
        return temp

    proxiesInStreams = pds_utils.find_proxies_in_streams(
        restOfHeader, streamNames, shProxyNames)
    #sortProxiesInStreams = proxiesInStreams
    for proxies in proxiesInStreams:
        proxies.sort(proxySort)

    #reserve space for our hash
    dataKeyHashIndex = len(locationHeader)
    dataKeyHashArray = array.array('I')
    dataKeyHashArray.fromlist([0] * 5)
    locationHeader += dataKeyHashArray

    maxNProxies = 0
    nProxies = array.array('I')
    nProxies.fromlist([0])
    proxiesArray = []
    for oldIndex in newStreamIndex2OldIndex:
        #print oldIndex
        proxies = proxiesInStreams[oldIndex]
        nProxies[0] = len(proxies)
        if nProxies[0] > maxNProxies:
            maxNProxies = nProxies[0]
        locationHeader += nProxies
        datakeys = ""
        #now add each string
        proxyList = []
        for proxy in proxies:
            proxyList.append(proxy)
            for key in proxy:
                datakeys += key + "\0"
            while (len(datakeys) % 4):
                datakeys += "\0"
        #print len(datakeys)
        #print datakeys
        #print len(datakeys)
        dataKeysArray = array.array('I')
        dataKeysArray.fromstring(datakeys)
        #        proxiesArray+=[dataKeysArray.tolist()]
        proxiesArray += [proxyList]
        #nProxies[0] = len(dataKeysArray)
        #locationHeader += nProxies
        locationHeader += dataKeysArray
    #calculate the hash
    hash = sha.new(locationHeader[dataKeyHashIndex + 1:].tostring()).digest()
    #print sha.new( locationHeader[dataKeyHashIndex+1:].tostring() ).hexdigest()
    dataKeyHashArray = array.array('I')
    dataKeyHashArray.fromstring(hash)
    locationHeader[dataKeyHashIndex:dataKeyHashIndex + 5] = dataKeyHashArray

    locationHeaderBeforePadding = locationHeader.tolist()
    #pad header so Records begin on 8 byte boundary
    if not (len(locationHeader) % 2):
        locationHeader.fromlist([0])
    headerLength = len(locationHeader) + 1 - 3
    locationHeader.fromlist([headerLength])
    locationHeader[2] = headerLength

    #pad the max number of ASUs to be a multiple of 8
    nASUperRecord = maxNProxies
    while ((nASUperRecord + 4) % 8):
        nASUperRecord += 1

    #For each stream, create the ASU to file ID list
    whichFileForStream = []
    whichFileForStreamFake = []
    for proxies in proxiesInStreams:
        whichFile = array.array('B')
        fakeArray = array.array('B')

        whichFile.fromlist([0] * len(proxies) + [255] *
                           (nASUperRecord - len(proxies)))
        for x in xrange(0, len(whichFile)):
            fakeArray.append(255)
        #print whichFile
        whichFileForStream.append(whichFile)
        whichFileForStreamFake.append(fakeArray)

    return (pdsFile, locationHeader, locationHeaderBeforePadding, nameWords,
            dataKeyHashArray, proxiesArray, streamNames,
            oldStreamIndex2NewIndex, newStreamIndex2OldIndex,
            whichFileForStream, whichFileForStreamFake, needToSwap)
Beispiel #3
0
def getStreamDataKeyDictFromPDS(iPDSFileName):
    """Extract from PDS file streamDataKey dictionary"""

    pdsFile = open(iPDSFileName, 'rb')
    pdsHeader = array.array('I')
    pdsHeader.fromfile(pdsFile, 3)
    global pdsSignature
    pdsSignature = pdsHeader[0] >> 8
    pds_utils.PDSSIGNATURE = PDSSIGNATURE
    pds_utils.pdsSignature = pdsSignature
    if pdsSignature != PDSSIGNATURE:
        pdsHeader.byteswap()
        needToSwap = 1
    else:
        needToSwap = 0

    # now get the names of the streams
    #  the location file and the PDS file use the same format for the
    #  stream names so just have to copy the info
    numberOfRecordNameWords = array.array('I')
    numberOfRecordNameWords.fromfile(pdsFile, 1)
    if pdsSignature != PDSSIGNATURE: numberOfRecordNameWords.byteswap()

    #need number of names
    pdsFile.seek(4 * 4)
    nameChars = array.array('c')
    nameChars.fromfile(pdsFile, numberOfRecordNameWords[0] * 4)
    if pdsSignature != PDSSIGNATURE: nameChars.byteswap()
    streamNames = pds_utils.name_list(nameChars)
    sortedStreamNames = list(streamNames)
    sortedStreamNames.sort()

    #build conversion from new stream index to old index
    newStreamIndex2OldIndex = []
    for name in sortedStreamNames:
        newStreamIndex2OldIndex.append(streamNames.index(name))

    #print streamNames
    shProxyNames = pds_utils.name_list_from_file(pdsFile)
    #print shProxyNames
    #print len(shProxyNames)
    shNames = pds_utils.name_list_from_file(pdsFile)
    #print shNames

    #grab the rest of the header
    restOfHeader = array.array('I')
    #the header size ignores the first 3 words in the event
    restOfHeader.fromfile(pdsFile, pdsHeader[2] - pdsFile.tell() / 4 + 3)
    if pdsSignature != PDSSIGNATURE: restOfHeader.byteswap()
    #print restOfHeader
    if restOfHeader[-1] != pdsHeader[2]:
        raise "header inconsistent"

    #create the list of 'type' 'usage' 'production' tags for each stream
    def proxySort(a, b):
        #print a, b
        temp = cmp(a[0], b[0])
        if 0 == temp:
            temp = cmp(a[1], b[1])
            if 0 == temp:
                temp = cmp(a[2], b[2])
        return temp

    proxiesInStreams = pds_utils.find_proxies_in_streams(
        restOfHeader, streamNames, shProxyNames)
    #sortProxiesInStreams = proxiesInStreams
    for proxies in proxiesInStreams:
        proxies.sort(proxySort)

    streamDataKeyDict = {}
    for oldIndex in newStreamIndex2OldIndex:
        #print oldIndex
        stream = streamNames[oldIndex]
        proxies = proxiesInStreams[oldIndex]
        streamDataKeyDict[stream] = proxies

    return streamDataKeyDict
def build_key(iFile, oFile, oFileID):
    """Build a key file from a pds file"""
    #open the pds file
    pdsFile = open(iFile,'rb')
    keyFile = open(oFile,'wb')

    #read the file's header info to make the key's header
    headerHeader = array.array('I')
    headerHeader.fromfile(pdsFile, 3)
    global pdsSignature
    pdsSignature = headerHeader[0]>>8
    pds_utils.pdsSignature=pdsSignature
    if pdsSignature != PDSSIGNATURE: headerHeader.byteswap()

    keyHeader = array.array('I')
    keyHeader.fromlist([2718281*256,oFileID ])

    # now get the names of the streams
    numberOfRecordNameWords = array.array('I')
    numberOfRecordNameWords.fromfile(pdsFile,1)
    if pdsSignature != PDSSIGNATURE: numberOfRecordNameWords.byteswap()
    nameWords = array.array('I')
    nameWords.fromfile(pdsFile, numberOfRecordNameWords[0])
    if pdsSignature != PDSSIGNATURE: nameWords.byteswap()

    #need number of names
    pdsFile.seek(4*4)
    nameChars = array.array('c')
    nameChars.fromfile(pdsFile,numberOfRecordNameWords[0]*4)
    if pdsSignature != PDSSIGNATURE: nameChars.byteswap()
    #print name_list(nameChars)
    streamNamesFromPDS = pds_utils.name_list(nameChars)
    nRecordNames = len( streamNamesFromPDS )
    sortedStreamNames = list(streamNamesFromPDS)
    sortedStreamNames.sort()

    streamNameString =""
    newStreamIndex2OldIndex = []
    oldStreamIndex2NewIndex = [0]*len(streamNamesFromPDS)
    for name in sortedStreamNames:
       newStreamIndex2OldIndex.append(streamNamesFromPDS.index(name))
       oldStreamIndex2NewIndex[newStreamIndex2OldIndex[-1] ] = len(newStreamIndex2OldIndex)-1
       streamNameString = streamNameString+name +"\0"
    streamNameString = streamNameString[:-1]
    while 0 != len(streamNameString) % 4:
       streamNameString = streamNameString + "\0"
    nameWords = array.array('I')
    nameWords.fromstring(streamNameString)


    #need to compute the length of the rest of the header
    keyHeaderLength = numberOfRecordNameWords[0]+1+1+1+1
    keyHeader.fromlist([keyHeaderLength])
    keyHeader.fromlist([nRecordNames])
    keyHeader=keyHeader +numberOfRecordNameWords +nameWords
    #print keyHeader
    keyHeaderOffsetToNRecords=len(keyHeader)*4
    keyHeader.fromlist([0,keyHeaderLength])
    keyHeader.tofile(keyFile)
    
    #grab the rest of the header
    restOfHeader = array.array('I')
    #the header size ignores the first 3 words in the event
    pdsFile.seek( (headerHeader[2]+2)*4-pdsFile.tell(),1 )
    restOfHeader.fromfile(pdsFile, 1)
    if pdsSignature != PDSSIGNATURE: restOfHeader.byteswap()
    if restOfHeader[0] != headerHeader[2]:
       raise "header inconsistent"
    
    #now loop over all the records
    recordsToSync = array.array('I')
    #print nRecordNames
    recordsToSync.fromlist([0xFFFFFFFFL]*nRecordNames)
    #print recordsToSync
    numRecord = 0
    numSyncValues = 0
    lastSyncValue = array.array('I')
    lastSyncValue.fromlist([0,0,0,0])
    isFirstRecord = 1
    while 1:
       try:
	  endReason = ""
	  # the 1st through 5 words are the same between the key file and pds
	  pdsRecordType = array.array('I')
	  pdsRecordType.fromfile(pdsFile,1)
	  if pdsSignature != PDSSIGNATURE: pdsRecordType.byteswap()
	  #translate the type from the index used in pds to key index
	  pdsRecordType[0] = oldStreamIndex2NewIndex[ pdsRecordType[0] ]
	  keyRecord = array.array('I')
	  keyRecord.fromfile(pdsFile,4)
	  if pdsSignature != PDSSIGNATURE: keyRecord.byteswap()
	  #print keyRecord
	  if lastSyncValue != keyRecord:
	     numSyncValues = numSyncValues + 1
	     lastSyncValue = keyRecord
	     if isFirstRecord:
		isFirstRecord = 0
	     else:
		recordsToSync.tofile(keyFile)
	     keyRecord.tofile(keyFile)
	  recordsToSync[pdsRecordType[0]] = numRecord	
	  numRecord = numRecord + 1
    
	  endReason = "bad Record: record size"
	  recordDataLength = array.array('I')
	  recordDataLength.fromfile(pdsFile,1)
	  if pdsSignature != PDSSIGNATURE: recordDataLength.byteswap()
	  endReason = "bad Record: record (type "+str(keyRecord[0])+") (size " + str(recordDataLength[0]) +")"
    
	  pdsFile.seek(recordDataLength[0]*4,1)
	  
       except EOFError:
#print endReason
	 break
    #pus out last record synchronization info
    recordsToSync.tofile(keyFile)
    
    keyFile.seek(keyHeaderOffsetToNRecords)
    nSyncValues = array.array('I')
    nSyncValues.fromlist([numSyncValues])
    nSyncValues.tofile(keyFile)
def buildLocationHeader(iPDSFileName, iFileID):
    """Build a PDS location header, from given pds file name and file id"""
#    print "iPDSFileName",iPDSFileName

    pdsFile = open(iPDSFileName,'rb')
    pdsFileID = int(iFileID)

    pdsHeader = array.array('I')
    pdsHeader.fromfile(pdsFile, 3)
    global pdsSignature
    pdsSignature=pdsHeader[0]>>8
    pds_utils.PDSSIGNATURE=PDSSIGNATURE
    pds_utils.pdsSignature=pdsSignature
    if pdsSignature != PDSSIGNATURE:
       pdsHeader.byteswap()
       needToSwap=1
    else:
       needToSwap=0
   
    locationHeader = array.array('I')
    locationHeader.fromlist([2951413*256])
    #file id for master index
    locationHeader.append(0)
    #remaining number of words in of header
    locationHeader.append(0)
    #associated file list
    fileList = array.array('I')
    # for now only set the low word for the file ID
    fileList.fromlist([1,pdsFileID,0])
    locationHeader =locationHeader + fileList

    # now get the names of the streams
    #  the location file and the PDS file use the same format for the
    #  stream names so just have to copy the info
    numberOfRecordNameWords = array.array('I')
    numberOfRecordNameWords.fromfile(pdsFile,1)
    if pdsSignature != PDSSIGNATURE: numberOfRecordNameWords.byteswap()

    #need number of names
    pdsFile.seek(4*4)
    nameChars = array.array('c')
    nameChars.fromfile(pdsFile,numberOfRecordNameWords[0]*4)
    if pdsSignature != PDSSIGNATURE: nameChars.byteswap()
    streamNames = pds_utils.name_list(nameChars)
    sortedStreamNames = list(streamNames)
    sortedStreamNames.sort()

    #build conversion from new stream index to old index
    newStreamIndex2OldIndex = []
    oldStreamIndex2NewIndex = [0]*len(streamNames)
    streamNameString =""
    for name in sortedStreamNames:
        newStreamIndex2OldIndex.append(streamNames.index(name))
        oldStreamIndex2NewIndex[newStreamIndex2OldIndex[-1] ] = len(newStreamIndex2OldIndex)-1
        streamNameString = streamNameString+name +"\0"
    streamNameString = streamNameString[:-1]
    while 0 != len(streamNameString) % 4:
        streamNameString = streamNameString + "\0"
    nameWords = array.array('I')
    nameWords.fromstring(streamNameString)

    locationHeader = locationHeader + numberOfRecordNameWords+nameWords

    #print streamNames
    shProxyNames = pds_utils.name_list_from_file(pdsFile)
    #print shProxyNames
    #print len(shProxyNames)
    shNames = pds_utils.name_list_from_file(pdsFile)
    #print shNames

    #grab the rest of the header
    restOfHeader = array.array('I')
    #the header size ignores the first 3 words in the event
    restOfHeader.fromfile(pdsFile, pdsHeader[2] -pdsFile.tell()/4 +3)
    if pdsSignature != PDSSIGNATURE: restOfHeader.byteswap()
    #print restOfHeader
    if restOfHeader[-1] != pdsHeader[2]:
      raise "header inconsistent"

    #create the list of 'type' 'usage' 'production' tags for each stream
    def proxySort(a,b):
        #print a, b
        temp = cmp(a[0],b[0])
        if 0 == temp:
	  temp=cmp(a[1],b[1])
	  if 0 == temp:
	     temp=cmp(a[2],b[2])
        return temp
    proxiesInStreams = pds_utils.find_proxies_in_streams( restOfHeader, streamNames, shProxyNames)
#sortProxiesInStreams = proxiesInStreams
    for proxies in proxiesInStreams:
        proxies.sort(proxySort)

    #reserve space for our hash
    dataKeyHashIndex = len(locationHeader)
    dataKeyHashArray = array.array('I')
    dataKeyHashArray.fromlist([0]*5)
    locationHeader += dataKeyHashArray

    maxNProxies = 0
    nProxies = array.array('I')
    nProxies.fromlist([0])
    proxiesArray = []
    for oldIndex in newStreamIndex2OldIndex:
	#print oldIndex
	proxies = proxiesInStreams[oldIndex]
	nProxies[0] = len(proxies)
	if nProxies[0] > maxNProxies:
	   maxNProxies = nProxies[0]
	locationHeader +=nProxies
	datakeys = ""
	#now add each string
	proxyList= []
	for proxy in proxies:
	    proxyList.append(proxy)
	    for key in proxy:
	        datakeys +=key+"\0"
	    while( len(datakeys) % 4 ):
	        datakeys +="\0"
	#print len(datakeys)
	#print datakeys
	#print len(datakeys)
	dataKeysArray=array.array('I')
	dataKeysArray.fromstring(datakeys)
#        proxiesArray+=[dataKeysArray.tolist()]
	proxiesArray+=[proxyList]
	#nProxies[0] = len(dataKeysArray)
	#locationHeader += nProxies
	locationHeader +=dataKeysArray
    #calculate the hash
    hash = sha.new( locationHeader[dataKeyHashIndex+1:].tostring() ).digest()
    #print sha.new( locationHeader[dataKeyHashIndex+1:].tostring() ).hexdigest()
    dataKeyHashArray = array.array('I')
    dataKeyHashArray.fromstring(hash)
    locationHeader[dataKeyHashIndex:dataKeyHashIndex+5]=dataKeyHashArray

    locationHeaderBeforePadding = locationHeader.tolist()
    #pad header so Records begin on 8 byte boundary
    if not (len(locationHeader) % 2):
       locationHeader.fromlist([0])
    headerLength = len(locationHeader)+1-3
    locationHeader.fromlist([headerLength])
    locationHeader[2] = headerLength

    #pad the max number of ASUs to be a multiple of 8
    nASUperRecord = maxNProxies
    while ((nASUperRecord + 4) % 8):
      nASUperRecord +=1
      
    #For each stream, create the ASU to file ID list
    whichFileForStream = []
    whichFileForStreamFake = []
    for proxies in proxiesInStreams:
      whichFile = array.array('B')
      fakeArray = array.array('B')

      whichFile.fromlist([0]*len(proxies)+[255]*(nASUperRecord-len(proxies)))
      for x in xrange(0,len(whichFile)):
          fakeArray.append(255)
      #print whichFile
      whichFileForStream.append(whichFile)
      whichFileForStreamFake.append(fakeArray)

    return (pdsFile,locationHeader,locationHeaderBeforePadding,nameWords,dataKeyHashArray,proxiesArray,streamNames,oldStreamIndex2NewIndex,newStreamIndex2OldIndex,whichFileForStream,whichFileForStreamFake,needToSwap)
def getStreamDataKeyDictFromPDS(iPDSFileName):
    """Extract from PDS file streamDataKey dictionary"""

    pdsFile = open(iPDSFileName,'rb')
    pdsHeader = array.array('I')
    pdsHeader.fromfile(pdsFile, 3)
    global pdsSignature
    pdsSignature=pdsHeader[0]>>8
    pds_utils.PDSSIGNATURE=PDSSIGNATURE
    pds_utils.pdsSignature=pdsSignature
    if pdsSignature != PDSSIGNATURE:
       pdsHeader.byteswap()
       needToSwap=1
    else:
       needToSwap=0
   
    # now get the names of the streams
    #  the location file and the PDS file use the same format for the
    #  stream names so just have to copy the info
    numberOfRecordNameWords = array.array('I')
    numberOfRecordNameWords.fromfile(pdsFile,1)
    if pdsSignature != PDSSIGNATURE: numberOfRecordNameWords.byteswap()

    #need number of names
    pdsFile.seek(4*4)
    nameChars = array.array('c')
    nameChars.fromfile(pdsFile,numberOfRecordNameWords[0]*4)
    if pdsSignature != PDSSIGNATURE: nameChars.byteswap()
    streamNames = pds_utils.name_list(nameChars)
    sortedStreamNames = list(streamNames)
    sortedStreamNames.sort()

    #build conversion from new stream index to old index
    newStreamIndex2OldIndex = []
    for name in sortedStreamNames:
        newStreamIndex2OldIndex.append(streamNames.index(name))

    #print streamNames
    shProxyNames = pds_utils.name_list_from_file(pdsFile)
    #print shProxyNames
    #print len(shProxyNames)
    shNames = pds_utils.name_list_from_file(pdsFile)
    #print shNames

    #grab the rest of the header
    restOfHeader = array.array('I')
    #the header size ignores the first 3 words in the event
    restOfHeader.fromfile(pdsFile, pdsHeader[2] -pdsFile.tell()/4 +3)
    if pdsSignature != PDSSIGNATURE: restOfHeader.byteswap()
    #print restOfHeader
    if restOfHeader[-1] != pdsHeader[2]:
      raise "header inconsistent"

    #create the list of 'type' 'usage' 'production' tags for each stream
    def proxySort(a,b):
        #print a, b
        temp = cmp(a[0],b[0])
        if 0 == temp:
	  temp=cmp(a[1],b[1])
	  if 0 == temp:
	     temp=cmp(a[2],b[2])
        return temp
    proxiesInStreams = pds_utils.find_proxies_in_streams( restOfHeader, streamNames, shProxyNames)
#sortProxiesInStreams = proxiesInStreams
    for proxies in proxiesInStreams:
        proxies.sort(proxySort)

    streamDataKeyDict = {}
    for oldIndex in newStreamIndex2OldIndex:
	#print oldIndex
	stream  = streamNames[oldIndex] 
	proxies = proxiesInStreams[oldIndex]
	streamDataKeyDict[stream] = proxies

    return streamDataKeyDict