[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Orekit Developers] Doy support



Le 10/07/2011 23:06, Andreas Rieger a écrit :
> Hello,
> 
> I would like to see support for doy (Day of Year) in orekit. This format is quit
> common in a lot of space application (SCOS use it for example) So conversion to
> and from are quit a standard task. I think a timeutil class with various support
> functions would be a good idea.

Day of year is already supported in Orekit, as well as dates based on
day of month, day of week and day since a reference epoch.
Look at the DateComponents class. The constructor that takes two
integers uses the first one as the year and the second one as day of
year. There is also a getDayOfYear method to retrieve the doy from
already built components.

This class (and AbsoluteDate as well if you add the time part) also
support parsing a date from a string in doy format amon others formats,
as long as it is complient with iso-8601 standard. The formats are as
follows:

     basic format calendar date:    YYYYMMDD
     extended format calendar date: YYYY-MM-DD
     basic format ordinal date:     YYYYDDD
     extended format ordinal date:  YYYY-DDD
     basic format week date:        YYYYWwwD
     extended format week date:     YYYY-Www-D

Where YYYY is a year number, MM a month number, DD a day of month, DDD a
day of year, D a day of week and W and - are litteral constant characters.

Hope this helps,
Luc


> 
> I have in my code for example:
> 
> 
> public class TimeConstants {
> 
> 
> 
>   /**
>    * sum of days in a year without a leap per month
>    */
>   public static final int [] month_to_day_no_leap = {0, 31, 59, 90, 120, 151, 181,
> 212, 243, 273, 304, 334, 365};
>   /**
>    * sum of days in a year without a leap per month
>    */
>   public static final int [] month_to_day_leap = {0, 31, 60, 91, 121, 152, 182,
> 213, 244, 274, 305, 335, 366};
> 
>   public static final double  SECONDS_IN_WEEK = 604800.0;
> 
> 
> 
> 
> }
> 
> #########################################################################
> 
> 
> 
> 
> /**
>    * Converts for a day month year input the corresponding doy
>    * doy = day of year
>    * @param year example 2008
>    * @param month from 1 to 12
>    * @param mday from 1 to 31
>    * @return the doy
>    * @throws JatcoreException
>    */
>   public static int day2doy(int year, int month, int mday) throws JatcoreException{
>      int doy = 0;
>     try {
> 
>       if ((year % 4) == 0) {
>         doy = TimeConstants.month_to_day_no_leap[month - 1] + mday;
>       } else {
>         doy = TimeConstants.month_to_day_leap[month - 1] + mday;
>       }
> 
>     } catch (Exception ioe) {
>        throw new JatcoreException(ioe.getMessage(), ioe);
>     }
>    return doy;
>   }
> 
> 
> 
>   /**
>    * Conversion from a given Year and Doy to a month
>    * Doy = Day of Year
>    * @param year sample 2008
>    * @param doy Day of Year from 1 to 366
>    * @return month from 1 to 12
>    * @throws JatcoreException
>    */
>   public static int doy2month(int year, int doy)
>   throws JatcoreException {
>     int month=0;
> 
>     try {
>          int guess = (int)(doy*0.0328);
>         int more = -1;
> 
> 
>         if((year%4) != 0 ){
>           month = ((doy - TimeConstants.month_to_day_no_leap[guess+1]) > 0) ?
> guess+2: guess+1;
>         }
>         else {
>           month = ((doy - TimeConstants.month_to_day_leap[guess+1]) > 0) ?
> guess+2: guess+1;
>         }
> 
>     } catch  (Exception ioe) {
>             throw new JatcoreException(ioe.getMessage(), ioe);
>     }
>         return month;
>   }
> 
> 
> 
>   /**
>    * Return for a given year doy the coresponding day
>    * @param year for example 2008
>    * @param doy Day of Year 1 to 366
>    * @return day 1 to 31
>    * @throws jatcore.errors.JatcoreException in case of a invalid input
>    */
>   public static int doy2day(int year, int doy)
>       throws JatcoreException
>     {
>       int day =0;
>     try {
>          int guess = (int)(doy*0.0328);
>         int more = -1;
> 
> 
>         if((year%4) != 0 ){
>           day = ((doy - TimeConstants.month_to_day_no_leap[guess+1]) > 0) ? doy -
> TimeConstants.month_to_day_no_leap[guess+1]: doy -
> TimeConstants.month_to_day_no_leap[guess];
>         }
>         else {
>           day = ((doy - TimeConstants.month_to_day_leap[guess+1]) > 0) ? doy -
> TimeConstants.month_to_day_leap[guess+1]: doy -
> TimeConstants.month_to_day_leap[guess];
>         }
> 
>     } catch  (Exception ioe) {
>             throw new JatcoreException(ioe.getMessage(), ioe);
>     }
> 
>       return day;
>     }
> 
> 
> Regards
>  Andreas Rieger
> 
> 
> -- URL: http://www.quinput.net
>