示例#1
0
int main(void)
{
	int i,nb=NBMAX;
	float tol,root,*xb1,*xb2;

	xb1=vector(1,NBMAX);
	xb2=vector(1,NBMAX);
	zbrak(fx,X1,X2,N,xb1,xb2,&nb);
	printf("\nRoots of bessj0:\n");
	printf("%21s %15s\n","x","f(x)");
	for (i=1;i<=nb;i++) {
		tol=(1.0e-6)*(xb1[i]+xb2[i])/2.0;
		root=zbrent(fx,xb1[i],xb2[i],tol);
		printf("root %3d %14.6f %14.6f\n",i,root,fx(root));
	}
	free_vector(xb2,1,NBMAX);
	free_vector(xb1,1,NBMAX);
	return 0;
}
示例#2
0
/* If modeling magnitude bin samples, then there will be a high mass
 * scale for central galaxies as well as a low mass scale. This finds
 * the mass at which N_cen(m)=0.001, where m>M_min.
 */
double set_high_central_mass()
{
  double m,n;

  if(HOD.pdfc==7)
    return(HOD.M_cen_max);
  if(!(HOD.pdfc==6 || HOD.pdfc==8 || HOD.pdfc==9))
    return(HOD.M_max);

  m = HOD.M_min;
  n = N_cen(m);

  while(n>0.001)
    {
      m*=2;
      n = N_cen(m);
      if(m>HOD.M_max)return(HOD.M_max);
    }
  m = exp(zbrent(func_mhi,log(m/2),log(m),1.0E-5));
  return(m);
}
示例#3
0
文件: fdt.c 项目: shidai/ptimeD
int get_toa (double *s, double *p, double *phasex, double *errphasex, double psrfreq, int nphase, double *rms, double *bx)
// calculate the phase shift between template and simulated (or real) data 
// error of phase can be calculated
// initial guess of phase shift is added
// try to do two-dim template matching
{
    //int nphase=1024;
    int nchn=1;

	// read a std
	
	//puts(argv[1]);
	//puts(argv[2]);
	//double t[nphase*nchn],s[nphase*nchn];
	//int n;

	//readfile(name,&n,t,s);
	//printf ("%d\n", n);
	//puts(argv[1]);

	//////////////////////////////////////////////////////////////////////////
	// simulate data

	//double p[nphase*nchn];
	//double SNR=atof(argv[2]);
	//simulate(n,SNR,s,p);//*/
	
	/////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
	// dft profile and template
	
	//nchn = n/nphase;
	//printf ("%d\n", nchn);
	int k;  // k=nphase/2

	//double amp_s[nchn][nphase/2],amp_p[nchn][nphase/2];  // elements for calculating A7
	//double phi_s[nchn][nphase/2],phi_p[nchn][nphase/2];
	params param;
	allocateMemoryPtime (&param, nchn, nphase);
	//double amp_s[nchn][NP],amp_p[nchn][NP];  // elements for calculating A7
	//double phi_s[nchn][NP],phi_p[nchn][NP];  // the second dim should be NP, which is large enough for different observations

	preA7(s, p, nphase, nchn, &param);
	//preA7(&k, amp_s, amp_p, phi_s, phi_p, s, p, nphase, nchn);
	//printf ("%d\n", nchn);
	
	// initial guess of the phase
	int peak_s, peak_p;	

	find_peak(0, nphase,s,&peak_s);
	find_peak(0, nphase,p,&peak_p);

	int d, chn;
	double step;
	double ini_phase,up_phase,low_phase;

	d = InitialGuess (s, p, nphase, 1, &chn);
	//d=peak_p-peak_s;
	//printf ("Initial guess: %d\n",d);
	step=2.0*M_PI/(10.0*nphase);

	if (d>=nphase/2)
	{
		if (d>0)
		{
			ini_phase=2.0*M_PI*(nphase-1-d)/nphase;
		}
		else
		{
			ini_phase=-2.0*M_PI*(nphase-1+d)/nphase;
		}
		up_phase=ini_phase+step;
		low_phase=ini_phase-step;
		while (A7(up_phase, param)*A7(low_phase, param)>0.0)
		{
		    up_phase+=step;
		    low_phase-=step;
		}
	}
	else
	{
		ini_phase=-2.0*M_PI*d/nphase;
		up_phase=ini_phase+step;
		low_phase=ini_phase-step;
		while (A7(up_phase, param)*A7(low_phase, param)>0.0)
		{
		    up_phase+=step;
		    low_phase-=step;
		}
	}

  // calculate phase shift, a and b
  double phase,b;
  phase=zbrent(A7, low_phase, up_phase, 1.0e-16, param);
  //phase=zbrent(A7, -1.0, 1.0, 1.0e-16);
  //phase=zbrent(A7, -0.005, 0.005, 1.0e-16);
  b=A9(phase, param);
  //a=A4(b);
	(*bx) = b;

		
	//printf ("Phase shift: %.10lf\n", phase/(2.0*M_PI));
	//printf ("%.10lf %.10lf\n", phase, A7(phase));
	//printf ("%.10lf \n", ((phase/3.1415926)*5.75/2.0)*1.0e+3);  // microseconds
	//printf ("%.10lf \n", b);
	//printf ("%.10lf \n", a);
	//printf ("///////////////////////// \n");
		
	
	// calculate the errors of phase and b
    double errphase, errb;	

	error(phase,b,&errphase,&errb, param);
	//printf ("%.10lf %.10lf\n", ((phase/3.1415926)/(psrfreq*2.0))*1.0e+6, ((errphase/3.1415926)/(psrfreq*2.0))*1.0e+6);  // microseconds
	//printf ("%.10lf %.10lf\n", ((phase/3.1415926)*4.569651/2.0)*1.0e+3, ((errphase/3.1415926)*4.569651/2.0)*1.0e+3);  // microseconds
	//printf ("errphase %.10lf \n", ((errphase/3.1415926)*5.75/2.0)*1.0e+6);
	//printf ("errb %.10lf \n", errb);
	
	(*phasex) = phase;
	(*errphasex) = errphase;

	// calculate the rms
	cal_rms(phase,b,rms, param);

	deallocateMemoryPtime (&param, nchn);
	return 0;
}
示例#4
0
double chi2_wp_color(double *a)
{
  double chi2,nsat_blue,ncen_blue,dlogm,m,func_blue_fraction(),temp1,temp2,fsat_blue,fsat_red,fsat_all;
  int i,j;
  static int iter=0;

  COVAR = 0;

  HOD.color = 0;
  GALAXY_DENSITY2 = GALAXY_DENSITY = wp_color.ngal_full;
  wp.np = wp_color.n_full;

  for(i=wp.ncf-2;i<=wp.ncf;++i)
    if(a[i]<0)return(1.0e+7);

  for(i=1;i<=wp.np;++i)
    {
      wp.r[i] = wp_color.r_full[i];
      wp.x[i] = wp_color.x_full[i];
      wp.e[i] = wp_color.e_full[i];
      for(j=1;j<=wp.np;++j)
	  wp.covar[i][j] = wp_color.covar_full[i][j];
    }

  chi2 = chi2_wp(a);

  HOD.M_low0 = set_low_mass();

  fsat_all = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;

  /*
  dlogm=(log(HOD.M_max)-log(HOD.M_low))/99;
  for(i=1;i<=100;++i)
    {
      m=exp((i-1)*dlogm)*HOD.M_low;
      printf("HODFULL %e %f %f %f\n",m,N_cen(m)+N_sat(m),N_cen(m),N_sat(m));
    }
  printf("GALDEN FULL %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt));
  sprintf(Task.root_filename,"gal_full");
  populate_simulation();
  */


  if(!iter)
    for(i=1;i<=wp.np;++i)
      for(j=1;j<=wp.np;++j)
	wp_color.covar_full[i][j] = wp.covar[i][j];

  HOD.fblue0_sat = a[wp.ncf - 2];
  HOD.sigma_fblue_sat = a[wp.ncf - 1];
  HOD.sigma_fblue_cen = a[wp.ncf - 0];
  HOD.fblue0_cen = 1.0;
  
  HOD.color = 1;
  HOD.fblue0_cen = pow(10.0,zbrent(func_blue_fraction,-5.0,1.0,1.0E-5));

  if(OUTPUT)
    fprintf(stdout,"old fblue0_cen= %e %e\n",HOD.fblue0_cen,HOD.M_min);

  if(DENSITY_DEPENDENCE) 
    {
      HOD.color = 2;
      temp1 = HOD.M_min;
      temp2 = HOD.M_low;
      populate_simulation();
      HOD.M_min = temp1;
      HOD.M_low = temp2;
      HOD.fblue0_cen = pow(10.0,zbrent(dd_func_red_fraction,-5.0,1.0,1.0E-5));
      HOD.color = 1;
      if(OUTPUT)
	fprintf(stdout,"new fblue0_cen= %e %e\n",HOD.fblue0_cen,
		(1. - HOD.M_min_fac*(1.0 - HOD.fblue0_cen)));
      //      populate_simulation();
      //exit(0);
    }
  else
    HOD.fblue0_cen = pow(10.0,zbrent(func_blue_fraction,-5.0,1.0,1.0E-5));

  if(ERROR_FLAG)
    {
      ERROR_FLAG=0;
      return(1.0e7);
    }


  /* This is only if you have square functions for the central occupation
   */
  /*
  nsat_blue = qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt);
  ncen_blue = qromo(func_central_density,log(HOD.M_low),log(HOD.M_cen_max),midpnt);
  HOD.fblue0_cen = (wp_color.ngal_blue - nsat_blue)/ncen_blue;
  */

  GALAXY_DENSITY2 = GALAXY_DENSITY = wp_color.ngal_blue;
  wp.np = wp_color.n_blue;

  for(i=1;i<=wp.np;++i)
    {
      wp.r[i] = wp_color.r_blue[i];
      wp.x[i] = wp_color.x_blue[i];
      wp.e[i] = wp_color.e_blue[i];
      for(j=1;j<=wp.np;++j)
	  wp.covar[i][j] = wp_color.covar_blue[i][j];
    }

  chi2 += chi2_wp(a);
  fsat_blue = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;

  /*
  dlogm=(log(HOD.M_max)-log(HOD.M_low))/99;
  for(i=1;i<=100;++i)
    {
      m=exp((i-1)*dlogm)*HOD.M_low;
      printf("HODBLUE %e %f %f %f\n",m,N_cen(m)+N_sat(m),N_cen(m),N_sat(m));
    }
  printf("GALDEN BLUE %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt));

  sprintf(Task.root_filename,"gal_blue");
  populate_simulation();
  */

  if(!iter)
    for(i=1;i<=wp.np;++i)
      for(j=1;j<=wp.np;++j)
	wp_color.covar_blue[i][j] = wp.covar[i][j];

  HOD.color = 2;
  GALAXY_DENSITY2 = GALAXY_DENSITY = wp_color.ngal_red;
  wp.np = wp_color.n_red;

  for(i=1;i<=wp.np;++i)
    {
      wp.r[i] = wp_color.r_red[i];
      wp.x[i] = wp_color.x_red[i];
      wp.e[i] = wp_color.e_red[i];
      for(j=1;j<=wp.np;++j)
	  wp.covar[i][j] = wp_color.covar_red[i][j];
    }

  chi2 += chi2_wp(a);
  fsat_red = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;

  /*
  dlogm=(log(HOD.M_max)-log(HOD.M_low))/99;
  for(i=1;i<=100;++i)
    {
      m=exp((i-1)*dlogm)*HOD.M_low;
      printf("HODRED %e %f %f %f\n",m,N_cen(m)+N_sat(m),N_cen(m),N_sat(m));
    }
  printf("GALDEN RED %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt));

  sprintf(Task.root_filename,"gal_red");
  populate_simulation();

  exit(0);
  */
  if(!iter)
    for(i=1;i<=wp.np;++i)
      for(j=1;j<=wp.np;++j)
	wp_color.covar_red[i][j] = wp.covar[i][j];
  
  iter++;

  wp.fsat_all = fsat_all;
  wp.fsat_red = fsat_red;
  wp.fsat_blue = fsat_blue;

  printf("COLOR_PARAMS %d %e %e %e %e %e %e %e %e\n",iter,chi2,HOD.M_min,HOD.M1,HOD.alpha,HOD.fblue0_cen,HOD.fblue0_sat,HOD.sigma_fblue_cen,HOD.sigma_fblue_sat);
  printf("COLOR_ITER %d %e %e %f %f %f\n",iter,chi2,HOD.fblue0_cen,fsat_all,fsat_blue,fsat_red);
  fflush(stdout);
  return(chi2);

}
示例#5
0
文件: scattering.cpp 项目: XSZHOU/SBE
int IntegrandCC2d_diag(unsigned ndim, const double *x,  void *fdata, unsigned fdim, double *fval)
{  // for diagonal part, we eliminate one integration over theta using delta(E)
	
	const std::complex<double> I(0,1);
    std::complex<double> K4, QD;
    double k4,qD,Ek1,Ek4;
    
	CInfPass_SBECC *cubpass=static_cast<CInfPass_SBECC*>(fdata);

    double K1  =cubpass->arr[0];
	int flag =(int)cubpass->arr[7];
	
    cubpass->arr[8]=x[0];//qD
    cubpass->arr[9]=x[1];//theta
    
    qD= x[0];
    QD= qD*exp(I*x[1]);
    K4 = K1 - QD;
    k4= std::abs(K4);
    
    if(flag==1||flag==3)
    {   // Ek1,Ek4->c
    Ek1= cubpass->gPPEc.interp(K1);
    Ek4= cubpass->gPPEc.interp(k4);
    }
    else
    {   // Ek1,Ek4->v
    Ek1= cubpass->gPPEv.interp(K1);
    Ek4= cubpass->gPPEv.interp(k4);	
    }
    
    double k2min=0.0; double k2max=3e9;
    double res, abserr; size_t neval;
	    
    gsl_integration_cquad_workspace *ws = NULL ;
    if ( ( ws = gsl_integration_cquad_workspace_alloc( 200 ) ) == NULL ) 
	{
      printf( "call to gsl_integration_cquad_workspace_alloc failed.\n" );
      abort();
    }
    
	gsl_function F;  
    F.params =   cubpass;
    
    switch (flag) 
    {
     case 1:
       F.function = &Integrand1d_cquad1;
       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
       fval[0]=res;
     break;
     
     case 2:
       F.function = &Integrand1d_cquad2;
       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
       fval[0]=res;
     break;
     
     case 3:
        F.function = &Integrand1d_cquad3;
        gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
        fval[0]=res;
     break;
     
     case 4:
       F.function = &Integrand1d_cquad4;
       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
       fval[0]=res;
     break;
     
     default: //debugging by plotting the 1d-integrand
     	{
     	  int nk2=10001; double xx, yy;
		  fval[0]=0.0;
          for (int ii=0; ii<nk2; ii++)
          {
            xx=3e9*ii/(nk2-1.0);
            yy=Integrand1d_cquad3(xx,cubpass);
            fval[0]+=yy*3e9/(nk2-1.0);
          }
            printf("->check ,%e \n",fval[0]);
     	}  	
     	
   }
                  
    
    if(std::isinf(fval[0])||std::isnan(fval[0])) // in case return NaN/Inf
    { 
    
      printf( "->divergence experienced,call InfPeak !\n" );
      
	    try
	      {   
	       InfPeak_diag Peak_ftor{Ek1,Ek4,QD,flag,cubpass->EC,cubpass->EV,cubpass->KGRID};
	       k2min=zbrent(Peak_ftor, k2min, k2max, 1e-1);
	      }
	      //catch(NRerror s) 
	      catch(const char* p)                   // by S.Z.
	      {
	       //NRcatch(s);
	       printf("%s\n", p);
	       fval[0]=0.0; return 0;                // no solution found by Peakfinder
	      }  
    
         gsl_function F;
         F.params =   cubpass;
       
	     switch (flag) {
	     case 1:
	       F.function = &Integrand1d_cquad1;
	       gsl_integration_cquad(&F, 0,k2min,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]=res;
	       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]+=res;
	     break;
	     
	     case 2:
	       F.function = &Integrand1d_cquad2;
	       gsl_integration_cquad(&F, 0,k2min,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]=res;
	       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]+=res;
	     break;
	     
	     case 3:
	       F.function = &Integrand1d_cquad3;
	       gsl_integration_cquad(&F, 0,k2min,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]=res;
	       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]+=res;
	     break;
	     
	     case 4:
	       F.function = &Integrand1d_cquad4;
	       gsl_integration_cquad(&F, 0,k2min,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]=res;
	       gsl_integration_cquad(&F, k2min,k2max,0, 1e-3, ws, &res, &abserr, &neval);
	       fval[0]+=res;
	     break;
	     
	     default:
	     	printf( "type illegal !" );
	                  }

    }
    
    gsl_integration_cquad_workspace_free( ws);
    
    return 0;	
	
}
示例#6
0
文件: drg_model.c 项目: rmredd/HOD_MN
double mag_at_fixed_ngal(double ngal)
{
  NGAL = ngal;
  return zbrent(sham_func1,-27.9,-2.0,1.0E-5);
}
示例#7
0
文件: drg_model.c 项目: rmredd/HOD_MN
void drg_model()
{
  int k,i,n=40,j,i1,i2,ngal;
  double dlogr,r,mass,xk,pnorm,psp,rm,sig,t0,t1,pnorm1,mp,mlo,mhi,dr,
    slo,shi,dsdM,rlo,rhi,dlogm,f1,f2,fac,cvir,rvir,rs,r1,r2,mtot=0,m,
    chi2lf,chi2w,x0,mlow;
  FILE *fp,*fp2;
  float x1,x2,x3,x4;
  char aa[1000],fname[1000],**argv;

  float *xx,*yy,*zz,*vx,*vy,*vz;
  float *wdata,*rdata,*edata,*lfdata,*lferr,*lfmag,*wmodel;
  int magcnt[100];
  int nwdata, nlf, j1, ngrid = 10, ingal;

  double tmin, tmax, dtheta, theta, delta_mag, maglo;

  /* FITTING RIK'S DATA
   */
  rlo = log(1.0);
  rhi = log(1000);
  dtheta = (rhi-rlo)/(19);
  for(i=0;i<20;++i)
    {
      theta = exp(rlo+i*dtheta);
      printf("ALL %e %e\n",theta,wtheta(theta));
      fflush(stdout);
    }

  sprintf(Task.root_filename,"all");
  tasks(ARGC,ARGV);

  RESET_FLAG_1H++;
  RESET_FLAG_2H++;

  GALAXY_DENSITY = 4.9e-4;
  NGAL_DRG = 4.9e-4;
  HOD.pdfc = 10; // set up centrals for RED
  HOD.freds = 0.3; // for RED
  HOD.fredc = 1;
  HOD.mass_shift = zbrent(func_findmshift2,0.0,2.434682,1.0E-4); //spaced in fcen0
  fprintf(stdout,"fsat = %.2f fcen = %.2f mu = %.2f ngal= %e %e\n",HOD.freds,HOD.fredc, HOD.mass_shift,GALAXY_DENSITY,qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt));

  for(i=0;i<20;++i)
    {
      theta = exp(rlo+i*dtheta);
      printf("RED %e %e\n",theta,wtheta(theta));
      fflush(stdout);
    }

  sprintf(Task.root_filename,"red");
  tasks(ARGC,ARGV);

  RESET_FLAG_1H++;
  RESET_FLAG_2H++;

  GALAXY_DENSITY = 1.9e-3;
  HOD.pdfc = 11; // set up centrals for BLUE
  HOD.freds = 1 - HOD.freds; // for BLUE
  printf("ngal = %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt));

  for(i=0;i<20;++i)
    {
      theta = exp(rlo+i*dtheta);
      printf("BLUE %e %e\n",theta,wtheta(theta));
      fflush(stdout);
    }

  sprintf(Task.root_filename,"blue");
  tasks(ARGC,ARGV);

  exit(0);



  BOX_SIZE = 160.;

  fp = openfile("wtheta_corrected.data");
  nwdata = filesize(fp);
  wdata = vector(1,nwdata);
  rdata = vector(1,nwdata);
  edata = vector(1,nwdata);
  wmodel = vector(1,nwdata);

  for(i=1;i<=nwdata;++i)
    fscanf(fp,"%f %f %f",&rdata[i],&wdata[i],&edata[i]);
  fclose(fp);

  fp = openfile("LF_DRG.data");
  nlf = filesize(fp);
  lfmag = vector(1,nlf);
  lfdata = vector(1,nlf);
  lferr = vector(1,nlf);
  for(i=1;i<=nlf;++i)
    fscanf(fp,"%f %f %f",&lfmag[i],&lfdata[i],&lferr[i]);
  fclose(fp);
  delta_mag = 0.45;
  maglo = -23.86-delta_mag/2;

  MSTAR = mstar();

  tmin = log(1.0);
  tmax = log(1000);
  dtheta = (tmax-tmin)/(19);

  //HOD.M_min *= 1.5;
  //HOD.M_low *= 1.5;

  // get mean mass of centrals (ALL)
  MALL = qromo(func_drg1,log(HOD.M_low),log(HOD.M_max),midpnt)/
    qromo(func_drg1a,log(HOD.M_low),log(HOD.M_max),midpnt);

  //new model
  HOD.mass_shift = 0.5;
  HOD.pdfc = 10;
  //HOD.shift_alpha = 1;

  mlow = 1;
  mhi = 2;
  dlogm = log(mhi/mlow)/ngrid;

  //analytic_sham();

  /**********************************
   */
  // let's do the prediction for the blue galaxies.
  // best-fit model (alpha=1) fit10 is 12 28
  // best-fit model (alpha=1) fit9 is 30 19

  j1 = 12;
  j = 19;

  // set up HOD as DRGs
  // for stepping in mshift
  ERROR_FLAG = 0;
  MSHIFT = exp((j1-0.5)*dlogm)*mlow;
  HOD.fredc = 1;
  HOD.mass_shift = zbrent(func_findmshift,0.0,10.0,1.0E-4);
  HOD.freds = (j-0.5)/ngrid;
  HOD.fredc = zbrent(func_findfredc,0.0,1.0,1.0E-4);

  // for stepping in fsat0
  HOD.fredc = (j1-0.5)/ngrid;
  HOD.freds = (j-0.5)/ngrid;
  HOD.mass_shift = zbrent(func_findmshift2,0.0,1.434682,1.0E-4); //spaced in fcen0

  // 9panel c2 5.9
  //CHI 993 0.978682 0.660204 0.181224 2.119014e-01 8.793237e+00 1.237535 6.664499e-04
  HOD.mass_shift = 0.181;
  HOD.fredc = 0.66;
  HOD.freds = 0.3;//0.979;


  HOD.freds = 1 - HOD.freds; // for NON-DRGs
  HOD.pdfc = 11; // set up centrals as 1-f(DRG)

  //HOD.pdfc = 7; // square-well blues at low-mass end
  //HOD.M_min = 7e11;
  //HOD.M_cen_max = 1.5e12;
  //HOD.M_low = 7e11;
 
  GALAXY_DENSITY = qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt);
 fprintf(stdout,"fsat = %.2f fcen = %.2f ngal= %e\n",HOD.freds,HOD.fredc, GALAXY_DENSITY);
  RESET_FLAG_1H++;
  RESET_FLAG_2H++;

  for(i=0;i<20;++i)
    {
      theta = exp(tmin+i*dtheta);
      fprintf(stdout,"WTH %e %e\n",theta,wtheta(theta));
    }
  sprintf(Task.root_filename,"BX");
  //tasks(2,argv);
  exit(0);
  /*
  ****************************************/

  if(ARGC>3)
    ingal = atoi(ARGV[3]);
  else
    ingal = 1;
      
  NGAL_DRG = 6.5e-4;//*(1+(ingal-5)/2.5*0.13);


  // do an outer loop of the mass shifts
  for(j1=1;j1<=ngrid;++j1)
    {
      // go in steps of mean mass shift from 1 to 2.
      MSHIFT = exp((j1-0.5)*dlogm)*mlow;
      HOD.fredc = 1;
      HOD.mass_shift = zbrent(func_findmshift,0.0,10.0,1.0E-4);

      //HOD.mass_shift = 1.116703; //figure 3

      // doing equally spaced in fcen0
      //HOD.fredc = (j1-0.5)/ngrid;

      for(j=1;j<=ngrid;++j)
	{
	  ERROR_FLAG = 0;
	  HOD.freds = (j-0.5)/ngrid;
	  HOD.fredc = zbrent(func_findfredc,0.0,1.0,1.0E-4);
	  if(ERROR_FLAG)
	    HOD.fredc = 1.0;
	  ERROR_FLAG = 0;

	  /*
	  HOD.mass_shift = zbrent(func_findmshift2,0.0,1.434682,1.0E-4); //spaced in fcen0
	  if(ERROR_FLAG)
	    {
	      chi2lf = chi2w = 1.0e6;
	      printf("CHI %d %d %f %f %f %e %e %f\n",j1,j,HOD.freds,HOD.fredc,HOD.mass_shift,chi2lf,chi2w,MSHIFT);
	      fflush(stdout);
	      continue;
	    }
	  */


	  //best fit model from chains (wth+n only)
	  //1.648589e-01 7.732068e-01 9.796977e-01
	  MSHIFT = pow(10.0,0.164);
	  HOD.freds = 0.7732;
	  HOD.fredc = 0.977;
	  HOD.mass_shift = zbrent(func_findmshift,0.0,10.0,1.0E-4);
	  
	  // third column 7.10
	  //CHI 7 10 0.950000 1.000000 0.661607 9.897945e+00 1.109673e+01 1.569168 6.500000e-04 5.905374e-04
	  HOD.mass_shift = 0.6616;
	  HOD.fredc = 1.00;
	  HOD.freds = 0.95;
	  j1 = 7;
	  j = 10;

	  // 9panel c2 5.9
	  //CHI 993 0.978682 0.660204 0.181224 2.119014e-01 8.793237e+00 1.237535 6.664499e-04
	  j1 = 5;
	  j = 9;
	  HOD.mass_shift = 0.181;
	  HOD.fredc = 0.66;
	  HOD.freds = 0.979;

	  //for 9panel c1 1.1
	  j1 = 1;
	  j = 1;
	  HOD.mass_shift = 0;
	  HOD.fredc = NGAL_DRG/GALAXY_DENSITY;
	  HOD.freds = NGAL_DRG/GALAXY_DENSITY;

	  //best-fit model without LF
	  HOD.mass_shift = 0.48;
	  HOD.fredc = 0.99;
	  HOD.freds = 0.69;

	  //best-fit model with LF (bottom row of 6panel)
	  //8.234815e-02 9.011035e-01 6.467542e-01 
	  j1 = 7;
	  j = 10;
	  HOD.mass_shift = 1.505979e-01 ;
	  HOD.fredc = 6.467542e-01 ;
	  HOD.freds = 9.011035e-01 ;

	  GALAXY_DENSITY = qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt);
	  fprintf(stdout,"fsat = %.1f fcen = %f ngal= %e\n",HOD.freds,HOD.fredc, GALAXY_DENSITY);
	  RESET_FLAG_1H++;
	  RESET_FLAG_2H++;

	  /*
	  for(i=0;i<20;++i)
	    {
	      theta = exp(tmin+i*dtheta);
	      fprintf(stdout,"WTH %e %e\n",theta,wtheta(theta));
	    }
	  exit(0);
	  */

	  //populate_simulation();
	  //exit(0);

	  // get qudari model points
	  //fp = openfile("q8m.dat");
	  

	  // calculate the chi^2 for the wtheta values.
	  chi2w = 0;
	  for(i=1;i<=nwdata;++i)
	    {
	      x0 = wtheta(rdata[i]);
	      wmodel[i] = x0;
	      //q8 model
	      //fscanf(fp,"%f %f",&x1,&wmodel[i]);
	      //x0 = wmodel[i];
	      printf("XX %e %e %e %e\n",rdata[i],wdata[i],x0,edata[i]);
	      chi2w += (wdata[i]-x0)*(wdata[i]-x0)/(edata[i]*edata[i]);
	    }
	  //fclose(fp);

	  fmuh(chi2w);
	  if(USE_COVAR)
	    chi2w = chi2wtheta_covar(wdata,wmodel);
	  fmuh(chi2w);
	  //exit(0);
	  
	  sprintf(fname,"wth_mshift_%d.%d",j1,j);
	  fp = fopen(fname,"w");
	  for(i=0;i<20;++i)
	    {
	      theta = exp(tmin+i*dtheta);
	      fprintf(fp,"%e %e\n",theta,wtheta(theta));
	    }
	  fclose(fp);
	  //continue;
	  
	  // do the real-space clustering and HOD
	  sprintf(Task.root_filename,"mshift_%d.%d",j1,j);
	  tasks(2,argv);
	  
	  // now make the luminosity function for this model
	  //output_drg_lf(j);      
	  sprintf(fname,"sham ../../SHAM/halosub_0.284 hod_mshift_%d.%d %f %f %f %f > gal_mshift_%d.%d",j1,j,HOD.freds,HOD.fredc,HOD.mass_shift,GALAXY_DENSITY,j1,j);
	  //sprintf(fname,"sham ../SHAM_120/halosub_0.3323.dat hod_mshift_%.1f.%d %f %f %f > gal_mshift_%.1f.%d",HOD.mass_shift,j,HOD.freds,HOD.fredc,HOD.mass_shift,HOD.mass_shift,j);
	  fprintf(stderr,"[%s]\n",fname);
	  system(fname);
	  
	  // calculate the clustering of this drg sample
	  sprintf(fname,"covar3 0.1 15 12 160 0 160 1 gal_mshift_%d.%d a 0 1 auto > xi.mshift_%d.%d",j1,j,j1,j);
	  //fprintf(stderr,"[%s]\n",fname);
	  //system(fname);
	  

	  // calculate the luminosity function
	  sprintf(fname,"gal_mshift_%d.%d",j1,j);
	  fp = openfile(fname);
	  n = filesize(fp);
	  for(i=1;i<=nlf;++i)
	    magcnt[i] = 0;
	  for(i=1;i<=n;++i)
	    {
	      for(k=1;k<=10;++k) fscanf(fp,"%f",&x1);// printf("%e\n",x1); }
	      k = (x1-maglo)/delta_mag + 1;
	      if(k>=1 && k<=nlf)magcnt[k]+=1;
	      //printf("%d %d %f %f %f\n",i,k,x1,maglo,delta_mag);
	      fscanf(fp,"%f",&x1);
	    }
	  fclose(fp);
	  
	  // calculate the chi^2 for the luminosity function
	  chi2lf = 0;
	  for(i=1;i<=nlf;++i)
	    {
	      if(i==nlf)
		x0 = log10(magcnt[i]/pow(BOX_SIZE/0.7,3.0)/delta_mag);
	      else
		x0 = log10(magcnt[i]/pow(BOX_SIZE/0.7,3.0)/delta_mag);
	      printf("LF %d %d %f %f %f\n",j1,j,x0,lfdata[i],lferr[i]);
	      chi2lf += (x0 - lfdata[i])*(x0 - lfdata[i])/(lferr[i]*lferr[i]);
	    }
	  
	  printf("CHI %d %d %f %f %f %e %e %f %e %e\n",j1,j,HOD.freds,HOD.fredc,HOD.mass_shift,chi2lf,chi2w,MSHIFT,NGAL_DRG,GALAXY_DENSITY);
	  fflush(stdout);
	  exit(0);
	}
    }

  exit(0);
}
示例#8
0
double find_maximum_halo_mass()
{
    return zbrent(func_max_mass,log(1.0E12),log(1.0E16),1.0E-4);
}
示例#9
0
double satellite_mass_scale()
{
  return zbrent(func_satmass,HOD.M1/100,HOD.M1*10,1.0E-4);
}
示例#10
0
/* This is a function to set the HOD parameters until 
 * I get some input code or batch file set up.
 * 
 */
void set_HOD_params()
{
  int i,j=1;
  double m,error=1.0,tol=1.0E-4,prev,s1,mlo;

  /* If the mass function at M_max is undefined, reset M_max
   */
  //LOCAL_DENSITY = -0.2;
  if(LOCAL_DENSITY!=0)
    {
      HOD.M_max = 1.0E16;
      while(dndM_interp(HOD.M_max)<=0) 
	{
	  HOD.M_max*=0.9;
	}
      fprintf(stderr,"NEW M_max= %e\n",HOD.M_max);
    }

  if(HOD.pdfc == 2 || HOD.pdfc == 3 || HOD.pdfc == 6 || HOD.pdfc == 8 || HOD.pdfc == 9 || HOD.pdfc == 12 || HOD.pdfc == 13)
    SOFT_CENTRAL_CUTOFF=1;

  /* Error trap both the galaxy density and M_min both left unspecified.
   */
  if(HOD.M_min<=0 && GALAXY_DENSITY<=0 && HOD.free[0]==0)
    endrun("ERROR: Must specify either M_min or GALAXY_DENSITY");

  /* If the user has specified M_min and M1, calculate the galaxy density.
   */
  if(HOD.M_min>0 && HOD.M1>0)
    {
      HOD.M_low = -1;
      if (wp.ngal==0) wp.ngal = GALAXY_DENSITY;
      if(SOFT_CENTRAL_CUTOFF)
	HOD.M_low0 = HOD.M_low = exp(zbrent(func_mlow,log(HOD.M_min*1.0E-6),log(HOD.M_min*1.1),1.0E-5));
      else
	HOD.M_low0 = HOD.M_low = HOD.M_min;

      if(HOD.M_low > HOD.M1 ) {
	while(N_cen(HOD.M_low) > 0.01) HOD.M_low /= 2.;
      }

      //if(HOD.M_low < HOD.M_min/100.) HOD.M_low = HOD.M_min/100.;

      GALAXY_DENSITY=qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt);
      if(OUTPUT) {
	fprintf(stdout,"M_low= %e\n",HOD.M_low);
	fprintf(stdout,"ng= %e\n",GALAXY_DENSITY); }
    }      

  /* If M_min<=0 then use the specified galaxy density to calculate M_min
   */
  if(HOD.M_min<=0)
    {
      if(HOD.pdfc==7 && HOD.pdfc==8)
	HOD.M_min=pow(10.0,zbrent(fixfunc1,8.0,log10(HOD.M_cen_max*0.99),1.0E-5));
      else
	HOD.M_min=pow(10.0,zbrent(fixfunc1,7.0,14.8,1.0E-5));

      if(OUTPUT) fprintf(stderr,"SOFT_CENTRAL_CUTOFF Delta-n %d %e\n",SOFT_CENTRAL_CUTOFF,
			 fixfunc1(log10(HOD.M_min)));
      HOD.M_low = -1;
      if(SOFT_CENTRAL_CUTOFF) {
	//HOD.M_low = exp(zbrent(func_mlow,log(HOD.M_min)-5*HOD.sigma_logM*2.3,
	//		       log(HOD.M_min),1.0E-5));
	HOD.M_low = HOD.M_min/2.;
	while(N_cen(HOD.M_low) > 0.01)
	  HOD.M_low /= 2.;
      } else
	HOD.M_low = HOD.M_min;
      if(HOD.M_low<1.0E7)HOD.M_low=1.0E+7;
      if(OUTPUT) {
	fprintf(stdout,"M_min %e [ng= %e]\n",HOD.M_min,GALAXY_DENSITY);
	fprintf(stdout,"M_low= %e\n",HOD.M_low); }
    }

  /* If M1<=0 then use the specified galaxy density to calculate M1
   */
  if(HOD.M1<=0)
    {
      HOD.M_low = -1;
      if(SOFT_CENTRAL_CUTOFF)
	HOD.M_low = exp(zbrent(func_mlow,log(HOD.M_min)-5*HOD.sigma_logM*2.3,
			       log(HOD.M_min*1.1),1.0E-5));
      else
	HOD.M_low = HOD.M_min;
      if(HOD.M_low<1.0E7)HOD.M_low=1.0E+7;
      HOD.M1=pow(10.0,zbrent(fixfunc2,log10(HOD.M_low),15.8,1.0E-5));
      if(OUTPUT) {
	fprintf(stdout,"M1 %e [ng= %e]\n",HOD.M1,GALAXY_DENSITY);
	fprintf(stdout,"M_min = %e M_low= %e\n",HOD.M_min,HOD.M_low); }
    }

  /* Set the number of halo mass bins we've got.
   */
  NUM_POW2MASS_BINS=log(HOD.M_max/HOD.M_low)/LN_2+1;

  if(HOD.pdfc==6)
    HOD.M_hi = set_high_central_mass();
  HOD.M_low0 = set_low_mass();
  return;

}
示例#11
0
void calc_saxton_stuff(params *p, double *fsoil) {
    /*
        Calculate the key parameters of the Saxton equations:
        cond1, 2, 3 and potA, B

        NB: Currently I'm assuming a single texture for the entire root zone,
            we could set it by layer obviously and we probably should...

        Reference:
        ---------
        * Saxton, K. E., Rawls, W. J., Romberger, J. S. & Papendick, R. I.
          (1986) Estimating generalized soil-water characteristics from texture.
          Soil Science Society of America Journal, 90, 1031-1036.
    */
    int    i;
    double mult1 = 100.0;
    double mult2 = 2.778E-6;
    double mult3 = 1000.0;
    double A = -4.396;
    double B = -0.0715;
    double CC = -4.880E-4;
    double D = -4.285E-5;
    double E = -3.140;
    double F = -2.22E-3;
    double G = -3.484E-5;
    double H = 0.332;
    double J = -7.251E-4;
    double K = 0.1276;
    double P = 12.012;
    double Q = -7.551E-2;
    double R = -3.895;
    double T = 3.671e-2;
    double U = -0.1103;
    double V = 8.7546E-4;
    double x1 = 0.1;
    double x2 = 0.7;
    double tol = 0.0001;
    double dummy = 0.0;
    double sand = fsoil[SAND] * 100.0;
    double clay = fsoil[CLAY] * 100.0;

    /*
    ** As we aren't currently changing texture by layer, the loop is redundant,
    ** but it is probably best to leave it under the assumption we change
    ** this later
    */

    for (i = 0; i < p->n_layers; i++) {
        p->potA[i] = exp(A + B * clay + CC * sand * \
                         sand + D * sand * sand * \
                         clay) * 100.0;
        p->potB[i]  = E + F * clay * clay + G * sand * sand * clay;
        p->cond1[i] = mult2;
        p->cond2[i] = P + Q * sand;
        p->cond3[i] = R + T * sand + U * clay + V * clay * clay;

        p->porosity[i] = H + J * sand + K * log10(clay);

        // field capacity is water content at which SWP = -10 kPa
        p->field_capacity[i] = zbrent(&saxton_field_capacity, x1, x2, tol,
                                      p->potA[i], p->potB[i],
                                      dummy, dummy, dummy);
    }

    return;
}
示例#12
0
/* It is straightforward to calculate what M_low
 * should be given the other parameters on N_cen.
 */
double set_low_mass()
{
  int i;
  double m,test_n;

  if(ERROR_FLAG)
    {
      fprintf(stdout,"uncaught ERROR at top of set low mass\n");
      ERROR_FLAG = 0;
    }

  if(!SOFT_CENTRAL_CUTOFF)return(HOD.M_min);
  switch(HOD.pdfc){
  case 8:
  case 6:
    m = log10(HOD.M_min) - sqrt(-2*HOD.sigma_logM*HOD.sigma_logM*log(0.001));
    m = pow(10.0,m);
    return(m);
  case 9:
    m = exp(zbrent(func_mlow,log(HOD.M_min)-5*HOD.sigma_logM*2.3,
		   log(HOD.M_min),1.0E-5));
    return(m);
  case 100:
  case 101:
    if(wpl.mlow_flag)
      return HOD.M_max*0.98;
    /*
    test_n=N_cen(HOD.M_min*1.0E-6);
    if(test_n > 0.001)
      return HOD.M_min*1.0E-7; // this is not ideal but should avoid getting some zbrent error here (notice returning 10^-7 here)
    */
    m = exp(zbrent(func_mlow,log(HOD.M_min*1.0E-6),
		   log(HOD.M_min),1.0E-5)); // changed to 1.0E-6. Could be a problem here is BOTH the data points are above 10^-3
    if(m>HOD.M_max)
      {
	if(ERROR_FLAG) ERROR_FLAG = 0;
	return HOD.M_max*1.0E-3;
      }    
    if(ERROR_FLAG && HOD.M_min > 1.0E+14)
      {
	ERROR_FLAG = 0;
	return HOD.M_min*1.0E-6;
      }
    if(ERROR_FLAG)
      {
	ERROR_FLAG = 0;
	return HOD.M_min*1.0E-3;
      }
    if(m>HOD.M_min)
      {
	return HOD.M_min*1.0E-3;
      }
    
    if(ERROR_FLAG)
      {
	fprintf(stdout," ERROR in set_low_mass: %e %e %e %d \n",HOD.M_min, HOD.sigma_logM, GALAXY_DENSITY,wpl.mlow_flag);
	fprintf(stdout,"n wpl.mlow_flag = %d \n",wpl.mlow_flag);
	test_n=N_cen(HOD.M_max);
	fprintf(stdout,"ncen at HOD max = %e \n",test_n);
	fprintf(stdout,"ncen at HOD.M_min*1.0E-6 = %e \n",N_cen(HOD.M_min*1.0E-6));
	fprintf(stdout,"ncen at HOD.M_min and Mmin = %e %e \n",N_cen(HOD.M_min),HOD.M_min);
	for(i=1000;i<=1600;++i)
	 printf("HODCEN %f %e\n",i/100.0,N_cen(pow(10.0,i/100.0)));
	exit(0);
      }

    return(m);
  default:
    m = exp(zbrent(func_mlow,log(HOD.M_min*1.0E-6),
		   log(HOD.M_min),1.0E-5));
    return(m);
  }
  return(0);
}
示例#13
0
void fitexy(float x[], float y[], int ndat, float sigx[], float sigy[],
	float *a, float *b, float *siga, float *sigb, float *chi2, float *q)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	float brent(float ax, float bx, float cx,
		float (*f)(float), float tol, float *xmin);
	float chixy(float bang);
	void fit(float x[], float y[], int ndata, float sig[], int mwt,
		float *a, float *b, float *siga, float *sigb, float *chi2, float *q);
	float gammq(float a, float x);
	void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb,
		float *fc, float (*func)(float));
	float zbrent(float (*func)(float), float x1, float x2, float tol);
	int j;
	float swap,amx,amn,varx,vary,ang[7],ch[7],scale,bmn,bmx,d1,d2,r2,
		dum1,dum2,dum3,dum4,dum5;

	xx=vector(1,ndat);
	yy=vector(1,ndat);
	sx=vector(1,ndat);
	sy=vector(1,ndat);
	ww=vector(1,ndat);
	avevar(x,ndat,&dum1,&varx);
	avevar(y,ndat,&dum1,&vary);
	scale=sqrt(varx/vary);
	nn=ndat;
	for (j=1;j<=ndat;j++) {
		xx[j]=x[j];
		yy[j]=y[j]*scale;
		sx[j]=sigx[j];
		sy[j]=sigy[j]*scale;
		ww[j]=sqrt(SQR(sx[j])+SQR(sy[j]));
	}
	fit(xx,yy,nn,ww,1,&dum1,b,&dum2,&dum3,&dum4,&dum5);
	offs=ang[1]=0.0;
	ang[2]=atan(*b);
	ang[4]=0.0;
	ang[5]=ang[2];
	ang[6]=POTN;
	for (j=4;j<=6;j++) ch[j]=chixy(ang[j]);
	mnbrak(&ang[1],&ang[2],&ang[3],&ch[1],&ch[2],&ch[3],chixy);
	*chi2=brent(ang[1],ang[2],ang[3],chixy,ACC,b);
	*chi2=chixy(*b);
	*a=aa;
	*q=gammq(0.5*(nn-2),*chi2*0.5);
	for (r2=0.0,j=1;j<=nn;j++) r2 += ww[j];
	r2=1.0/r2;
	bmx=BIG;
	bmn=BIG;
	offs=(*chi2)+1.0;
	for (j=1;j<=6;j++) {
		if (ch[j] > offs) {
			d1=fabs(ang[j]-(*b));
			while (d1 >= PI) d1 -= PI;
			d2=PI-d1;
			if (ang[j] < *b) {
				swap=d1;
				d1=d2;
				d2=swap;
			}
			if (d1 < bmx) bmx=d1;
			if (d2 < bmn) bmn=d2;
		}
	}
	if (bmx < BIG) {
		bmx=zbrent(chixy,*b,*b+bmx,ACC)-(*b);
		amx=aa-(*a);
		bmn=zbrent(chixy,*b,*b-bmn,ACC)-(*b);
		amn=aa-(*a);
		*sigb=sqrt(0.5*(bmx*bmx+bmn*bmn))/(scale*SQR(cos(*b)));
		*siga=sqrt(0.5*(amx*amx+amn*amn)+r2)/scale;
	} else (*sigb)=(*siga)=BIG;
	*a /= scale;
	*b=tan(*b)/scale;
	free_vector(ww,1,ndat);
	free_vector(sy,1,ndat);
	free_vector(sx,1,ndat);
	free_vector(yy,1,ndat);
	free_vector(xx,1,ndat);
}
示例#14
0
int jitter (double *resi, double *e, double *stdy, double psrfreq, int nint, int nphase, double degree, FILE *fp)
// calculate the jitter parameter and its error
{
    //puts (argv[1]);
    //puts (argv[2]);

    //int nint=0; 
    //int nsample=0; 
    //double resi[64],e[64];
    //double x[1024],y[1024];

    //read (argv[1], &nint, resi, e);
    //read (argv[2], &nsample, x, y);

    //printf("Number of subint is: %d \n", nint);

    /////////////////////////////////////////////////////////////////////////////////
	// calculate jitter parameter
	double k;
	//k=nint-10.0;
	k = degree;

	double reduced_chi;

	double sigma_J;

	reduced_chi = 1.0;

	// initial guess of the result
	/////////////////////////////////////////////////////////////////////
	double up = 1.0e-5;
	double step = up/100.0;
	double low = 1.0e-5;

	while( chi(up, k, nint, resi, e, reduced_chi)*chi(low, k, nint, resi, e, reduced_chi) > 0 )
	{
		low = low - step;
	}
	/////////////////////////////////////////////////////////////////////

	sigma_J = zbrent(chi, low, up, 1.0e-16, k, nint, resi, e, reduced_chi);

    printf("low: %e \n", low);
    printf("sigma_J: %e \n", sigma_J);
    printf("reduced_chisqr: %e \n", chi(sigma_J, k, nint, resi, e, reduced_chi));

    ////////////////////////////////////////////////////////////////////////////////

    double U;
    U = u_0(stdy, nphase, psrfreq);

    ///////////////////////////////////////////////////////////////////////////////

    double jitter;
    //double N=floor(60*173.687946184761);

    //jitter=sqrt(N)*sigma_J/U;
    jitter = sigma_J/sqrt(U);
    printf ("jitter parameter is: %f\n", jitter);
    //printf ("jitter parameter is: %f\n", sqrt(N)*sigma_J/0.14e-3);
    //printf ("jitter parameter is: %f\n", sqrt(N)*sigma_J/1.02e-3);
	
	//////////////////////////////////////////////////////////////////////////////////
	// calculte the error of jitter parameter
	struct my_params params;
    double chisqr_new;

	Gamma_cal(k, &params);

	// initial guess of the result
	/////////////////////////////////////////////////////////////////////
	double up_e = k+10;
	double step_e = up_e/100.0;
	double low_e = up_e;
    //printf ("%lf %lf\n", low_e, up_e);

	while( error(up_e, &params, 500000)*error(low_e, &params, 500000) > 0 )
	{
		low_e = low_e - step_e;
	}
    //printf ("%lf %lf\n", low_e, up_e);
	/////////////////////////////////////////////////////////////////////

	//chisqr_new = zbrent_err(error, low_e-10.0, up_e+10.0, 1.0e-16, &params); // get the 1 sigma chisqr
	chisqr_new = zbrent_err(error, low_e, up_e, 1.0e-16, &params, 500000); // get the 1 sigma chisqr

	reduced_chi = chisqr_new/k;
    printf ("chisqr_new: %lf\n", chisqr_new);

	/////////////////////////////////////////////////////////////////////

	// initial guess of the result
	/////////////////////////////////////////////////////////////////////
	up = 1.0e-6;
	step = up/100.0;
	low = 1.0e-6;

	while( chi(up, k, nint, resi, e, reduced_chi)*chi(low, k, nint, resi, e, reduced_chi) > 0 )
	{
		low = low - step;
	}
	/////////////////////////////////////////////////////////////////////

	double sigma_J_error, jitter_error;
	//sigma_J_error = zbrent(chi, 1e-7, 1e-5, 1.0e-16, k, nint, resi, e, reduced_chi);   // for 1 sigma reduced_chi, get the sigma_J
	sigma_J_error = zbrent(chi, low, up, 1.0e-16, k, nint, resi, e, reduced_chi);   // for 1 sigma reduced_chi, get the sigma_J

	jitter_error = fabs(sigma_J_error/sqrt(U)-jitter);
    printf ("The error of jitter parameter is: %f\n", jitter_error);
	
	// print out the results
    fprintf (fp, "%lf %lf\n", jitter, jitter_error);

    return 0;
}
示例#15
0
INT4 InitPcsi (void)

/***********************************************************************
*
*_Title	InitPcsi - Initialize pcsi parameters 
*
*_DESC	Initialize pcsi parameters 
*
*_HIST	Mar 9 2003 Janet Barrett, Created by modifying the initpcp
*                  subroutine used by the pc2d program.
*       Jul 9 2003 JB - Fixed a problem with handling Level 1 labels
*                  in a Level 2 image.
*       Jan 11 2010 JB - The both.level element is not being set by
*                  calls to lev1_init or lev2_init. This was fixed by
*                  using the spi_level value in place of it.
*                  
*_END
************************************************************************/

{

  BOOLEAN levels,frame;
  INT4 level,spi_level;
  INT4 lzin,llog,lnote;
  INT4 length,icount,ret;
  INT4 naxes,order,core_size[3],suf_size[3];
  INT4 issi,isli;
  INT4 iesi,ieli;
  INT4 iflag,jflag;
  INT4 lpos;
  INT4 icard;
  INT4 ccont[3];
  INT4 scont[3];
  INT4 dflag;
  INT4 icountx,icounty;
  INT4 levgroups;
  INT4 found;
  INT4 i,j;
  int (*p)();

  FLOAT4 dzxm,dzym;
  FLOAT4 z01,z02,z03,z04;
  FLOAT4 xsinci,xlinci;
  FLOAT4 dzdip;
  FLOAT4 arg;
  FLOAT4 si0;
  FLOAT4 se0;
  FLOAT4 bclipin;
  FLOAT4 db1,db2;
  FLOAT4 cardaz;
  FLOAT4 delaza,delazb;
  FLOAT4 xcenter,ycenter;
  FLOAT4 clat,clon;

  FLOAT8 azi;
  FLOAT8 lat,lon;
  FLOAT8 inc,emi,pha;
  FLOAT8 scaz,sunaz;
  FLOAT8 ssclat,ssclon;
  FLOAT8 ssunlat,ssunlon;
  FLOAT8 radii[3];
  FLOAT8 slantrange;

  CHAR axis_name[3][MAXLITLEN+1];
  CHAR item_type[MAXLITLEN+1];
  CHAR prmname[30];
  CHAR tmpstr[256];
  CHAR planet[11];
  CHAR sfrom[136];
  CHAR scan_sc[NUMOFSCAN][LEV_SPACECRAFT_NAME_SIZE] = 
       {"MARS_GLOBAL_SURVEYOR",
        "MARS_GLOBAL_SURVEYOR",
	"MARS_ODYSSEY"};
  CHAR scan_inst[NUMOFSCAN][LEV_INSTRUMENT_NAME_SIZE] =
       {"MOC_NA_A",
        "MOC_WA_A",
	"THEMIS_IR"};
  CHAR frame_sc[NUMOFFRAME][LEV_SPACECRAFT_NAME_SIZE] =
       {"GALILEO_1",
        "VIKING_ORBITER_1",
	"VIKING_ORBITER_1",
	"VIKING_ORBITER_2",
	"VIKING_ORBITER_2",
	"MARS_ODYSSEY",
	"VOYAGER_1",
	"VOYAGER_1",
	"VOYAGER_2",
	"VOYAGER_2"};
/*	"CASSINI",
	"LUNAR_ORBITER_4"};*/
  CHAR frame_inst[NUMOFFRAME][LEV_INSTRUMENT_NAME_SIZE] =
       {"SSI",
        "VISUAL_IMAGING_SUBSYSTEM_CAMERA_A",
	"VISUAL_IMAGING_SUBSYSTEM_CAMERA_B",
        "VISUAL_IMAGING_SUBSYSTEM_CAMERA_A",
	"VISUAL_IMAGING_SUBSYSTEM_CAMERA_B",
	"THEMIS_VIS",
	"WA",
	"NA",
	"WA",
	"NA"};
/*	"VIMS",
	"24_INCH_FOCAL_LENGTH_CAMERA"};*/
  FLOAT8 frame_flinpix[NUMOFFRAME] =
       {98455.811,
        40323.83,
	40328.08,
	40341.85,
	40298.585,
	22222.222,
        16989.138,
	127248.26,
	17209.598,
	127528.17};

  LevData data,tmpdata;
  Lev both;
  FLOAT8 sampres,lineres,azres,sres;

  INT4 PcsiFirstGuess (void);
  CHAR obj_name[5]="QUBE";
  CHAR grp_name[1] = "";
  CHAR grp2_name[21]="IMAGE_MAP_PROJECTION";
  INT4 mulpht;
  INT4 whcnt,hg1cnt,hg2cnt,hhcnt,b0cnt,thetacnt,bhcnt,chcnt,kcnt,lcnt;

/******************************************************************************
* Get input and output file parameters from TAE
******************************************************************************/
  strcpy(prmname,"TO");
  u_get_file_parm("TO",1,(CHAR *)topofile,sizeof(topofile),&length,&icount,&ret);
  if (ret) goto user_parameter_error;
  if (icount <= 0 || strlen(topofile) == 0) goto to_file_error;
  (void) u_strtrim(topofile,topofile);
  if (strlen(topofile) == 0) goto to_file_error;
  if (u_file_exist(topofile)) goto to_file_exist;
  if (strncmp(&(topofile[strlen(topofile)-1]),"/",1) == 0) goto to_file_error;
  (void) u_ver_flspec(topofile,"cub",topofile,sizeof(topofile),&ret);
  if (ret) goto to_file_invalid;

  strcpy(prmname,"ZOUT");
  u_get_file_parm("ZOUT",1,(CHAR *)zoutfile,sizeof(zoutfile),&length,&icount,&ret);
  if (ret) goto user_parameter_error;
  if (icount <= 0 || strlen(zoutfile) == 0) {
    dozout = FALSE;
  } else {
    (void) u_strtrim(zoutfile,zoutfile);
    (void) u_ver_flspec(zoutfile,"cub",zoutfile,sizeof(zoutfile),&ret);
    if (ret) goto flspec_error;
    if (u_file_exist(zoutfile)) {
      if (remove(zoutfile)) goto zout_remove_error;
    }
    dozout = TRUE;
  }

  (void) xctae_files(zin,sizeof(zin),&lzin,&usezin,&usedatum,logfile,
	      sizeof(logfile),&llog,note,sizeof(note),&lnote);

/******************************************************************************
* Get subcube specifiers
******************************************************************************/
  strcpy(prmname,"SFROM");
  u_get_str_parm("SFROM",1,(CHAR *)sfrom,sizeof(sfrom),&length,&icount,&ret);
  if (ret) goto user_parameter_error;
  if (icount <= 0) (void) strcpy(sfrom," ");

/******************************************************************************
* Open the FROM file and get information
******************************************************************************/
  strcpy(prmname,"FROM");
  u_get_file_parm("FROM",1,(CHAR *)from,sizeof(from),&length,&icount,&ret);
  if (ret) goto user_parameter_error;
  q_open(&fid,0,from,READ_ONLY,0,"",0,0,1,0,&ret);
  if (ret) goto cube_open_error;
  q_check_std(fid,&ret);
  if (ret) goto invalid_cube;
  q_get_sys_keys(fid,1,&naxes,&order,core_size,suf_size,
	         (char *)axis_name,sizeof(axis_name[0]),&image_bytes,
		 item_type,sizeof(item_type),image_scale,&ret);
  if (ret) goto bad_sys_keys;

/******************************************************************************
* Parse the SFROM subcube specification and apply to the FROM file and
* then read system keywords again to reflect the virtual cube
******************************************************************************/
  (void) u_parse_isub(fid,sfrom,1,core_size,suf_size,ccont,scont,&ret);
  if (ret) goto parse_sfrom_error;
  (void) q_set_virtual(fid,&ret);
  if (ret) goto sfrom_error;
  q_get_sys_keys(fid,1,&naxes,&order,core_size,suf_size,
                 (char *)axis_name,sizeof(axis_name[0]),&image_bytes,
		 item_type,sizeof(item_type),image_scale,&ret);
  if (ret) goto bad_sys_keys;
  ins = core_size[0];
  inl = core_size[1];

/******************************************************************************
* Set the variables describing the FROM subcube
******************************************************************************/
  u_get_isub_desc(fid,"SAMPLE",&issi,&iesi,&xsinci,&dflag,&ret);
  if (ret) goto bad_subsamp;
  if (dflag != 1) goto bad_subsamp;
  u_get_isub_desc(fid,"LINE",&isli,&ieli,&xlinci,&dflag,&ret);
  if (ret) goto bad_subline;
  if (dflag != 1) goto bad_subline;
  if (xsinci != xlinci) goto inc_error; /* Need to be equal or
                               map scale will not be accurate */

/******************************************************************************
* Set center of virtual subcube
******************************************************************************/
  xcenter = 0.5*(ins+1);
  ycenter = 0.5*(inl+1);

/******************************************************************************
* Get photoclinometry parameters from TAE
******************************************************************************/
  strcpy(prmname,"MAXMEM");
  u_get_int_parm("MAXMEM",1,&C_MEM3->nmax,&icount,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"DNATM");
  u_get_real_parm("DNATM",1,1,6,&C_DNORM->dnatm,&icount,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"DNDATUM");
  u_get_real_parm("DNDATUM",1,1,6,&dndatum,&icount,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"DATUMTYP");
  u_get_int_parm("DATUMTYP",1,&C_DATUM2->datumtyp,&icount,&ret);
  if (ret) goto user_parameter_error;

  nucenter = FALSE; /* Reset to True if X,Y inputs given */

  strcpy(prmname,"X");
  u_get_dbl_parm("X",1,1,6,&x,&icountx,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"Y");
  u_get_dbl_parm("Y",1,1,6,&y,&icounty,&ret);
  if (ret) goto user_parameter_error;
  if ((icountx >= 1) && (icounty >= 1)) nucenter = TRUE;

  strcpy(prmname,"DISTORTD");
  u_get_str_parm("DISTORTD",1,(CHAR *)tmpstr,sizeof(tmpstr),&length,&icount,&ret);
  if (ret) goto user_parameter_error;

  distortd = TRUE;
  if (strncmp(tmpstr,"NO",1) == 0) 
    distortd = FALSE;

  strcpy(prmname,"METHOD");
  u_get_str_parm("METHOD",1,(CHAR *)method,sizeof(method),&length,&icount,&ret);
  if (ret) goto user_parameter_error;
  if (strncmp(method,"SOR",3) == 0) {
    sordir = 0;
  } else if (strncmp(method,"DIR",3) == 0) {
    sordir = 1;
  } else {
    sordir = 2;
  }

  strcpy(prmname,"PHOFUNC");
  u_get_str_parm("PHOFUNC",1,(CHAR *)C_PPARS4->phofunc,sizeof(C_PPARS4->phofunc),
                 &length,&icount,&ret);
  if (ret) goto user_parameter_error;

  C_PPARS1->piopt = 0;
  if (strncmp(C_PPARS4->phofunc,"HAPLEG",6) == 0)
    C_PPARS1->piopt = 1;
  if (strncmp(C_PPARS4->phofunc,"HAPL_S",6) == 0)
    C_PPARS1->piopt = 1;

  C_PPARS3->hapke = FALSE;
  if (strncmp(C_PPARS4->phofunc,"H",1) == 0) 
    C_PPARS3->hapke = TRUE;

  strcpy(prmname,"WH");
  u_get_real_parm("WH",1,1,6,&C_PPARS5->pwh,&whcnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"HG1");
  u_get_real_parm("HG1",1,1,6,&C_PPARS5->phg1,&hg1cnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"HG2");
  u_get_real_parm("HG2",1,1,6,&C_PPARS5->phg2,&hg2cnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"HH");
  u_get_real_parm("HH",1,1,6,&C_PPARS5->phh,&hhcnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"B0");
  u_get_real_parm("B0",1,1,6,&C_PPARS5->pb0,&b0cnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"THETA");
  u_get_real_parm("THETA",1,1,6,&C_PPARS5->ptheta,&thetacnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"BH");
  u_get_real_parm("BH",1,1,6,&C_PPARS5->pbh,&bhcnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"CH");
  u_get_real_parm("CH",1,1,6,&C_PPARS5->pch,&chcnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"K");
  u_get_real_parm("K",1,1,6,&C_PPARS2->pex,&kcnt,&ret);
  if (ret) goto user_parameter_error;

  strcpy(prmname,"L");
  u_get_real_parm("L",1,1,6,&C_PPARS2->pl2,&lcnt,&ret);
  if (ret) goto user_parameter_error;

  if (strncmp(C_PPARS4->phofunc,"HAPHEN",6) == 0) {
    if (whcnt < 1 || thetacnt < 1 || b0cnt < 1 || hhcnt < 1 ||
        hg1cnt < 1 || hg2cnt < 1) goto haphen_error;
  } else if (strncmp(C_PPARS4->phofunc,"HAPLEG",6) == 0) {
    if (whcnt < 1 || thetacnt < 1 || b0cnt < 1 || hhcnt < 1 ||
        bhcnt < 1 || chcnt < 1) goto hapleg_error;
  } else if (strncmp(C_PPARS4->phofunc,"HAPH_S",6) == 0) {
    if (whcnt < 1 || hg1cnt < 1 || hg2cnt < 1) goto haph_s_error;
  } else if (strncmp(C_PPARS4->phofunc,"HAPL_S",6) == 0) {
    if (whcnt < 1 || bhcnt < 1 || chcnt < 1) goto hapl_s_error;
  } else if (strncmp(C_PPARS4->phofunc,"LAMBER",6) == 0) {
    C_PPARS2->pex = 1.0;
    C_PPARS2->pl2 = 0.0;
  } else if (strncmp(C_PPARS4->phofunc,"LOMSEL",6) == 0) {
    C_PPARS2->pex = 1.0;
    C_PPARS2->pl2 = 1.0;
  } else if (strncmp(C_PPARS4->phofunc,"MIN",3) == 0) {
    C_PPARS2->pl2 = 0.0;
    if (kcnt < 1) goto min_error;
  } else if (strncmp(C_PPARS4->phofunc,"LUNLAM",6) == 0) {
    C_PPARS2->pex = 1.0;
    if (lcnt < 1) goto lunlam_error;
  }

/******************************************************************************
* Evaluate default (center) location in image if needed. This default is in
* ALL cases now the center of the virtual subcube, transferred to full cube.
******************************************************************************/
  if (!nucenter) {
    x = (FLOAT8) (issi) + (xcenter-1.0) * xsinci;
    y = (FLOAT8) (isli) + (ycenter-1.0) * xlinci;
  }

/******************************************************************************
* Check for the 3 levels Level 1 keyword groups (ISIS_INSTRUMENT, 
* ISIS_GEOMETRY, ISIS_TARGET)
******************************************************************************/
  levgroups = 0;
  (void) p_check_key(fid,"QUBE","ISIS_INSTRUMENT","",&icount,&ret);
  if (!ret) levgroups = levgroups + 1;
  (void) p_check_key(fid,"QUBE","ISIS_GEOMETRY","",&icount,&ret);
  if (!ret) levgroups = levgroups + 1;
  (void) p_check_key(fid,"QUBE","ISIS_TARGET","",&icount,&ret);
  if (!ret) levgroups = levgroups + 1;

/******************************************************************************
* All 3 levels Level 1 keyword groups are missing. This is either a 
* pre-levels Level 1 file or a Level 2 file that has had the groups erased
* from the labels (or, under the old system, never got them). If the file
* contains either ISIS_MOSAIC or IMAGE_MAP_PROJECTION keyword groups, then
* it is not a Level 1 image and cannot be used. Otherwise, try using old
* SPICE routines.
******************************************************************************/
  if (levgroups == 0) {
    (void) p_check_key(fid,"QUBE","ISIS_MOSAIC","",&icount,&ret);
    if (!ret) {
    /* Level 2 mosaic without required groups */
      goto mosaic_group_error;
    } else {
      (void) p_check_key(fid,"QUBE","IMAGE_MAP_PROJECTION","",&icount,&ret);
      if (!ret) {
        (void) p_check_key(fid,"QUBE","IMAGE_MAP_PROJECTION",
	  "MAP_PROJECTION_TYPE",&icount,&ret);
        if (!ret) {
        /* Level 2 image without required groups */
	  goto level2_group_error;
        }
      }
    }

    /* Pre-levels Level 1 image */
    iflag = 0;
    jflag = 1;
    if (!distortd) jflag = 0;
    spi_phosun(fid,obj_name,sizeof(obj_name),grp_name,sizeof(grp_name),
      grp2_name,sizeof(grp2_name),jflag,0,&iflag,
      &y,&x,&lat,&lon,&emi,&inc,&pha,&ssclat,&ssclon,
      &scaz,&ssunlat,&ssunlon,&sunaz,&aznor,&res,&ret);
    if (ret) goto spi_phosun_error;

/******************************************************************************
* If we got to here without an error, this is a valid pre-levels Level 1
* file. 
******************************************************************************/
    levels = FALSE;
    level = 1;

/******************************************************************************
* All pre-levels instruments are framing cameras.
******************************************************************************/
    frame = TRUE;

/******************************************************************************
* Now assign emi, inc, pha, scaz, sunaz to variables used below as
* appropriate for the framing camera case:  Z axis is viewing direction, so
* emission angle is used to calculate datum derivatives rather than emission
* vector. Also calculate the scaling variable, cosemi, to be used for 
* line-of-sight to vertical on output and vertical to line-of-sight on
* input.
******************************************************************************/
    res = res * xsinci; /* xsinci=xlinci guaranteed */
    sres = res;
    C_AEPAR1->aspect = 1.0;
    C_PSNPAR->phase[0] = pha;
    clinc = pha;
    azinc = sunaz;
    clemi = 0.0;
    azemi = 0.0;
    dip = emi;
    az = scaz + 180.0;
    cosemi = cos(emi*L_DEG2RAD);

/******************************************************************************
* Obtain parameters needed for spherical datum
******************************************************************************/
    if (C_DATUM2->datumtyp == 2) {
      spi_lbplan(fid,"QUBE","","IMAGE_MAP_PROJECTION",planet,
	sizeof(planet),radii,&lpos,&ret);
      if (ret) goto spi_lbplan_error;
      if (radii[1] == 0.0) radii[1] = radii[0];
      if (radii[2] == 0.0) radii[2] = radii[0];
      radius = sqrt((radii[0]*radii[0]+radii[1]*radii[1]+
		 radii[2]*radii[2])/3.0);
      C_DATUM1->r0 = radius/res;  /* Planet radius in pixels */

/******************************************************************************
* Obtain planet center in full frame and convert to subframe. xs0 and yl0 are
* with respect to the pixels of the subcube; xs0f and yl0f are computed by
* geomset at each grid resolution and refer to coordinates equal to indices
* in the DEM.
******************************************************************************/
      spi_findlin(fid,(FLOAT4) ssclat,(FLOAT4) ssclon,
        &C_DATUM1->xs0,&C_DATUM1->yl0,&ret);
      if (ret) goto spi_findlin_error;
      C_DATUM1->xs0=(C_DATUM1->xs0 - (FLOAT4) issi)/xsinci
	+ 1.0;
      C_DATUM1->yl0=(C_DATUM1->yl0 - (FLOAT4) isli)/xlinci
	+ 1.0;

/******************************************************************************
* Must calculate z00s for sphere, putting middle of image at height 0
******************************************************************************/
      C_DATUM1->z00s = -sqrt(C_DATUM1->r0*C_DATUM1->r0 - 
	pow((xcenter-C_DATUM1->xs0)*C_AEPAR1->aspect,2.0) -
	pow((ycenter-C_DATUM1->yl0),2.0));
    }

/******************************************************************************
* All 3 levels Level 1 keyword groups were found. This is a levels file,
* either Level 1 or Level 2 with the Level 1 groups included.
******************************************************************************/
  } else if (levgroups == 3) {
    levels = TRUE;
    spi_level = lev_find_level(fid);
    if (spi_level == 2) {
      if (lev2_init_from_labels(fid,&both.map)) goto lev_init_error;
      if (lev1_init(fid,&both.spi,0)) goto lev_init_error;
    } else {
      if (lev1_init(fid,&both.spi,0)) goto lev_init_error;
    }
/*      if (lev_init(fid,&both)) goto lev_init_error;*/

/******************************************************************************
* If input file is level 2 (map projected), then get the center latitude
* and center longitude.
******************************************************************************/
    if (spi_level > 1 && strcmp(both.map.proj.name,"ORTHOGRAPHIC") == 0) {
      (void) p_check_key(fid,"QUBE","IMAGE_MAP_PROJECTION","CENTER_LATITUDE",
        &icount,&ret);
      if (ret) goto clat_error;
      icount = 1;
      p_get_real_key(fid,"QUBE","IMAGE_MAP_PROJECTION","CENTER_LATITUDE",
        2,&icount,&clat,&ret);
      if (ret) goto clat_error;
      (void) p_check_key(fid,"QUBE","IMAGE_MAP_PROJECTION","CENTER_LONGITUDE",
        &icount,&ret);
      if (ret) goto clon_error;
      icount = 1;
      p_get_real_key(fid,"QUBE","IMAGE_MAP_PROJECTION","CENTER_LONGITUDE",
        2,&icount,&clon,&ret);
      if (ret) goto clon_error;
    }

/******************************************************************************
* This is a levels Level 1 image. 
******************************************************************************/
    if (spi_level == 1) {
      level = 1;
      data.samp = x;
      data.line = y;
      if (lev1_linesamp_to_latlon(&both.spi,&data)) goto ls_to_latlon;

/******************************************************************************
* Return emission, incidence, phase as emi, inc, pha respectively. These
* angles are evaluated at location x,y relative to the full image cube.
******************************************************************************/
      lev1_calc_emission_angle(&data,&emi);
      lev1_calc_incidence_angle(&data,&inc);
      lev1_calc_phase_angle(&data,&pha);

/******************************************************************************
* Check spacecraft/instrument to determine if it's a scanner or a
* framing camera .
******************************************************************************/
      found = -1;
      for (icount=0; icount<NUMOFSCAN; icount++) {
        if (strcmp(both.spi.inst.spacecraft,scan_sc[icount]) == 0
	    && strcmp(both.spi.inst.name,scan_inst[icount]) == 0) {
          frame = FALSE;
	  found = icount;
        }
      }
      if (found == -1) {
	for (icount=0; icount<NUMOFFRAME; icount++) {
	  if (strcmp(both.spi.inst.spacecraft,frame_sc[icount]) == 0
	      && strcmp(both.spi.inst.name,frame_inst[icount]) == 0) {
            frame = TRUE;
	    found = icount;
          }
	}
      }
      /* Invalid instrument */
      if (found == -1) goto unknown_inst;
	
/******************************************************************************
* This is a scanner. Spherical datum model does not make sense.
******************************************************************************/
      if (frame == FALSE) {

/******************************************************************************
* Return sample resolution, line resolution, spacecraft azimuth, sun azimuth 
* as sampres, lineres, scaz, sunaz respectively. All of these values are
* evaluated at location x,y relative to the full image cube.
******************************************************************************/
        if (lev1_calc_resolution(&both.spi,&data,&sampres,&lineres)) 
          goto res_error;
        if (sampres > lineres) azres = sampres;
        else azres = lineres;
        lev1_calc_spacecraft_azimuth(&both.spi,&data,azres,&scaz);
        lev1_calc_sun_azimuth(&both.spi,&data,azres,&sunaz);

/******************************************************************************
* Now assign emi, inc, pha, scaz, sunaz to variables used below as
* appropriate for the scanner case:  Z axis is local vertical, so
* emission angle is used to calculate emission vector rather than datum
* derivatives. Also calculate the scaling variable, cosemi, to be used 
* for line-of-sight to vertical on output and vertical to line-of-sight on
* input (cosemi=1.0 for scanners).
******************************************************************************/
        res = lineres*xlinci;
	sres = sampres*xsinci;
	C_AEPAR1->aspect = sampres/lineres;
	C_PSNPAR->phase[0] = pha;
	clinc = inc;
	azinc = sunaz;
	clemi = emi;
	azemi = scaz;
	dip = 0.0;
	az = 0.0;
	C_DATUM2->datumtyp = 1;
	cosemi = 1.0;

/******************************************************************************
* This is a framing camera. 
******************************************************************************/
      } else {
        lev1_calc_slant_distance(&data,&slantrange);

/******************************************************************************
* Return resolution, spacecraft azimuth, sun azimuth as res[index],
* scaz, sunaz respectively. All of these values are evaluated at
* location x,y relative to the full image cube.
******************************************************************************/
        res = (slantrange/frame_flinpix[found])*xlinci;
        sres = res;
        azres = res;
        lev1_calc_spacecraft_azimuth(&both.spi,&data,azres,&scaz);
        lev1_calc_sun_azimuth(&both.spi,&data,azres,&sunaz);

/******************************************************************************
* Now assign emi, inc, pha, scaz, sunaz to variables used below as
* appropriate for the framing camera case:  Z axis is viewing direction, so
* emission angle is used to calculate datum derivatives rather than 
* emission vector. Also calculate the scaling variable, cosemi, to be used 
* for line-of-sight to vertical on output and vertical to line-of-sight on
* input.
******************************************************************************/
        C_AEPAR1->aspect = 1.0;
	C_PSNPAR->phase[0] = pha;
	clinc = pha;
	azinc = sunaz;
	clemi = 0.0;
	azemi = 0.0;
	dip = emi;
	az = scaz + 180.0;
        cosemi = cos(emi*L_DEG2RAD);

/******************************************************************************
* Obtain parameters needed for spherical datum
******************************************************************************/
	if (C_DATUM2->datumtyp == 2) {
          radii[0] = both.spi.target.radii[0];
	  radii[1] = both.spi.target.radii[1];
	  radii[2] = both.spi.target.radii[2];
          if (radii[1] == 0.0) radii[1] = radii[0];
          if (radii[2] == 0.0) radii[2] = radii[0];
          radius = sqrt((radii[0]*radii[0]+radii[1]*radii[1]+
		   radii[2]*radii[2])/3.0);
          C_DATUM1->r0 = radius/res;  /* Planet radius in pixels */
	  lev1_calc_subspace(&both.spi,&data,&tmpdata);
	  ssclat = tmpdata.lat;
	  ssclon = tmpdata.lon;
	  data.lat = ssclat;
	  data.lon = ssclon;
	  if (lev1_latlon_to_linesamp(&both.spi,&data)) goto latlon_to_ls;
          C_DATUM1->xs0 = data.samp;
          C_DATUM1->yl0 = data.line;

/******************************************************************************
* We have planet center in terms of full image cube, so convert to 
* subcube.
******************************************************************************/
          C_DATUM1->xs0=(C_DATUM1->xs0 - (FLOAT4) issi)/xsinci
	    + 1.0;
          C_DATUM1->yl0=(C_DATUM1->yl0 - (FLOAT4) isli)/xlinci
	    + 1.0;

/******************************************************************************
* Must calculate z00s for sphere, putting middle of image at height 0
******************************************************************************/
          C_DATUM1->z00s = -sqrt(C_DATUM1->r0*C_DATUM1->r0 - 
	    pow((xcenter-C_DATUM1->xs0)*C_AEPAR1->aspect,2.0) -
	    pow((ycenter-C_DATUM1->yl0),2.0));
        }
      }

/******************************************************************************
* This is a levels Level 2 image. Having two Level 2 files is permitted
* in software. The two image subcubes are the same size and they have
* the same projection parameters. It doesn't matter if the scalar datum
* variables are set twice from labels for both images; the results are
* guaranteed to be the same.
******************************************************************************/
    } else {
      level = 2;

/******************************************************************************
* Next, check the projection. Orthographic projection allows a set of
* images to be treated like one image. The coverage could be highly
* localized (setting projection center to subspacecraft point minimizes
* parallax) or hemispheric in scale. Spherical datum type is always used
* in this projection, with center at center of projection. Coordinate
* system has Z axis out of planet at center of projection. This point is
* known by lat and lon. First find its Level 1 coordinates so we can find
* ema, inc, phase here. NOTE that azimuths are returned but they are with
* respect to Level 1 space so they don't get used.
******************************************************************************/
      if (strcmp(both.map.proj.name,"ORTHOGRAPHIC") == 0) {
	data.lat = (FLOAT8) clat;
	data.lon = (FLOAT8) clon;
	if (lev1_latlon_to_linesamp(&both.spi,&data)) 
	  goto latlon_to_ls;
        lev1_calc_emission_angle(&data,&emi);
        lev1_calc_incidence_angle(&data,&inc);
        lev1_calc_phase_angle(&data,&pha);

/******************************************************************************
* Now find the Level 2 coordinates of the center so we can find the
* azimuths. This routine gives line and sample of a given lat,lon in
* terms of the full image. Will later calculate position relative to
* subcube.
******************************************************************************/
        if (lev2_latlon_to_linesamp(&both.map,&data))
	  goto latlon_to_ls;
        C_DATUM1->xs0 = data.samp;
        C_DATUM1->yl0 = data.line;

/******************************************************************************
* Find azimuth to sun and spacecraft in Level 2.
******************************************************************************/
        /* Convert mapscale to units of resolution */
        res = both.map.proj.kmperpix * xlinci;
	sres = res;
        azres = res;
	tmpdata = data;
        lev2_calc_spacecraft_azimuth(&both.spi,&data,
	  &both.map,&tmpdata,azres,&scaz);
	tmpdata = data;
        lev2_calc_sun_azimuth(&both.spi,&data,
	  &both.map,&tmpdata,azres,&sunaz);

/******************************************************************************
* Now assign emi, inc, pha, scaz, sunaz to variables used below as
* appropriate for the map projected case:  Z axis is the local vertical,
* so emission angle is used to calculate emission vector rather than datum
* derivatives. Also calculate the scaling variable, cosemi, to be used 
* for line-of-sight to vertical on output and vertical to line-of-sight on
* input (cosemi=1.0 for map-projected images).
******************************************************************************/
        C_AEPAR1->aspect = 1.0;
	C_PSNPAR->phase[0] = pha;
	clinc = inc;
	azinc = sunaz;
	clemi = emi;
	azemi = scaz;
	C_DATUM2->datumtyp = 2;
	cosemi = 1.0;

/******************************************************************************
* Obtain parameters needed for spherical datum
******************************************************************************/
        radii[0] = both.spi.target.radii[0];
	radii[1] = both.spi.target.radii[1];
	radii[2] = both.spi.target.radii[2];
        if (radii[1] == 0.0) radii[1] = radii[0];
        if (radii[2] == 0.0) radii[2] = radii[0];
        radius = sqrt((radii[0]*radii[0]+radii[1]*radii[1]+
	  radii[2]*radii[2])/3.0);
        C_DATUM1->r0 = radius/res;  /* Planet radius in pixels */

/******************************************************************************
* Call above gave planet center in full image cube, so convert to subcube
******************************************************************************/
        C_DATUM1->xs0=(C_DATUM1->xs0 - (FLOAT4) issi)/xsinci
	  + 1.0;
        C_DATUM1->yl0=(C_DATUM1->yl0 - (FLOAT4) isli)/xlinci
	  + 1.0;

/******************************************************************************
* Must calculate z00s for sphere, putting middle of image at height 0
******************************************************************************/
        C_DATUM1->z00s = -sqrt(C_DATUM1->r0*C_DATUM1->r0 - 
	  pow((xcenter-C_DATUM1->xs0)*C_AEPAR1->aspect,2.0) -
	  pow((ycenter-C_DATUM1->yl0),2.0));

/******************************************************************************
* Finally, calculate dip and azimuth of dip for sphere at reference
* point. Do this by calling datum to get the elevation differences across
* the pixel near the reference point (converted from full image cube to 
* subcube coordinates.
******************************************************************************/
        (void) geomset(1.0); /* Setup needed before datum */
	i = (INT4) ((x - (FLOAT4) issi)/
	  xsinci + .5) + 1;
	j = (INT4) ((y - (FLOAT4) isli)/
	  xlinci + .5) + 1;
        (void) datum(i,j,4,&z01,&z02,&z03,&z04);
	dzxm = 0.5*(-z01+z02+z03-z04);
	dzym = 0.5*(-z01-z02+z03+z04);
        dip = atan(sqrt((dzxm/C_AEPAR1->aspect)*(dzxm/C_AEPAR1->aspect)+
	  dzym*dzym))*L_RAD2DEG;
	az = atan2(dzym*C_AEPAR1->aspect*C_AEPAR1->aspect,dzxm)*L_RAD2DEG;

/******************************************************************************
* Non-orthographic projections can be used to rectify scanner images.
* Intent is to represent a small region only, so datum type is planar and
* in fact has zero dip, similar to handling of Level 1 scanner images. No
* error checking is done to see if the projection has severe skew or other
* distortions that would make the calculation nonsensical.
*
* User input or default reference point location is in Level 2 space, so
* convert to Level 1 space using lat,lon coordinates.
******************************************************************************/
      } else {
        data.samp = x;
        data.line = y;
        if (lev2_linesamp_to_latlon(&both.map,&data)) goto ls_to_latlon;
        if (lev2_latlon_to_linesamp(&both.map,&data)) goto latlon_to_ls;
	tmpdata = data;
	if (lev1_latlon_to_linesamp(&both.spi,&data)) 
	  goto latlon_to_ls;
        else if (lev1_linesamp_to_latlon(&both.spi,&data)) 
	  goto ls_to_latlon;

/******************************************************************************
* Return emission, incidence, phase as emi, inc, pha respectively. These
* angles are evaluated at a location relative to the whole image.
******************************************************************************/
        lev1_calc_emission_angle(&data,&emi);
        lev1_calc_incidence_angle(&data,&inc);
        lev1_calc_phase_angle(&data,&pha);

/******************************************************************************
* Get ground resolutions of map at the reference point.
******************************************************************************/
        if (lev2_calc_map_resolution(&both.spi,&data,
	    &both.map,&tmpdata,&sampres,&lineres)) 
	  goto res_error;

/******************************************************************************
* Now, find azimuth to sun and spacecraft in Level 2. 
******************************************************************************/
        res = lineres*xlinci;
	sres = sampres*xsinci;
        azres = res;
        lev2_calc_spacecraft_azimuth(&both.spi,&data,
	  &both.map,&tmpdata,azres,&scaz);
        lev2_calc_sun_azimuth(&both.spi,&data,
	  &both.map,&tmpdata,azres,&sunaz);

/******************************************************************************
* Now assign emi, inc, pha, scaz, sunaz to variables used below as
* appropriate for the map projected case:  Z axis is the local vertical,
* so emission angle is used to calculate emission vector rather than datum
* derivatives. Also calculate the scaling variable, cosemi, to be used 
* for line-of-sight to vertical on output and vertical to line-of-sight on
* input (cosemi=1.0 for map-projected images).
******************************************************************************/
        C_AEPAR1->aspect = sampres/lineres;
	C_PSNPAR->phase[0] = pha;
	clinc = inc;
	azinc = sunaz;
	clemi = emi;
	azemi = scaz;
	dip = 0.0;
	az = 0.0;
	C_DATUM2->datumtyp = 1;
	cosemi = 1.0;
      }
    }

/******************************************************************************
* Only some of the needed levels Level 1 keyword groups found. Image is
* invalid.
******************************************************************************/
  } else {
    goto labels_not_complete;
  }

/******************************************************************************
* Now proceed to calculate stored values with label information. Still
* need to calculate derivatives of the plane datum.
******************************************************************************/
  dzdip = tan(dip*L_DEG2RAD)/sqrt(pow(cos(az*L_DEG2RAD)*C_AEPAR1->aspect,2.0)
    +pow(sin(az*L_DEG2RAD),2.0));
  C_DATUM1->dzx0 = -cos(az*L_DEG2RAD)*dzdip*C_AEPAR1->aspect*C_AEPAR1->aspect;
  C_DATUM1->dzy0 = -sin(az*L_DEG2RAD)*dzdip;
  C_DATUM1->z00 = -(C_DATUM1->dzx0*xcenter+C_DATUM1->dzy0*ycenter);
  C_DATUM1->dz10 = -(C_DATUM1->dzx0+C_DATUM1->dzy0);
  C_DATUM1->dz20 = -(C_DATUM1->dzx0-C_DATUM1->dzy0);
  (void) geomset(1.0);
  scale = 1000.0*res;

  C_PGEOM->ci[0] = cos(clinc*L_DEG2RAD)*SQ2;
  si0 = sin(clinc*L_DEG2RAD)/
    sqrt(pow(cos(azinc*L_DEG2RAD)*C_AEPAR1->aspect,2.0)
    +pow(sin(azinc*L_DEG2RAD),2.0));
  C_PGEOM->si1[0] = si0*cos((45.0-azinc)*L_DEG2RAD);
  C_PGEOM->si2[0] = si0*sin((45.0-azinc)*L_DEG2RAD);
  C_PGEOM->ce[0] = cos(clemi*L_DEG2RAD)*SQ2;
  se0 = sin(clemi*L_DEG2RAD)/
    sqrt(pow(cos(azemi*L_DEG2RAD)*C_AEPAR1->aspect,2.0)
    +pow(sin(azemi*L_DEG2RAD),2.0));
  C_PGEOM->se1[0] = se0*cos((45.0-azemi)*L_DEG2RAD);
  C_PGEOM->se2[0] = se0*sin((45.0-azemi)*L_DEG2RAD);

/******************************************************************************
* Calculations above are purely GEOMETRIC; those below are PHOTOMETRIC,
* i.e., depending on surface properties and description thereof.
******************************************************************************/
  if (C_PPARS3->hapke) {
    if (strncmp(C_PPARS4->phofunc,"HAPH_S",6) == 0) {
      C_PPARS2->falpha[0] = (1.0-C_PPARS5->phg2)*
	(1.0-C_PPARS5->phg1*C_PPARS5->phg1)/
        (pow(1.0+C_PPARS5->phg1*C_PPARS5->phg1+2.0*
	C_PPARS5->phg1*cos(C_PSNPAR->phase[0]*L_DEG2RAD),1.5))+
	C_PPARS5->phg2*(1.0-C_PPARS5->phg1*C_PPARS5->phg1)/
	(pow(1.0+C_PPARS5->phg1*C_PPARS5->phg1-2.0*C_PPARS5->phg1*
        cos(C_PSNPAR->phase[0]*L_DEG2RAD),1.5))-1.0;
    } else if (strncmp(C_PPARS4->phofunc,"HAPL_S",6) == 0) {
      C_PPARS2->falpha[0] = 1.0+C_PPARS5->pbh*cos(C_PSNPAR->phase[0]*
	L_DEG2RAD)+C_PPARS5->pch*(1.5*cos(C_PSNPAR->phase[0]*L_DEG2RAD)+
	0.5);
    }
    C_PPARS2->twogam = 2.0*sqrt(1.0-C_PPARS5->pwh);
    if (strncmp(C_PPARS4->phofunc,"HAPLEG",6) == 0 ||
        strncmp(C_PPARS4->phofunc,"HAPHEN",6) == 0) {
      mulpht = 62;
      if (strncmp(C_PPARS4->phofunc,"HAPHEN",6) == 0) 
	mulpht = mulpht + 23;
      if (C_PPARS5->phh != 0.0) mulpht = mulpht + 3;
      if (C_PPARS5->ptheta != 0.0) mulpht = mulpht + 127;
      C_PPARS1->mulps = (16+mulpht)*2;
      C_PPARS1->mulpsp = (42+mulpht*4)*2;
    } else {
      C_PPARS1->mulps = 15;
      C_PPARS1->mulpsp = 15;
    }
  } else {
    C_PPARS2->pex1 = C_PPARS2->pex-1.0;
    C_PPARS2->pl2 = C_PPARS2->pl2+C_PPARS2->pl2;
    C_PPARS2->pl1 = 1.0-.5*C_PPARS2->pl2;
    if (C_PPARS2->pl2 == 0.0) {
      if (C_PPARS2->pex == 1.0) {
        C_PPARS1->mulps = 0;
	C_PPARS1->mulpsp = 0;
      } else {
        C_PPARS1->mulps = 15;
        C_PPARS1->mulpsp = 4;
      }
    } else if (C_PPARS2->pl2 == 2.0) {
      C_PPARS1->mulps = 3;
      C_PPARS1->mulpsp = 4;
    } else {
      if (C_PPARS2->pex == 1.0) {
        C_PPARS1->mulps = 5;
	C_PPARS1->mulpsp = 7;
      } else {
        C_PPARS1->mulps = 21;
	C_PPARS1->mulpsp = 14;
      }
    }
  }

/******************************************************************************
* End of material-dependent code. Find photometric function
* normalization, etc. (AEPARs)
******************************************************************************/
  bclipin = 1.0e30;
  C_AEPAR1->bmax = 1.0e30;
  i = (INT4) (xcenter+.5);
  j = (INT4) (ycenter+.5);
  (void) pbder(i,j,0.0,0.0,&C_AEPAR1->bnorm,&db1,&db2,1);
  (void) findbmax();
  if (C_AEPAR1->bmax < C_AEPAR1->bnorm*bclipin) C_AEPAR1->bclip = C_AEPAR1->bmax;
  else C_AEPAR1->bclip = C_AEPAR1->bnorm*bclipin;
  C_AEPAR2->flop = FALSE;
  C_AEPAR2->logimg = FALSE;

/******************************************************************************
* Find azimuth of characteristic strips. This is calculated in terms of
* true Cartesian azimuth, whereas the other azimuths are those on the
* pixel grid, which may not have a unit aspect ratio. We will output the
* azimuth of characteristics transformed to a grid azimuth. Starting point
* for the search is the (numerator) sun azimuth, transformed to Cartesian.
******************************************************************************/
  azi = atan2(sin(azinc*L_DEG2RAD)/C_AEPAR1->aspect,cos(azinc*L_DEG2RAD))*
    L_RAD2DEG;
  if (azi >= 0) icount = (INT4) (azi/90.0+.5);
  else icount = (INT4) (azi/90.0-.5);
  icard = icount-4*(icount/4);
  cardaz = 90.0*((FLOAT4) icard);
  delaza = cardaz-azi-45.0;
  delazb = cardaz-azi+45.0;
  p = ddberr;
  C_AEPAR1->delaz = zbrent(p,delaza,delazb,1.0e-6);
  if (ins > inl) arg = ins;
  else arg = inl;
  if (fabs(sin(C_AEPAR1->delaz*L_DEG2RAD)*cos(C_AEPAR1->delaz*L_DEG2RAD))*
      arg <= 0.5) C_AEPAR1->delaz = 0.0;
  while (icard < 0) {
    icard = icard+4;
    cardaz = cardaz+360.;
  }
  C_AEPAR1->charaz = cardaz-C_AEPAR1->delaz;
  if (C_AEPAR1->dbrat*(0.5-(icard%2)) < 0.0) C_AEPAR1->charaz = 
                                             C_AEPAR1->charaz+90.;
  C_AEPAR1->charaz = C_AEPAR1->charaz-360.0*((INT4) (C_AEPAR1->charaz/360.0));
  if (C_AEPAR1->charaz > 180.) C_AEPAR1->charaz = C_AEPAR1->charaz-360.;
  if (C_AEPAR1->charaz >= 0.) arg = 180.;
  else arg = -180.;
  if (fabs(C_AEPAR1->charaz) > 90.) C_AEPAR1->charaz = C_AEPAR1->charaz-arg;

/******************************************************************************
* We use Cartesian charaz but print grid-relative charazgrid for the
* user. No longer need to use ioct in main program to control geometric
* transformation of images on input. Instead, set ioct1=1 and use it in
* calls to pblinein1, etc. By setting the octant of the characteristics
* ioct here and using it to control the SOR sweep direction, we get
* optimal results for any charaz. ioct is passed via common.
******************************************************************************/
  charazgrid = atan2(sin(C_AEPAR1->charaz*L_DEG2RAD)*C_AEPAR1->aspect,
               cos(C_AEPAR1->charaz*L_DEG2RAD))*L_RAD2DEG;
  if ((-90.0 <= C_AEPAR1->charaz) && (C_AEPAR1->charaz < -45.0)) 
    C_AEPAR3->ioct = -2;
  else if ((-45.0 <= C_AEPAR1->charaz) && (C_AEPAR1->charaz < 0.0)) 
    C_AEPAR3->ioct = -1;
  else if ((45.0 < C_AEPAR1->charaz) && (C_AEPAR1->charaz <= 90.0))
    C_AEPAR3->ioct = 2;
  else
    C_AEPAR3->ioct = 1;

  sprintf(C_MSG->pcpmsg,"X= %f",x);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Y= %f",y);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"RADIUS= %f",radius);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Dip of datum= %f",dip);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Azimuth of dip= %f",az);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Azimuth of characteristics= %f",charazgrid);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Resolution (km/pix) = %f",sres);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Aspect = %f",1.0/C_AEPAR1->aspect);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Emission angle = %f",emi);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Incidence angle = %f",inc);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Phase angle = %f",pha);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Spacecraft azimuth = %f",scaz);
  u_write_msg(3,C_MSG->pcpmsg);
  sprintf(C_MSG->pcpmsg,"Sun azimuth = %f",sunaz);
  u_write_msg(3,C_MSG->pcpmsg);

/******************************************************************************
* Finish getting TAE parameters
******************************************************************************/
  (void) pcsi_ipars(&nsmooth);

  (void) write2log(1,ins,inl,note,lnote,from,clinc,
                   azinc,clemi,azemi,sres,
		   dip,az,charazgrid,radius,dndatum,
		   1.0/C_AEPAR1->aspect);

  if (PcsiFirstGuess()) goto first_guess_error;

  (void) write2log(2,ins,inl,note,lnote,from,clinc,
                   azinc,clemi,azemi,sres,
		   dip,az,charazgrid,radius,dndatum,
		   1.0/C_AEPAR1->aspect);

  return(0);

/*****************************************************
  Error section
*****************************************************/
  user_parameter_error:
    sprintf(C_MSG->pcpmsg,"Error retrieving TAE parameter %s",prmname);
    u_error("INITPCSI-TAEERR",C_MSG->pcpmsg,-1,1);
    return(-1);

  spi_phosun_error:
    sprintf(C_MSG->pcpmsg,
      "Unable to get spacecraft and sun position information for %s",from);
    u_error("INITPCSI-SPIPHOSUN",C_MSG->pcpmsg,-2,1);
    return(-2);

  spi_findlin_error:
    sprintf(C_MSG->pcpmsg,"Unable to convert lat,lon to line,samp for %s",
            from);
    u_error("INITPCSI-SPIFINDLIN",C_MSG->pcpmsg,-3,1);
    return(-3);

  spi_lbplan_error:
    sprintf(C_MSG->pcpmsg,"Unable to read planet radii and lpos from %s",
            from);
    u_error("INITPCSI-SPILBP",C_MSG->pcpmsg,-4,1);
    return(-4);

  first_guess_error:
    sprintf(C_MSG->pcpmsg,"Unable to generate an initial solution for topography");
    u_error("INITPCSI-SOLNERR",C_MSG->pcpmsg,-5,1);
    return(-5);
  
  lev_init_error:
    sprintf(C_MSG->pcpmsg,"Unable to initialize input file %s",from);
    u_error("INITPCSI-LEVINIT",C_MSG->pcpmsg,-6,1);
    return(-6);

  ls_to_latlon:
    sprintf(C_MSG->pcpmsg,"Unable to convert line,sample to lat,lon for %s",
            from);
    u_error("INITPCSI-LSTOLATLON",C_MSG->pcpmsg,-7,1);
    return(-7);

  latlon_to_ls:
    sprintf(C_MSG->pcpmsg,"Unable to convert lat,lon to line,sample for %s",
            from);
    u_error("INITPCSI-LATLONTOLS",C_MSG->pcpmsg,-8,1);
    return(-8);

  res_error:
    sprintf(C_MSG->pcpmsg,
    "Unable to determine line,sample resolution for %s",from);
    u_error("INITPCSI-LSRES",C_MSG->pcpmsg,-9,1);
    return(-9);

  bad_subsamp:
    sprintf(C_MSG->pcpmsg,"Error getting sub-sample information for %s",
            from);
    u_error("INITPCSI-SUBSAMP",C_MSG->pcpmsg,-10,1);
    return(-10);

  bad_subline:
    sprintf(C_MSG->pcpmsg,"Error getting sub-line information for %s",
            from);
    u_error("INITPCSI-SUBLINE",C_MSG->pcpmsg,-11,1);
    return(-11);

  inc_error:
    sprintf(C_MSG->pcpmsg,"Line and sample increment of file %s are not equal",
            from);
    u_error("INITPCSI-LINCSINC",C_MSG->pcpmsg,-12,1);
    return(-12);

  mosaic_group_error:
    sprintf(C_MSG->pcpmsg,
    "Mosaicked images must have all of groups: ISIS_INSTRUMENT, ISIS_GEOMETRY, ISIS_TARGET");
    u_error("INITPCSI-MOSGRP",C_MSG->pcpmsg,-13,1);
    return(-13);

  level2_group_error:
    sprintf(C_MSG->pcpmsg,
    "Level 2 images must have all of groups: ISIS_INSTRUMENT, ISIS_GEOMETRY, ISIS_TARGET");
    u_error("INITPCSI-LEV2GRP",C_MSG->pcpmsg,-14,1);
    return(-14);

  unknown_inst:
    sprintf(C_MSG->pcpmsg,
    "Unknown spacecraft/instrument found in %s",from);
    u_error("INITPCSI-BILEV1",C_MSG->pcpmsg,-15,1);
    return(-15);

  labels_not_complete:
    sprintf(C_MSG->pcpmsg,
    "Necessary Level 1 keyword groups not found in %s",from);
    u_error("INITPCSI-INCLAB",C_MSG->pcpmsg,-16,1);
    return(-16);

  haphen_error:
    sprintf(C_MSG->pcpmsg,
    "The following values must be provided for function HAPHEN: wh,theta,b0,hh,hg1,hg2");
    u_error("INITPCSI-HAPHEN",C_MSG->pcpmsg,-17,1);
    return(-17);

  hapleg_error:
    sprintf(C_MSG->pcpmsg,
    "The following values must be provided for function HAPLEG: wh,theta,b0,hh,bh,ch");
    u_error("INITPCSI-HAPLEG",C_MSG->pcpmsg,-18,1);
    return(-18);

  haph_s_error:
    sprintf(C_MSG->pcpmsg,
    "The following values must be provided for function HAPH_S: wh,hg1,hg2");
    u_error("INITPCSI-HAPH_S",C_MSG->pcpmsg,-19,1);
    return(-19);

  hapl_s_error:
    sprintf(C_MSG->pcpmsg,
    "The following values must be provided for function HAPL_S: wh,bh,ch");
    u_error("INITPCSI-HAPL_S",C_MSG->pcpmsg,-20,1);
    return(-20);

  min_error:
    sprintf(C_MSG->pcpmsg,
    "The following values must be provided for function MIN: k");
    u_error("INITPCSI-MIN",C_MSG->pcpmsg,-21,1);
    return(-21);

  lunlam_error:
    sprintf(C_MSG->pcpmsg,
    "The following values must be provided for function LUNLAM: l");
    u_error("INITPCSI-LUNLAM",C_MSG->pcpmsg,-22,1);
    return(-22);

  zout_remove_error:
    sprintf(C_MSG->pcpmsg,
    "Unable to delete pre-existing ZOUT file");
    u_error("INITPCSI-ZOUTREM",C_MSG->pcpmsg,-23,1);
    return(-23);

  flspec_error:
    sprintf(C_MSG->pcpmsg,
    "The ZOUT filename is invalid");
    u_error("INITPCSI-FLSPEC",C_MSG->pcpmsg,-24,1);
    return(-24);

  to_file_error:
    sprintf(C_MSG->pcpmsg,
    "TO file was not specified by user");
    u_error("INITPCSI-TOERR",C_MSG->pcpmsg,-25,1);
    return(-25);

  to_file_exist:
    sprintf(C_MSG->pcpmsg,
    "TO file already exists");
    u_error("INITPCSI-TOEXIST",C_MSG->pcpmsg,-26,1);
    return(-26);

  to_file_invalid:
    sprintf(C_MSG->pcpmsg,
    "TO file name invalid");
    u_error("INITPCSI-TOINV",C_MSG->pcpmsg,-27,1);
    return(-27);

  cube_open_error:
    sprintf(C_MSG->pcpmsg,"Error opening input cube file %s",from);
    u_error("INITPCSI-OPENERR",C_MSG->pcpmsg,-28,1);
    return(-28);

  invalid_cube:
    sprintf(C_MSG->pcpmsg,"%s is not a standard cube file",from);
    u_error("INITPCSI-INVCUBE",C_MSG->pcpmsg,-29,1);
    return(-29); 

  bad_sys_keys:
    sprintf(C_MSG->pcpmsg,"Error obtaining system keywords from %s",from);
    u_error("INITPCSI-SYSKEYS",C_MSG->pcpmsg,-30,1);
    return(-30);

  parse_sfrom_error:
    sprintf(C_MSG->pcpmsg,"Unable to parse SFROM parameter %s for %s",sfrom,from);
    u_error("INITPCSI-PRSSFROM",C_MSG->pcpmsg,-31,1);
    return(-31); 

  sfrom_error:
    sprintf(C_MSG->pcpmsg,"Unable to apply SFROM %s to input file %s",sfrom,from);
    u_error("INITPCSI-SFROMERR",C_MSG->pcpmsg,-32,1);
    return(-32); 

  clat_error:
    sprintf(C_MSG->pcpmsg,"Unable to get clat from labels of %s",from);
    u_error("INITPCSI-CLAT",C_MSG->pcpmsg,-33,1);
    return(-33); 

  clon_error:
    sprintf(C_MSG->pcpmsg,"Unable to get clon from labels of %s",from);
    u_error("INITPCSI-CLON",C_MSG->pcpmsg,-34,1);
    return(-34); 
}
示例#16
0
void aspen_breakout()
{
  int i,j,k,i1;
  double x1,x2,x3,x4,x5,x6,r,dr,fsat,m1,mcut,mmin,dlogm;
  char fname[100];
  FILE *fp;

  //goto loop_m1;
  //  goto loop_cvir;
  //goto loop_alpha;
  //goto loop_mcut2;

  /* Vary M_min, find M1 such that fsat is fixed.
   */
  OUTPUT=3;
  HOD.alpha = 1.0;
  for(i=1;i<=5;++i)
    {
      muh(0);
      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;

      sprintf(Task.root_filename,"fsat.%d",i);

      HOD.M_min = pow(10.0,i+9.0);
      HOD.M_cut = 4*HOD.M_min;
      if(i==1) {
	HOD.M1 = 30*HOD.M_min;
	set_HOD_params();
	fsat = fsat_g1 = qromo(func_satfrac,log(HOD.M_min),log(HOD.M_max),midpnt)/GALAXY_DENSITY;
	fprintf(stderr,"%e %e %e %f %f\n",HOD.M_min,HOD.M1,GALAXY_DENSITY,fsat,HOD.M1/HOD.M_min);
	muh(2);
      }
      set_HOD_params();
      HOD.M1 = exp(zbrent(func_find_mone2,log(HOD.M_low/10),log(HOD.M_max),1.0E-4));
      fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;
      
      fprintf(stdout,"MUH %e %e %e %f %f\n",HOD.M_min,HOD.M1,GALAXY_DENSITY,fsat,HOD.M1/HOD.M_min);

      sprintf(fname,"fsat_1h.%d",i);
      output_wp(fname);
      sprintf(fname,"fsat_wp.%d",i);
      output_wp(fname);
      sprintf(fname,"fsat_hod.%d",i);
      output_hod(fname);

    }
  exit(0);

  /* Vary sigma8 at fixed HOD/ngal (so change mmin)
   */
  for(i=0;i<=3;++i)
    {
      SIGMA_8 = 0.9*growthfactor((double)i);
      fprintf(stderr,"z=%d, sigma8= %f\n",i,SIGMA_8);
      RESET_COSMOLOGY++;
      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;
      //HOD.M_min = 0;
      set_HOD_params();
      
      sprintf(fname,"SIGMA8a.%d",i);
      output_wp(fname);
    }
  exit(0);
  SIGMA_8=0.9;
  RESET_COSMOLOGY++;

 loop_m1:

  /* Vary fsat by varying M1
   */

  m1 = HOD.M1;
  i1 = 0;
  for(i=-2;i<=2;++i)
    {
      HOD.M1 = m1*pow(10.0,i/10.0);
      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;
      HOD.M_min = HOD.M_low = 0;
      set_HOD_params();
      
      fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;
      fprintf(stderr,"M1= %e fsat=%f\n",HOD.M1,fsat);
      
      sprintf(fname,"M1_wp.%d",++i1);
      output_wp(fname);
      sprintf(fname,"M1_hod.%d",i1);
      output_hod(fname);
    }
  HOD.M1 = m1;
  exit(0);

 loop_mcut1:

  /* Vary M_cut, fix M1 and fsat
   */
  mcut = HOD.M_cut;
  HOD.M_min = 0;
  set_HOD_params();

  dlogm = (log(HOD.M1) - log(HOD.M_min))/4;
  mmin = log(HOD.M_min);

  fsat_g1 = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;
  i1 = 0;
  for(i=0;i<5;++i)
    {
      HOD.M_cut = exp(dlogm*i+mmin);
      HOD.M1 = exp(zbrent(func_find_mone,log(HOD.M_min),log(HOD.M_max),1.0E-4));
      
      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;
      HOD.M_min = HOD.M_low = 0;
      set_HOD_params();

      fprintf(stderr,"M_cut= %e M1= %e %f\n",HOD.M_cut,HOD.M1,HOD.M1/HOD.M_cut);
      
      sprintf(fname,"Mcut1_wp.%d",++i1);
      output_wp(fname);
      sprintf(fname,"Mcut1_hod.%d",i1);
      output_hod(fname);
    }

 loop_mcut2:

  /* Vary M_cut/fsat, keep M_cut/M1 = 1
   */
  mcut = 3.0e13;
  m1 = 3.0e13;

  i1 = 0;
  dlogm = (log(3.0e13) - log(10.0e12))/4;
  for(i=0;i<5;++i)
    {
      HOD.M_cut = HOD.M1 = exp(dlogm*i)*10.0e12;

      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;
      HOD.M_min = HOD.M_low = 0;
      set_HOD_params();

      fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;
      fprintf(stderr,"M_min= %e M1= %e fsat=%f\n",HOD.M_min,HOD.M1,fsat);
      

      sprintf(fname,"Mcut2_wp.%d",++i1);
      output_wp(fname);
      sprintf(fname,"Mcut2_hod.%d",i1);
      output_hod(fname);
    }

 loop_alpha:
  
  /* Vary Mcut as above, but fix f_sat by varying alpha
   */
  mcut = 3.0e13;
  m1 = 3.0e13;

  i1 = 0;
  mmin = log(6.0e12);
  dlogm = (log(3.0e13) - mmin)/4;
  HOD.M_cut = HOD.M1 = exp(mmin);
  
  HOD.alpha = 0.1;
  HOD.M_min = HOD.M_low = 0;
  set_HOD_params();

  fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY;
  fprintf(stderr,"fsat = %f %e\n",fsat,HOD.M_min);
  for(i=0;i<5;++i)
    {
      HOD.M_cut = HOD.M1 = exp(dlogm*i + mmin);

      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;

      HOD.alpha = zbrent(func_find_alpha,0.05,2.0,1.0E-4);
      fprintf(stderr,"M_cut= %e alpha = %f\n",HOD.M_cut,HOD.alpha);
      

      sprintf(fname,"alpha_wp.%d",++i1);
      output_wp(fname);
      sprintf(fname,"alpha_hod.%d",i1);
      output_hod(fname);
    }
  
 loop_cvir:

  /* Vary cvir with central one above
   */
  HOD.M_cut = HOD.M1 = exp(dlogm*2 + mmin);
  
  HOD.alpha = zbrent(func_find_alpha,0.05,2.0,1.0E-4);
  fprintf(stderr,"M_cut= %e alpha = %f\n",HOD.M_cut,HOD.alpha);

  i1 = 0;
  for(i=0;i<5;++i)
    {
      CVIR_FAC = pow(3.0,i-2);
      RESET_FLAG_1H++;
      RESET_FLAG_2H++;
      RESET_KAISER++;

      sprintf(fname,"cvir_wp.%d",++i1);
      output_wp(fname);
      sprintf(fname,"cvir_hod.%d",i1);
      output_hod(fname);
    }

  exit(0);
}