//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, BeamWidth, TotalPower, TimeAveragePower;
    public double DipoleLength_lambda, DipoleLength_meters;
    public Complex Electric_Field, Magnetic_Field;
    //--------------------------------------------------------------------------
    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, Maximus;
    
    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[], ethetaM_dB[];
    public double EfieldMax, ethetaS, ethetaS2, ethetaS_dB; 
    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, Is_dB;
    public boolean microFlag2, microFlag3;
    
    public int Nmaxint, ShowWhich;
    
    public boolean IsDirectivity, IsRad, IsRin, IsXrad, IsXin;
    
    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);
	today_week = Greg.get(Calendar.DAY_OF_WEEK);
	this_year = Greg.get(Calendar.YEAR);
	this_hour = Greg.get(Calendar.HOUR_OF_DAY);
	this_minute = Greg.get(Calendar.MINUTE);
	today_month = Greg.get(Calendar.DAY_OF_MONTH);
	today_year = Greg.get(Calendar.DAY_OF_YEAR);
	this_zone = Greg.get(Calendar.ZONE_OFFSET);
	saving_time = Greg.get(Calendar.DST_OFFSET);
	
	wire_radius = 1.0e-5; //radius of dipole wire (wavelengths) 
	
	// Tables for Sin and Cos Integrals
	Nmaxint = 1001;
        ShowWhich = 1;
        
	//new initialization for antenna
	    
            taper_parameter = 0.5;
            
	    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;
	Is_dB = true;
	microFlag2 = false; microFlag3 = false; 
        double pr = 0;
        
	scan_coefficients();
	ignition();
        
        
 }
    
    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);
	
	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];
        ethetaM_dB = 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;
        
        //scanField();
        //double Maxim = MaestroA.getMax(ethetaM);
        // scan theta ----------------------------------------------------------
        double sum_thetaS;
        double factorAS;
        double xmaxS, DeltaxS, Maxx;
        xmaxS = DipoleLength_meters * 0.5;
	DeltaxS = xmaxS / Nsections;
	Maxx = 0.0;
        factorAS = 0.0;
        for(int j=0;j<Nsections+1;j++){

            factorAS = (1.0-DeltaxS*j/xmaxS*taper_parameter)*DeltaxS*j
                      *MaestroA.J0(DeltaxS*j*beta * Math.sin(0.0));    
            Maxx += factorAS;
        }
        Maxx = Maxx/xmaxS;
        factorAS = 0.0;
        sum_thetaS = 0.0;
        for(int j=0;j<Nsections+1;j++){    
            factorAS = (1.0-DeltaxS*j/xmaxS*taper_parameter)*DeltaxS*j
                      *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)/Maxx;
        ethetaS2 = Math.pow(ethetaS,2.0);
        ethetaS_dB = 10.0*Math.log10(ethetaS2);
        //----------------------------------------------------------------------
	//scanField();
        //scan_coefficients();
}

public void scanField(){
    double xstart, xmax, Deltax, sum_theta, sum_dir;
    double theta_deg, theta_rad, factorA;
    int bw;
	xstart = 0.0;
        bw = 0;
        xmax = DipoleLength_meters * 0.5;
        Deltax = xmax / Nsections;
	theta = theta_angle*angle_factor; 
	factorA = 0.0;
	Maximus = 0.0;
        for(int i=0;i<91;i++){
            theta_deg = (double)i;
            theta_rad = theta_deg * angle_factor;
            // simple integration 
            sum_theta = 0.0;
            double factor = 0.0;
            for(int j=0;j<Nsections+1;j++){
                factor = Deltax*j*beta * Math.sin(theta_rad);
                factorA = (1.0-Deltax*j/xmax*taper_parameter)*Deltax*j
                         *MaestroA.J0(factor); 
                sum_theta += factorA;
            }
            
            sum_theta = (Math.abs(sum_theta/xmax));
            ethetaM[i+90] = Math.abs(sum_theta);
            ethetaM[90-i] = Math.abs(sum_theta);
        }
        Maximus = MaestroA.getMax(ethetaM);
        
        double temp = 0.0;
        bw = 0;
        for(int i=0;i<181;i++){
            temp = ethetaM[i]/Maximus;
            ethetaM[i] = (temp);
            ethetaM2[i] = temp*temp;
            ethetaM_dB[i] = 10.0*Math.log10(temp*temp);
            if(ethetaM_dB[i] < -100.0){ethetaM_dB[i] = -100.0;}
        }
        
        // Calculate Directivity
        sum_dir = 0.0;
        for(int j=0;j<181+1;j++){
            //sum_dir += ethetaM2[j]*Math.sin(j*Math.PI/180.0f)*Math.PI/180.0f;// * Math.sin((double)j*angle_factor));  
            sum_dir += ethetaM2[j];//*Math.PI/180.0f;// * Math.sin((double)j*angle_factor));  
        }
        Directivity = 4*Math.PI/(Math.abs(sum_dir*angle_factor)*Math.abs(sum_dir*angle_factor));
        //Directivity = 2.0/Math.abs(sum_theta*angle_factor);
        
        // Calculate numerically beamwidth
        bw = 0;
        for(int i=90;i<181;i++){
            if(ethetaM_dB[i] < -3.0){bw = i-90; break;}
        }
        
        // interpolate for beamwidth
        double DeltaE, DeltaBW, extra;
        DeltaE = Math.abs(ethetaM2[bw+90-1] - ethetaM2[bw+90]);
        DeltaBW = Math.abs(0.5 - ethetaM2[bw+90-1]);
        extra = DeltaBW/DeltaE;
        BeamWidth = 2.0*((double)(bw-1) + extra);
        
}        
            
        

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


    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);	
	}
    }
}
