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

[Orekit Developers] Doy support



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.

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