// MPC format report
// This macro reads a result of multiaperture plugin and prints a report in Minor Planet Center (MPC) format 
// to report astrometric (and photometric) data of minor planets, comets or natural satellites to MPC
// See http://www.minorplanetcenter.net/iau/info/OpticalObs.html for more information on MPC format

// Copyright (C) 2014 Ferran Casarramona
// This library is free software; you can redistribute it and/or */
// modify it under the terms of the GNU Lesser General Public */
// License as published by the Free Software Foundation; either */
// version 2.1 of the License, or (at your option) any later version. */

// This library is distributed in the hope that it will be useful, */
// but WITHOUT ANY WARRANTY; without even the implied warranty of */
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
// Lesser General Public License for more details. */

// You should have received a copy of the GNU Lesser General Public */
// License along with this library; if not, write to the Free Software */
// Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301 USA */

function JD_to_MPCDate(jd)
{
	// Fliegel-Van Flandern algorithm
	J = floor(jd);
	F = jd -J;
	if (F >= 0.5)
	{
		J++;
		F -= 0.5;
	}
	else
		F += 0.5;

	p = J + 68569;
   	q = floor(4*p/146097);
   	r = p - floor((146097*q + 3)/4);
   	s = floor(4000*(r+1)/1461001);
   	t = r - floor(1461*s/4) + 31;
   	u = floor(80*t/2447);
   	v = floor(u/11);

   	Y = 100*(q-49)+s+v;
   	M = u + 2 - 12*v;
   	D = t - floor(2447*u/80);

	D += F;

	if (M < 10)
		SM = "0" + M;
	else
		SM = "" + M;

	if (D < 10)
		SD = "0" + d2s(D, 5);
	else
		SD = d2s(D,5);
	
	return "" + Y + " " + SM + " " + SD + " ";	
}

function RA_to_MPCRA(ra)
{
	H = floor(ra);
	S = (ra - H) * 3600;
	M = floor(S / 60);
	S -= M * 60;

	if (H < 10)
		SH = "0" + H;
	else
		SH = "" + H;

	if (M < 10)
		SM = "0" + M;
	else
		SM = "" + M;

	if (S < 10)
		SS = "0" + d2s(S,2);
	else
		SS = "" + d2s(S,2);
	
	return "" + SH + " " + SM + " " + SS + " ";
}

function Dec_to_MPCDec(dec)
{
	if (dec < 0)
	{
		Sign = "-";
		dec = -dec;
	}
	else
	{
		Sign = "+";
	}
	
	D = floor(dec);
	S = (dec - D) * 3600;
	M = floor(S / 60);
	S -= M * 60;

	if (D < 10)
		SD = "0" + D;
	else
		SD = "" + D;

	if (M < 10)
		SM = "0" + M;
	else
		SM = "" + M;

	if (S < 10)
		SS = "0" + d2s(S,1);
	else
		SS = "" + d2s(S,1);
	
	return "" + Sign + SD + " " + SM + " " + SS + " ";
}

Dialog.create("MPC Report");
Dialog.addString("Definitive Designation (5 chars or empty)","");
Dialog.addString("Provisional Designation (7 chars or empty)","");
Dialog.addString("Observatrory (3 chars)", "");
Dialog.show();

Designation = Dialog.getString();
ProvDesign = Dialog.getString();
Observatory = Dialog.getString();
if (lengthOf(Designation) == 0)
	Designation = "     ";
if (lengthOf(ProvDesign) == 0)
	ProvDesign = "       ";
Discovery = " "; // 1 Char
Note1 = " "; // 1 Char
Note2 = "C"; // 1 Char
Magnitude = "     "; // 5 chars F5.2
Filter = " ";  //1 char

print("COD " + Observatory);
print("CON ");
print("OBS ");
print("MEA ");
print("TEL ");
print("NET ");
print("ACK ");
print(nResults);

for (i = 0; i < nResults; i++)
{
  date_time = getResult("JD_UTC", i);
  ra = getResult("RA_T1",i);
  dec = getResult("DEC_T1",i);

  print(Designation + ProvDesign + Discovery + Note1 + Note2 + JD_to_MPCDate(date_time) + RA_to_MPCRA(ra) + Dec_to_MPCDec(dec) + "         " 
		+ Magnitude + Filter + "      " + Observatory);
}