Exemple #1
0
/****************************************************************************
 *  FUNCTION: main() - local routine                                        *
 ****************************************************************************/
int main(int argc, char *argv[])
    {
    int key_entered, Ch;

    intro();

    /* Set all 16 buffers to 0 */
    for (Ch = 0; Ch < 4; Ch++)
	   OUTPORT(base + (Ch * 2), Cal(0x000, Ch));

    inb(base+0xA);    // set automatic update mode
    inb(base+0XF);    // release zero latch

    do
	{
	CLRSCR();
	write_DAC();
	CPRINTF("\n\nWould you like to output another value (Y or N)?\n");
	key_entered = GETCH();
	} while((key_entered != 'N') && (key_entered != 'n'));

    /* Set all 16 buffers to 0 */
    for (Ch = 0; Ch < 4; Ch++)
	   OUTPORT(base + (Ch * 2), Cal(0x000, Ch));

    CLRSCR();
    PUTS("DA12-16 Sample1 complete.");
    } /* end main */
int main(void)
{
    int nLimit;
    printf("Enter a limit integer: ");
    while (scanf("%d", &nLimit) == 1)
    {
        printf("Up: %g Down: %g\n", Cal(nLimit).fUp, Cal(nLimit).fDown);
        printf("Enter a limit integer: ");
    }

    return 0;
}
Exemple #3
0
 inline Int64 Work(void) {
     f[1] = 0; stack[top = 1] = 1;
     for (Int64 i = 2; i <= n + 2; i++) {
         f[i] = INF;
         Int64 last = 0;
         while (top && h[stack[top]] < h[i]) {
             f[i] = std::min(f[i], f[stack[top]] + Cal(stack[top], i, last));
             last = h[stack[top--]];
         }
         f[i] = std::min(f[i], f[stack[top]] + Cal(stack[top], i, last));
         stack[++top] = i;
     }
     return f[n + 2];
 }
Exemple #4
0
	void solve(void) {
		scanf("%lf%lf%d%d", &k1, &k2, &n, &m);
		int n1 = n - m % n, n2 = m % n;
		int m1 = m / n, m2 = m / n + 1;
		double Ans = inf;
		if (!n2) {
			printf("%.1lf\n", Cal(100, n1, m1));
			return;
		}
		for (int i = n1 * m1; i + n2 * m2 <= 100; i++) {
			double now = Cal(i, n1, m1) + Cal(100 - i, n2, m2);
			Ans = std::min(now, Ans);
		}
		printf("%.1lf\n", Ans);
	}
Exemple #5
0
int main() {
	int u, v, l, sum;
	while(scanf("%s",st) != EOF) {
		Ini();
		sum = 0;
		u = st[0] - 'a';
		l = strlen(st);
		sum += l;
		v = st[l-1] - 'a';
		Link[u][v] = Link[v][u] = l;
		F[u] ++;
		F[v] ++;
		while(1) {
			scanf("%s",st);
			if(!strcmp(st, "deadend"))
				break;
			u = st[0] - 'a';
			l = strlen(st);
			v = st[l-1] - 'a';
			Link[u][v] = Link[v][u] = l;
			sum += l;
			F[u] ++;
			F[v] ++;
		}
		Cal(sum);
		for(int i = 0; i<30; i++) F[i] = 0;

	}
	return 0;
}
Exemple #6
0
int main(void) {
	freopen("in", "r", stdin);
	while (scanf("%d", &n) == 1) {
		Cal(n);
	}
	return 0;
}
void main_routine(){
	// Basic iterations
	if (ITERFLAG == 1){
		for (int iter = 0; iter < ITER_NUM; iter++){
			iterGlobal = iter;
			Cal();
			printf("[INFO] ROUND %02d: CALCULATION DONE\n", iter+1);
			if (ITER_ALGO == 0){
				Norm();
				printf("[INFO] ROUND %02d: NORMALIZATION DONE\n", iter+1);
			}
		}
		matrix_dump();
	} else
		matrix_retrieve();
	
	
	//Compute the matrix of same node pair;
	node_info();

	//The greedy approach of matrix mapping
	matrix_mapping(ITER_NUM, 0);

	//Using the mapped pairs to induce other mapping
	if (PICK_ALGO != 0)
		mapping_induction();
	
}
Exemple #8
0
/****************************************************************************
 *  FUNCTION: write_DAC() - local routine                                   *
 *  PURPOSE: Prompts the user for DAC number and voltage, then calculates  *
 *            the actual output voltage based on resolution, displays it    *
 *            and writes the value to the DAC.                              *
 ****************************************************************************/
void write_DAC(void)
{
float           volt_value_entered,volt_value_expected;
unsigned        counts_entered,dac_number;

/* prompt for the DAC number and desire voltage. */
CPRINTF("Enter the DAC number (0 through 15 only)\n: ");
scanf("%u",&dac_number);
CPRINTF("\n\nEnter a voltage between 0.000v to 9.997v\n: ");
scanf("%f",&volt_value_entered);

/* convert entered voltage to associated number of DAC counts. Scale
   factor for 12 bit resolution of DA12-16 is 0.002442. */
volt_value_entered /= 0.002442;

/* compute the digital output needed for this value and the actual voltage
   that will be expected, and display the voltage. */

counts_entered=(int) volt_value_entered;
volt_value_expected = (float) counts_entered * 0.002442;
CPRINTF("\nDue to the 12-bit resolution of the DAC, you should expect\n");
CPRINTF("to see a voltage of %4.3f.",volt_value_expected);

/* write to the DAC */
OUTPORT(base + (2 * dac_number), Cal(counts_entered, dac_number)); /* write low byte      */

} /* end write_DAC */
Exemple #9
0
	void Work(void) {
		scanf("%ld %ld", &n, &s);
		for (long i = 1; i <= n; i++)
			a[i] = Read(), b[i] = Read(), k[i] = Read();
		f[1] = s;
		Y[1] = Cal(1);
		X[1] = Y[1] * k[1];
		Add(X[1], Y[1]);
		for (long i = 2; i <= n; i++) {
			double v = Ask(a[i], b[i]);
			f[i] = std::max(f[i - 1], v);

			Y[i] = Cal(i);
			X[i] = Y[i] * k[i];
			Add(X[i], Y[i]);
		}
		printf("%.3lf\n", f[n]);
	}
ll Comb(int n, int m, int k) {
	set(dp, 0); v.clear(); int tmp = k;
	for (int i = 2; i * i <= tmp; i++) {
		if (tmp % i == 0) {
			int num = 0;
			while (tmp % i == 0) {
				tmp /= i;
				num++;
			}
			v.pb(i);
		}
	} if (tmp != 1) v.pb(tmp);
	ll ans = Cal(n - m + 1, n, k, 1);
	for (int j = 0; j < v.size(); j++) {
		ans = ans * Pow(v[j], dp[j], k) % k;
	}
	ans = ans * inv(Cal(2, m, k, -1), k) % k;
	return ans;
}
Exemple #11
0
void Cal( int t , int money , int remain)  //t表示当前搜到了第t层,money表示1~(t-1)层已经报销的数额,remain表示t~k的总额数
{
	if( t > k )  //如果当前层超出了ary数组的最大范围k,则到底了
	{
		if( max < money ) max = money;   //如果这次搜索到底的money比以前记录的最大额度max大,则进行替换
		return;
	}
	
	
	//不报销当前这张发票ary[t]
	//如果当前的money与剩下所有发票的总额数remain-art[t]加起来比max大,则说明进去搜索还有得到更优解的可能,否则就不去浪费时间去搜索了,这里其实是一个剪枝
	if( money + remain - ary[t] > max )   
		Cal( t+1 , money , remain-ary[t] );  //去搜索下一层t+1,因为不算当前这张发票,所以总钱数还是money
	//报销当前这张发票ary[t]
	//这里是两个剪枝,已报销的money与当前这张发票ary[t]加起来如果已经超过了报销限额q就不用搜了
	//已报销的money与剩下的所有发票的总额数remain加起来都不如max大,则没有搜索的必要了
	if( money + ary[t] <= q && money + remain > max )
		Cal( t+1 , money + ary[t] , remain-ary[t] );  //去搜索下一层t+1,因为算当前这张发票,所以总钱数是money+ary[t]
}
void JrimmyGyro::Init()
{
	lastUpdate = 0;
	Write(kDLPFRegister, 0x1B);
	Write(kSampleRateDivider, 9);
	Write(kPowerMgmRegister, 1);
	Write(kIntCfg, 1);

	Cal();
}
Exemple #13
0
	void Product(Int64 p, int fac) {
		if (p > B) return;
		cnt++; cur = p;
		l = (long long)ceil((double)A / (double)p), r = (long long)floor((double)B / (double)p);
		Ans += Cal(l, r, p);
		if (fac <= 2) Product(p * 2, 2);
		if (fac <= 3) Product(p * 3, 3);
		if (fac <= 5) Product(p * 5, 5);
		if (fac <= 7) Product(p * 7, 7);
	}
Exemple #14
0
	inline void Work(void) {
		f[1] = r[1]; stack[++top] = 1;
		for (int i = 2; i <= n; i++) {
			long double &tmp = f[i]; tmp = r[i];
			while (top) {
				tmp = std::min(tmp, Cal(stack[top], i));
				if (tmp >= f[stack[top]]) top--; else break;
			}
			stack[++top] = i;
		}
		for (int i = 1; i <= n; i++) printf("%.3Lf\n", f[i]);
	}
Exemple #15
0
void main()
{
	int n,m,i;
	int p,sum;
	double pp;
	char type;
	char flag;
	ary=price;
	int abc[4];
	while (scanf("%lf%d",&pp,&n),n)
	{
		k=0;
		q=(int)(pp*100);
		max=0;
		while(n--)
		{
			scanf("%d",&m);
			flag=1;
			sum=0;
			memset(abc,0,sizeof(abc));
			while (m--)
			{
				scanf(" %c:%lf",&type,&pp);
				p=(int)(pp*100);
				
				
				if ((type>='A')&&(type<='C')&&(p<=60000))
				{
					abc[type-'A']+=p;
					sum+=p;
				}
				else
					flag=0;
			}
			if (sum>100000||abc[0]>60000||abc[1]>60000||abc[2]>60000)
			{
				flag=0;
			}
			if (flag)
				price[++k]=sum;
		}
		int remain=0;
		for (i=1;i<=k;i++)
		{
			remain+=price[i];
		}
		Cal(1,0,remain);
		printf("%.2lf\n",max/100.0);
	}
}
Exemple #16
0
int main() {
    int kase, i;
	
	scanf("%d",&kase);
	while(kase--) {
		scanf("%d",&N);
		for(i = 0; i<N; i++)
			scanf("%lf%lf",&pp[i].x,&pp[i].y);
		Cal();
		if(kase) printf("\n");

	}

}
Exemple #17
0
int main()
{
	int n;
	double temp;
	while( scanf( "%lf%d" , &temp , &n ) && n )
	{
		int i;
		q=(int)(temp*100);
		k = 0;
		for( i = 0 ; i < n ; i++ )
		{
			int m;
			bool tag = true;
			int tot,tempi;
			tot = 0;
			char type;
			scanf( "%d" , &m );
			abc[0] = abc[1] = abc[2] = 0;
			while( m-- )
			{
				scanf( " %c:%lf" , &type , &temp );
				tempi=(int)(temp*100);
				if( type == 'A' )  abc[0] += tempi;   //统计A类物品的总额
				else if( type == 'B' ) abc[1] += tempi;  //统计B类物品的总额
				else if( type == 'C') abc[2] += tempi;  //统计C类物品的总额
				else  tag = false;
				if( tempi > 60000 ) 
					tag = false;
				tot += tempi;   //统计单张发票上的总额
			}
			
			//如果单张发票总额大于1000,或任何一类单项总额大于600,则此类发票不能报销
			if( tot > 100000 || abc[0] > 60000 || abc[1] > 60000 || abc[2] > 60000) tag = false;
			
			//将可以报销的发票总额记录到ary数组中
			if( tag )
				ary[++k] = tot;
		}
		
		max = 0;   //max用来表示最大的报销额
		int remain = 0;  //remain用来表示剩下所有发票加起来的总额度
		for( i = 1 ; i <= k ; i++ )  remain += ary[i];  //初始化remain,是所有发票的总额度
		Cal(1,0,remain);  //调用递归函数进行搜索
		
		printf( "%.2lf\n" , max/100.0 );
	}
	return 1;
}
Exemple #18
0
int main()
{
    LL N, K, Left, Right, Mid;
    scanf("%I64d %I64d", &N, &K);
    Left = 1;
    Right = K * N;
    while(Right > Left) {
        Mid = (Left + Right) / 2;
        if(Cal(Mid, K) < N) {
            Left = Mid + 1;
        } else {
            Right = Mid;
        }
    }
    printf("%d\n", Left);
    return 0;
}
Exemple #19
0
int startFunction(int n)
{
    return Cal(n);
}
Exemple #20
0
	inline double Cal(int L, int n) {
		int L1 = L / n, L2 = L % n;
		return  L2 * Cal(L1 + 1, k2) + (n - L2) * Cal(L1, k2) + Cal(L, k1);
	}
Exemple #21
0
	inline double Cal(int L, int n, int m) {
		int L1 = L / n, L2 = L % n;
		return L2 * Cal(L1 + 1, m) + (n - L2) * Cal(L1, m);
	}
int main() {
	while(scanf("%lld",&n) == 1) {
		Cal();
	}
	return 0;
}
Exemple #23
0
int main(void)
{
	Cal();
	getch();
	return 0;
}
Exemple #24
0
		inline int Sum(int l, int r, int t) {
			if (l > r) return 0;
			int L = Cal(l - 1, t);
			int R = Cal(r, t);
			return R - L;
		}
int main (int argc, const char **argv) {
  Param_t *param = NULL;
  Input_t *input = NULL;
  Lut_t *lut = NULL;
  Output_t *output = NULL;
  Output_t *output_th = NULL;
  int iline, isamp,oline, ib, jb, iz, val;
  unsigned char *line_in = NULL;
  unsigned char *line_in_thz = NULL;
  unsigned char *line_out_qa = NULL;
  int16 *line_out = NULL;
  int16 *line_out_th = NULL;
  int16 *line_out_thz = NULL;
  Cal_stats_t cal_stats;
  Cal_stats6_t cal_stats6;
  int nps,nls, nps6, nls6;
  int zoomx, zoomy;
  int i,odometer_flag=0;
  char msgbuf[1024];
  char envi_file[STR_SIZE]; /* name of the output ENVI header file */
  char *cptr=NULL;          /* pointer to the file extension */
  size_t input_psize;
  int qa_band = QA_BAND_NUM;
  int nband_refl = NBAND_REFL_MAX;
  int ifill, num_zero;
  int maxth=0;
  int mss_flag=0;
  Espa_internal_meta_t xml_metadata;  /* XML metadata structure */
  Envi_header_t envi_hdr;   /* output ENVI header information */

  printf ("\nRunning lndcal ...\n");
  for (i=1; i<argc; i++)if ( !strcmp(argv[i],"-o") )odometer_flag=1;

  /* Read the parameters from the input parameter file */
  param = GetParam(argc, argv);
  if (param == (Param_t *)NULL) EXIT_ERROR("getting runtime parameters",
    "main");

  /* Validate the input metadata file */
  if (validate_xml_file (param->input_xml_file_name) != SUCCESS)
  {  /* Error messages already written */
      EXIT_ERROR("Failure validating XML file", "main");
  }

  /* Initialize the metadata structure */
  init_metadata_struct (&xml_metadata);

  /* Parse the metadata file into our internal metadata structure; also
     allocates space as needed for various pointers in the global and band
     metadata */
  if (parse_metadata (param->input_xml_file_name, &xml_metadata) != SUCCESS)
  {  /* Error messages already written */
    EXIT_ERROR("parsing XML file", "main");
  }

  /* Check to see if the gain and bias values were specified */
  if (!existRadGB (&xml_metadata))
    EXIT_ERROR("Gains and biases don't exist in XML file (TOA radiance gain "
      "and bias fields) for each band.  Make sure to utilize the latest LPGS "
      "MTL file for conversion to the ESPA internal raw binary format as the "
      "gains and biases should be in that file.", "main");
  
  /* Open input file */
  input = OpenInput (&xml_metadata);
  if (input == (Input_t *)NULL)
    EXIT_ERROR("setting up input from XML structure", "main");

  /* Get Lookup table */
  lut = GetLut(param, input->nband, input);
  if (lut == (Lut_t *)NULL) EXIT_ERROR("bad lut file", "main");

  nps6=  input->size_th.s;
  nls6=  input->size_th.l;
  nps =  input->size.s;
  nls =  input->size.l;
  zoomx= nint( (float)nps / (float)nps6 );
  zoomy= nint( (float)nls / (float)nls6 );

  for (ib = 0; ib < input->nband; ib++) 
    cal_stats.first[ib] = true;
  cal_stats6.first = true;
  if (input->meta.inst == INST_MSS)mss_flag=1; 

  /* Open the output files.  Raw binary band files will be be opened. */
  output = OpenOutput(&xml_metadata, input, param, lut, false /*not thermal*/,
    mss_flag);
  if (output == NULL) EXIT_ERROR("opening output file", "main");

  /* Allocate memory for the input buffer, enough for all reflectance bands */
  input_psize = sizeof(unsigned char);
  line_in = calloc (input->size.s * nband_refl, input_psize);
   if (line_in == NULL) 
     EXIT_ERROR("allocating input line buffer", "main");

  /* Create and open output thermal band, if one exists */
  if ( input->nband_th > 0 ) {
    output_th = OpenOutput (&xml_metadata, input, param, lut, true /*thermal*/,
      mss_flag);
    if (output_th == NULL)
      EXIT_ERROR("opening output therm file", "main");

    /* Allocate memory for the thermal input and output buffer, only holds
       one band */
    line_out_th = calloc(input->size_th.s, sizeof(int16));
    if (line_out_th == NULL) 
      EXIT_ERROR("allocating thermal output line buffer", "main");

    if (zoomx == 1) {
      line_out_thz = line_out_th;
      line_in_thz = line_in;
    }
    else {
      line_out_thz = calloc (input->size.s, sizeof(int16));
      if (line_out_thz == NULL) 
        EXIT_ERROR("allocating thermal zoom output line buffer", "main");
      line_in_thz = calloc (input->size.s, input_psize);
      if (line_in_thz == NULL) 
        EXIT_ERROR("allocating thermal zoom input line buffer", "main");
    }
  } else {
    printf("*** no output thermal file ***\n"); 
  }

  /* Allocate memory for output lines for both the image and QA data */
  line_out = calloc (input->size.s, sizeof (int16));
  if (line_out == NULL) 
    EXIT_ERROR("allocating output line buffer", "main");

  line_out_qa = calloc (input->size.s, sizeof(unsigned char));
  if (line_out_qa == NULL) 
    EXIT_ERROR("allocating qa output line buffer", "main");
  memset (line_out_qa, 0, input->size.s * sizeof(unsigned char));    

  /* Do for each THERMAL line */
  oline= 0;
  if (input->nband_th > 0) {
    ifill= (int)lut->in_fill;
    for (iline = 0; iline < input->size_th.l; iline++) {
      ib=0;
      if (!GetInputLineTh(input, iline, line_in))
        EXIT_ERROR("reading input data for a line", "main");

      if ( odometer_flag && ( iline==0 || iline ==(nls-1) || iline%100==0  ) ){ 
        if ( zoomy == 1 )
          printf("--- main loop BAND6 Line %d --- \r",iline); 
        else
          printf("--- main loop BAND6 Line in=%d out=%d --- \r",iline,oline); 
        fflush(stdout); 
      }

      memset(line_out_qa, 0, input->size.s*sizeof(unsigned char));    
      if (!Cal6(lut, input, line_in, line_out_th, line_out_qa, &cal_stats6,
        iline))
        EXIT_ERROR("doing calibration for a line", "main");

      if ( zoomx>1 ) {
        zoomIt(line_out_thz, line_out_th, nps/zoomx, zoomx );
        zoomIt8(line_in_thz, line_in, nps/zoomx, zoomx );
      }

      for ( iz=0; iz<zoomy; iz++ ) {
        for (isamp = 0; isamp < input->size.s; isamp++) {
          val= getValue(line_in_thz, isamp);
          if ( val> maxth) maxth=val;
          if ( val==ifill) line_out_qa[isamp] = lut->qa_fill; 
          else if ( val>=SATU_VAL6 ) line_out_qa[isamp] = ( 0x000001 << 6 ); 
        }

        if ( oline<nls ) {
          if (!PutOutputLine(output_th, ib, oline, line_out_thz)) {
            sprintf(msgbuf,"write thermal error ib=%d oline=%d iline=%d",ib,
              oline,iline);
            EXIT_ERROR(msgbuf, "main");
          }

          if (input->meta.inst != INST_MSS) 
            if (!PutOutputLine(output_th, ib+1, oline, line_out_qa)) {
	          sprintf(msgbuf,"write thermal QA error ib=%d oline=%d iline=%d",
                ib+1,oline,iline);
              EXIT_ERROR(msgbuf, "main");
            }
        }
        oline++;
      }
    } /* end loop for each thermal line */
  }
  if (odometer_flag) printf("\n");

  if (input->nband_th > 0)
    if (!CloseOutput(output_th))
      EXIT_ERROR("closing output thermal file", "main");

  /* Do for each REFLECTIVE line */
  ifill= (int)lut->in_fill;
  for (iline = 0; iline < input->size.l; iline++){
    /* Do for each band */

    if ( odometer_flag && ( iline==0 || iline ==(nls-1) || iline%100==0  ) )
     {printf("--- main reflective loop Line %d ---\r",iline); fflush(stdout);}

    memset(line_out_qa, 0, input->size.s*sizeof(unsigned char));
    
    for (ib = 0; ib < input->nband; ib++) {
      if (!GetInputLine(input, ib, iline, &line_in[ib*nps]))
        EXIT_ERROR("reading input data for a line", "main");
    }
    
    for (isamp = 0; isamp < input->size.s; isamp++){
      num_zero=0;
      for (ib = 0; ib < input->nband; ib++) {
        jb= (ib != 5 ) ? ib+1 : ib+2;
        val= getValue((unsigned char *)&line_in[ib*nps], isamp);
	    if ( val==ifill   )num_zero++;
        if ( val==SATU_VAL[ib] ) line_out_qa[isamp]|= ( 0x000001 <<jb ); 
      }
      /* Feng fixed bug by changing "|=" to "=" below (4/17/09) */
      if ( num_zero >  0 )line_out_qa[isamp] = lut->qa_fill; 
    }

    for (ib = 0; ib < input->nband; ib++) {
      if (!Cal(lut, ib, input, &line_in[ib*nps], line_out, line_out_qa,
        &cal_stats,iline))
        EXIT_ERROR("doing calibraton for a line", "main");

      if (!PutOutputLine(output, ib, iline, line_out))
        EXIT_ERROR("reading input data for a line", "main");
    } /* End loop for each band */
        
    if (input->meta.inst != INST_MSS) 
      if (!PutOutputLine(output, qa_band, iline, line_out_qa))
        EXIT_ERROR("writing qa data for a line", "main");
  } /* End loop for each line */

  if ( odometer_flag )printf("\n");

  for (ib = 0; ib < input->nband; ib++) {
    printf(
      " band %d rad min %8.5g max %8.4f  |  ref min  %8.5f max  %8.4f\n", 
      input->meta.iband[ib], cal_stats.rad_min[ib], cal_stats.rad_max[ib],
      cal_stats.ref_min[ib], cal_stats.ref_max[ib]);
  }

  if ( input->nband_th > 0 )
    printf(
      " band %d rad min %8.5g max %8.4f  |  tmp min  %8.5f max  %8.4f\n", 6,
      cal_stats6.rad_min,  cal_stats6.rad_max,
      cal_stats6.temp_min, cal_stats6.temp_max);

  /* Close input and output files */
  if (!CloseInput(input)) EXIT_ERROR("closing input file", "main");
  if (!CloseOutput(output)) EXIT_ERROR("closing input file", "main");

  /* Write the ENVI header for reflectance files */
  for (ib = 0; ib < output->nband; ib++) {
    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[ib], &xml_metadata.global,
      &envi_hdr) != SUCCESS)
        EXIT_ERROR("Creating the ENVI header structure for this file.", "main");

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[ib].file_name);
    cptr = strchr (envi_file, '.');
    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
        EXIT_ERROR("Writing the ENVI header file.", "main");
  }

  /* Write the ENVI header for thermal files */
  for (ib = 0; ib < output_th->nband; ib++) {
    /* Create the ENVI header file this band */
    if (create_envi_struct (&output_th->metadata.band[ib], &xml_metadata.global,
      &envi_hdr) != SUCCESS)
        EXIT_ERROR("Creating the ENVI header structure for this file.", "main");

    /* Write the ENVI header */
    strcpy (envi_file, output_th->metadata.band[ib].file_name);
    cptr = strchr (envi_file, '.');
    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
        EXIT_ERROR("Writing the ENVI header file.", "main");
  }

  /* Append the reflective and thermal bands to the XML file */
  if (append_metadata (output->nband, output->metadata.band,
    param->input_xml_file_name) != SUCCESS)
    EXIT_ERROR("appending reflectance and QA bands", "main");
  if (input->nband_th > 0) {
    if (append_metadata (output_th->nband, output_th->metadata.band,
      param->input_xml_file_name) != SUCCESS)
      EXIT_ERROR("appending thermal and QA bands", "main");
  }

  /* Free the metadata structure */
  free_metadata (&xml_metadata);

  /* Free memory */
  if (!FreeParam(param)) 
    EXIT_ERROR("freeing parameter stucture", "main");

  if (!FreeInput(input)) 
    EXIT_ERROR("freeing input file stucture", "main");

  if (!FreeLut(lut)) 
    EXIT_ERROR("freeing lut file stucture", "main");

  if (!FreeOutput(output)) 
    EXIT_ERROR("freeing output file stucture", "main");

  free(line_out);
  line_out = NULL;
  free(line_in);
  line_in = NULL;
  free(line_out_qa);
  line_out_qa = NULL;
  free(line_out_th);
  line_out_th = NULL;
  if (zoomx != 1) {
    free(line_in_thz);
    free(line_out_thz);
  }
  line_in_thz = NULL;
  line_out_thz = NULL;

  /* All done */
  printf ("lndcal complete.\n");
  return (EXIT_SUCCESS);
}