import struct


def getheader(filename, gtype):
    """Read header data from Gadget data file 'filename' with Gadget file
    type 'gtype'. Returns a dictionary with loaded values and filename."""
    DESC = '=I4sII'                                # struct formatting string
    HEAD = '=I6I6dddii6iiiddddii6ii60xI'        # struct formatting string
    keys = ('Npart', 'Massarr', 'Time', 'Redshift', 'FlagSfr', 'FlagFeedback', 'Nall', 'FlagCooling', 'NumFiles', 'BoxSize', 'Omega0', 'OmegaLambda', 'HubbleParam', 'FlagAge', 'FlagMetals', 'NallHW', 'flag_entr_ics', 'filename')
    f = open(filename, 'rb')
    
    if gtype == 2:
        f.seek(16) # If you want to use the data: desc = struct.unpack(DESC,f.read(16))
    
    raw = struct.unpack(HEAD,f.read(264))[1:-1]
    values = (raw[:6], raw[6:12]) + raw[12:16] + (raw[16:22],) + raw[22:30] + (raw[30:36], raw[36], filename)
    header = dict(zip(keys, values))
    
    f.close()
    return header