//Polarization_State.java
import java.util.*;

public class Polarization_State {
    public double ampA, ampB, phaseA, phaseB; //Old State
    public double ampAA, ampBB, phaseAA, phaseBB; //New State
    public double wt, dwt;
    public boolean isTracerOn;
    public int NSteps;
    public boolean RESET;
    public int SleepTime  = 35;
    public boolean LicenseExpired;
    public int this_month, today_week, this_year, this_hour, this_minute, today_month, 
	       today_year,this_zone, saving_time;       
    GregorianCalendar Greg = new GregorianCalendar();

    
    public Polarization_State(){
	NSteps = 180;
	ampA = 1.0;
	ampB = 1.0;
	phaseA = 0.0;
	phaseB = 0.0;
	push();
	
	wt = 0.0;
	dwt = Math.PI*2/(NSteps);
	isTracerOn = false;
	RESET = false;
	
	this_month = Greg.get(Calendar.MONTH);
	//System.out.println("  This is the month = "+this_month);
	today_week = Greg.get(Calendar.DAY_OF_WEEK);
	//System.out.println("  This is the day_week = "+today_week);
	this_year = Greg.get(Calendar.YEAR);
	this_hour = Greg.get(Calendar.HOUR_OF_DAY);
	//System.out.println("  This is the year = "+this_year);
	this_minute = Greg.get(Calendar.MINUTE);
	//System.out.println("  This is the minute = "+this_minute);
	today_month = Greg.get(Calendar.DAY_OF_MONTH);
	//System.out.println("  This is the day_month = "+today_month);
	today_year = Greg.get(Calendar.DAY_OF_YEAR);
	//System.out.println("  This is the day_year = "+today_year);
	this_zone = Greg.get(Calendar.ZONE_OFFSET);
	//System.out.println("  This is the zone_offset = "+this_zone/3600000);
	saving_time = Greg.get(Calendar.DST_OFFSET);
	//System.out.println("  This is the dst_offset = "+saving_time/3600000);
	
    }
    
    public double getMagnitude(int tmp){
	double dtmp = 0.0;
	if(tmp == 1){
	    dtmp = Math.sqrt(Math.pow(ampA*Math.cos(phaseA+wt),2.0)+
			     Math.pow(ampB*Math.cos(phaseB+wt),2.0));
	}
	else if(tmp == 2){
	    dtmp = Math.sqrt(Math.pow(ampAA*Math.cos(phaseAA+wt),2.0)+
			     Math.pow(ampBB*Math.cos(phaseBB+wt),2.0));
	}
	else {
	    dtmp = 0.0;
	}
	return dtmp;
    }
    
    public synchronized void increment(){
	wt+=dwt;
	if(wt > Math.PI * 2){
		wt -= 2 * Math.PI;
	}
    }
    
    public synchronized void push(){
	ampAA = ampA;
	ampBB = ampB;
	phaseAA = phaseA;
	phaseBB = phaseB;
    }
    
 
    
    public String getInfo(){
		String tmp;
		double difphase, difamp;
		double tol=1.0E-5;
		difphase=phaseB-phaseA;
		difamp=ampA-ampB;
		
		// Linear Polarization
		  if (difphase == 0.0 || Math.abs(difphase) == Math.PI 
			|| Math.abs(difphase) == 2.0*Math.PI 
			|| ampA ==0.0 || ampB ==0 ){
		    tmp = "The polarization is LINEAR";
		}
		
		// Circular Polarization
		else if (Math.abs(difamp) < tol && 
			 (Math.abs(Math.abs(difphase)-Math.PI/2.0)< tol
			  || Math.abs(Math.abs(difphase)-1.5*Math.PI) < tol))			
		{
		   if(difphase == -Math.PI/2.0 || difphase == 1.5*Math.PI){
                      tmp = "The polarization is CIRCULAR ( right-handed )";
		   }
		   else{
	              tmp = "The polarization is CIRCULAR ( left-handed )";
		   }
		}
		else{
		   if((difphase<0.0 && difphase > -Math.PI) 
			|| (difphase < 2.0*Math.PI && difphase > Math.PI))
		   {
                      tmp = "The polarization is ELLIPTICAL ( right-handed )";
		   }
		   else{
	              tmp = "The polarization is ELLIPTICAL ( left-handed )";
		   }
		   
		}
	    return tmp;
	}
	
	public synchronized void reset(){
	    RESET = true;
	    wt = 0.0;
	}
	
	public synchronized void preset(){
	    RESET = false;
	}
}