Creating a 2D histogram of stellar mass distribution

Thiago Bueno Dalpiaz
  • 1
  • 24 Jan '23

Hi there,

I'm trying to create a 2D histogram of stellar mass distribution from a TNG50 subhalo. I've been trying to adapt the example availabre here with task 6, as I show below

baseUrl = 'http://www.tng-project.org/api/'
headers = {"api-key":" "}
r = get(baseUrl)
sim = get(r['simulations'][18]['url'])
snaps = get( sim['snapshots'] )
snap = get( snaps[-1]['url'] )
subs = get( snap['subhalos'] )
sub = get( subs['results'][1]['url'] )

sub_prog_url = subs['results'][8]['url']
sub_prog = get(sub_prog_url)
params = {'stars':'Coordinates,Masses,GFM_Metallicity'}
cutout = get(sub_prog_url+"cutout.hdf5", params)

with h5py.File(cutout) as f:
    dx = f['PartType4']['Coordinates'][:,0] - sub['pos_x']
    dy = f['PartType4']['Coordinates'][:,1] - sub['pos_y']
    dz = f['PartType4']['Coordinates'][:,2] - sub['pos_z']

dens = np.log10(f['PartType4']['Masses'][:])

plt.hist2d(dx,dy,weights=dens**20,bins=[250,250])
plt.xlabel('$\Delta x$ [ckpc/h]')
plt.ylabel('$\Delta y$ [ckpc/h]');

However, it presents an error at dens, which says ValueError: Not a location (invalid object ID)

Thanks in advance!

Dylan Nelson
  • 24 Jan '23

This ValueError: Not a location (invalid object ID) comes from h5py. It means you are trying to read from a HDF5 file which has already been closed.

If you just indent dens to be within the with block, then this will work, because f is closed automatically at the end of the with block.

Thiago Bueno Dalpiaz
  • 3 Feb '23

Thanks!

  • Page 1 of 1