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

Markus Neteler neteler at itc.it
Mon Dec 6 11:20:38 EST 2004


On Mon, Dec 06, 2004 at 03:02:43PM +0000, Glynn Clements wrote:
> 
> 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 */

I'm not sure what's preferred.

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

Thanks for the quick help, Glynn.

Markus




More information about the grass-dev mailing list