//NewGuide_State.java

import java.util.*;

public class NewGuide_State {
    //----------------------new variables for antenna
    public double theta_angle, theta_maximum, theta_minimum, 
		  phi_angle, phi_maximum, phi_minimum, 
		  point_distance, distance_minimum, distance_maximum, 
		  wavelength, frequency, wire_radius;
    public double Directivity, TotalPower, TimeAveragePower;
    public double DipoleLength_lambda, DipoleLength_meters;
    public Complex Electric_Field, Magnetic_Field;
    public int N_elements;
    //--------------------------------------------------------------------------
    public double a, a_meters; // dimensions
    public double a_minimum, a_maximum, DeltaLength;
    public double mu_r, mu_r0, lambda_0, lambda; 
    public double taper_parameter, dist;
    
    public double epsilon_r, DeltaZ;
    public double frequency_minimum, frequency_maximum, Deltaf;
    public double angular_frequency, beta_total;
    public double beta;
    public double phase_velocity, phase_velocity0, light_velocity, light_velocity0;
    
    public Complex ZeroC;
    public double distance;
    public double medium_impedance;
    public double DVector[], RVector[], RinVector[], XVector[], XinVector[], leng[], DVMax, RVMax, RinVMax, XVMax, XVMin, XinVMax, XinVMin;
    
    public double x[], x_polar[];
    public Complex etheta[];
    public double ethetaM[], ethetaM2[];
    public double Fe_theta[], Fa_theta[], Ftot_theta[];
    public double FeD_theta[], FeP_theta[];
    
    public double EfieldMax, ethetaS, ethetaS2; 
    public double area_front;
    public double zpos[], zposLarge[], zLambda[], theta, phi, zposMax;
    
    public int Numpoints, Numx, Nsections, NsectionsLarge;
    public double dz, angle_factor;
    public boolean inputpanelflag, Is_theta, Is_radius, IsPolar;
    public boolean microFlag2, microFlag3;
    
    public int Nmaxint, ShowWhich;
    
    public boolean IsDirectivity, IsRad, IsRin, IsXrad, IsXin, IsParabolic;
    
    public static final double epsilon0 = 8.841941286E-12; // approximate value
    public static final double mu0 = 4.0*Math.PI*1.0e-7;//1.25663706144E-6; //Units H/m
    public Complex jay = new Complex(0.0,1.0);
    public Complex minusjay = new Complex(0.0,-1.0);
    public Complex One = new Complex(1.0,0.0);
    public Complex minusOne = new Complex(-1.0,0.0);
    
    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 NewGuide_State(){
    
	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);
	
	wire_radius = 1.0e-5; //radius of dipole wire (wavelengths) 
	
	// Tables for Sin and Cos Integrals
	Nmaxint = 1001;
        ShowWhich = 1;
        
        Fe_theta = new double[361];
        FeP_theta = new double[361];
        FeD_theta = new double[361];
        
        
	//new initialization for antenna
	    N_elements = 2;
            taper_parameter = 1.0;
            IsParabolic = false;
            dist = taper_parameter;
	    theta_angle = 135.0;//degrees
	    theta_maximum = 180.0;
	    theta_minimum = 0.0;
	    phi_angle = 45.0;//degrees
	    phi_maximum = 360.0;
	    phi_minimum = 0.0;
	    point_distance = 10.0; //wavelength
	    distance_minimum = 0;//      "
	    distance_maximum = 1000;//   "
	    frequency = 1.0E9;//Hertz
	    
            area_front = distance * distance * 4.0 * Math.PI; // Area of spherical front
            
	    Numpoints = 1001;
            Numx = 1001;
            
	    a = 2.0; // wavelengths of antenna length
	    DipoleLength_lambda = a;//lambda
            
	    a_minimum = 2.0;
	    a_maximum = 12.0;// wavelengths
	    DeltaLength = (a_maximum - a_minimum)/(Numpoints - 1);
	    
	    wavelength = 1.0/Math.sqrt(epsilon0 * mu0)/frequency;//meters
	    
	    DipoleLength_meters = DipoleLength_lambda * wavelength;
	    Directivity = 0.0;
	    TotalPower = 0.0;
	    
	    DVector = new double[Numx];
	    leng = new double[Numx];
	    
	//------------------------------------------------------------------
	
	x_polar = new double[361];
	for(int i=0; i<361; i++){
	    x_polar[i] = i;
	}

	epsilon_r = 1.0;
	mu_r = 1.0;
	
	angle_factor = Math.PI/180.0;
	
	medium_impedance = Math.sqrt(mu0*mu_r/(epsilon0*epsilon_r));
	frequency = 1.0E9;
	frequency_minimum = 0.0;
	frequency_maximum = 5.0E9;
	angular_frequency = 2.0 * Math.PI * frequency;
	
	lambda_0 = 1.0/Math.sqrt(epsilon0*mu0)/frequency;
	lambda = lambda_0;
	ZeroC = new Complex(0.0,0.0);
	light_velocity = 1.0/(Math.sqrt(epsilon0*epsilon_r*mu0*mu_r));
	light_velocity0 = 1.0/(Math.sqrt(epsilon0*mu0));
	
	Nsections = 40;
	NsectionsLarge = 501;
	
	phi = Math.PI/2.0;
	
	inputpanelflag = true;
	Deltaf = (frequency_maximum - frequency_minimum)/(Numpoints - 1);
	
	Is_theta = true;
	Is_radius = false;
	IsPolar = false;
	IsDirectivity = true;
	IsRad = false;
	IsRin = false;
	IsXrad = false;
	IsXin = false;
	
	microFlag2 = false; microFlag3 = false; 
	
	scan_coefficients();
	ignition();
        getParabola();
 }
    
    public void ignition(){
	int Lratio;
	
	wavelength = 1.0/Math.sqrt(epsilon0 * mu0 * epsilon_r * mu_r)/frequency;//meters
	DipoleLength_lambda = a;//lambda
	DipoleLength_meters = DipoleLength_lambda * wavelength;
	Lratio = (int)(100*DipoleLength_meters/wavelength)+1;
	if(Lratio < 51){Lratio = 51;}
	Nsections = Lratio;
	//NsectionsLarge = Lratio*10+1;
	NsectionsLarge = 501;
	DeltaZ = DipoleLength_meters / Nsections;
	DeltaLength = (a_maximum - a_minimum)/(Numpoints - 1);
	dist = taper_parameter;
        
	distance = point_distance*wavelength;//meters
	area_front = distance * distance * 4.0 * Math.PI;
	
        etheta = new Complex[361];
	ethetaM = new double[361];
	ethetaM2 = new double[361];
        Fa_theta = new double[361];
        Ftot_theta = new double[361];
                
        zpos = new double[Nsections];
	dz = DipoleLength_meters / Nsections;
	
	angular_frequency = 2.0 * Math.PI * frequency;
	light_velocity = 1.0/(Math.sqrt(epsilon0*epsilon_r*mu0*mu_r));
	light_velocity0 = 1.0/(Math.sqrt(epsilon0*mu0));
	lambda_0 = light_velocity0/frequency;
	lambda = light_velocity/frequency;
	
	beta = 2.0*Math.PI/lambda;
	medium_impedance = Math.sqrt(mu0*mu_r/(epsilon0*epsilon_r));
		
	// Obtain Directivity, Radiation Resistance and Time-Average Power  (Far Field assumption)
	double Func[], Func2[], Func2Max, angle, betafactor, FuncInt, Delta,theta_angle_radians;
	int imax, Nmax;
	Nmax = 1800;
	Func   = new double[Nmax+1];
	Func2   = new double[Nmax+1];
	Func2Max = 0.0;
	imax = 0;
	Delta = 0.5*Math.PI/Nmax;
	betafactor = beta*DipoleLength_meters*0.5;
	theta_angle_radians = theta_angle*Math.PI/180.0;
	Func[0] = 0.0;
	Func2[0] = 0.0;
        /*
        // scan theta ----------------------------------------------------------
        double sum_thetaS;
        double factorAS;
        double xmaxS, DeltaxS;
        
	xmaxS = DipoleLength_meters * 0.5;
	DeltaxS = DipoleLength_meters / Nsections;
		
        factorAS = 0.0;
        DeltaxS = DipoleLength_meters / Nsections;
        sum_thetaS = 0.0;
            for(int j=0;j<Nsections+1;j++){
                
                factorAS = (1.0-DeltaxS*j/xmaxS*taper_parameter)
                          *MaestroA.J0(DeltaxS*j*beta * Math.sin(theta_angle_radians-(Math.PI*0.5)));    
                sum_thetaS += factorAS;
            }
            
            sum_thetaS = sum_thetaS/xmaxS;
            ethetaS = Math.abs(sum_thetaS);
            ethetaS2 = Math.pow(ethetaS,2.0);
            
        */
        //----------------------------------------------------------------------
	    
	//scanField();
        //scan_coefficients();
}
public void getParabola(){
    //----------------------------------------------------------------------
	double xstart, xmax, Deltax, sum_theta;
        double theta_deg, theta_rad, factorA, rad90;
        
        rad90 = 90.0*angle_factor;
	xstart = 0.0;
        xmax = DipoleLength_meters * 0.5;
	Deltax = xmax / Nsections;
	theta = theta_angle*angle_factor; 
	
        factorA = 0.0;
	for(int i=0;i<181;i++){
                theta_deg = (double)i;
                theta_rad = theta_deg * angle_factor;
                
                sum_theta = 0.0;
                for(int j=0;j<Nsections+1;j++){
                    factorA = Deltax*j*MaestroA.J0(Deltax*j*beta * Math.sin(theta_rad+rad90));    
                    sum_theta += factorA;
                }
            
                sum_theta = sum_theta/xmax;
                //factorA = MaestroA.J1(2.0 * Math.PI * Math.sin(theta_rad+rad90))/
                  //        (Math.PI*Math.sin(theta_rad+rad90));
                   
                //FeP_theta[i] = Math.pow(factorA,2.0);
                FeP_theta[i] = Math.pow(sum_theta,2.0);
                
                
            }
        
            for(int i=0;i<360;i++){
                theta_deg = (double)i;
                theta_rad = theta_deg * angle_factor;
            
                FeD_theta[i] = Math.pow(Math.sin(theta_rad),2.0);
            } 
}
    
    
public void scanField(){
    double xstart, xmax, Deltax, sum_theta;
    double dtheta, theta_deg, theta_rad, factorA, rad90;
        
        rad90 = 90.0*angle_factor;
	xstart = 0.0;
        xmax = DipoleLength_meters * 0.5;
	Deltax = DipoleLength_meters / Nsections;
	theta = theta_angle*angle_factor; 
	dtheta = (theta_angle*1.01)*angle_factor;
        
        factorA = 0.0;
	if(IsParabolic){// Parabolic antenna
            
            for(int i=1;i<180;i++){
                theta_deg = (double)i;
                theta_rad = theta_deg * angle_factor;
                
                factorA = Math.sin(N_elements*Math.PI*dist*Math.cos(theta_rad))/
                          Math.sin(Math.PI*dist*Math.cos(theta_rad));
                Fa_theta[i] = Math.pow(factorA,2.0);
                
            }
             
            for(int i=181;i<360;i++){
                theta_deg = (double)i;
                theta_rad = theta_deg * angle_factor;
                
                factorA = Math.sin(N_elements*Math.PI*dist*Math.cos(theta_rad))/
                          Math.sin(Math.PI*dist*Math.cos(theta_rad));
                Fa_theta[i] = Math.pow(factorA,2.0);
            }
            
            Fa_theta[0]=Fa_theta[1];
            Fa_theta[360]=Fa_theta[1];
            Fa_theta[180]=Fa_theta[179];
            
            for(int i=0;i<360;i++){
                ethetaM[i] = Math.sqrt(FeP_theta[i] * Fa_theta[i]);
                ethetaM2[i] = FeP_theta[i] * Fa_theta[i];
            }
        }
        else{// lambda/2 antenna
             for(int i=1;i<181;i++){
                theta_deg = (double)i;
                theta_rad = theta_deg * angle_factor;
            
                Fe_theta[i] = Math.pow(Math.sin(theta_rad),2.0);
                
                factorA = Math.sin(N_elements*dist*Math.PI*Math.cos(theta_rad))/
                          Math.sin(dist*Math.PI*Math.cos(theta_rad));
                Fa_theta[i] = Math.pow(factorA,2.0);
                ethetaM[i] = Math.sqrt(Fe_theta[i] * Fa_theta[i]);
                ethetaM2[i] = Fe_theta[i] * Fa_theta[i];
            } 
             
             Fa_theta[0]=Fa_theta[1];
             Fa_theta[360]=Fa_theta[1];
             
            for(int i=181;i<360;i++){
                theta_deg = (double)i;
                theta_rad = theta_deg * angle_factor;
            
                Fe_theta[i] = Math.pow(Math.sin(theta_rad),2.0);
                
                factorA = Math.sin(N_elements*dist*Math.PI*Math.cos(theta_rad))/
                          Math.sin(dist*Math.PI*Math.cos(theta_rad));
                Fa_theta[i] = Math.pow(factorA,2.0);
                ethetaM[i] = Math.sqrt(Fe_theta[i] * Fa_theta[i]);
                ethetaM2[i] = Fe_theta[i] * Fa_theta[i];
            } 
        }
        
        
        /*
        for(int i=0;i<91;i++){
            theta_deg = (double)i;
            theta_rad = theta_deg * angle_factor;
            
            // simple integration just for testing
            sum_theta = 0.0;
            for(int j=0;j<Nsections+1;j++){
                
                factorA = (1.0-Deltax*j/xmax)
                          *MaestroA.J0(Deltax*j*beta * Math.sin(theta_rad));    
                sum_theta += factorA;
                
            }
            
            sum_theta = sum_theta/xmax;
            ethetaM[i+90] = Math.abs(sum_theta);
            ethetaM[90-i] = Math.abs(sum_theta);
            ethetaM2[i+90] = Math.abs(sum_theta*sum_theta);
            ethetaM2[90-i] = Math.abs(sum_theta*sum_theta);
            //{System.out.println(i+"   "+ethetaM[i+90]+"   sum = "+sum_theta);}
        }
          */ 
        
        
}

//---------------------------


    public void scan3Dfield(){
        
    }
    public void scan_reactance(){
	
    }


    //Gets array for plot of Field as function of frequency    
    public void scan_coefficients(){
	
	DVMax = 0.0;
	for(int iL=0;iL<Numx;iL++){
            leng[iL] = iL;
            DVector[iL] = 1.0 - taper_parameter*iL/(Numx-1);	
	}
    }
}
