## Calculate Time of Max Temperature? document.SUBSCRIPTION_OPTIONS = { "thing": "thread", "subscribed": false, "url": "subscribe", "icon": { "css": "fa fa-envelope-o" } };

Help
John
2014-01-18
2014-01-24
• 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?

Thanks.

• augusto - 2014-01-21

John,
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 in.nc out.nc

Hope it helps,

Augusto

• 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.
cz

• Charlie Zender - 2014-01-21

Augusto,

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.

cz

• 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 http://nco.sf.net/nco.html#gsl
cz

• 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 - 2017-08-09

Could you provide this script?

• 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,:,:);
}
}

max_time.ram_write();
```

Last edit: John 2017-08-12