ncap2 script to calculate mode

Help
John
2014-02-11
2014-02-11
  • John
    John
    2014-02-11

    I have a netcdf file with categorical variable, cat(time,lat,lon). For each grid cell, I want to calculate the mode of the category values. I'm starting by tabulating a histogram, using the ncap2 script below, but the result is just 0's and 50's. I suspect that the way I'm incrementing the histogram is wrong. Can someone suggest a better way? Thanks!

    *sz_time = $time.size;
    *sz_lat = $lat.size;
    *sz_lon = $lon.size;
    defdim("num_categories", 50);
    
    *histo[$num_categories,$lat,$lon] = 0s;
    
    for(*t = 0; t < sz_time; t++)
    {
            for(*c = 0; c < 50; c++)
            {
                    where (cat(t,:,:) == c)
                    {
                            histo(c,:,:) = histo(c,:,:) + 1;
                    }
            }
    }
    histo.ram_write();