Help save net neutrality! Learn more.

Calculate Time of Max Temperature?

  • John

    John - 2014-01-18

    I have temperature data w/ 3 dimensions (lat, lon, time). I want to create a grid that contains the value of the time dimension when max temperature occurs. I'm guessing I'll have to use ncap2 and write a script. Is there a simpler way?


    • augusto

      augusto - 2014-01-21

      When you extract the max of your variable using 'ncwa' it gives you the corresponding coordinates and time step, I use:
      ncwa -y max -v temp

      Hope it helps,


  • Charlie Zender

    Charlie Zender - 2014-01-20

    There is no simpler way that I can think of.
    This would be a great feature to have in ncap2, and has been suggested before,
    I don't think we ever implemented a simpler way than writing a script.
    Henry will correct me if I am wrong.

  • Charlie Zender

    Charlie Zender - 2014-01-21


    ncwa -y max returns the maximum value in each variable independently.
    John wants to know the value of a second variable (time) corresponding to the maximum values of the first variable (temperature). The two are linked, and NCO/ncap2 does not have a simple, elegant way of getting that linked information.


  • Charlie Zender

    Charlie Zender - 2014-01-23

    Henry pointed out that NCO supports what you want to do through
    ncap2/GSL functions. Specifically, gsl_stats_min_index() and gsl_stats_max_index(). Please see
    for more info on calling them from ncap2.

  • John

    John - 2014-01-24

    Thanks all for your ideas. I wrote a script for ncap2 and it wasn't too bad. About 15 lines of code, and it runs pretty fast too. The trick was to always use 'where' rather than iterating over the lat, lon dimensions.

  • Sean Davis

    Sean Davis - 2017-08-09

    Could you provide this script?

  • John

    John - 2017-08-12

    I had trouble finding that script, so I just kind of typed it out. I hope it works! Or gets you close to it.

    // Assume temperature data is stored in a 3-dimensional variable called 'temperature',
    //  and the 3 dimensions are time, lat, lon.
    // Create 2-dim variables using 'temperature' as a template.
    *max_value = array(0, 1, temperature(0,:,:));
    *max_time =  array(0, 1, temperature(0,:,:));
    // Set max time to the first time step.
    max_value = temperature(0,:,:);
    max_time = 0;
    // Iterate over time.
    for(*i = 1; i < $time.size; i++)
            // If the i'th value is greater than max time found so far, update max_time.
            where (temperature(i,:,:) > max_value)
                    max_time = i;
                    max_value = temperature(i,:,:);
    Last edit: John 2017-08-12

Log in to post a comment.