function to compute HDI from a grid