# Gmt math: Calculating a cumulative monthly sum on table data

Using `gmtmath 6.1.0_81d4900_2020.04.08`

I have table of time series data (see below) on which I would like to calculate running cumulative monthly sum, i.e., a cumulative sum that resets to zero on the first day of each month. Is there a way to do this with `gmt math`?

``````1871-1-1 0
1871-1-2 0.4
1871-1-3 0
1871-1-4 0
1871-1-5 0
1871-1-6 0
1871-1-7 0
1871-1-8 0
1871-1-9 0
1871-1-10 0
1871-1-11 2.9
1871-1-12 0
1871-1-13 0.9
1871-1-14 2.9
1871-1-15 2
1871-1-16 0.2
1871-1-17 14.7
1871-1-18 0
1871-1-19 0
1871-1-20 0
1871-1-21 0
1871-1-22 0
1871-1-23 0
1871-1-24 2.9
1871-1-25 0.3
1871-1-26 0
1871-1-27 0
1871-1-28 0
1871-1-29 0
1871-1-30 0
1871-1-31 0.7
1871-2-1 0
1871-2-2 0
1871-2-3 0
1871-2-4 0
1871-2-5 0
1871-2-6 2
1871-2-7 0.2
1871-2-8 2.4
1871-2-9 0
1871-2-10 0
1871-2-11 1.3
1871-2-12 0
1871-2-13 1.3
1871-2-14 0
1871-2-15 1.9
1871-2-16 0.7
1871-2-17 0``````

No. Maybe break file into months and then do the cumulate sum per month file, then stitch back together. Or maybe run filter with a 30-day box car, then only print out one value for day 15, then multiply by 30. WIth months varying in length it is not a good unit.

Solved this with the following awk script. The input file has year, month, day in columns 3,4,5 and the variable of interest in column 20 (column positions would be different in another file).

``````BEGIN{
getline
lastmon=\$4
cumsum=\$20
}
{
mon=\$4
if (mon == lastmon) {
cumsum = cumsum + \$20
} else {
cumsum = \$20
lastmon = mon
}
printf "%lg-%lg-%lg %lg %lg\n",\$3,\$4,\$5,cumsum,\$20
}``````