File Name from Header - GOES Satellite Hunt

File Name from Header

Some of the files has a filename in the header. So if they have, we can rename it. The header that contains the filename is header type 4 (Annotation Record). so I created a funcion called manageFile inside to do the work of the filename.

def manageFile(filename):
  f = open(filename, "r")
    k = readHeader(f)
    type, filetypecode, headerlength, datalength = k
    print "   Header 0 is corrupted for file %s" %filename
  newfilename = filename
  while f.tell() < headerlength:
    data = readHeader(f)
    if data[0] == 4:
      #print "   Filename is %s" % data[1]
      newfilename = data[1]
  if filename != newfilename:
    print "   Renaming %s to %s/%s" %(filename, os.path.dirname(filename), newfilename)
    os.rename(filename, "%s/%s" %(os.path.dirname(filename), newfilename))
    print "   Couldn't find name in %s" %filename

This code will search for a filename in header, if it finds, it will rename the input filename to whatever is in the header. If not, it will just keep the same name. So in the I can just do this to have everything processed:

if (packet["sequenceflag_int"] == 2 or packet["sequenceflag_int"] == 3):
  if isCompressed:
    if USEDECOMPRESSOR and startnum != -1:
      decompressed = Decompressor("channels/%s/%s_%s_" % (channelid, packet["apid"], packet["version"]), pixels, startnum, endnum)
    print "File is not compressed. Checking headers."

After that, you should have all files with the correct naming (if they have in the header) and decompressed! The filenames are usually like