[GRASS5] r.sunmask: how to avoid hh:60 output for sunset/sunrise?

Glynn Clements glynn at gclements.plus.com
Mon Dec 6 10:02:43 EST 2004


Markus Neteler wrote:

> small C programming question:
> 
> r.sunmask -sg el=dtm.10meters timezone=1 output=vuoto  east=1694215 north=5104320 year=1998 month=12 day=23 hour=12 minute=15 sec=0

> sunrise=07:60   <------- ! Would like to see 8:00 here

> The related code looks like this:
>   fprintf (stdout, "sunrise=%02.0f:%02.0f\n", floor(pdat->sretr/60.), fmod(pdat->sretr, 60.));
>   fprintf (stdout, "sunset=%02.0f:%02.0f\n", floor(pdat->ssetr/60.), fmod(pdat->ssetr, 60.));

This will happen if the modulus is between 59.5 and 60; printf("%.0f") will
round to nearest rather than truncating.

> Should I add 'if' (if minutes >=60) conditions or is there a better way
> to avoid 07:60 output?

Convert pdat->sretr to pdat->ssetr to integers, e.g.:

   int sretr = (int) floor(pdat->sretr + 0.5);
   int ssetr = (int) floor(pdat->ssetr + 0.5);
   /* Remove +0.5 above if you want round-down instead of round-to-nearest */

   fprintf (stdout, "sunrise=%02d:%02d\n", sretr/60, sretr%60));
   fprintf (stdout, "sunset=%02d:%02d\n", ssetr/60, ssetr%60));

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list