Пример #1
0
def mass_from_mergers(base, snap=135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    f=hdf5lib.OpenFile(base+'/postprocessing/StellarAssembly/galaxies_'+snaptag+'.hdf5', mode ='r' )
    delta=hdf5lib.GetData(f, "StellarMassFromMergers")[:]
    f.close()
    return np.array(delta)
Пример #2
0
def mass_from_minor_mergers(base, snap=135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    f=hdf5lib.OpenFile(base+'/postprocessing/MergerHistory/merger_history_'+snaptag+'.hdf5', mode ='r' )
    data=hdf5lib.GetData(f, "StellarMassFromMinorMergers")[:]
    f.close()
    return np.array(data)
Пример #3
0
def return_tags(filename, parttype=-1, verbose=False):
    if os.path.exists(filename):
        curfilename = filename
    elif os.path.exists(filename + ".hdf5"):
        curfilename = filename + ".hdf5"
    elif os.path.exists(filename + ".0.hdf5"):
        curfilename = filename + ".0.hdf5"
    else:
        print "[error] file not found : ", filename
        sys.stdout.flush()
        sys.exit()

    all_blocks = []

    f = hdf5lib.OpenFile(filename)
    for parttype in range(0, 6):
        this_block = []
        part_name = 'PartType' + str(parttype)
        if (hdf5lib.Contains(f, "", part_name)):
            iter = it = datablocks.__iter__()
            next = iter.next()
            while (1):
                if (hdf5lib.Contains(f, part_name, datablocks[next][0])):
                    sys.stdout.flush()
                    this_block.append(next)
                try:
                    next = iter.next()
                except StopIteration:
                    break
        all_blocks.append(this_block)

    f.close()
    gc.collect()
    return all_blocks
Пример #4
0
def subhalo_overdensity(base, snap = 135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    f=hdf5lib.OpenFile(base+'/postprocessing/environment/environment_'+snaptag+'.hdf5', mode ='r' )
    delta=hdf5lib.GetData(f, "delta")[:]
    f.close()
    return np.array(delta)
Пример #5
0
def subhalo_insitu_fraction(snap = 135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    f=hdf5lib.OpenFile('/n/ghernquist/Illustris/Runs/Illustris-1/postprocessing/InSituFraction/insitu_stellar_fraction_'+snaptag+'.hdf5', mode ='r' )
    data=hdf5lib.GetData(f, "InSitu")[:]
    f.close()
    return np.array(data)
Пример #6
0
def contains_block(filename, tag, parttype=-1, verbose=False):
    if os.path.exists(filename):
        curfilename = filename
    elif os.path.exists(filename + ".hdf5"):
        curfilename = filename + ".hdf5"
    elif os.path.exists(filename + ".0.hdf5"):
        curfilename = filename + ".0.hdf5"
    else:
        print "[error] file not found : ", filename
        sys.stdout.flush()
        sys.exit()

    contains_flag = False
    f = hdf5lib.OpenFile(filename)
    for parttype in range(0, 6):
        part_name = 'PartType' + str(parttype)
        if (hdf5lib.Contains(f, "", part_name)):
            iter = it = datablocks.__iter__()
            next = iter.next()
            while (1):
                if (verbose):
                    print "check ", next, datablocks[next][0]
                    sys.stdout.flush()
                if (hdf5lib.Contains(f, part_name, datablocks[next][0])):
                    if (next.find(tag) > -1):
                        contains_flag = True
                try:
                    next = iter.next()
                except StopIteration:
                    break
    f.close()
    gc.collect()
    return contains_flag
Пример #7
0
def number_of_minor_mergers(base, snap=135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    f=hdf5lib.OpenFile(base+'/postprocessing/MergerHistory/merger_history_'+snaptag+'.hdf5', mode ='r' )
    data=hdf5lib.GetData(f, "NumMinorMergersTotal")[:]
    f.close()
    return np.array(data)
Пример #8
0
def get_offsets(cat, part_types=[0, 1, 4, 5], snap=None, run=None):
    if snap and run:
        group_file = "/n/ghernquist/Illustris/Runs/%s/postprocessing/offsets/snap_offsets_group_%s.hdf5" % (run, snap)
        halo_file = "/n/ghernquist/Illustris/Runs/%s/postprocessing/offsets/snap_offsets_subhalo_%s.hdf5" % (run, snap)
        if os.path.isfile(group_file) and os.path.isfile(halo_file):
	    print "READSUBF: found pretabulated offsets to read"
            f = hdf5lib.OpenFile(group_file)
            group_offsets = hdf5lib.GetData(f, "Offsets")[:]
            f.close()

            f = hdf5lib.OpenFile(halo_file)
            halo_offsets = hdf5lib.GetData(f, "Offsets")[:]
            f.close()

            return np.array(group_offsets), np.array(halo_offsets)

        else:
          # /n/hernquistfs3/IllustrisTNG/Runs/L75n910TNG/postprocessing/offsets/
            group_file = "/n/hernquistfs3/IllustrisTNG/Runs/%s/postprocessing/offsets/offsets_%s.hdf5" % (run, str(snap).zfill(3)) 	
#            sys.exit()
            if os.path.isfile(group_file):
                f = hdf5lib.OpenFile(group_file)
                group_offsets = np.copy(hdf5lib.GetData(f, "Group/SnapByType"))
                halo_offsets  = np.copy(hdf5lib.GetData(f, "Subhalo/SnapByType"))
                return group_offsets, halo_offsets
     
   
    GroupOffset = np.zeros((cat.ngroups, 6), dtype="int64")
    HaloOffset  = np.zeros((cat.nsubs, 6), dtype="int64")

    for parttype in part_types:
        print "Calculating offsets for PartType: %d" % parttype
        k = 0
        for i in range(0, cat.ngroups):
                    if i > 0:
                           GroupOffset[i, parttype] = GroupOffset[i-1, parttype] + cat.GroupLenType[i-1, parttype]
                    if cat.GroupNsubs[i] > 0:
                            HaloOffset[k, parttype] = GroupOffset[i, parttype]
                            k += 1
                            for j in range(1, cat.GroupNsubs[i]):
                                    HaloOffset[k, parttype] =  HaloOffset[k-1, parttype] + cat.SubhaloLenType[k-1, parttype]
                                    k += 1
    if k != cat.nsubs:
        print "READHALO: problem with offset table", k, cat.nsubs
        sys.exit()

    return np.array(GroupOffset), np.array(HaloOffset)
Пример #9
0
def subhalo_gas_kinematics(base, snap = 135, which="v_5"):
    snaptag=str(snap)
    file = base+'/postprocessing/gas_kinematics/gas_kinematic_info_'+snaptag+'.hdf5'
    print file
    f=hdf5lib.OpenFile(file, mode ='r' )
    data=hdf5lib.GetData(f, "Subhalo/"+which)[:]
    f.close()
    return np.array(data)
Пример #10
0
def subhalo_gas_z_grad(base, snap = 135, which="GradMetallicity_5"):
    snaptag=str(snap)
    file = base+'/postprocessing/gas_metallicity/gas_metallicity_info_'+snaptag+'.hdf5'
    print file
    f=hdf5lib.OpenFile(file, mode ='r' )
    data=hdf5lib.GetData(f, "Subhalo/"+which)[:]
    f.close()
    return np.array(data)
Пример #11
0
def subhalo_stellar_vel_disp(base, snap = 135, which="StellarVelDisp_HalfMassRad"):
    #snaptag='000'+str(snap)
    #snaptag=snaptag[-3:]
    snaptag=str(snap)
    print base+'/postprocessing/stellar_vel_disp/stellar_vel_disp_'+snaptag+'.hdf5'
    f=hdf5lib.OpenFile(base+'/postprocessing/stellar_vel_disp/stellar_vel_disp_'+snaptag+'.hdf5', mode ='r' )
    delta=hdf5lib.GetData(f, "Subhalo/"+which)[:]
    f.close()
    return np.array(delta)
Пример #12
0
def subhalo_circularities(base, snap = 135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    print base+'/postprocessing/circularities/circularities_'+snaptag+'.hdf5'
    f=hdf5lib.OpenFile(base+'/postprocessing/circularities/circularities_'+snaptag+'.hdf5', mode ='r' )
    data=np.array(hdf5lib.GetData(f, "CircAbove05Frac")[:])
    data=np.reshape(data, -1)
    f.close()
    return data
Пример #13
0
    def __init__(self, basedir, snapnum):
        self.filename = basedir + "tree_offsets_subgroup_" + str(
            snapnum) + "_135.hdf5"
        self.snapnum = snapnum

        f = hdf5lib.OpenFile(self.filename)
        self.TreeFile = hdf5lib.GetData(f, "TreeFile")[:]
        self.TreeIndex = hdf5lib.GetData(f, "TreeIndex")[:]
        self.TreeNum = hdf5lib.GetData(f, "TreeNum")[:]
        f.close()
Пример #14
0
def subhalo_stellar_age(snap = 135):
    snaptag='000'+str(snap)
    snaptag=snaptag[-3:]
    file='/n/ghernquist/Illustris/Runs/Illustris-1/postprocessing/galprop/galprop_'+snaptag+'.hdf5'
    if os.path.exists(file):
        f=hdf5lib.OpenFile('/n/ghernquist/Illustris/Runs/Illustris-1/postprocessing/galprop/galprop_'+snaptag+'.hdf5', mode='r')
        data=np.array(hdf5lib.GetData(f, "stellar_age_inrad")[:])
        f.close()
    else:
        data = None
    return data
Пример #15
0
    def collect_at_root(self, comm, write=True):
        for name in self.values:
            tmp = np.zeros_like(self.values[name])
            comm.Allreduce(self.values[name], tmp)
            self.values[name] = np.copy(tmp)

        if (comm.Get_rank() == 0) and write == True:
            f_write = hdf5lib.OpenFile('./saved_data/' + self.savetag +
                                       '.hdf5',
                                       mode="w")
            group = hdf5lib.CreateGroup(f_write, "data")
            for name in self.values:
                hdf5lib.CreateArray(f_write, group, name, self.values[name])
            f_write.close()
Пример #16
0
    def __init__(self,
                 basedir,
                 snapnum,
                 long_ids=False,
                 name="fof_subhalo_tab",
                 verbose=False):
        self.filebase = basedir + "/groups_" + str(snapnum).zfill(
            3) + "/" + name + "_" + str(snapnum).zfill(3) + "."

        if long_ids: self.id_type = np.uint64
        else: self.id_type = np.uint32

        filenum = 0
        doneflag = False
        skip_ids = 0

        while not doneflag:
            curfile = self.filebase + str(filenum) + ".hdf5"

            if not os.path.exists(curfile):
                self.filebase = basedir + "/" + name + "_" + str(
                    snapnum).zfill(3)
                curfile = self.filebase + ".hdf5"

            if not os.path.exists(curfile):
                print "file not found:", curfile
                sys.exit()

            f = hdf5lib.OpenFile(curfile)
            nfiles = hdf5lib.GetAttr(f, "Header", "NumFiles")
            nids = hdf5lib.GetAttr(f, "Header", "Nids_ThisFile")
            idlen = hdf5lib.GetAttr(f, "Header", "Nids_Total")

            if filenum == 0:
                if not long_ids:
                    self.IDs = np.empty(idlen, dtype=np.uint32)
                else:
                    self.IDs = np.empty(idlen, dtype=np.uint64)

            # TODO: This call seems to fail when FOF_STOREIDS is not
            # switched on in Config.sh, since the dataset IDs/ID
            # will not exist.
            self.IDs[skip_ids:skip_ids + nids] = hdf5lib.GetData(f, "IDs/ID")

            skip_ids += nids

            f.close()
            filenum += 1
            if filenum == self.nfiles: doneflag = True
Пример #17
0
    def __init__(self,
                 basedir,
                 skipfac,
                 snapnum,
                 filenum=0,
                 tree_start=-1,
                 tree_num=-1,
                 keysel=None):

        self.filebase = basedir + "trees_sf" + str(skipfac) + "_" + str(
            snapnum).zfill(3)
        self.basedir = basedir
        self.filenum = filenum
        filename = self.filebase + "." + str(filenum) + ".hdf5"
        f = hdf5lib.OpenFile(filename)
        self.NtreesPerFile = hdf5lib.GetAttr(f, "Header", "NtreesPerFile")
        self.NumberOfOutputFiles = hdf5lib.GetAttr(f, "Header",
                                                   "NumberOfOutputFiles")
        self.ParticleMass = hdf5lib.GetAttr(f, "Header", "ParticleMass")
        if self.ParticleMass == 0:
            print "WARNING: ParticleMass = 0, needed for merger rate calculation"
        self.TreeNHalos = hdf5lib.GetData(f, "Header/TreeNHalos")[:]
        self.TotNsubhalos = hdf5lib.GetData(f, "Header/TotNsubhalos")[:]
        self.Redshifts = hdf5lib.GetData(f, "Header/Redshifts")[:]
        if (tree_start == -1) | (tree_num == -1):
            tree_start = 0
            tree_num = self.NtreesPerFile
#        self.trees = np.empty(tree_num - tree_start, dtype='object')
        self.trees = np.empty(tree_num, dtype='object')
        self.tree_start = tree_start
        self.tree_num = tree_num
        for ntree in range(tree_start, tree_start + tree_num):
            list = []
            if keysel is None:
                for datablock in mergertree_datablocks.keys():
                    data = hdf5lib.GetData(
                        f, "Tree" + str(ntree) + "/" + datablock)[:]
                    list.append((datablock, data))
            else:
                for datablock in keysel:
                    if hdf5lib.Contains(f, "Tree" + str(ntree), datablock):
                        data = hdf5lib.GetData(
                            f, "Tree" + str(ntree) + "/" + datablock)[:]
                        list.append((datablock, data))
            print ntree, tree_start
            self.trees[ntree - tree_start] = dict(list)
        f.close()
Пример #18
0
def subhalo_petrosian_radius(snap = 135):
    file = '/n/ghernquist/Illustris/Runs/Illustris-1/postprocessing/PhotometricMorphologies/nonparmorphs_iSDSS_135.hdf5'
    if os.path.exists(file):
        f=hdf5lib.OpenFile(file,mode='r')
        data0=np.array(hdf5lib.GetData(f, "RP_cam0")[:])
        data = np.zeros( (4 , data0.shape[0]) )

        data[1,:] = np.array(hdf5lib.GetData(f, "RP_cam1")[:])
        data[2,:] = np.array(hdf5lib.GetData(f, "RP_cam2")[:])
        data[3,:] = np.array(hdf5lib.GetData(f, "RP_cam3")[:])

	data = np.median( data, axis=0 )
 

        f.close()
    else:
        data = None
    return data
Пример #19
0
    def list_blocks(self, parttype=-1, verbose=False):
        curfile = self.firstfile
        if not os.path.exists(curfile):
            print "file not found:", curfile
            sys.exit()

        f=hdf5lib.OpenFile(curfile)
        iter = it=sub_datablocks.__iter__()
        next = iter.next()
        while (1):
            print next
            if (hdf5lib.Contains(f,"Subhalo",next)):
                print "Subhalo: "+next
                sys.stdout.flush()
            try:
                next=iter.next()
            except StopIteration:
                break
        f.close()
Пример #20
0
 def __init__(self, basedir, snapnum, keysel=None, long_ids=False):
     
     if long_ids: id_type = np.uint64
     else: id_type = np.uint32
     
     vardict = {}
     if keysel is None:
         keysel = galprop_datablocks.items()
     
     file=naming.return_galprop_file(basedir, snapnum)
     if os.path.exists(file):
         f=hdf5lib.OpenFile(file, mode='r')
         for key in keysel:
             if hdf5lib.Contains(f, "", key):
                 val = galprop_datablocks[key]
                 type = val[0]
                 dim = val[1]
                 vars(self)[key]=np.array(hdf5lib.GetData(f, key)[:])
         f.close()
     else:
         print "Galprop File Not Found..."
Пример #21
0
def check_file(filename):
    f = hdf5lib.OpenFile(filename)
    f.close()
Пример #22
0
def read_block(filename,
               block,
               parttype=-1,
               no_mass_replicate=False,
               fill_block="",
               slab_start=-1,
               slab_len=-1,
               ids=-1,
               verbose=False,
               multiple_files=False):

    if os.path.exists(filename):
        curfilename = filename
    elif os.path.exists(filename + ".hdf5"):
        curfilename = filename + ".hdf5"
    elif os.path.exists(filename + ".0.hdf5"):
        curfilename = filename + ".0.hdf5"
        multiple_files = True
    else:
        print "[error] file not found : ", filename
        sys.stdout.flush()
        sys.exit()

    if (verbose):
        print "reading block          : ", block
        sys.stdout.flush()

    if parttype not in [-1, 0, 1, 2, 3, 4, 5]:
        print "[error] wrong parttype given"
        sys.stdout.flush()
        sys.exit()

    slabflag = False
    if ((slab_start != -1) | (slab_len != -1)):
        slabflag = True
        if (parttype == -1):
            print "[error] slabs only supported for specific parttype"
            sys.stdout.flush()
            sys.exit()
    idsflag = False
    if type(ids) != int:
        idsflag = True
        ids = np.array(ids)
        if parttype == -1:
            print "[error] id list only supported for specific parttype"
            sys.stdout.flush()
            sys.exit()
        if np.sum(ids == np.sort(ids)) < len(ids):
            print "[error] input ids not sorted. must be in order!"
            return

    if (verbose):
        print curfilename
    head = snapshot_header(curfilename)
    filenum = head.filenum
    npart_all = np.array(head.nall)
    highword = head.nall_highword

    npart_all.astype(long)

    # Need to determine size of array and data type to pre-allocate data.
    if idsflag == True:
        length = len(ids)
    if slabflag == True:
        length = slab_len
    if idsflag == False and slabflag == False:
        if parttype != -1:
            length = head.nall[parttype]
            if highword[parttype] == 1:
                length += 2**32
        else:
            length = head.nall.sum()
    if (verbose):
        print "Length of data allocation:", length  #GREG

    if (datablocks.has_key(block)):
        block_name = datablocks[block][0]
        dim2 = datablocks[block][1]
        first = True
        if (verbose):
            print "Reading HDF5           : ", block_name
            print "Data dimension         : ", dim2
            print "Multiple file          : ", multiple_files
            print "Slab data              : ", slabflag
            sys.stdout.flush()
    else:
        print "[error] Block type ", block, "not known!"
        sys.stdout.flush()
        sys.exit()

    fill_block_name = ""
    if (fill_block != ""):
        if (datablocks.has_key(fill_block)):
            fill_block_name = datablocks[fill_block][0]
            dim2 = datablocks[fill_block][1]
            if (verbose):
                print "Block filling active   : ", fill_block_name
                sys.stdout.flush()

    # set alloc_type here. read in example item to determine data type.
    alloc_type = None

    # need to loop over all files until block is found, or no more files left.
    if not multiple_files:
        filepaths = [curfilename]
    else:
        filepaths = [
            filename + "." + str(subfile) + ".hdf5"
            for subfile in np.arange(filenum)
        ]
    for filepath in filepaths:
        g = hdf5lib.OpenFile(filepath)
        if parttype == -1:
            for ptype in range(0, 6):
                try:
                    contains = hdf5lib.Contains(g, 'PartType' + str(ptype),
                                                block_name)
                except:
                    contains = False
                if contains:
                    alloc_type = str(
                        hdf5lib.GetData(
                            g, 'PartType' + str(ptype) + '/' +
                            block_name)[0:1].dtype)
                    break
        else:
            try:
                contains = hdf5lib.Contains(g, 'PartType' + str(parttype),
                                            block_name)
            except:
                contains = False
            if contains:
                alloc_type = str(
                    hdf5lib.GetData(
                        g, 'PartType' + str(parttype) + '/' +
                        block_name)[0:1].dtype)
        g.close()
        gc.collect()
        if contains == True:
            break

    # if block does not exist
    if alloc_type == None:
        if block == "ID  ":
            alloc_type = np.uint32
        #else:
        #       alloc_type=np.float32
        elif block == "MASS":
            alloc_type = np.float32  #default to float32 for MASS
        else:
            print "[error] block : ", block, "of parttype : ", parttype, "not found"
            sys.stdout.flush()
            sys.exit()

    if dim2 > 1:
        ret_val = np.ndarray((length, dim2), alloc_type)
    else:
        ret_val = np.ndarray((length, ), alloc_type)
    if (verbose):
        print "Allocated array"

    if (multiple_files):
        if slabflag == False and idsflag == False:
            first = True
            dim1 = long(0)
            for num in range(0, filenum):
                curfilename = filename + "." + str(num) + ".hdf5"
                if (verbose):
                    print "Reading file           : ", num, curfilename
                    sys.stdout.flush()

                data, succ = read_block_single_file(curfilename,
                                                    block_name,
                                                    dim2,
                                                    parttype,
                                                    no_mass_replicate,
                                                    fill_block_name,
                                                    slab_start,
                                                    slab_len,
                                                    verbose=False)

                if succ == True:
                    if dim2 > 1:
                        ret_val[dim1:dim1 + len(data), :] = data
                    else:
                        ret_val[dim1:dim1 + len(data)] = data
                    dim1 += long(data.shape[0])

                if (verbose):
                    if (succ):
                        print "Read particles (total) : ", ret_val.shape[0]
                        sys.stdout.flush()
                    else:
                        print "Read particles (total) : none"
                        sys.stdout.flush()

        # Implementation of reading specified particle positions.
        if (idsflag == True):
            dim1 = long(0)
            for num in range(0, filenum):
                curfilename = filename + "." + str(num) + ".hdf5"
                head = snapshot_header(curfilename)
                nloc = head.npart[parttype]

                low = ids[0]  # First particle to read
                high = ids[-1]  # Last particle to read

                if (nloc > low):  # Something to read in this file
                    toread = ids[
                        ids <
                        nloc]  # Need to get subset of ids still in this file
                    if (verbose):
                        print "Reading file           : ", num, curfilename
                        sys.stdout.flush()
                    data, succ = read_block_single_file(curfilename,
                                                        block_name,
                                                        dim2,
                                                        parttype,
                                                        no_mass_replicate,
                                                        fill_block_name,
                                                        ids=toread,
                                                        verbose=verbose)
                    if (succ == True):
                        if dim2 > 1:
                            ret_val[dim1:dim1 + len(data), :] = data
                        else:
                            ret_val[dim1:dim1 + len(data)] = data
                        dim1 += data.shape[0]
                    if (verbose):
                        if (succ):
                            # No longer need to divide by dim2. append would flatten array, not now.
                            print "Read particles (total) : ", ret_val.shape[0]
                            sys.stdout.flush()
                        else:
                            print "Read particles (total) : none"
                            sys.stdout.flush()

                ids -= nloc
                ids = ids[ids >= 0]  # Only keep particles not yet read
                if (len(ids) == 0 or high < 0):
                    break

        if (slabflag == True):
            off = slab_start
            left = slab_len
            first = True
            dim1 = long(0)
            for num in range(0, filenum):
                curfilename = filename + "." + str(num) + ".hdf5"
                head = snapshot_header(curfilename)
                nloc = head.npart[parttype]
                if (nloc > off):
                    start = off
                    if (nloc - off > left):
                        count = left
                    else:
                        count = nloc - off
                    if (verbose):
                        print "Reading file           : ", num, curfilename, start, count
                        sys.stdout.flush()
                    data, succ = read_block_single_file(curfilename,
                                                        block_name,
                                                        dim2,
                                                        parttype,
                                                        no_mass_replicate,
                                                        fill_block_name,
                                                        slab_start=start,
                                                        slab_len=count,
                                                        verbose=verbose)
                    if (succ == True):
                        if dim2 > 1:
                            ret_val[dim1:dim1 + len(data), :] = data
                        else:
                            ret_val[dim1:dim1 + len(data)] = data
                        dim1 += data.shape[0]
                    if (verbose):
                        if (succ):
                            # No longer need to divide by dim2, append would flatten array, not now.
                            print "Read particles (total) : ", ret_val.shape[0]
                            sys.stdout.flush()
                        else:
                            print "Read particles (total) : none"
                            sys.stdout.flush()

                    left -= count
                    off += count
                if (left == 0):
                    break
                off -= nloc

        if (verbose):
            print "all partial files read in"
            sys.stdout.flush()

    else:
        ret_val, succ = read_block_single_file(curfilename, block_name, dim2,
                                               parttype, no_mass_replicate,
                                               fill_block_name, slab_start,
                                               slab_len, verbose)

    return ret_val
Пример #23
0
def read_block_single_file(filename,
                           block_name,
                           dim2,
                           parttype=-1,
                           no_mass_replicate=False,
                           fill_block_name="",
                           slab_start=-1,
                           slab_len=-1,
                           ids=-1,
                           verbose=False):

    if os.path.exists(filename):
        curfilename = filename
    elif os.path.exists(filename + ".hdf5"):
        curfilename = filename + ".hdf5"
    elif os.path.exists(filename + ".0.hdf5"):
        curfilename = filename + ".0.hdf5"
    else:
        print "[error] file not found : ", filename
        sys.stdout.flush()
        sys.exit()

    if verbose:
        print "[single] reading file           : ", curfilename
        print "[single] reading                : ", block_name
        sys.stdout.flush()

    head = snapshot_header(curfilename)
    npart = head.npart
    massarr = head.massarr
    nall = head.nall
    filenum = head.filenum
    doubleflag = head.double

    if (parttype != -1):
        if (head.npart[parttype] == 0):
            return [0, False]
    else:
        if (head.npart.sum() == 0):
            return [0, False]
    del head

    #construct indices for partial access
    idsflag = False
    if (slab_start != -1) and (slab_len != -1):
        data_slice = slice(slab_start, (slab_start + slab_len))
    elif type(ids) == np.ndarray:
        idsflag = True
        data_slice = ids
    else:
        data_slice = slice(None, None, 1)

    if verbose:
        print "[single] data slice: ", data_slice
        sys.stdout.flush()

    f = hdf5lib.OpenFile(curfilename)

    #read specific particle type (parttype>=0, non-default)
    if parttype >= 0:
        if verbose:
            print "[single] parttype               : ", parttype
            sys.stdout.flush()
        if ((block_name == "Masses") and (npart[parttype] > 0)
                and (massarr[parttype] > 0)):
            if verbose:
                print "[single] replicate mass block"
                sys.stdout.flush()
            ret_val = np.repeat(massarr[parttype], npart[parttype])[data_slice]
        else:
            part_name = 'PartType' + str(parttype)
            if idsflag:
                ret_val = hdf5lib.GetData(f, part_name + "/" +
                                          block_name)[:][[data_slice]]
            else:
                ret_val = hdf5lib.GetData(f, part_name + "/" +
                                          block_name)[data_slice]
        if verbose:
            print "[single] read particles (total) : ", ret_val.shape[0] / dim2
            sys.stdout.flush()

    #read all particle types (parttype=-1, default)
    if parttype == -1:
        first = True
        dim1 = long(0)
        for parttype in range(0, 6):
            part_name = 'PartType' + str(parttype)
            if hdf5lib.Contains(f, "", part_name):
                if verbose:
                    print "[single] parttype               : ", parttype
                    print "[single] massarr                : ", massarr
                    print "[single] npart                  : ", npart
                    sys.stdout.flush()

                #replicate mass block per default (unless no_mass_replicate is set)
                if ((block_name == "Masses") and (npart[parttype] > 0)
                        and (massarr[parttype] > 0)
                        and (no_mass_replicate == False)):
                    if (verbose):
                        print "[single] replicate mass block"
                        sys.stdout.flush()
                    if (first):
                        data = np.repeat(massarr[parttype], npart[parttype])
                        dim1 += long(data.shape[0])
                        ret_val = data
                        first = False
                    else:
                        data = np.repeat(massarr[parttype], npart[parttype])
                        dim1 += long(data.shape[0])
                        ret_val = np.append(ret_val, data)
                    if (verbose):
                        print "[single] read particles (total) : ", ret_val.shape[
                            0] / dim2
                        sys.stdout.flush()
                    if (doubleflag == 0):
                        ret_val = ret_val.astype("float32")

                #fill fill_block_name with zeros if fill_block_name is set and particle type is present and fill_block_name not already stored in file for that particle type
                if ((block_name == fill_block_name)
                        and (block_name != "Masses") and (npart[parttype] > 0)
                        and
                    (hdf5lib.Contains(f, part_name, block_name) == False)):
                    if (verbose):
                        print "[single] replicate block name", fill_block_name
                        sys.stdout.flush()
                    if (first):
                        data = np.repeat(0.0, npart[parttype] * dim2)
                        dim1 += long(data.shape[0])
                        ret_val = data
                        first = False
                    else:
                        data = np.repeat(0.0, npart[parttype] * dim2)
                        dim1 += long(data.shape[0])
                        ret_val = np.append(ret_val, data)
                    if (verbose):
                        print "[single] read particles (total) : ", ret_val.shape[
                            0] / dim2
                        sys.stdout.flush()
                    if (doubleflag == 0):
                        ret_val = ret_val.astype("float32")

                #default: just read the block
                if (hdf5lib.Contains(f, part_name, block_name)):
                    if (first):
                        data = hdf5lib.GetData(f,
                                               part_name + "/" + block_name)[:]
                        dim1 += long(data.shape[0])
                        ret_val = data
                        first = False
                    else:
                        data = hdf5lib.GetData(f,
                                               part_name + "/" + block_name)[:]
                        dim1 += long(data.shape[0])
                        ret_val = np.append(ret_val, data)
                    if (verbose):
                        print "[single] read particles (total) : ", ret_val.shape[
                            0] / dim2
                        sys.stdout.flush()

        if ((dim1 > 0) & (dim2 > 1)):
            ret_val = ret_val.reshape(dim1, dim2)
            if (verbose):
                print "[single] reshape done: ", ret_val.shape
                sys.stdout.flush()

    f.close()
    gc.collect()

    return [ret_val, True]
Пример #24
0
    def __init__(self, *args, **kwargs):
        if (len(args) == 1):
            filename = args[0]

            if os.path.isfile(filename):
                curfilename = filename
            elif os.path.isfile(filename + ".hdf5"):
                curfilename = filename + ".hdf5"
            elif os.path.isfile(filename + ".0.hdf5"):
                curfilename = filename + ".0.hdf5"
            else:
                print "[error] file not found : ", filename
                sys.stdout.flush()
                sys.exit()

            print curfilename
            #                        sys.exit()

            f = hdf5lib.OpenFile(curfilename)
            self.npart = hdf5lib.GetAttr(f, "Header", "NumPart_ThisFile")
            self.nall = hdf5lib.GetAttr(f, "Header", "NumPart_Total")
            self.nall_highword = hdf5lib.GetAttr(f, "Header",
                                                 "NumPart_Total_HighWord")
            self.massarr = hdf5lib.GetAttr(f, "Header", "MassTable")
            self.time = hdf5lib.GetAttr(f, "Header", "Time")
            self.redshift = hdf5lib.GetAttr(f, "Header", "Redshift")
            self.boxsize = hdf5lib.GetAttr(f, "Header", "BoxSize")
            self.filenum = hdf5lib.GetAttr(f, "Header", "NumFilesPerSnapshot")
            self.omega0 = hdf5lib.GetAttr(f, "Header", "Omega0")
            self.omegaL = hdf5lib.GetAttr(f, "Header", "OmegaLambda")
            self.hubble = hdf5lib.GetAttr(f, "Header", "HubbleParam")
            self.sfr = hdf5lib.GetAttr(f, "Header", "Flag_Sfr")
            self.cooling = hdf5lib.GetAttr(f, "Header", "Flag_Cooling")
            self.stellar_age = hdf5lib.GetAttr(f, "Header", "Flag_StellarAge")
            self.metals = hdf5lib.GetAttr(f, "Header", "Flag_Metals")
            self.feedback = hdf5lib.GetAttr(f, "Header", "Flag_Feedback")
            try:
                self.double = hdf5lib.GetAttr(
                    f, "Header", "Flag_DoublePrecision")  #GADGET-2 change
            except:
                self.double = 0
            f.close()
            gc.collect()
        else:
            #read arguments
            self.npart = kwargs.get("npart")
            self.nall = kwargs.get("nall")
            self.nall_highword = kwargs.get("nall_highword")
            self.massarr = kwargs.get("massarr")
            self.time = kwargs.get("time")
            self.redshift = kwargs.get("redshift")
            self.boxsize = kwargs.get("boxsize")
            self.filenum = kwargs.get("filenum")
            self.omega0 = kwargs.get("omega0")
            self.omegaL = kwargs.get("omegaL")
            self.hubble = kwargs.get("hubble")
            self.sfr = kwargs.get("sfr")
            self.cooling = kwargs.get("cooling")
            self.stellar_age = kwargs.get("stellar_age")
            self.metals = kwargs.get("metals")
            self.feedback = kwargs.get("feedback")
            self.double = kwargs.get("double")

            #set default values
            if (self.npart is None):
                self.npart = np.array([0, 0, 0, 0, 0, 0], dtype="int32")
            if (self.nall is None):
                self.nall = np.array([0, 0, 0, 0, 0, 0], dtype="uint32")
            if (self.nall_highword is None):
                self.nall_highword = np.array([0, 0, 0, 0, 0, 0],
                                              dtype="uint32")
            if (self.massarr is None):
                self.massarr = np.array([0, 0, 0, 0, 0, 0], dtype="float64")
            if (self.time is None):
                self.time = np.array([0], dtype="float64")
            if (self.redshift is None):
                self.redshift = np.array([0], dtype="float64")
            if (self.boxsize is None):
                self.boxsize = np.array([0], dtype="float64")
            if (self.filenum is None):
                self.filenum = np.array([1], dtype="int32")
            if (self.omega0 is None):
                self.omega0 = np.array([0], dtype="float64")
            if (self.omegaL is None):
                self.omegaL = np.array([0], dtype="float64")
            if (self.hubble is None):
                self.hubble = np.array([0], dtype="float64")
            if (self.sfr is None):
                self.sfr = np.array([0], dtype="int32")
            if (self.cooling is None):
                self.cooling = np.array([0], dtype="int32")
            if (self.stellar_age is None):
                self.stellar_age = np.array([0], dtype="int32")
            if (self.metals is None):
                self.metals = np.array([0], dtype="int32")
            if (self.feedback is None):
                self.feedback = np.array([0], dtype="int32")
            if (self.double is None):
                self.double = np.array([0], dtype="int32")
Пример #25
0
def subhalo_offsets(snap = 135, run='Illustris-1'):
    snaptag=str(snap)
    f=hdf5lib.OpenFile('/n/ghernquist/Illustris/Runs/'+run+'/postprocessing/offsets/snap_offsets_subhalo_'+snaptag+'.hdf5', mode ='r' )
    data=hdf5lib.GetData(f, "Offsets")[:]
    f.close()
    return np.array(data)
Пример #26
0
    def make_one_frame(self,
                       iii,
                       frame_type=0,
                       snapnr_label=False,
                       overwrite=True,
                       show_time_label=False,
                       **kwargs):

        frame_name = "./plots/frames/" + self.savetag + "/frame_" + self.frametag + self.savetag + "_" + str(
            self.all_snapnrs[iii]).zfill(4) + ".png"
        if (overwrite or not os.path.exists(frame_name)):
            this_time = iii * self.dt_frames
            time_diff = self.snap_timing - this_time - self.snap_timing[0]

            try:
                index = np.where(
                    np.abs(time_diff) == np.min(np.abs(time_diff)))[0][0]
            except:
                print np.where(np.abs(time_diff) == np.min(np.abs(time_diff)))
                sys.exit()

            if time_diff[
                    index] < 0 and self.interp == True:  # on the negative side, get the positive side
                this_file1 = self.snap_list[index]
                this_file2 = self.snap_list[index + 1]
                interp_coef = (this_time - self.snap_timing[index]) / (
                    self.snap_timing[index + 1] - self.snap_timing[index])
            elif time_diff[index] > 0 and self.interp == True:
                this_file1 = self.snap_list[index - 1]
                this_file2 = self.snap_list[index]
                interp_coef = (this_time - self.snap_timing[index - 1]) / (
                    self.snap_timing[index + 1] - self.snap_timing[index])
            else:
                this_file1 = self.snap_list[index]
                this_file2 = self.snap_list[index]
                interp_coef = 0.0

            fov_min = (1.0 - self.zoom_factor) * self.fov
            fov_max = self.fov
            log_fac = np.log10(fov_max / fov_min)
            fac = (3.1416 / 2.0 + np.arctan(
                (1.0 * iii /
                 (1.0 * self.n_frames)) * 2 * 3.14159)) / (3.1416)  #0.0 -> 1.0
            this_fov = fov_max * 10.0**(-1.0 * fac * log_fac)
            if self.proj_depth > 0:
                this_thick = self.proj_depth
            else:
                this_thick = this_fov

            print " this_fov and thickness: "
            print this_fov, this_thick

            if frame_type == 0:  # single gas projection
                image = gasimages.gas_image('',
                                            0,
                                            this_file=this_file1,
                                            next_file=this_file2,
                                            interp_coef=interp_coef,
                                            xrange=[-self.fov, self.fov],
                                            yrange=[-self.fov, self.fov],
                                            dynrange=self.dynrange,
                                            maxden=self.maxden,
                                            pixels=self.npixels,
                                            interp=self.interp,
                                            **kwargs)

                fig = plt.figure(figsize=(5, 5))
                ax = fig.add_subplot(1, 1, 1)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                imgplot = ax.imshow(
                    image,
                    origin='lower',
                    extent=[-self.fov, self.fov, -self.fov, self.fov])
                ax.set_xlim([-self.fov, self.fov])
                ax.set_ylim([-self.fov, self.fov])
                fig.savefig(frame_name, dpi=self.npixels)
            elif frame_type == 1:
                image1 = gasimages.gas_image('',
                                             0,
                                             this_file=this_file1,
                                             next_file=this_file2,
                                             interp_coef=interp_coef,
                                             xrange=[-self.fov, self.fov],
                                             yrange=[-self.fov, self.fov],
                                             dynrange=self.dynrange,
                                             maxden=self.maxden,
                                             pixels=self.npixels,
                                             interp=self.interp,
                                             **kwargs)

                image2 = gasimages.gas_image('',
                                             0,
                                             this_file=this_file1,
                                             next_file=this_file2,
                                             interp_coef=interp_coef,
                                             xrange=[-self.fov, self.fov],
                                             yrange=[-self.fov, self.fov],
                                             dynrange=self.dynrange,
                                             maxden=self.maxden,
                                             pixels=self.npixels,
                                             interp=self.interp,
                                             **kwargs)

                fig = plt.figure(figsize=(10, 5))
                ax = fig.add_subplot(1, 2, 1)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                imgplot = ax.imshow(
                    image1,
                    origin='lower',
                    extent=[-self.fov, self.fov, -self.fov, self.fov])
                ax.set_xlim([-self.fov, self.fov])
                ax.set_ylim([-self.fov, self.fov])

                ax = fig.add_subplot(1, 2, 2)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                imgplot = ax.imshow(
                    image2,
                    origin='lower',
                    extent=[-self.fov, self.fov, -self.fov, self.fov])
                ax.set_xlim([-self.fov, self.fov])
                ax.set_ylim([-self.fov, self.fov])
                fig.savefig(frame_name, dpi=self.npixels)

            elif frame_type == 2:
                image = stellarimages.stellar_image(
                    '',
                    0,
                    this_file=this_file1,
                    next_file=this_file2,
                    interp_coef=interp_coef,
                    xrange=[-self.fov, self.fov],
                    yrange=[-self.fov, self.fov],
                    dynrange=self.dynrange,
                    maxden=self.maxden,
                    pixels=self.npixels,
                    interp=self.interp,
                    **kwargs)

                #		massmap = np.transpose( massmap )
                pic = np.zeros_like(image)
                for jjj in np.arange(image.shape[2]):
                    pic[:, :, jjj] = np.transpose(image[:, :, jjj])

                fig = plt.figure(figsize=(1, 1))
                ax = fig.add_subplot(1, 1, 1)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                extent = [-this_fov, this_fov, -this_fov, this_fov]
                imgplot = ax.imshow(pic, origin='lower', extent=extent)

                fig.savefig(frame_name, dpi=self.npixels)

            elif frame_type == 3:

                print kwargs.get('center_pos', [0, 0, 0])[0]
                print self.center_pos
                print self.arepo
                print this_fov
                print this_thick
                show_gasstarxray = 'gas'
                if self.plottype == 'gas': show_gasstarxray = 'gas'
                if self.plottype == 'sfr': show_gasstarxray = 'sfr'
                if self.IFU or self.plottype == 'sfr':
                    self.threecolor = False

                if this_thick > 0:
                    zrange = [-this_thick, this_thick]
                else:
                    zrange = 0

                image, massmap = pfh_image_maker.image_maker(
                    self.sdir,
                    self.all_snapnrs[iii],
                    snapdir_master='',
                    outdir_master='',
                    filename_set_manually='tmp_remove',
                    center_pos=self.center_pos,
                    #center_on_bh=0,
                    xrange=[-this_fov, this_fov],
                    zrange=zrange,
                    #                                                        dynrange=self.dynrange, maxden=self.maxden,
                    threecolor=self.threecolor,
                    pixels=self.npixels,
                    show_gasstarxray=show_gasstarxray,
                    theta=self.theta,
                    arepo=self.arepo,
                    **kwargs)

                print "image values"
                print image
                print image.shape
                print massmap
                print " "

                massmap = np.transpose(massmap)
                pic = np.zeros_like(image)
                for jjj in np.arange(image.shape[2]):
                    pic[:, :, jjj] = np.transpose(image[:, :, jjj])

                if self.IFU:
                    manga_mask = self.create_ifu_mask(pic)

                #star_xyz = ws.read_block(this_file1, 'POS ', 4)

                fig = plt.figure(figsize=(1, 1))
                ax = fig.add_subplot(1, 1, 1)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                extent = [-this_fov, this_fov, -this_fov, this_fov]
                imgplot = ax.imshow(pic, origin='lower', extent=extent)
                if self.IFU:
                    imgplot = ax.imshow(np.log10(manga_mask),
                                        origin='lower',
                                        extent=extent,
                                        cmap='Greys',
                                        vmin=-1,
                                        vmax=1,
                                        alpha=0.5)

                if self.plot_stars:
                    try:
                        star_xyz = ws.read_block(this_file1, 'POS ', 4)
                        ax.scatter(star_xyz[:, 0] -
                                   kwargs.get('center_pos', [0, 0, 0])[0],
                                   star_xyz[:, 1] -
                                   kwargs.get('center_pos', [0, 0, 0])[1],
                                   s=0.1,
                                   lw=0,
                                   facecolor='r')
                    except:
                        print "no stars to scatter"

                ax.set_xlim([-1.0 * this_fov, this_fov])
                ax.set_ylim([-1.0 * this_fov, this_fov])
                plt.axis('off')
                if snapnr_label:
                    ax.text(0.75 * this_fov,
                            0.85 * this_fov,
                            str(iii).zfill(3),
                            fontsize=3,
                            path_effects=[
                                PathEffects.withStroke(linewidth=1,
                                                       foreground="w")
                            ])  #color='w' )
                if show_time_label:
                    ax.text(0.05,
                            0.95,
                            "t={:.2f}Gyr".format(iii * self.dt_frames),
                            fontsize=2,
                            ha='left',
                            transform=ax.transAxes,
                            path_effects=[
                                PathEffects.withStroke(linewidth=0.2,
                                                       foreground="w")
                            ])

                if self.IFU:
                    png_file = "./plots/frames/frame_ifu_" + self.savetag + "_" + str(
                        self.all_snapnrs[iii]).zfill(4) + ".png"
                    hdf5_file = "./plots/frames/frame_ifu_" + self.savetag + "_" + str(
                        self.all_snapnrs[iii]).zfill(4) + ".hdf5"

                    fig.savefig(png_file, dpi=self.npixels)

                    f = hdf5lib.OpenFile(hdf5_file, mode='w')

                    group = hdf5lib.CreateGroup(f, "GasSurfaceDensity")
                    hdf5lib.CreateArray(f, group, 'data', massmap.flatten())
                    group = hdf5lib.CreateGroup(f, "MangaMap")
                    hdf5lib.CreateArray(f, group, 'manga_mask',
                                        -1.0 * manga_mask.flatten())
                    f.close()

                else:
                    fig.savefig(frame_name, dpi=self.npixels)
            elif frame_type == 4:
                image, massmap = pfh_image_maker.image_maker(
                    self.sdir,
                    self.all_snapnrs[iii],
                    snapdir_master='',
                    outdir_master='',
                    show_time_label=0,
                    filename_set_manually='tmp_remove',
                    center_on_bh=0,
                    xrange=[-this_fov, this_fov],
                    dynrange=self.dynrange,
                    maxden=self.maxden,
                    pixels=self.npixels,
                    **kwargs)

                pic1 = np.zeros_like(image)
                for jjj in np.arange(3):
                    pic1[:, :, jjj] = np.transpose(image[:, :, jjj])

                image, massmap = pfh_image_maker.image_maker(
                    self.sdir,
                    self.all_snapnrs[iii],
                    snapdir_master='',
                    outdir_master='',
                    show_time_label=0,
                    filename_set_manually='tmp_remove',
                    center_on_bh=0,
                    xrange=[-this_fov, this_fov],
                    dynrange=self.dynrange,
                    maxden=self.maxden,
                    pixels=self.npixels,
                    theta=90,
                    **kwargs)

                pic2 = np.zeros_like(image)
                for jjj in np.arange(3):
                    pic2[:, :, jjj] = np.transpose(image[:, :, jjj])

                fig = plt.figure(figsize=(2, 1))
                ax = fig.add_subplot(1, 2, 1)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                imgplot = ax.imshow(
                    pic1,
                    origin='lower',
                    extent=[-this_fov, this_fov, -this_fov, this_fov])
                ax.set_xlim([-1.0 * this_fov, this_fov])
                ax.set_ylim([-1.0 * this_fov, this_fov])
                plt.axis('off')

                ax = fig.add_subplot(1, 2, 2)
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)
                imgplot = ax.imshow(
                    pic2,
                    origin='lower',
                    extent=[-this_fov, this_fov, -this_fov, this_fov])
                ax.set_xlim([-1.0 * this_fov, this_fov])
                ax.set_ylim([-1.0 * this_fov, this_fov])
                plt.axis('off')
                fig.savefig(frame_name, dpi=self.npixels)

            else:
                fig = plt.figure(figsize=(10, 5))
                fig.subplots_adjust(left=0.0,
                                    right=1.0,
                                    top=1.0,
                                    bottom=0.0,
                                    wspace=0.0,
                                    hspace=0.0)

                ax = fig.add_subplot(1, 2, 1)
                imgplot = ax.imshow(
                    image1,
                    origin='lower',
                    extent=[-self.fov, self.fov, -self.fov, self.fov])
                ax.set_xlim([-self.fov, self.fov])
                ax.set_ylim([-self.fov, self.fov])
                ax.axes.xaxis.set_ticklabels([])
                ax.axes.yaxis.set_ticklabels([])

                ax = fig.add_subplot(1, 2, 2)
                imgplot = ax.imshow(image2,
                                    origin='lower',
                                    extent=[
                                        -self.fov / 10.0, self.fov / 10.0,
                                        -self.fov / 10.0, self.fov / 10.0
                                    ])
                ax.set_xlim([-self.fov / 10.0, self.fov / 10.0])
                ax.set_ylim([-self.fov / 10.0, self.fov / 10.0])

                ax.axes.xaxis.set_ticklabels([])
                ax.axes.yaxis.set_ticklabels([])

                fig.savefig(frame_name, dpi=self.npixels)
Пример #27
0
    def __init__(self, basedir, snapnum, long_ids=False, double_output=False, grpcat=True, subcat=True, name="fof_subhalo_tab", keysel=[]):

        if long_ids: self.id_type = np.uint64
        else: self.id_type = np.uint32
        if double_output: self.double_type = np.float32
        else: self.double_type = np.float64

        filenum = 0
        doneflag = False
        skip_gr = 0
        skip_sub = 0
        vardict = {}
        if keysel is None:
            keysel = grp_datablocks.items()

        while not doneflag:
            self.filebase, curfile = naming.return_subfind_filebase(basedir, snapnum, name, filenum)
            self.firstfile = curfile

            f=hdf5lib.OpenFile(curfile)
            ngroups = hdf5lib.GetAttr(f, "Header", "Ngroups_ThisFile")
            nsubs = hdf5lib.GetAttr(f, "Header", "Nsubgroups_ThisFile")
            nfiles = hdf5lib.GetAttr(f, "Header", "NumFiles")
            if filenum == 0:
                self.ngroups = hdf5lib.GetAttr(f, "Header", "Ngroups_Total")
                self.nids = hdf5lib.GetAttr(f, "Header", "Nids_Total")
                self.nsubs = hdf5lib.GetAttr(f, "Header", "Nsubgroups_Total")
                #GROUPS
                if grpcat:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Group", key):
                            val = grp_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            if (type=='FLOAT'):
                                vars(self)[key]=np.empty(self.ngroups, dtype=np.dtype((self.double_type,dim)))
                            if (type=='INT'):
                                vars(self)[key]=np.empty(self.ngroups, dtype=np.dtype((np.int32,dim)))
                            if (type=='INT64'):
                                vars(self)[key]=np.empty(self.ngroups, dtype=np.dtype((np.int64,dim)))
                            if (type=='ID'):
                                vars(self)[key]=np.empty(self.ngroups, dtype=np.dtype((self.id_type,dim)))
                            vardict[key]=vars(self)[key]


                #SUBHALOS
                if subcat:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Subhalo", key):
                            val = sub_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            if (type=='FLOAT'):
                                vars(self)[key]=np.empty(self.nsubs, dtype=np.dtype((self.double_type,dim)))
                            if (type=='INT'):
                                vars(self)[key]=np.empty(self.nsubs, dtype=np.dtype((np.int32,dim)))
                            if (type=='INT64'):
                                vars(self)[key]=np.empty(self.nsubs, dtype=np.dtype((np.int64,dim)))
                            if (type=='ID'):
                                vars(self)[key]=np.empty(self.nsubs, dtype=np.dtype((self.id_type,dim)))
                            vardict[key]=vars(self)[key]

            #GROUPS
            if grpcat:
                if ngroups > 0:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Group", key):
                            val = grp_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            a=hdf5lib.GetData(f, "Group/"+key)
                            if dim==1:
                                vardict[key][skip_gr:skip_gr + ngroups]=a[:]
                            else:
                                for d in range(0,dim):
                                    vardict[key][skip_gr:skip_gr + ngroups,d]=a[:,d]

                    skip_gr += ngroups
            #SUBHALOS
            if subcat:
                if nsubs > 0:
                    for key in keysel:
                        if hdf5lib.Contains(f, "Subhalo", key):
                            val = sub_datablocks[key]
                            type = val[0]
                            dim = val[1]
                            a=hdf5lib.GetData(f, "Subhalo/"+key)
                            if dim==1:
                                vardict[key][skip_sub:skip_sub + nsubs]=a[:]
                            else:
                                for d in range(0,dim):
                                    vardict[key][skip_sub:skip_sub + nsubs,d]=a[:,d]

                    skip_sub += nsubs

            f.close()

            filenum += 1
            if filenum == nfiles: doneflag = True
Пример #28
0
def openfile(filename, mode="w"):
    f = hdf5lib.OpenFile(filename, mode=mode)
    return f
Пример #29
0
def read_insitu():
    file = "/n/ghernquist/vrodrigu/StellarAssembly/output/Illustris/L75n1820FP/stars_135.hdf5"
    f = hdf5lib.OpenFile(file, mode='r')
    data = hdf5lib.GetData(f, "InSitu")[:]
    f.close()
    return np.array(data)
Пример #30
0
    def make_one_frame(self, iii, frame_type=0, **kwargs):
        this_time = iii * self.dt_frames
        this_fov = self.fov

        pic = np.zeros((self.npixels, self.npixels))
        ifu_r, manga_mask = self.create_ifu_mask(pic)

        hdf5_file = "./plots/frames/frame_ifu_" + self.savetag + "_" + str(
            self.all_snapnrs[iii]).zfill(4) + ".hdf5"
        f = hdf5lib.OpenFile(hdf5_file, mode='w')
        group = hdf5lib.CreateGroup(f, "MangaMap")
        hdf5lib.CreateArray(f, group, 'manga_mask',
                            -1.0 * manga_mask.flatten())
        hdf5lib.CreateArray(f, group, 'fiber_r', ifu_r)

        group = hdf5lib.CreateGroup(f, "Header")
        hdf5lib.CreateArray(f, group, 'time', [this_time])

        # first do gas mass plot
        show_gasstarxray = 'gas'
        threecolor = False

        if False:
            image, massmap = pfh_image_maker.image_maker(
                self.sdir,
                self.all_snapnrs[iii],
                snapdir_master='',
                outdir_master='',
                show_time_label=0,
                filename_set_manually='tmp_remove',
                center_on_bh=0,
                xrange=[-this_fov, this_fov],
                dynrange=self.gas_dynrange,
                maxden=self.gas_maxden,
                threecolor=threecolor,
                pixels=self.npixels,
                show_gasstarxray=show_gasstarxray,
                #							include_lighting=0,
                **kwargs)

            massmap = np.transpose(massmap)
            pic = np.zeros_like(image)
            for jjj in np.arange(image.shape[2]):
                pic[:, :, jjj] = np.transpose(image[:, :, jjj])

            fig, ax = setup_my_ifu_figure(this_fov)  #
            extent = [-this_fov, this_fov, -this_fov, this_fov]
            imgplot = ax.imshow(pic, origin='lower', extent=extent)
            print "pic min max:"
            print pic.min(), pic.max(), pic.shape
            imgplot = ax.imshow(np.log10(manga_mask),
                                origin='lower',
                                extent=extent,
                                cmap='Greys',
                                vmin=-1,
                                vmax=1,
                                alpha=0.5)

            png_file = "./plots/frames/frame_ifu_gas_" + self.savetag + "_" + str(
                self.all_snapnrs[iii]).zfill(4) + ".png"
            fig.savefig(png_file, dpi=self.npixels)

            group = hdf5lib.CreateGroup(f, "GasSurfaceDensity")
            hdf5lib.CreateArray(f, group, 'map', massmap.flatten())

            gas_ifu_data = self.create_ifu_average(
                massmap)  # aveage surface density in fiber
            hdf5lib.CreateArray(f, group, 'fiber_data', gas_ifu_data)
        # =========================================== #
        # second do sfr mass plot
        if False:
            show_gasstarxray = 'sfr'
            threecolor = False

            image, massmap = pfh_image_maker.image_maker(
                self.sdir,
                self.all_snapnrs[iii],
                snapdir_master='',
                outdir_master='',
                show_time_label=0,
                filename_set_manually='tmp_remove',
                center_on_bh=0,
                xrange=[-this_fov, this_fov],
                dynrange=self.sfr_dynrange,
                maxden=self.sfr_maxden,
                threecolor=threecolor,
                pixels=self.npixels,
                show_gasstarxray=show_gasstarxray,
                **kwargs)

            massmap = np.transpose(massmap)
            pic = np.zeros_like(image)
            for jjj in np.arange(image.shape[2]):
                pic[:, :, jjj] = np.transpose(image[:, :, jjj])

            fig, ax = setup_my_ifu_figure(this_fov)
            extent = [-this_fov, this_fov, -this_fov, this_fov]
            imgplot = ax.imshow(pic, origin='lower', extent=extent)
            imgplot = ax.imshow(np.log10(manga_mask),
                                origin='lower',
                                extent=extent,
                                cmap='Greys',
                                vmin=-1,
                                vmax=1,
                                alpha=0.5)

            png_file = "./plots/frames/frame_ifu_sfr_" + self.savetag + "_" + str(
                self.all_snapnrs[iii]).zfill(4) + ".png"
            fig.savefig(png_file, dpi=self.npixels)

            group = hdf5lib.CreateGroup(f, "GasSFR")
            hdf5lib.CreateArray(f, group, 'map', massmap.flatten())
            sfr_ifu_data = self.create_ifu_total(
                massmap)  # aveage surface density in fiber
            hdf5lib.CreateArray(f, group, 'fiber_data', sfr_ifu_data)
    # =========================================== #

    # =========================================== #
    # third do stellar light plot
        if False:
            show_gasstarxray = 'star'
            threecolor = True

            image, massmap = pfh_image_maker.image_maker(
                self.sdir,
                self.all_snapnrs[iii],
                snapdir_master='',
                outdir_master='',
                show_time_label=0,
                filename_set_manually='tmp_remove',
                center_on_bh=0,
                xrange=[-this_fov, this_fov],
                dynrange=self.star_dynrange,
                maxden=self.star_maxden,
                threecolor=threecolor,
                pixels=self.npixels,
                show_gasstarxray=show_gasstarxray,
                include_lighting=0,
                **kwargs)

            massmap = np.transpose(massmap)
            print massmap.shape
            print image.shape

            pic = np.zeros_like(image)
            for jjj in np.arange(image.shape[2]):
                pic[:, :, jjj] = np.transpose(image[:, :, jjj])

            fig, ax = setup_my_ifu_figure(this_fov)
            extent = [-this_fov, this_fov, -this_fov, this_fov]
            imgplot = ax.imshow(pic,
                                origin='lower',
                                extent=extent,
                                interpolation='bicubic',
                                aspect='normal')
            imgplot = ax.imshow(np.log10(manga_mask),
                                origin='lower',
                                extent=extent,
                                cmap='Greys',
                                vmin=-1,
                                vmax=1,
                                alpha=0.5)

            png_file = "./plots/frames/frame_ifu_star_" + self.savetag + "_" + str(
                self.all_snapnrs[iii]).zfill(4) + ".png"
            fig.savefig(png_file, dpi=self.npixels)

            group = hdf5lib.CreateGroup(f, "StellarImage")
            hdf5lib.CreateArray(f, group, 'map', pic.flatten())
    # =========================================== #

        if True:
            show_gasstarxray = 'gas'
            threecolor = False
            image, tot_massmap = pfh_image_maker.image_maker(
                self.sdir,
                self.all_snapnrs[iii],
                snapdir_master='',
                outdir_master='',
                show_time_label=0,
                filename_set_manually='tmp_remove',
                center_on_bh=0,
                xrange=[-this_fov, this_fov],
                dynrange=self.gas_dynrange * 1e9,
                maxden=self.gas_maxden * 1e3,
                threecolor=threecolor,
                pixels=self.npixels,
                show_gasstarxray=show_gasstarxray,
                include_lighting=0,
                do_with_colors=0,
                h_rescale_factor=3.0,
                **kwargs)
            show_gasstarxray = 'gas_metallicity'
            threecolor = False
            image, met_massmap = pfh_image_maker.image_maker(
                self.sdir,
                self.all_snapnrs[iii],
                snapdir_master='',
                outdir_master='',
                show_time_label=0,
                filename_set_manually='tmp_remove',
                center_on_bh=0,
                xrange=[-this_fov, this_fov],
                dynrange=self.gas_dynrange * 1e9,
                maxden=self.gas_maxden * 1e3,
                threecolor=threecolor,
                pixels=self.npixels,
                show_gasstarxray=show_gasstarxray,
                include_lighting=0,
                h_rescale_factor=3.0,
                **kwargs)

            metallicity_map = met_massmap / tot_massmap  # + np.min( tot_massmap[ tot_massmap > 0 ] ) )
            print " "
            print np.min(met_massmap), np.max(met_massmap), np.median(
                met_massmap), np.mean(met_massmap)
            print np.min(tot_massmap), np.max(tot_massmap), np.median(
                tot_massmap), np.mean(tot_massmap)
            print np.min(metallicity_map), np.max(metallicity_map), np.median(
                metallicity_map), np.mean(metallicity_map)
            print " "

            #                massmap = np.transpose( massmap )
            #                pic = np.zeros_like(image)
            #                for jjj in np.arange(image.shape[2]):
            #                    pic[:,:,jjj] = np.transpose( image[:,:,jjj] )

            fig, ax = setup_my_ifu_figure(this_fov)  #
            extent = [-this_fov, this_fov, -this_fov, this_fov]
            min_met = 2e-2  # the starting metallicity
            max_met = 6e-2
            metallicity_map[metallicity_map < min_met] = min_met
            metallicity_map[metallicity_map > max_met] = max_met
            metallicity_map[tot_massmap < self.gas_maxden /
                            self.gas_dynrange] = min_met

            cmap = plt.get_cmap('afmhot_r')
            scaled_met = np.log10(metallicity_map / min_met) / (np.log10(
                max_met / min_met))
            image = cmap(scaled_met)

            alpha_vals = np.log10(tot_massmap / (
                (self.gas_maxden / 10.0) / self.gas_dynrange)) / np.log10(
                    self.gas_dynrange)
            alpha_vals[alpha_vals > 1.0] = 1.0
            alpha_vals[alpha_vals < 0] = 0.0
            image[:, :, 3] = image[:, :, 3] * alpha_vals

            implot = ax.imshow(
                image, origin='lower', extent=extent
            )  #, cmap='afmhoat', vmin=np.log10(min_met), vmax=np.log10(max_met))
            print "pic min max:"
            print pic.min(), pic.max(), pic.shape
            imgplot = ax.imshow(np.log10(manga_mask),
                                origin='lower',
                                extent=extent,
                                cmap='Greys',
                                vmin=-1,
                                vmax=1,
                                alpha=0.5)

            png_file = "./plots/frames/frame_ifu_gas_metallicity_" + self.savetag + "_" + str(
                self.all_snapnrs[iii]).zfill(4) + ".png"
            fig.savefig(png_file, dpi=self.npixels)

            #group = hdf5lib.CreateGroup(f, "GasMetallicity")
            #hdf5lib.CreateArray(f, group, 'map', massmap.flatten() )

            #gas_ifu_data = self.create_ifu_average( massmap )       # aveage surface density in fiber
            #hdf5lib.CreateArray(f, group, 'fiber_data', gas_ifu_data )

    # =========================================== #

        f.close()