/*-----------------------------------------------------------*/

function getTipDatesFromNames2 (treeNameID)
{
	retAVL = {};
	
	ExecuteCommands ("treeAVL="+treeNameID+"^0;");
	maxV            = 0; /* scaling factor */
	minV            = 1e100;
	ttipCount = 0;
	for (tc = 1; tc < Abs(treeAVL); tc += 1)
	{
		nodeInfo = treeAVL[tc];
		if (Abs(nodeInfo["Children"])==0)
		{	
			rgm = nodeInfo["Length"];
			if (rgm<=0)
			{
				fprintf (stdout, "\nERROR: No date information found for sequence ", nodeInfo["Names"], "\n");
				return {};
			}
			maxV = Max (labelV,maxV);
			minV = Min (labelV,minV);
			ttipCount += 1;
			retAVL[nodeInfo["Name"]] = rgm;
		}
	}
	
	if (maxV>1)
	{
		maxV = 10^(-((Log(maxV)/Log(10)+0.5)$1));
		for (tc = 0; tc < ttipCount; tc+=1)
		{
			abn = Eval ("TipName("+treeNameID+",tc)");
			retAVL[abn] = retAVL[abn]*maxV;
		}		
		minV = minV * maxV;
	}
	return retAVL;
}

/*-----------------------------------------------------------*/

function getTipDatesFromNames1 (treeNameID)
{
	retAVL = {};
	ExecuteCommands ("ttipCount=TipCount("+treeNameID+");");
	regExpToMatch = "[0-9]*\\.?[0-9]+$";
	maxV          = 0;
	minV          = 1e100;
    
	for (tc = 0; tc < ttipCount; tc += 1)
	{
		abn = Eval ("TipName("+treeNameID+",tc)");
		rgm = abn$regExpToMatch;
		if (rgm[0]<0)
		{
			fprintf (stdout, "\nERROR: No date information found for sequence ", nodeInfo["Names"], "\n");
			return {};
		}
		labelV = 0+abn[rgm[0]][rgm[1]];
		if (labelV < 0)
		{
			fprintf (stdout, "\nERROR: Negative length for sequence ", nodeInfo["Names"], "\n");
			return {};
		}
		maxV = Max (labelV,maxV);
		minV = Min (labelV,minV);
		
		retAVL[abn] = labelV;
	}
	if (maxV>1)
	{
		maxV = 10^(-((Log(maxV)/Log(10)+0.5)$1));
		for (tc = 0; tc < ttipCount; tc+=1)
		{
			abn = Eval ("TipName("+treeNameID+",tc)");
			retAVL[abn] = retAVL[abn]*maxV;
		}		
		minV = minV * maxV;
	}
	return retAVL;
}
