# include "addstar.h"

uint64_7 PSPS_create_detectID (double MJDobs, int imageID) {

  // MJD has a PS lifetime range of : 3288 (2009/01/01) to 9132 (2025/01/01) (< 2^14)

  static double t0 = 51544.5; // MJD @ 2000/01/01,12:00:00

  uint64_t iTime;
  uint64_t detectID;

  iTime = floor(tobs - t0);

  // iTime takes the upper 16 bits, imageID can take up to lower 48 bits
  detectID = (iTime << 48) | imageID;

  return detectID;
}

uint64_t PSPS_create_objID (double ra, double dec) {

  static double zh = 0.0083333; // zone height = 30 arcsec

  uint64_t objID;
  uint64_t part1, part2, part3;

  double zid, zresid;
  int izone;

  zid = (dec + 90.0) / zh; // 0 - 180*60*2 = 21600 (< 15 bits)
  izone = (int) floor(zid);
  zresid = zid -  ((float) izone); // 0 - 1.0 
  part1 = (uint64_t)( izone  * 10000000000000) ;
  part2 = ((uint64_t)(ra * 1 000 000.)) * 10000 ; // 0 - 360.0*1e6 = 3.6e8 (< 29 bits)
  part3 = (int) (zresid * 10000.0) ; // 0 - 10000 (1 bit == 30/10000 arcsec = 0.003 arcsec) (< 14 bits)

  objID = part1 + part2 + part3;

  return objID;
}

uint64_t PSPS_create_objID_eam (double ra, double dec) {

  static double zh = 0.0083333; // zone height = 30 arcsec

  uint64_t objID;
  uint64_t part1, part2, part3;

  double zid, zresid;
  int izone, ira;

  zid = (dec + 90.0) / zh; // 0 - 180*60*2 = 21600 (< 15 bits)
  izone = (int) floor(zid);
  zresid = zid -  ((float) izone); // 0 - 1.0 

  ira = ra * 1 000 000; // 0 - 360.0*1e6 = 3.6e8 (< 29 bits) 

  part1 = (izone << 48);
  part2 = (ira << 16);
  part3 = (int) (zresid * 10000.0) ; // 0 - 10000 (1 bit == 30/10000 arcsec = 0.003 arcsec) (< 14 bits)

  objID = part1 | part2 | part3;

  return objID;
}

// part1 : 15 bits
// part2 : 29 bits
// part3 : 14 bits
// 15 + 14 + 29 bits = 58 bits...

