Exemplo n.º 1
0
/*!
 * @brief An entry point of this program
 * @return exit-status
 */
int main(void) {
  uint row, col;
  uint i;
  int **array, **t_array;
  printf("Number of ROW    = ?\b"); scanf("%u", &row);
  printf("Number of COLUMN = ?\b"); scanf("%u", &col);

  array   = alloc_2d_array(row, col);
  t_array = alloc_2d_array(col, row);
  if (array == NULL || t_array == NULL) {
    fputs("Memory allocation error!\n", stderr);
    return EXIT_FAILURE;
  }

  for (i = 0; i < row; i++) {
    uint j;
    for (j = 0; j < col; j++) {
      int tmp;
      scanf("%d", &tmp);
      array[i][j] = tmp;
    }
  }
  puts("");
  puts("Matrix = ");
  show_2d_array((const int *const *)array, row, col);
  puts("============================================================");

  transpose(t_array, (const int *const *)array, row, col);
  puts("Transposed Matrix = ");
  show_2d_array((const int *const *)t_array, col, row);

  free_2d_array(array, row);
  free_2d_array(t_array, row);
  return EXIT_SUCCESS;
}
Exemplo n.º 2
0
/*! \fn void MultiNSH(int n, Real *tstop, Real *mratio, Real etavk,
 *                   Real *uxNSH, Real *uyNSH, Real *wxNSH, Real *wyNSH)
 *  \brief Multi-species NSH equilibrium 
 *
 * Input: # of particle types (n), dust stopping time and mass ratio array, and 
 *        drift speed etavk.
 * Output: gas NSH equlibrium velocity (u), and dust NSH equilibrium velocity
 *         array (w).
 */
void MultiNSH(int n, Real *tstop, Real *mratio, Real etavk,
                     Real *uxNSH, Real *uyNSH, Real *wxNSH, Real *wyNSH)
{
  int i,j;
  Real *Lambda1,**Lam1GamP1, **A, **B, **Tmp;

  Lambda1 = (Real*)calloc_1d_array(n, sizeof(Real));     /* Lambda^{-1} */
  Lam1GamP1=(Real**)calloc_2d_array(n, n, sizeof(Real)); /* Lambda1*(1+Gamma) */
  A       = (Real**)calloc_2d_array(n, n, sizeof(Real));
  B       = (Real**)calloc_2d_array(n, n, sizeof(Real));
  Tmp     = (Real**)calloc_2d_array(n, n, sizeof(Real));

  /* definitions */
  for (i=0; i<n; i++){
    for (j=0; j<n; j++)
      Lam1GamP1[i][j] = mratio[j];
    Lam1GamP1[i][i] += 1.0;
    Lambda1[i] = 1.0/(tstop[i]+1.0e-16);
    for (j=0; j<n; j++)
      Lam1GamP1[i][j] *= Lambda1[i];
  }

  /* Calculate A and B */
  MatrixMult(Lam1GamP1, Lam1GamP1, n,n,n, Tmp);
  for (i=0; i<n; i++) Tmp[i][i] += 1.0;
  InverseMatrix(Tmp, n, B);
  for (i=0; i<n; i++)
  for (j=0; j<n; j++)
    B[i][j] *= Lambda1[j];
  MatrixMult(Lam1GamP1, B, n,n,n, A);

  /* Obtain NSH velocities */
  *uxNSH = 0.0;  *uyNSH = 0.0;
  for (i=0; i<n; i++){
    wxNSH[i] = 0.0;
    wyNSH[i] = 0.0;
    for (j=0; j<n; j++){
      wxNSH[i] -= B[i][j];
      wyNSH[i] -= A[i][j];
    }
    wxNSH[i] *= 2.0*etavk;
    wyNSH[i] *= etavk;
    *uxNSH -= mratio[i]*wxNSH[i];
    *uyNSH -= mratio[i]*wyNSH[i];
    wyNSH[i] += etavk;
  }

  free(Lambda1);
  free_2d_array(A);         free_2d_array(B);
  free_2d_array(Lam1GamP1); free_2d_array(Tmp);

  return;
}
Exemplo n.º 3
0
Arquivo: interface.c Projeto: naa/SLE
void draw_interface(int M, int N, int **table, double beta)
{
  long long niter=10000; //size*size*floor(1/fabs(1/beta-2.2698));

  evolve_table_pm_boundary(M,N,table,beta, niter);
  int **hint=allocate_2d_rectangle(M,N);
  int **vint=allocate_2d_rectangle(M,N);
  
  int **interface=calc_interface(M,N, table,hint,vint);
  FILE *f=fopen("lastrun.xpm","w");
  print_table_xpm(f, M,  N, table, interface);
  fclose(f);
  free_2d_array(M,table);
  free_2d_array(M,interface);
}
Exemplo n.º 4
0
Arquivo: interface.c Projeto: naa/SLE
double measure_Ul(int size,  double beta, int measurements)
{
  long long niter=size*size*size;
  int **table=allocate_2d_array(size);
  init_table_no_boundary(size,size,table);

  evolve_table_cyclic_boundary(size,size,table,beta, niter);
  double M=0;
  double Msq=0;
  double Mfourth=0;
  double mc=0;
  for (long i=0; i<measurements; i++){
    mc=((double)calc_magnetization(size,size,table))/(size*size);
    M=M+mc;
    Msq=Msq+mc*mc;
    Mfourth=Mfourth+mc*mc*mc*mc;
    evolve_table_cyclic_boundary(size,size,table,beta,1);
  }
  free_2d_array(size,table);
  M=M/measurements;
  Msq=Msq/measurements; 
  Mfourth=Mfourth/measurements;

  double res=1.0-1.0/3.0*Mfourth/(Msq*Msq);
  return res;
}
Exemplo n.º 5
0
Arquivo: interface.c Projeto: naa/SLE
void generate_interfaces(int M, int N, int **table, double beta, int measurements)
{
  long long niter=(long long)((double)(M*10)*log(M)); //size*size*floor(1/fabs(1/beta-2.2698));
  long long i,j;
  long long len;
  interface inter;
  //  interface uniform;
  init_table_pm_third_boundary(M,N,table);
  for (i=0;i<niter;i++) modify_cluster_bc(M,N,table,1+rand_int(M-2), 1+rand_int(N-2), beta);
  printf("{");
  fprintf(stderr,"{");  
  for (i=0;i<measurements; i++){
    inter=get_interface(M,N,table);
    print_interface_to_file(stderr, inter);

    //    uniform=uniformize_interface(inter);
    //    print_interface_to_file(stderr, uniform);

    print_interface_as_ts_to_file(stdout, inter);
    
    free(inter.points);
    //    free(uniform.points);        
    if (i<measurements-1){    
      for (j=0;j<M/10;j++)
	modify_cluster_bc(M,N,table,1+rand_int(M-2), 1+rand_int(N-2), beta);
      printf(",\n");
      fprintf(stderr,",\n");
    }
  }
  printf("}");
  fprintf(stderr,"}");  
  free_2d_array(M,table);
}
Exemplo n.º 6
0
Arquivo: interface.c Projeto: naa/SLE
double measure_susceptibility_cluster(int M, int N, int **table, double beta, int measurements)
{
  long long niter=1000;
  long i;

  for (i=0;i<niter;i++) modify_cluster(M,N,table,rand_int(M), rand_int(N), beta);
  
  double mm=0;
  double Msq=0;
  double mc=0;
  double Mfourth=0;
  long E=0;
  for (long i=0; i<measurements; i++){
    mc=((double)calc_magnetization(M,N,table))/(M*N);
    mm=mm+fabs(mc);
    Msq=Msq+mc*mc;
    Mfourth=Mfourth+mc*mc*mc*mc;

    modify_cluster(M,N,table,rand_int(M), rand_int(N), beta);
    //    E=state_energy(M,N,table);
    //    printf("%lf %ld\n",mc, E);
    
  }
  mm=mm/measurements;
  Msq=Msq/measurements;
  Mfourth=Mfourth/measurements;
  
  free_2d_array(M,table);
  printf("%lf %lf %lf %lf %lf \n", 1/beta, mm, beta*(Msq-mm*mm), beta*Msq,1.0-1.0/3.0*Mfourth/(Msq*Msq));
  return beta*(Msq-mm*mm);
}
Exemplo n.º 7
0
Arquivo: interface.c Projeto: naa/SLE
double measure_susceptibility(int M, int N, int **table, double beta, int measurements)
{
  long long niter=10000; //size*size*floor(1/fabs(1/beta-2.2698));

  evolve_table_cyclic_boundary(M,N,table,beta, niter);
  double mm=0;
  double Msq=0;
  double mc=0;
  double Mfourth=0;  
  for (long i=0; i<measurements; i++){
    mc=((double)calc_magnetization(M,N,table))/(M*N);
    mm=mm+fabs(mc);
    Msq=Msq+mc*mc;
    Mfourth=Mfourth+mc*mc*mc*mc;

    evolve_table_cyclic_boundary(M,N,table,beta,1);
    printf("%lf ",mc);
    
  }
  mm=mm/measurements;
  Msq=Msq/measurements;
  Mfourth=Mfourth/measurements;
  
  free_2d_array(M,table);
  //  printf("%lf %lf %lf %lf %lf \n", 1/beta, M, beta*(Msq-M*M), beta*Msq,1.0-1.0/3.0*Mfourth/(Msq*Msq));
  return beta*(Msq-mm*mm);
}
Exemplo n.º 8
0
int calculate_lcs(char *a, char *b)
{
	int lena = strlen(a);
	int lenb = strlen(b);
	int **array;
	int ret;
	int i, j;

	ret = allocate_2d_array(&array, lena + 1, lenb + 1);

	for (i = 0; i < lena; i++)
		array[i][0] = 0;
	for (j = 0; j < lenb; j++)
		array[0][j] = 0;

	for (i = 1; i < lena + 1; i++) {
		for (j = 1; j < lenb + 1; j++) {
			if (a[i - 1] == b[j - 1])
				array[i][j] = array[i - 1][j - 1] + 1;
			else
				array[i][j] = MAX(array[i][j - 1], array[i - 1][j]);
		}
	}
 
	ret = array[lena][lenb];

	print_lcs1(array, a, b, ret);
	print_lcs(array, a, b, ret);

	free_2d_array(array, lena + 1, lenb + 1);

	return ret;
}
Exemplo n.º 9
0
Arquivo: interface.c Projeto: naa/SLE
void draw_interface_cluster(int M, int N, int **table, double beta)
{
  long long niter=(long long)((double)(M*10)*log(M)); //size*size*floor(1/fabs(1/beta-2.2698));
  long long i;
  init_table_pm_boundary(M,N,table);
  for (i=0;i<niter;i++) modify_cluster_bc(M,N,table,1+rand_int(M-2), 1+rand_int(N-2), beta);
  int **hint=allocate_2d_rectangle(M,N);
  int **vint=allocate_2d_rectangle(M,N);
  
  int **interface=calc_interface(M,N, table,hint,vint);
  FILE *f=fopen("lastrun.xpm","w");
  print_table_xpm(f, M,  N, table, interface);
  fclose(f);
  free_2d_array(M,table);
  free_2d_array(M,interface);
}
Exemplo n.º 10
0
static void freeLogInfo(struct logInfo *log)
{
    if (log->pattern)
	free(log->pattern);
    if (log->files)
	free_2d_array(log->files, log->numFiles);
    if (log->oldDir)
	free(log->oldDir);
    if (log->pre)
	free(log->pre);
    if (log->post)
	free(log->post);
    if (log->first)
	free(log->first);
    if (log->last)
	free(log->last);
    if (log->logAddress)
	free(log->logAddress);
    if (log->extension)
	free(log->extension);
    if (log->compress_prog)
	free(log->compress_prog);
    if (log->uncompress_prog)
	free(log->uncompress_prog);
    if (log->compress_ext)
	free(log->compress_ext);
    if (log->compress_options_list)
	free(log->compress_options_list);
	free(log->dateformat);
}
Exemplo n.º 11
0
char* irc_subconftok(char *buf, unsigned char citem, char *subname, unsigned int tok) {
    unsigned long i = 0, lcount = 0;
    char **lines = NULL;
    char *conftok = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char));
    
    lcount = get_confitem(&lines, citem);
    for(i = 0;i < lcount;i++) {
        if(istrcmp(get_itemtok(conftok, lines[i], 2), subname)) {
            get_itemtok(conftok, lines[i], tok);
            strncpy(buf, conftok, CONF_MAX_ITEMLEN);
            freem(conftok);
            free_2d_array(lines, lcount);
            return buf;
        }
    }
    
    freem(conftok);
    free_2d_array(lines, lcount);
    return NULL;
}
Exemplo n.º 12
0
Arquivo: interface.c Projeto: naa/SLE
void compute_probability(int size, double beta, long measurements,int **res) 
{
  long long niter=3*size*size;
  int **table=allocate_2d_array(size);
  //  int ** res = allocate_2d_array(size);
  int ** hint=allocate_2d_array(size);
  int ** vint=allocate_2d_array(size);
  int **interface;

  evolve_table_pm_boundary(size,size,table,beta, niter);

  for (long i=0; i<measurements; i++){
    interface=calc_interface(size,size, table,hint, vint);  
    add_crossing_number(size,hint,res);
    free_2d_array(size,interface);
    evolve_table_pm_boundary(size,size,table,beta,50);
  }
  free_2d_array(size,table);
  free_2d_array(size,hint);
  free_2d_array(size,vint);
  //  return res;
}
/******************************************************************************
MODULE:  potential_cloud_shadow_snow_mask

PURPOSE: Identify the cloud pixels, snow pixels, water pixels, clear land 
         pixels, and potential shadow pixels

RETURN: SUCCESS
        FAILURE

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
3/15/2013   Song Guo         Original Development

NOTES: 
1. Thermal buffer is expected to be in degrees Celsius with a factor applied
   of 100.  Many values which compare to the thermal buffer in this code are
   hardcoded and assume degrees celsius * 100.
******************************************************************************/
int potential_cloud_shadow_snow_mask
(
    Input_t * input,            /*I: input structure */
    float cloud_prob_threshold, /*I: cloud probability threshold */
    float *ptm,                 /*O: percent of clear-sky pixels */
    float *t_templ,             /*O: percentile of low background temp */
    float *t_temph,             /*O: percentile of high background temp */
    unsigned char **pixel_mask, /*I/O: pixel mask */
    bool verbose                /*I: value to indicate if intermediate
                                     messages be printed */
)
{
    char errstr[MAX_STR_LEN];   /* error string */
    int nrows = input->size.l;  /* number of rows */
    int ncols = input->size.s;  /* number of columns */
    int ib = 0;                 /* band index */
    int row = 0;                /* row index */
    int col = 0;                /* column index */
    int mask_counter = 0;       /* mask counter */
    int clear_pixel_counter = 0;        /* clear sky pixel counter */
    int clear_land_pixel_counter = 0;   /* clear land pixel counter */
    float ndvi, ndsi;           /* NDVI and NDSI values */
    int16 *f_temp = NULL;       /* clear land temperature */
    int16 *f_wtemp = NULL;      /* clear water temperature */
    float visi_mean;            /* mean of visible bands */
    float whiteness = 0.0;      /* whiteness value */
    float hot;                  /* hot value for hot test */
    float lndptm;               /* clear land pixel percentage */
    float l_pt;                 /* low percentile threshold */
    float h_pt;                 /* high percentile threshold */
    float t_wtemp;              /* high percentile water temperature */
    float **wfinal_prob = NULL; /* final water pixel probabilty value */
    float **final_prob = NULL;  /* final land pixel probability value */
    float wtemp_prob;           /* water temperature probability value */
    int t_bright;               /* brightness test value for water */
    float brightness_prob;      /* brightness probability value */
    int t_buffer;               /* temperature test buffer */
    float temp_l;               /* difference of low/high tempearture
                                   percentiles */
    float temp_prob;            /* temperature probability */
    float vari_prob;            /* probability from NDVI, NDSI, and whiteness */
    float max_value;            /* maximum value */
    float *prob = NULL;         /* probability value */
    float *wprob = NULL;        /* probability value */
    float clr_mask = 0.0;       /* clear sky pixel threshold */
    float wclr_mask = 0.0;      /* water pixel threshold */
    int16 *nir = NULL;          /* near infrared band (band 4) data */
    int16 *swir = NULL;         /* short wavelength infrared (band 5) data */
    float backg_b4;             /* background band 4 value */
    float backg_b5;             /* background band 5 value */
    int16 shadow_prob;          /* shadow probability */
    int status;                 /* return value */
    int satu_bv;                /* sum of saturated bands 1, 2, 3 value */
    unsigned char mask;         /* mask used for 1 pixel */

    /* Dynamic memory allocation */
    unsigned char **clear_mask = NULL;

    clear_mask = (unsigned char **) allocate_2d_array (input->size.l,
                                                       input->size.s,
                                                       sizeof (unsigned char));
    if (clear_mask == NULL)
        RETURN_ERROR ("Allocating mask memory", "pcloud", FAILURE);

    if (verbose)
        printf ("The first pass\n");

    /* Loop through each line in the image */
    for (row = 0; row < nrows; row++)
    {
        if (verbose)
        {
            /* Print status on every 1000 lines */
            if (!(row % 1000))
            {
                printf ("Processing line %d\r", row);
                fflush (stdout);
            }
        }

        /* For each of the image bands */
        for (ib = 0; ib < input->nband; ib++)
        {
            /* Read each input reflective band -- data is read into
               input->buf[ib] */
            if (!GetInputLine (input, ib, row))
            {
                sprintf (errstr, "Reading input image data for line %d, "
                         "band %d", row, ib);
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }
        }

        /* For the thermal band */
        /* Read the input thermal band -- data is read into input->therm_buf */
        if (!GetInputThermLine (input, row))
        {
            sprintf (errstr, "Reading input thermal data for line %d", row);
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        for (col = 0; col < ncols; col++)
        {
            int ib;
            for (ib = 0; ib < BI_REFL_BAND_COUNT; ib++)
            {
                if (input->buf[ib][col] == input->meta.satu_value_ref[ib])
                    input->buf[ib][col] = input->meta.satu_value_max[ib];
            }
            if (input->therm_buf[col] == input->meta.therm_satu_value_ref)
                input->therm_buf[col] = input->meta.therm_satu_value_max;

            /* process non-fill pixels only
               Due to a problem with the input LPGS data, the thermal band
               may have values less than -9999 after scaling so exclude those
               as well */
            if (input->therm_buf[col] <= -9999
                || input->buf[BI_BLUE][col] == -9999
                || input->buf[BI_GREEN][col] == -9999
                || input->buf[BI_RED][col] == -9999
                || input->buf[BI_NIR][col] == -9999
                || input->buf[BI_SWIR_1][col] == -9999
                || input->buf[BI_SWIR_2][col] == -9999)
            {
                mask = 0;
            }
            else
            {
                mask = 1;
                mask_counter++;
            }

            if ((input->buf[BI_RED][col] + input->buf[BI_NIR][col]) != 0
                && mask == 1)
            {
                ndvi = (float) (input->buf[BI_NIR][col]
                                - input->buf[BI_RED][col])
                       / (float) (input->buf[BI_NIR][col]
                                  + input->buf[BI_RED][col]);
            }
            else
                ndvi = 0.01;

            if ((input->buf[BI_GREEN][col] + input->buf[BI_SWIR_1][col]) != 0
                && mask == 1)
            {
                ndsi = (float) (input->buf[BI_GREEN][col]
                                - input->buf[BI_SWIR_1][col])
                       / (float) (input->buf[BI_GREEN][col]
                                  + input->buf[BI_SWIR_1][col]);
            }
            else
                ndsi = 0.01;

            /* Basic cloud test, equation 1 */
            if (((ndsi - 0.8) < MINSIGMA)
                && ((ndvi - 0.8) < MINSIGMA)
                && (input->buf[BI_SWIR_2][col] > 300)
                && (input->therm_buf[col] < 2700))
            {
                pixel_mask[row][col] |= 1 << CLOUD_BIT;
            }
            else
                pixel_mask[row][col] &= ~(1 << CLOUD_BIT);

            /* It takes every snow pixels including snow pixel under thin 
               clouds or icy clouds, equation 20 */
            if (((ndsi - 0.15) > MINSIGMA)
                && (input->therm_buf[col] < 1000)
                && (input->buf[BI_NIR][col] > 1100)
                && (input->buf[BI_GREEN][col] > 1000))
            {
                pixel_mask[row][col] |= 1 << SNOW_BIT;
            }
            else
                pixel_mask[row][col] &= ~(1 << SNOW_BIT);

            /* Zhe's water test (works over thin cloud), equation 5 */
            if (((((ndvi - 0.01) < MINSIGMA)
                  && (input->buf[BI_NIR][col] < 1100))
                 || (((ndvi - 0.1) < MINSIGMA)
                     && (ndvi > MINSIGMA)
                     && (input->buf[BI_NIR][col] < 500)))
                && (mask == 1))
            {
                pixel_mask[row][col] |= 1 << WATER_BIT;
            }
            else
                pixel_mask[row][col] &= ~(1 << WATER_BIT);
            if (mask == 0)
                pixel_mask[row][col] |= 1 << FILL_BIT;

            /* visible bands flatness (sum(abs)/mean < 0.6 => brigt and dark 
               cloud), equation 2 */
            if ((pixel_mask[row][col] & (1 << CLOUD_BIT)) && mask == 1)
            {
                visi_mean = (float) (input->buf[BI_BLUE][col]
                                     + input->buf[BI_GREEN][col]
                                     + input->buf[BI_RED][col]) / 3.0;
                if (visi_mean != 0)
                {
                    whiteness =
                        ((fabs ((float) input->buf[BI_BLUE][col] - visi_mean)
                          + fabs ((float) input->buf[BI_GREEN][col] - visi_mean)
                          + fabs ((float) input->buf[BI_RED][col]
                                  - visi_mean))) / visi_mean;
                }
                else
                {
                    /* Just put a large value to remove them from cloud pixel
                       identification */
                    whiteness = 100.0;
                }
            }

            /* Update cloud_mask,  if one visible band is saturated,
               whiteness = 0, due to data type conversion, pixel value
               difference of 1 is possible */
            if ((input->buf[BI_BLUE][col]
                 >= (input->meta.satu_value_max[BI_BLUE] - 1))
                ||
                (input->buf[BI_GREEN][col]
                 >= (input->meta.satu_value_max[BI_GREEN] - 1))
                ||
                (input->buf[BI_RED][col]
                 >= (input->meta.satu_value_max[BI_RED] - 1)))
            {
                whiteness = 0.0;
                satu_bv = 1;
            }
            else
            {
                satu_bv = 0;
            }

            if ((pixel_mask[row][col] & (1 << CLOUD_BIT)) &&
                (whiteness - 0.7) < MINSIGMA)
                pixel_mask[row][col] |= 1 << CLOUD_BIT;
            else
                pixel_mask[row][col] &= ~(1 << CLOUD_BIT);

            /* Haze test, equation 3 */
            hot =
                (float) input->buf[BI_BLUE][col] -
                0.5 * (float) input->buf[BI_RED][col] - 800.0;
            if ((pixel_mask[row][col] & (1 << CLOUD_BIT))
                && (hot > MINSIGMA || satu_bv == 1))
                pixel_mask[row][col] |= 1 << CLOUD_BIT;
            else
                pixel_mask[row][col] &= ~(1 << CLOUD_BIT);

            /* Ratio 4/5 > 0.75 test, equation 4 */
            if ((pixel_mask[row][col] & (1 << CLOUD_BIT)) &&
                input->buf[BI_SWIR_1][col] != 0)
            {
                if ((float) input->buf[BI_NIR][col] /
                    (float) (input->buf[BI_SWIR_1][col]) - 0.75 > MINSIGMA)
                    pixel_mask[row][col] |= 1 << CLOUD_BIT;
                else
                    pixel_mask[row][col] &= ~(1 << CLOUD_BIT);
            }
            else
                pixel_mask[row][col] &= ~(1 << CLOUD_BIT);

            /* Test whether use thermal band or not */
            if ((!(pixel_mask[row][col] & (1 << CLOUD_BIT))) && mask == 1)
            {
                clear_mask[row][col] |= 1 << CLEAR_BIT;
                clear_pixel_counter++;
            }
            else
                clear_mask[row][col] &= ~(1 << CLEAR_BIT);

            if ((!(pixel_mask[row][col] & (1 << WATER_BIT))) &&
                clear_mask[row][col] & (1 << CLEAR_BIT))
            {
                clear_mask[row][col] |= 1 << CLEAR_LAND_BIT;
                clear_land_pixel_counter++;
                clear_mask[row][col] &= ~(1 << CLEAR_WATER_BIT);
            }
            else if ((pixel_mask[row][col] & (1 << WATER_BIT)) &&
                     clear_mask[row][col] & (1 << CLEAR_BIT))
            {
                clear_mask[row][col] |= 1 << CLEAR_WATER_BIT;
                clear_mask[row][col] &= ~(1 << CLEAR_LAND_BIT);
            }
            else
            {
                clear_mask[row][col] &= ~(1 << CLEAR_WATER_BIT);
                clear_mask[row][col] &= ~(1 << CLEAR_LAND_BIT);
            }
        }
    }
    printf ("\n");

    *ptm = 100. * ((float) clear_pixel_counter / (float) mask_counter);
    lndptm = 100. * ((float) clear_land_pixel_counter / (float) mask_counter);

    if (verbose)
    {
        printf ("clear_pixels, clear_land_pixels, mask_counter = %d,%d,%d\n",
                clear_pixel_counter, clear_land_pixel_counter, mask_counter);
        printf ("*ptm, lndptm=%f,%f\n", *ptm, lndptm);
    }

    if ((*ptm - 0.1) <= MINSIGMA) /* No thermal test is needed, all clouds */
    {
        *t_templ = -1.0;
        *t_temph = -1.0;
        for (row = 0; row < nrows; row++)
        {
            for (col = 0; col < ncols; col++)
            {
                /* All cloud */
                if (!(pixel_mask[row][col] & (1 << CLOUD_BIT)))
                    pixel_mask[row][col] |= 1 << SHADOW_BIT;
                else
                    pixel_mask[row][col] &= ~(1 << SHADOW_BIT);
            }
        }
    }
    else
    {
        f_temp = malloc (input->size.l * input->size.s * sizeof (int16));
        f_wtemp = malloc (input->size.l * input->size.s * sizeof (int16));
        if (f_temp == NULL || f_wtemp == NULL)
        {
            sprintf (errstr, "Allocating temp memory");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        if (verbose)
            printf ("The second pass\n");

        int16 f_temp_max = SHRT_MIN;
        int16 f_temp_min = SHRT_MAX;
        int16 f_wtemp_max = SHRT_MIN;
        int16 f_wtemp_min = SHRT_MAX;
        int index = 0;
        int index2 = 0;
        /* Loop through each line in the image */
        for (row = 0; row < nrows; row++)
        {
            if (verbose)
            {
                /* Print status on every 1000 lines */
                if (!(row % 1000))
                {
                    printf ("Processing line %d\r", row);
                    fflush (stdout);
                }
            }

            /* For the thermal band, read the input thermal band  */
            if (!GetInputThermLine (input, row))
            {
                sprintf (errstr, "Reading input thermal data for line %d",
                         row);
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }

            for (col = 0; col < ncols; col++)
            {
                if (input->therm_buf[col] == input->meta.therm_satu_value_ref)
                    input->therm_buf[col] = input->meta.therm_satu_value_max;

                if ((lndptm - 0.1) >= MINSIGMA)
                {
                    /* get clear land temperature */
                    if (clear_mask[row][col] & (1 << CLEAR_LAND_BIT))
                    {
                        f_temp[index] = input->therm_buf[col];
                        if (f_temp_max < f_temp[index])
                            f_temp_max = f_temp[index];
                        if (f_temp_min > f_temp[index])
                            f_temp_min = f_temp[index];
                        index++;
                    }
                }
                else
                {
                    /* get clear water temperature */
                    if (clear_mask[row][col] & (1 << CLEAR_BIT))
                    {
                        f_temp[index] = input->therm_buf[col];
                        if (f_temp_max < f_temp[index])
                            f_temp_max = f_temp[index];
                        if (f_temp_min > f_temp[index])
                            f_temp_min = f_temp[index];
                        index++;
                    }
                }
                /* Equation 7 */
                if (clear_mask[row][col] & (1 << CLEAR_WATER_BIT))
                {
                    f_wtemp[index2] = input->therm_buf[col];
                    if (f_wtemp[index2] > f_wtemp_max)
                        f_wtemp_max = f_wtemp[index2];
                    if (f_wtemp[index2] < f_wtemp_min)
                        f_wtemp_min = f_wtemp[index2];
                    index2++;
                }
            }
        }
        printf ("\n");

        /* Set maximum and minimum values to zero if no clear land/water 
           pixels */
        if (f_temp_min == SHRT_MAX)
            f_temp_min = 0;
        if (f_temp_max == SHRT_MIN)
            f_temp_max = 0;
        if (f_wtemp_min == SHRT_MAX)
            f_wtemp_min = 0;
        if (f_wtemp_max == SHRT_MIN)
            f_wtemp_max = 0;

        /* Tempearture for snow test */
        l_pt = 0.175;
        h_pt = 1.0 - l_pt;
        /* 0.175 percentile background temperature (low) */
        status = prctile (f_temp, index, f_temp_min, f_temp_max, 100.0 * l_pt,
                          t_templ);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Error calling prctile routine");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        /* 0.825 percentile background temperature (high) */
        status = prctile (f_temp, index, f_temp_min, f_temp_max, 100.0 * h_pt,
                          t_temph);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Error calling prctile routine");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = prctile (f_wtemp, index2, f_wtemp_min, f_wtemp_max,
                          100.0 * h_pt, &t_wtemp);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Error calling prctile routine");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Temperature test */
        t_buffer = 4 * 100;
        *t_templ -= (float) t_buffer;
        *t_temph += (float) t_buffer;
        temp_l = *t_temph - *t_templ;

        /* Relase f_temp memory */
        free (f_wtemp);
        free (f_temp);
        f_wtemp = NULL;
        f_temp = NULL;

        wfinal_prob = (float **) allocate_2d_array (input->size.l,
                                                    input->size.s,
                                                    sizeof (float));
        final_prob =
            (float **) allocate_2d_array (input->size.l, input->size.s,
                                          sizeof (float));
        if (wfinal_prob == NULL || final_prob == NULL)
        {
            sprintf (errstr, "Allocating prob memory");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        if (verbose)
            printf ("The third pass\n");

        /* Loop through each line in the image */
        for (row = 0; row < nrows; row++)
        {
            if (verbose)
            {
                /* Print status on every 1000 lines */
                if (!(row % 1000))
                {
                    printf ("Processing line %d\r", row);
                    fflush (stdout);
                }
            }

            /* For each of the image bands */
            for (ib = 0; ib < input->nband; ib++)
            {
                /* Read each input reflective band -- data is read into
                   input->buf[ib] */
                if (!GetInputLine (input, ib, row))
                {
                    sprintf (errstr, "Reading input image data for line %d, "
                             "band %d", row, ib);
                    RETURN_ERROR (errstr, "pcloud", FAILURE);
                }
            }

            /* For the thermal band, data is read into input->therm_buf */
            if (!GetInputThermLine (input, row))
            {
                sprintf (errstr, "Reading input thermal data for line %d",
                         row);
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }

            /* Loop through each line in the image */
            for (col = 0; col < ncols; col++)
            {
                for (ib = 0; ib < BI_REFL_BAND_COUNT - 1; ib++)
                {
                    if (input->buf[ib][col] == input->meta.satu_value_ref[ib])
                        input->buf[ib][col] = input->meta.satu_value_max[ib];
                }
                if (input->therm_buf[col] == input->meta.therm_satu_value_ref)
                    input->therm_buf[col] = input->meta.therm_satu_value_max;

                if (pixel_mask[row][col] & (1 << WATER_BIT))
                {
                    /* Get cloud prob over water */
                    /* Temperature test over water */
                    wtemp_prob = (t_wtemp - (float) input->therm_buf[col]) /
                        400.0;
                    if (wtemp_prob < MINSIGMA)
                        wtemp_prob = 0.0;

                    /* Brightness test (over water) */
                    t_bright = 1100;
                    brightness_prob = (float) input->buf[BI_SWIR_1][col] /
                        (float) t_bright;
                    if ((brightness_prob - 1.0) > MINSIGMA)
                        brightness_prob = 1.0;
                    if (brightness_prob < MINSIGMA)
                        brightness_prob = 0.0;

                    /*Final prob mask (water), cloud over water probability */
                    wfinal_prob[row][col] =
                        100.0 * wtemp_prob * brightness_prob;
                    final_prob[row][col] = 0.0;
                }
                else
                {
                    temp_prob =
                        (*t_temph - (float) input->therm_buf[col]) / temp_l;
                    /* Temperature can have prob > 1 */
                    if (temp_prob < MINSIGMA)
                        temp_prob = 0.0;

                    /* label the non-fill pixels
                       Due to a problem with the input LPGS data, the thermal
                       band may have values less than -9999 after scaling so
                       exclude those as well */
                    if (input->therm_buf[col] <= -9999
                        || input->buf[BI_BLUE][col] == -9999
                        || input->buf[BI_GREEN][col] == -9999
                        || input->buf[BI_RED][col] == -9999
                        || input->buf[BI_NIR][col] == -9999
                        || input->buf[BI_SWIR_1][col] == -9999
                        || input->buf[BI_SWIR_2][col] == -9999)
                    {
                        mask = 0;
                    }
                    else
                        mask = 1;

                    if ((input->buf[BI_RED][col]
                         + input->buf[BI_NIR][col]) != 0
                        && mask == 1)
                    {
                        ndvi = (float) (input->buf[BI_NIR][col]
                                        - input->buf[BI_RED][col])
                               / (float) (input->buf[BI_NIR][col]
                                          + input->buf[BI_RED][col]);
                    }
                    else
                        ndvi = 0.01;

                    if ((input->buf[BI_GREEN][col]
                         + input->buf[BI_SWIR_1][col]) != 0
                        && mask == 1)
                    {
                        ndsi = (float) (input->buf[BI_GREEN][col]
                                        - input->buf[BI_SWIR_1][col])
                               / (float) (input->buf[BI_GREEN][col]
                                          + input->buf[BI_SWIR_1][col]);
                    }
                    else
                        ndsi = 0.01;

                    /* NDVI and NDSI should not be negative */
                    if (ndsi < MINSIGMA)
                        ndsi = 0.0;
                    if (ndvi < MINSIGMA)
                        ndvi = 0.0;

                    visi_mean = (input->buf[BI_BLUE][col]
                                 + input->buf[BI_GREEN][col]
                                 + input->buf[BI_RED][col]) / 3.0;
                    if (visi_mean != 0)
                    {
                        whiteness =
                            ((fabs ((float) input->buf[BI_BLUE][col]
                                    - visi_mean)
                              + fabs ((float) input->buf[BI_GREEN][col]
                                      - visi_mean)
                              + fabs ((float) input->buf[BI_RED][col]
                                      - visi_mean))) / visi_mean;
                    }
                    else
                        whiteness = 0.0;

                    /* If one visible band is saturated, whiteness = 0 */
                    if ((input->buf[BI_BLUE][col]
                         >= (input->meta.satu_value_max[BI_BLUE] - 1))
                        ||
                        (input->buf[BI_GREEN][col]
                         >= (input->meta.satu_value_max[BI_GREEN] - 1))
                        ||
                        (input->buf[BI_RED][col]
                         >= (input->meta.satu_value_max[BI_RED] - 1)))
                    {
                        whiteness = 0.0;
                    }

                    /* Vari_prob=1-max(max(abs(NDSI),abs(NDVI)),whiteness); */
                    if ((ndsi - ndvi) > MINSIGMA)
                        max_value = ndsi;
                    else
                        max_value = ndvi;
                    if ((whiteness - max_value) > MINSIGMA)
                        max_value = whiteness;
                    vari_prob = 1.0 - max_value;

                    /*Final prob mask (land) */
                    final_prob[row][col] = 100.0 * (temp_prob * vari_prob);
                    wfinal_prob[row][col] = 0.0;
                }
            }
        }
        printf ("\n");

        /* Allocate memory for prob */
        prob = malloc (input->size.l * input->size.s * sizeof (float));
        if (prob == NULL)
        {
            sprintf (errstr, "Allocating prob memory");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        float prob_max = 0.0;
        float prob_min = 0.0;
        int index3 = 0;
        for (row = 0; row < nrows; row++)
        {
            for (col = 0; col < ncols; col++)
            {
                if (clear_mask[row][col] & (1 << CLEAR_LAND_BIT))
                {
                    prob[index3] = final_prob[row][col];
                    if ((prob[index3] - prob_max) > MINSIGMA)
                        prob_max = prob[index3];
                    if ((prob_min - prob[index3]) > MINSIGMA)
                        prob_min = prob[index3];
                    index3++;
                }
            }
        }

        /* Dynamic threshold for land */
        if (index3 != 0)
        {
            status = prctile2 (prob, index3, prob_min, prob_max, 100.0 * h_pt,
                               &clr_mask);
            if (status != SUCCESS)
            {
                sprintf (errstr, "Error calling prctile2 routine");
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }
        }
        else
        {
            clr_mask = 27.5; /* no clear land pixel, make clr_mask double of
                                cloud_prob_threshold */
        }
        clr_mask += cloud_prob_threshold;

        /* Relase memory for prob */
        free (prob);
        prob = NULL;

        /* Allocate memory for wprob */
        wprob = malloc (input->size.l * input->size.s * sizeof (float));
        if (wprob == NULL)
        {
            sprintf (errstr, "Allocating wprob memory");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        float wprob_max = 0.0;
        float wprob_min = 0.0;
        int index4 = 0;
        for (row = 0; row < nrows; row++)
        {
            for (col = 0; col < ncols; col++)
            {
                if (clear_mask[row][col] & (1 << CLEAR_WATER_BIT))
                {
                    wprob[index4] = wfinal_prob[row][col];
                    if ((wprob[index4] - wprob_max) > MINSIGMA)
                        wprob_max = wprob[index4];
                    if ((wprob_min - wprob[index4]) > MINSIGMA)
                        wprob_min = wprob[index4];
                    index4++;
                }
            }
        }

        /* Dynamic threshold for water */
        if (index4 != 0)
        {
            status =
                prctile2 (wprob, index4, wprob_min, wprob_max, 100.0 * h_pt,
                          &wclr_mask);
            if (status != SUCCESS)
            {
                sprintf (errstr, "Error calling prctile2 routine");
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }
        }
        else
        {
            wclr_mask = 27.5; /* no clear water pixel, make wclr_mask 27.5 */
        }
        wclr_mask += cloud_prob_threshold;

        /* Relase memory for wprob */
        free (wprob);
        wprob = NULL;

        if (verbose)
        {
            printf ("pcloud probability threshold (land) = %.2f\n", clr_mask);
            printf ("pcloud probability threshold (water) = %.2f\n",
                    wclr_mask);

            printf ("The fourth pass\n");
        }

        /* Loop through each line in the image */
        for (row = 0; row < nrows; row++)
        {
            if (verbose)
            {
                /* Print status on every 1000 lines */
                if (!(row % 1000))
                {
                    printf ("Processing line %d\r", row);
                    fflush (stdout);
                }
            }

            /* For the thermal band, data is read into input->therm_buf */
            if (!GetInputThermLine (input, row))
            {
                sprintf (errstr, "Reading input thermal data for line %d",
                         row);
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }

            for (col = 0; col < ncols; col++)
            {
                if (input->therm_buf[col] == input->meta.therm_satu_value_ref)
                    input->therm_buf[col] = input->meta.therm_satu_value_max;

                if (((pixel_mask[row][col] & (1 << CLOUD_BIT)) &&
                     final_prob[row][col] > clr_mask &&
                     (!(pixel_mask[row][col] & (1 << WATER_BIT)))) ||
                    ((pixel_mask[row][col] & (1 << CLOUD_BIT)) &&
                     wfinal_prob[row][col] > wclr_mask &&
                     (pixel_mask[row][col] & (1 << WATER_BIT)))
                    || (input->therm_buf[col] < *t_templ + t_buffer - 3500))
                    pixel_mask[row][col] |= 1 << CLOUD_BIT;
                else
                    pixel_mask[row][col] &= ~(1 << CLOUD_BIT);
            }
        }
        printf ("\n");

        /* Free the memory */
        status = free_2d_array ((void **) wfinal_prob);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Freeing memory: wfinal_prob\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = free_2d_array ((void **) final_prob);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Freeing memory: final_prob\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Band 4 &5 flood fill */
        nir = calloc (input->size.l * input->size.s, sizeof (int16));
        swir = calloc (input->size.l * input->size.s, sizeof (int16));
        if (nir == NULL || swir == NULL)
        {
            sprintf (errstr, "Allocating nir and swir memory");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Open the intermediate file for writing */
        FILE *fd1;
        FILE *fd2;
        fd1 = fopen ("b4.bin", "wb");
        if (fd1 == NULL)
        {
            sprintf (errstr, "Opening file: b4.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        fd2 = fopen ("b5.bin", "wb");
        if (fd2 == NULL)
        {
            sprintf (errstr, "Opening file: b5.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        if (verbose)
            printf ("The fifth pass\n");

        int16 nir_max = 0;
        int16 nir_min = 0;
        int16 swir_max = 0;
        int16 swir_min = 0;
        int idx = 0;
        int idx2 = 0;
        /* Loop through each line in the image */
        for (row = 0; row < nrows; row++)
        {
            if (verbose)
            {
                /* Print status on every 1000 lines */
                if (!(row % 1000))
                {
                    printf ("Processing line %d\r", row);
                    fflush (stdout);
                }
            }

            /* For each of the image bands */
            for (ib = 0; ib < input->nband; ib++)
            {
                /* Read each input reflective band -- data is read into
                   input->buf[ib] */
                if (!GetInputLine (input, ib, row))
                {
                    sprintf (errstr, "Reading input image data for line %d, "
                             "band %d", row, ib);
                    RETURN_ERROR (errstr, "pcloud", FAILURE);
                }
            }

            for (col = 0; col < ncols; col++)
            {
                if (input->buf[BI_NIR][col]
                    == input->meta.satu_value_ref[BI_NIR])
                {
                    input->buf[BI_NIR][col] =
                        input->meta.satu_value_max[BI_NIR];
                }
                if (input->buf[BI_SWIR_1][col]
                    == input->meta.satu_value_ref[BI_SWIR_1])
                {
                    input->buf[BI_SWIR_1][col] =
                        input->meta.satu_value_max[BI_SWIR_1];
                }

                if (clear_mask[row][col] & (1 << CLEAR_LAND_BIT))
                {
                    nir[idx] = input->buf[BI_NIR][col];
                    if (nir[idx] > nir_max)
                        nir_max = nir[idx];
                    if (nir[idx] < nir_min)
                        nir_min = nir[idx];
                    idx++;
                }

                if (clear_mask[row][col] & (1 << CLEAR_LAND_BIT))
                {
                    swir[idx2] = input->buf[BI_SWIR_1][col];
                    if (swir[idx2] > swir_max)
                        swir_max = swir[idx2];
                    if (swir[idx2] < swir_min)
                        swir_min = swir[idx2];
                    idx2++;
                }
            }

            /* Write out the intermediate file */
            status = fwrite (&input->buf[BI_NIR][0], sizeof (int16),
                             input->size.s, fd1);
            if (status != input->size.s)
            {
                sprintf (errstr, "Writing file: b4.bin\n");
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }
            status = fwrite (&input->buf[BI_SWIR_1][0], sizeof (int16),
                             input->size.s, fd2);
            if (status != input->size.s)
            {
                sprintf (errstr, "Writing file: b5.bin\n");
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }
        }
        printf ("\n");

        /* Close the intermediate file */
        status = fclose (fd1);
        if (status)
        {
            sprintf (errstr, "Closing file: b4.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = fclose (fd2);
        if (status)
        {
            sprintf (errstr, "Closing file: b5.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Estimating background (land) Band 4 Ref */
        status =
            prctile (nir, idx + 1, nir_min, nir_max, 100.0 * l_pt, &backg_b4);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Calling prctile function\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status =
            prctile (swir, idx2 + 1, swir_min, swir_max, 100.0 * l_pt,
                     &backg_b5);
        if (status != SUCCESS)
        {
            sprintf (errstr, "Calling prctile function\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Release the memory */
        free (nir);
        free (swir);
        nir = NULL;
        swir = NULL;

        /* Write out the intermediate values */
        fd1 = fopen ("b4_b5.txt", "w");
        if (fd1 == NULL)
        {
            sprintf (errstr, "Opening file: b4_b5.txt\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Write out the intermediate file */
        fprintf (fd1, "%f\n", backg_b4);
        fprintf (fd1, "%f\n", backg_b5);
        fprintf (fd1, "%d\n", input->size.l);
        fprintf (fd1, "%d\n", input->size.s);

        /* Close the intermediate file */
        status = fclose (fd1);
        if (status)
        {
            sprintf (errstr, "Closing file: b4_b5.txt\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Call the fill minima routine to do image fill */
        status = system ("run_fillminima.py");
        if (status != SUCCESS)
        {
            sprintf (errstr, "Running run_fillminima.py routine\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Open the intermediate file for reading */
        fd1 = fopen ("filled_b4.bin", "rb");
        if (fd1 == NULL)
        {
            sprintf (errstr, "Opening file: filled_b4.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        fd2 = fopen ("filled_b5.bin", "rb");
        if (fd2 == NULL)
        {
            sprintf (errstr, "Opening file: filled_b5.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* May need allocate two memory for new band 4 and 5 */
        int16 *new_nir;
        int16 *new_swir;

        new_nir = (int16 *) malloc (input->size.s * sizeof (int16));
        new_swir = (int16 *) malloc (input->size.s * sizeof (int16));
        if (new_nir == NULL || new_swir == NULL)
        {
            sprintf (errstr, "Allocating new_nir/new_swir memory");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        if (verbose)
            printf ("The sixth pass\n");

        for (row = 0; row < nrows; row++)
        {
            if (verbose)
            {
                /* Print status on every 1000 lines */
                if (!(row % 1000))
                {
                    printf ("Processing line %d\r", row);
                    fflush (stdout);
                }
            }

            /* For each of the image bands */
            for (ib = 0; ib < input->nband; ib++)
            {
                /* Read each input reflective band -- data is read into
                   input->buf[ib] */
                if (!GetInputLine (input, ib, row))
                {
                    sprintf (errstr, "Reading input image data for line %d, "
                             "band %d", row, ib);
                    RETURN_ERROR (errstr, "pcloud", FAILURE);
                }
            }

            /* For the thermal band, data is read into input->therm_buf */
            if (!GetInputThermLine (input, row))
            {
                sprintf (errstr, "Reading input thermal data for line %d",
                         row);
                RETURN_ERROR (errstr, "pcloud", FAILURE);
            }

            /* Read out the intermediate file */
            fread (&new_nir[0], sizeof (int16), input->size.s, fd1);
            fread (&new_swir[0], sizeof (int16), input->size.s, fd2);

            for (col = 0; col < ncols; col++)
            {
                if (input->buf[BI_NIR][col]
                    == input->meta.satu_value_ref[BI_NIR])
                {
                    input->buf[BI_NIR][col] =
                        input->meta.satu_value_max[BI_NIR];
                }
                if (input->buf[BI_SWIR_1][col]
                    == input->meta.satu_value_ref[BI_SWIR_1])
                {
                    input->buf[BI_SWIR_1][col] =
                        input->meta.satu_value_max[BI_SWIR_1];
                }

                /* process non-fill pixels only
                   Due to a problem with the input LPGS data, the thermal
                   band may have values less than -9999 after scaling so
                   exclude those as well */
                if (input->therm_buf[col] <= -9999
                    || input->buf[BI_BLUE][col] == -9999
                    || input->buf[BI_GREEN][col] == -9999
                    || input->buf[BI_RED][col] == -9999
                    || input->buf[BI_NIR][col] == -9999
                    || input->buf[BI_SWIR_1][col] == -9999
                    || input->buf[BI_SWIR_2][col] == -9999)
                {
                    mask = 0;
                }
                else
                    mask = 1;

                if (mask == 1)
                {
                    new_nir[col] -= input->buf[BI_NIR][col];
                    new_swir[col] -= input->buf[BI_SWIR_1][col];

                    if (new_nir[col] < new_swir[col])
                        shadow_prob = new_nir[col];
                    else
                        shadow_prob = new_swir[col];

                    if (shadow_prob > 200)
                        pixel_mask[row][col] |= 1 << SHADOW_BIT;
                    else
                        pixel_mask[row][col] &= ~(1 << SHADOW_BIT);
                }
                else
                {
                    pixel_mask[row][col] |= 1 << FILL_BIT;
                    pixel_mask[row][col] &= ~(1 << CLOUD_BIT);
                    pixel_mask[row][col] &= ~(1 << SHADOW_BIT);
                    pixel_mask[row][col] &= ~(1 << WATER_BIT);
                    pixel_mask[row][col] &= ~(1 << SNOW_BIT);
                }

                /* refine Water mask (no confusion water/cloud) */
                if ((pixel_mask[row][col] & (1 << WATER_BIT)) &&
                    (pixel_mask[row][col] & (1 << CLOUD_BIT)))
                    pixel_mask[row][col] &= ~(1 << WATER_BIT);
            }
        }
        printf ("\n");

        /* Release the memory */
        free (new_nir);
        free (new_swir);
        new_nir = NULL;
        new_swir = NULL;

        /* Close the intermediate file */
        status = fclose (fd1);
        if (status)
        {
            sprintf (errstr, "Closing file: filled_b4.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = fclose (fd2);
        if (status)
        {
            sprintf (errstr, "Closing file: filled_b5.bin\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }

        /* Remove the intermediate files */
        status = system ("rm b4_b5.txt");
        if (status != SUCCESS)
        {
            sprintf (errstr, "Removing b4_b5.txt file\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = system ("rm b4.bin");
        if (status != SUCCESS)
        {
            sprintf (errstr, "Removing b4.bin file\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = system ("rm b5.bin");
        if (status != SUCCESS)
        {
            sprintf (errstr, "Removing b5.bin file\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = system ("rm filled_b4.bin");
        if (status != SUCCESS)
        {
            sprintf (errstr, "Removing filled_b4.bin file\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
        status = system ("rm filled_b5.bin");
        if (status != SUCCESS)
        {
            sprintf (errstr, "Removing filled_b5.bin file\n");
            RETURN_ERROR (errstr, "pcloud", FAILURE);
        }
    }

    status = free_2d_array ((void **) clear_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing memory: clear_mask\n");
        RETURN_ERROR (errstr, "pcloud", FAILURE);
    }

    return SUCCESS;
}
Exemplo n.º 14
0
/******************************************************************************
METHOD:  cfmask

PURPOSE:  the main routine for fmask in C

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the cfmask
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
3/15/2013   Song Guo         Original Development
5/14/2013   Song Guo         Added in Polar Stereographic support
7/17/2013   Song Guo         Added in Map info 
8/15/2013   Song Guo         Modified to use TOA reflectance file 
                             as input instead of metadata file

NOTES: type ./cfmask --help for information to run the code
******************************************************************************/
int main (int argc, char *argv[])
{
    char errstr[MAX_STR_LEN];           /* error string */
    char lndth_name[MAX_STR_LEN];       /* ledaps Brightness Temperature file */
    char fmask_name[MAX_STR_LEN];       /* output fmask binary file name */
    char fmask_header[MAX_STR_LEN];     /* output fmask binary file header */
    char fmask_hdf_name[MAX_STR_LEN];   /* output fmask HDF file name */
    char fmask_hdf_hdr[MAX_STR_LEN];    /* output fmask HDF file header */
    char *lndcal_name = NULL;           /* input lndcal data filename */
    char directory[MAX_STR_LEN];        /* input/output data directory */
    char extension[MAX_STR_LEN];        /* input TOA file extension */
    int ib;                             /* band counters */
    char sds_names[NBAND_REFL_MAX][MAX_STR_LEN]; /* array of image SDS names */
    Input_t *input = NULL;              /* input data and meta data */
    char  scene_name[MAX_STR_LEN];      /* input data scene name */
    char *hdf_grid_name = "Grid";  /* name of the grid for HDF-EOS */
    unsigned char **cloud_mask;    /* cloud pixel mask */
    unsigned char **shadow_mask;   /* shadow pixel mask */
    unsigned char **snow_mask;     /* snow pixel mask */
    unsigned char **water_mask;    /* water pixel mask */
    int status;                    /* return value from function call */
    FILE *fd = NULL;               /* file pointer */
    float ptm;                     /* percent of clear-sky pixels */
    float t_templ = 0.0;     /* percentile of low background temperature */
    float t_temph = 0.0;     /* percentile of high background temperature */
    int out_sds_types[NUM_OUT_SDS];     /* array of image SDS types */
    char *sds_name="fmask_band";        /* Fmask hdf SDS name */
    Output_t *output = NULL;            /* output structure and metadata */
    bool verbose;            /* verbose flag for printing messages */
    bool write_binary;       /* should we write raw binary output? */
    bool no_hdf_output;      /* should we don't write HDF4 output file? */
    int cldpix = 2;          /* Default buffer for cloud pixel dilate */
    int sdpix = 2;           /* Default buffer for shadow pixel dilate */
    float cloud_prob;        /* Default cloud probability */
    Space_def_t space_def;   /* spatial definition information */
    float sun_azi_temp = 0.0;/* Keep the original sun azimuth angle */
  
    time_t now;
    time(&now);
    printf("CFmask start_time=%s\n",ctime(&now));

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    status = get_args (argc, argv, &lndcal_name, &cloud_prob, &cldpix,
                       &sdpix, &write_binary, &no_hdf_output, &verbose);
    if (status != SUCCESS)
    { 
        sprintf (errstr, "calling get_args");
        ERROR (errstr, "main");
    }

    split_filename(lndcal_name, directory, scene_name, extension);
    if (verbose)
        printf("directory, scene_name, extension=%s,%s,%s\n", 
            directory, scene_name, extension);
    sprintf(lndcal_name, "%slndcal.%s.hdf", directory, scene_name);
    sprintf(lndth_name, "%slndth.%s.hdf", directory, scene_name);
    sprintf(fmask_name, "%sfmask.%s.img", directory, scene_name);
    sprintf(fmask_header, "%sfmask.%s.img.hdr", directory, scene_name);
    sprintf(fmask_hdf_name, "%sfmask.%s.hdf", directory, scene_name);
    if (verbose)
    {
        printf("lndcal_name, lndth_name = %s, %s\n", lndcal_name, lndth_name); 
        printf("fmask_name, fmask_header, fmask_hdf_name = %s, %s, %s\n", 
                fmask_name, fmask_header, fmask_hdf_name); 
    }

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput(lndth_name, lndcal_name);
    if (input == NULL)
    {
        sprintf (errstr, "opening the input files: %s & %s", lndth_name,
                 lndcal_name);
        ERROR (errstr, "main");
    }

    /* Get the projection and spatial information from the input TOA
       reflectance product */
    status = get_space_def_hdf(&space_def, lndcal_name, hdf_grid_name);
    if (status != SUCCESS)
    {
        sprintf(errstr, "Reading spatial metadata from the HDF file: %s", 
               lndcal_name);
        ERROR (errstr, "main");
    }
    input->meta.zone = space_def.zone;

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("DEBUG: Number of input TOA bands: %d\n", input->nband);
        printf ("DEBUG: Number of input thermal bands: %d\n", 1);
        printf ("DEBUG: Number of input lines: %d\n", input->size.l);
        printf ("DEBUG: Number of input samples: %d\n", input->size.s);
        printf ("DEBUG: Number of input TOA lines: %d\n", input->toa_size.l);
        printf ("DEBUG: Number of input TOA samples: %d\n", input->toa_size.s);
        printf ("DEBUG: Provider is %s\n", input->meta.provider);
        printf ("DEBUG: Satellite is %s\n", input->meta.sat);
        printf ("DEBUG: Instrument is %s\n", input->meta.inst);
        printf ("DEBUG: ACQUISITION_DATE.DOY is %d\n", input->meta.acq_date.doy);
        printf ("DEBUG: WRS system is %s\n", input->meta.wrs_sys);
        printf ("DEBUG: Path is %d\n", input->meta.path);
        printf ("DEBUG: Row is %d\n", input->meta.row);
        printf ("DEBUG: Fill value is %d\n", input->meta.fill);
        for (ib = 0; ib < input->nband; ib++)
        {
            printf ("DEBUG: Band %d-->\n", ib);
            printf ("DEBUG:   SDS name is %s\n", input->sds[ib].name);
            printf ("DEBUG:   SDS rank: %d\n", input->sds[ib].rank);
            printf ("DEBUG:   band satu_value_ref: %d\n", 
                   input->meta.satu_value_ref[ib]);
            printf ("DEBUG:   band satu_value_max: %d\n", 
                   input->meta.satu_value_max[ib]);
            printf ("DEBUG:   band gains: %f, band biases: %f\n", 
                    input->meta.gain[ib], input->meta.bias[ib]);      
        }
        printf ("DEBUG: Thermal Band -->\n");
        printf ("DEBUG:   SDS name is %s\n", input->therm_sds.name);
        printf ("DEBUG:   SDS rank: %d\n", input->therm_sds.rank);
        printf ("DEBUG:   therm_satu_value_ref: %d\n", 
                   input->meta.therm_satu_value_ref);
        printf ("DEBUG:   therm_satu_value_max: %d\n", 
                   input->meta.therm_satu_value_max);
        printf ("DEBUG:   therm_gain: %f, therm_bias: %f\n", 
                input->meta.gain_th, input->meta.bias_th);

        printf("DEBUG: ROW is %d\n", input->meta.row);
        printf("DEBUG: PATH is %d\n", input->meta.path);
        printf("DEBUG: SUN AZIMUTH is %f\n", input->meta.sun_az);
        printf("DEBUG: SUN ZENITH is %f\n", input->meta.sun_zen);
        printf("DEBUG: Projection Zone is %d\n", input->meta.zone);

        printf("DEBUG: unit_ref is %s\n", input->meta.unit_ref);
        printf("DEBUG: valid_range_ref is %f & %f\n", 
           input->meta.valid_range_ref[0], 
           input->meta.valid_range_ref[1]);
    }

    /* If the scene is an ascending polar scene (flipped upside down), then
       the solar azimuth needs to be adjusted by 180 degrees.  The scene in
       this case would be north down and the solar azimuth is based on north
       being up clock-wise direction. Flip the south to be up will not change 
       the actual sun location, with the below relations, the solar azimuth
       angle will need add in 180.0 for correct sun location */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
    {
        /* Keep the original solar azimuth angle */
        sun_azi_temp = input->meta.sun_az;
        input->meta.sun_az += 180.0;
        if ((input->meta.sun_az - 360.0) > MINSIGMA)
            input->meta.sun_az -= 360.0;
        if (verbose)
            printf ("  Polar or ascending scene.  Readjusting solar azimuth by "
                "180 degrees.\n  New value: %f degrees\n", input->meta.sun_az);
    }

    /* Copy the SDS names and QA SDS names from the input structure for the
       output structure, since we are simply duplicating the input */
    for (ib = 0; ib < input->nband; ib++)
        strcpy (&sds_names[ib][0], input->sds[ib].name);

    /* Dynamic allocate the 2d mask memory */
    cloud_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    shadow_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    snow_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    water_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    if (cloud_mask == NULL  || shadow_mask == NULL || snow_mask == NULL
        || water_mask == NULL)
    {
        sprintf (errstr, "Allocating mask memory");
        ERROR (errstr, "main");
    }

    /* Build the potential cloud, shadow, snow, water mask */
    status = potential_cloud_shadow_snow_mask(input, cloud_prob, &ptm,
             &t_templ, &t_temph, cloud_mask, shadow_mask, snow_mask, 
             water_mask, verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing potential_cloud_shadow_snow_mask");
        ERROR (errstr, "main");
    }

    printf("Pcloud done, starting cloud/shadow match\n");


    /* Build the final cloud shadow based on geometry matching and
       combine the final cloud, shadow, snow, water masks into fmask */
    status = object_cloud_shadow_match(input, ptm, t_templ, t_temph,
             cldpix, sdpix, cloud_mask, shadow_mask, snow_mask, water_mask,
             verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing object_cloud_and_shadow_match");
        ERROR (errstr, "main");
    }    

    status = free_2d_array((void **)shadow_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }
    status = free_2d_array((void **)snow_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }
    status = free_2d_array((void **)water_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }

    /* Reassign solar azimuth angle for output purpose if south up north 
       down scene is involved */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
        input->meta.sun_az = sun_azi_temp;
   
    if (write_binary)
    {
        /* Create an ENVI header file for the binary fmask */
        status = write_envi_hdr(fmask_header, BINARY_FILE, input, &space_def);
        if (status != SUCCESS)
        {
            sprintf(errstr, "Creating ENVI header for binary fmask");
            ERROR (errstr, "main");
        }

        /* Open the mask file for writing */
        fd = fopen(fmask_name, "w"); 
        if (fd == NULL)
        {
            sprintf(errstr, "Opening report file: %s", fmask_name);
            ERROR (errstr, "main");
        }

        /* Write out the mask file */
        status = fwrite(&cloud_mask[0][0], sizeof(unsigned char),
            input->size.l * input->size.s, fd);
        if (status != input->size.l * input->size.s)
        {
            sprintf(errstr, "Writing to %s", fmask_name);
            ERROR (errstr, "main");
        }

        /* Close the mask file */
        status = fclose(fd);
        if (status)
        {
            sprintf(errstr, "Closing file %s", fmask_name);
            ERROR (errstr, "main");
        }
    }

    if (!no_hdf_output)
    {
        /* Create and open fmask HDF output file */
        if (!CreateOutput(fmask_hdf_name))
        {
            sprintf(errstr, "Creating HDF fmask output file");
            ERROR (errstr, "main");
        }
        output = OpenOutput (fmask_hdf_name, sds_name, &input->size);
        if (output == NULL)
        {
            sprintf (errstr, "opening output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }

        if (!PutOutput(output, cloud_mask))
        {
            sprintf (errstr, "Writing output fmask in HDF files\n");
            ERROR (errstr, "main");
        }

        if (!PutMetadata(output, input))
        {
            sprintf (errstr, "Writing output fmask metadata in HDF files\n");
            ERROR (errstr, "main");
        }

        /* Close the output file and free the structure */
        if (!CloseOutput (output))
        {
            sprintf (errstr, "closing output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }
        if (!FreeOutput (output))
        {
            sprintf (errstr, "freeing output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }

        /* Write the spatial information, after the file has been closed */
        out_sds_types[0] = DFNT_UINT8;
        status = put_space_def_hdf (&space_def, fmask_hdf_name, NUM_OUT_SDS, 
            out_sds_names, out_sds_types, hdf_grid_name);
        if (status != SUCCESS)
        {
            sprintf("Putting spatial metadata to the HDF file: "
            "%s", lndcal_name);
            ERROR (errstr, "main");
        }

        /* Write CFmask HDF header to add in envi map info */
        sprintf (fmask_hdf_hdr, "%s.hdr", fmask_hdf_name);
        status = write_envi_hdr (fmask_hdf_hdr, HDF_FILE, input, &space_def);
        if (status != SUCCESS)
        {
            sprintf(errstr, "Error writing the ENVI header for CFmask HDF hdr");
            ERROR (errstr, "main");
        }
    }

    /* Free the final output cloud_mask */
    status = free_2d_array((void **)cloud_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    free(lndcal_name);
    printf ("Processing complete.\n");
    time(&now);
    printf("CFmask end_time=%s\n",ctime(&now));
    return (SUCCESS);
}
Exemplo n.º 15
0
/******************************************************************************
METHOD:  l8cfmask

PURPOSE:  the main routine for fmask in C

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the l8cfmask
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
3/15/2013   Song Guo         Original Development
5/14/2013   Song Guo         Added in Polar Stereographic support
7/17/2013   Song Guo         Added in Map info 
8/15/2013   Song Guo         Modified to use TOA reflectance file 
                             as input instead of metadata file
Oct/2014    Ron Dilley       Modified to utilize the ESPA internal raw binary
                             file format

NOTES: type ./l8cfmask --help for information to run the code
******************************************************************************/
int
main (int argc, char *argv[])
{
    char errstr[MAX_STR_LEN];     /* error string */
    char *cptr = NULL;            /* pointer to the file extension */
    char *xml_name = NULL;        /* input XML filename */
    char directory[MAX_STR_LEN];  /* input/output data directory */
    char extension[MAX_STR_LEN];  /* input TOA file extension */
    int ib;                       /* band counters */
    Input_t *input = NULL;        /* input data and meta data */
    char envi_file[MAX_STR_LEN];  /* output ENVI file name */
    char scene_name[MAX_STR_LEN]; /* input data scene name */
    unsigned char **pixel_mask;   /* pixel mask */
    unsigned char **conf_mask;    /* confidence mask */
    int status;               /* return value from function call */
    float clear_ptm;          /* percent of clear-sky pixels */
    float t_templ = 0.0;      /* percentile of low background temperature */
    float t_temph = 0.0;      /* percentile of high background temperature */
    Output_t *output = NULL;  /* output structure and metadata */
    bool verbose;             /* verbose flag for printing messages */
    bool use_l8_cirrus;       /* should we use L8 cirrus cloud bit results? */
    int cldpix = 2;           /* Default buffer for cloud pixel dilate */
    int sdpix = 2;            /* Default buffer for shadow pixel dilate */
    float cloud_prob;         /* Default cloud probability */
    float sun_azi_temp = 0.0; /* Keep the original sun azimuth angle */
    int max_cloud_pixels; /* Maximum cloud pixel number in cloud division */
    Espa_internal_meta_t xml_metadata; /* XML metadata structure */
    Envi_header_t envi_hdr;            /* output ENVI header information */

    time_t now;
    time (&now);
    printf ("CFmask start_time=%s\n", ctime (&now));

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    status = get_args (argc, argv, &xml_name, &cloud_prob, &cldpix,
                       &sdpix, &max_cloud_pixels, &use_l8_cirrus, &verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "calling get_args");
        CFMASK_ERROR (errstr, "main");
    }

    /* Validate the input metadata file */
    if (validate_xml_file (xml_name) != SUCCESS)
    {                           /* Error messages already written */
        CFMASK_ERROR (errstr, "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 (xml_name, &xml_metadata) != SUCCESS)
    {                           /* Error messages already written */
        CFMASK_ERROR (errstr, "main");
    }

    /* Verify supported satellites */
    if (strcmp (xml_metadata.global.satellite, "LANDSAT_8") != 0)
    {
        sprintf (errstr, "Unsupported satellite sensor");
        CFMASK_ERROR (errstr, "main");
    }

    /* Split the filename to obtain the directory, scene name, and extension */
    split_filename (xml_name, directory, scene_name, extension);
    if (verbose)
        printf ("directory, scene_name, extension=%s,%s,%s\n",
                directory, scene_name, extension);

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput (&xml_metadata);
    if (input == NULL)
    {
        sprintf (errstr, "opening the TOA and brightness temp files in: %s",
                 xml_name);
        CFMASK_ERROR (errstr, "main");
    }

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("DEBUG: Number of input TOA bands: %d\n", input->nband);
        printf ("DEBUG: Number of input thermal bands: %d\n", 1);
        printf ("DEBUG: Number of input TOA lines: %d\n", input->size.l);
        printf ("DEBUG: Number of input TOA samples: %d\n", input->size.s);
        printf ("DEBUG: ACQUISITION_DATE.DOY is %d\n",
                input->meta.acq_date.doy);
        printf ("DEBUG: Fill value is %d\n", input->meta.fill);
        for (ib = 0; ib < input->nband; ib++)
        {
            printf ("DEBUG: Band %d-->\n", ib);
            printf ("DEBUG:   band satu_value_ref: %d\n",
                    input->meta.satu_value_ref[ib]);
            printf ("DEBUG:   band satu_value_max: %d\n",
                    input->meta.satu_value_max[ib]);
            printf ("DEBUG:   band gains: %f, band biases: %f\n",
                    input->meta.gain[ib], input->meta.bias[ib]);
        }
        printf ("DEBUG: Thermal Band (Band 10) -->\n");
        printf ("DEBUG:   therm_satu_value_ref: %d\n",
                input->meta.therm_satu_value_ref);
        printf ("DEBUG:   therm_satu_value_max: %d\n",
                input->meta.therm_satu_value_max);
        printf ("DEBUG:   therm_gain: %f, therm_bias: %f\n",
                input->meta.gain_th, input->meta.bias_th);

        printf ("DEBUG: SUN AZIMUTH is %f\n", input->meta.sun_az);
        printf ("DEBUG: SUN ZENITH is %f\n", input->meta.sun_zen);
    }

    /* If the scene is an ascending polar scene (flipped upside down), then
       the solar azimuth needs to be adjusted by 180 degrees.  The scene in
       this case would be north down and the solar azimuth is based on north
       being up clock-wise direction. Flip the south to be up will not change
       the actual sun location, with the below relations, the solar azimuth
       angle will need add in 180.0 for correct sun location */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
    {
        /* Keep the original solar azimuth angle */
        sun_azi_temp = input->meta.sun_az;
        input->meta.sun_az += 180.0;
        if ((input->meta.sun_az - 360.0) > MINSIGMA)
            input->meta.sun_az -= 360.0;
        if (verbose)
            printf ("  Polar or ascending scene."
                    "  Readjusting solar azimuth by 180 degrees.\n"
                    "  New value: %f degrees\n", input->meta.sun_az);
    }

    /* Dynamic allocate the 2d mask memory */
    pixel_mask = (unsigned char **) allocate_2d_array (input->size.l,
                                                       input->size.s,
                                                       sizeof (unsigned char));
    if (pixel_mask == NULL)
    {
        sprintf (errstr, "Allocating pixel mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    conf_mask = (unsigned char **) allocate_2d_array (input->size.l,
                                                      input->size.s,
                                                      sizeof (unsigned char));
    if (conf_mask == NULL)
    {
        sprintf (errstr, "Allocating confidence mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    /* Initialize the mask to clear data */
    int row, col;
    for (row = 0; row < input->size.l; row++)
        for (col = 0; col < input->size.s; col++)
        {
            pixel_mask[row][col] = MASK_CLEAR_LAND;
            conf_mask[row][col] = CLOUD_CONFIDENCE_NONE;
        }

    /* Build the potential cloud, shadow, snow, water mask */
    status = potential_cloud_shadow_snow_mask (input, cloud_prob, &clear_ptm,
                                               &t_templ, &t_temph, pixel_mask,
                                               conf_mask, use_l8_cirrus,
                                               verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing potential_cloud_shadow_snow_mask");
        CFMASK_ERROR (errstr, "main");
    }

    printf ("Pcloud done, starting cloud/shadow match\n");

    /* Build the final cloud shadow based on geometry matching and
       combine the final cloud, shadow, snow, water masks into fmask
       the pixel_mask is a bit mask as input and a value mask as output */
    status = object_cloud_shadow_match (input, clear_ptm, t_templ, t_temph,
                                        cldpix, sdpix, max_cloud_pixels,
                                        pixel_mask, verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing object_cloud_and_shadow_match");
        CFMASK_ERROR (errstr, "main");
    }

    /* Reassign solar azimuth angle for output purpose if south up north
       down scene is involved */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
        input->meta.sun_az = sun_azi_temp;

    /* Open the output file */
    output = OpenOutput (&xml_metadata, input);
    if (output == NULL)
    {                           /* error message already printed */
        sprintf (errstr, "Opening output file");
        CFMASK_ERROR (errstr, "main");
    }

    if (!PutOutput (output, pixel_mask))
    {
        sprintf (errstr, "Writing output fmask files\n");
        CFMASK_ERROR (errstr, "main");
    }

    /* Close the output file */
    if (!CloseOutput (output))
    {
        sprintf (errstr, "closing output file");
        CFMASK_ERROR (errstr, "main");
    }

    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[0], &xml_metadata.global,
                            &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Creating ENVI header structure.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[0].file_name);
    cptr = strchr (envi_file, '.');
    if (cptr == NULL)
    {
        sprintf (errstr, "error in ENVI header filename");
        CFMASK_ERROR (errstr, "main");
    }

    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Writing ENVI header file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Append the cfmask band to the XML file */
    if (append_metadata (output->nband, output->metadata.band, xml_name)
        != SUCCESS)
    {
        sprintf (errstr, "Appending spectral index bands to XML file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Free the structure */
    if (!FreeOutput (output))
    {
        sprintf (errstr, "freeing output file structure");
        CFMASK_ERROR (errstr, "main");
    }

    output = OpenOutputConfidence (&xml_metadata, input);
    if (output == NULL)
    {                           /* error message already printed */
        sprintf (errstr, "Opening output file");
        CFMASK_ERROR (errstr, "main");
    }

    if (!PutOutput (output, conf_mask))
    {
        sprintf (errstr, "Writing output fmask files\n");
        CFMASK_ERROR (errstr, "main");
    }

    /* Close the output file */
    if (!CloseOutput (output))
    {
        sprintf (errstr, "closing output file");
        CFMASK_ERROR (errstr, "main");
    }

    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[0], &xml_metadata.global,
                            &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Creating ENVI header structure.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[0].file_name);
    cptr = strchr (envi_file, '.');
    if (cptr == NULL)
    {
        sprintf (errstr, "error in ENVI header filename");
        CFMASK_ERROR (errstr, "main");
    }

    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
    {
        sprintf (errstr, "Writing ENVI header file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Append the cfmask band to the XML file */
    if (append_metadata (output->nband, output->metadata.band, xml_name)
        != SUCCESS)
    {
        sprintf (errstr, "Appending spectral index bands to XML file.");
        CFMASK_ERROR (errstr, "main");
    }

    /* Free the structure */
    if (!FreeOutput (output))
    {
        sprintf (errstr, "freeing output file structure");
        CFMASK_ERROR (errstr, "main");
    }

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

    /* Free the pixel mask */
    status = free_2d_array ((void **) pixel_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing pixel mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    status = free_2d_array ((void **) conf_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing confidence mask memory");
        CFMASK_ERROR (errstr, "main");
    }

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    free (xml_name);

    printf ("Processing complete.\n");
    time (&now);
    printf ("CFmask end_time=%s\n", ctime (&now));

    return SUCCESS;
}
Exemplo n.º 16
0
/* This function should not be called more than once */
unsigned int init(void) {
    char **xlines, **clines;
    char *conftok = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char));
    unsigned int i = 0, xcount = 0, ccount = 0,
    acount = 0; /* Don't get this confused with the word "account" */
    extern q_maxbytes, q_maxqueue;
    
    /* For making sure when these variables
        are tested, they are set to NULL first. */
    init_handle(&q_first);
    init_handle(&q_last);
    init_handle(&event_first);
    init_handle(&event_last);
    init_handle(&chan_first);
    init_handle(&chan_last);
    init_handle(&user_first);
    init_handle(&user_last);
    init_handle(&me);
    init_handle(&bc_first);
    init_handle(&bc_last);
    init_handle(&dcc_first);
    init_handle(&dcc_last);
    
    eventloop_running = 0;
    bot.cid = 0;
    bot.connected = 0;
    
    xstrcpy(bot.config, XION_CONFIG, 260);
    
    bot.admin_current_count = 0;
    bot.current_try = 1;
    q_first = NULL;
    
    /*X:Line*/
    xcount = get_confitem(&xlines, 'X');
    if(!xcount) {
        make_error("No X:lines in configuration file.");
        freem(conftok);
        return 0;
    }
    /*nickname*/    xstrcpy(bot.nick, get_itemtok(conftok, xlines[0], 2), 32);
    
    /*altnick*/     xstrcpy(bot.altnick, get_itemtok(conftok, xlines[0], 3), 32);
    
    /*username*/    xstrcpy(bot.username, get_itemtok(conftok, xlines[0], 4), 12);
    
    /*fullname*/    xstrcpy(bot.info, get_itemtok(conftok, xlines[0], 5), 256);
                    xstrcpy(bot.info, conf_replace_alias(conftok), 256);
    
    
    /*C:Line*/
    ccount = get_confitem(&clines, 'C');
    if(!ccount) {
        make_error("No C:lines in configuration file.");
        freem(conftok);
        free_2d_array(xlines, xcount);
        return 0;
    }
    /*servaddr*/    xstrcpy(bot.servaddr, get_itemtok(conftok, clines[0], 2), 256);
    
    /*servport*/    bot.servport = atoi(get_itemtok(conftok, clines[0], 3));
    
    /*servpass*/    xstrcpy(bot.servpass, get_itemtok(conftok, clines[0], 4), 256);
    
    
    /*S:Lines*/
    /*maxretry*/    irc_subconftok(conftok, 'S', "maxretry", 3);
                    bot.maxretry = atoi(conftok);
                    
    /*pingtimeout*/ irc_subconftok(conftok, 'S', "pingtimeout", 3);
                    bot.ping_timeout = atoi(conftok);
    
    /*freshlog*/    irc_subconftok(conftok, 'S', "freshlog", 3);
                    bot.fresh_log = (atoi(conftok) ? 1 : 0);
                    
    /*antiflood*/   irc_subconftok(conftok, 'S', "antiflood", 5);
                    bot.floodcheck = (atoi(conftok) ? 1 : 0);
                    irc_subconftok(conftok, 'S', "antiflood", 3);
                    q_maxbytes = (unsigned int)atoi(conftok);
                    irc_subconftok(conftok, 'S', "antiflood", 4);
                    q_maxqueue = (unsigned int)atoi(conftok);
                    
    /*ctrigger*/    irc_subconftok(conftok, 'S', "ctrigger", 3);
                    conf_replace_alias(conftok);
                    bot.ctrigger = conftok[0];
                    
    /*ptrigger*/    irc_subconftok(conftok, 'S', "ptrigger", 3);
                    conf_replace_alias(conftok);
                    bot.ptrigger = conftok[0];
                    
    /*A:Lines*/
    if(bot.admin_array != NULL)
        free_2d_array(bot.admin_array, bot.admin_lines);
    
    acount = get_confitem(&bot.admin_array, 'A');
    bot.admin_lines = acount;
    if(acount) {
        for(i = 0;i < bot.admin_lines;i++) {
            get_itemtok(conftok, bot.admin_array[i], 3);
            if((istrcmp(conftok, ";")) || (istrcmp(conftok, "*")))
                adm_loginuser(get_itemtok(conftok, bot.admin_array[i], 2),
                                get_itemtok(conftok, bot.admin_array[i], 4),
                                atoi(get_itemtok(conftok, bot.admin_array[i], 5)));
        }
    }
    
    /* URL Module: mod-weburlcache.c */
    urlmod_init();
    
    freem(conftok);
    free_2d_array(xlines, xcount);
    free_2d_array(clines, ccount);
    
    event_call(EVENT_INIT, 0);
    
    return 1;
}
Exemplo n.º 17
0
unsigned int rehashconfig(void) {
    char **xlines;
    char *conftok = (char*)callocm(CONF_MAX_ITEMLEN, sizeof(char));
    unsigned int i = 0, xcount = 0,
    acount = 0; /* Don't get this confused with the word "account" */
    extern q_maxbytes;
    
    /*X:Line*/
    xcount = get_confitem(&xlines, 'X');
    if(!xcount) {
        make_error("No X:lines in configuration file.");
        freem(conftok);
        return 0;
    }
    /*nickname*/    xstrcpy(bot.nick, get_itemtok(conftok, xlines[0], 2), 32);
    if(!istrcmp(bot.current_nick, bot.nick))
        irc_nick(bot.nick);
    /*altnick*/     xstrcpy(bot.altnick, get_itemtok(conftok, xlines[0], 3), 32);
    
    /*S:Lines*/
    /*maxretry*/    irc_subconftok(conftok, 'S', "maxretry", 3);
                    bot.maxretry = atoi(conftok);
                    
    /*pingtimeout*/ irc_subconftok(conftok, 'S', "pingtimeout", 3);
                    bot.ping_timeout = atoi(conftok);
    
    /*antiflood*/   irc_subconftok(conftok, 'S', "antiflood", 5);
                    bot.floodcheck = (atoi(conftok) ? 1 : 0);
                    irc_subconftok(conftok, 'S', "antiflood", 3);
                    q_maxbytes = (unsigned int)atoi(conftok);
    
    /*ctrigger*/    irc_subconftok(conftok, 'S', "ctrigger", 3);
                    conf_replace_alias(conftok);
                    bot.ctrigger = conftok[0];
    
    /*ptrigger*/    irc_subconftok(conftok, 'S', "ptrigger", 3);
                    conf_replace_alias(conftok);
                    bot.ptrigger = conftok[0];
                    
    /*A:Lines*/
    if(bot.admin_array != NULL)
        free_2d_array(bot.admin_array, bot.admin_lines);
    
    acount = get_confitem(&bot.admin_array, 'A');
    bot.admin_lines = acount;
    if(acount) {
        for(i = 0;i < bot.admin_lines;i++) {
            get_itemtok(conftok, bot.admin_array[i], 3);
            if((istrcmp(conftok, ";")) || (istrcmp(conftok, "*")))
                adm_loginuser(get_itemtok(conftok, bot.admin_array[i], 2),
                                get_itemtok(conftok, bot.admin_array[i], 4),
                                atoi(get_itemtok(conftok, bot.admin_array[i], 5)));
        }
    }
    
    freem(conftok);
    free_2d_array(xlines, xcount);
    
    event_call(EVENT_REHASH, 0);
    
    return 1;
}
int calculate_point_atmospheric_parameters
(
    Input_t *input,            /* I: Input structure */
    REANALYSIS_POINTS *points, /* I: The coordinate points */
    double albedo,             /* I: Albedo */
    double **modtran_results,  /* O: Atmospheric parameters from modtran runs */
    bool verbose               /* I: Value to indicate if intermediate
                                     messages should be printed */
)
{
    char FUNC_NAME[] = "calculate_point_atmospheric_parameters";

    FILE *fd;
    FILE *used_points_fd;

    int i;
    int j;
    int k;
    int entry;

    double **spectral_response = NULL;
    double temp_radiance_0;
    double obs_radiance_0;
    double temp_radiance_273;
    double temp_radiance_310;
    int counter;
    int index;
    int num_entries;   /* Number of MODTRAN output results to read and use */
    int num_srs;       /* Number of spectral response values available */
    int result_loc;

    char *lst_data_dir = NULL;
    char current_file[PATH_MAX];
    char srs_file_path[PATH_MAX];
    char msg[PATH_MAX];

    double modtran_wavelength;
    double modtran_radiance;
    double zero_temp;
    double **current_data;
    double y_0;
    double y_1;
    double tau; /* Transmission */
    double lu;  /* Upwelled Radiance */
    double ld;  /* Downwelled Radiance */
    /* TODO TODO TODO - This emissivity/albedo is just for water which is all
                        that has been implemented.  But the goal is to be
                        doing LAND, so integration with the Aster data and
                        JPL conversion code will probably be needed before
                        execution of this routine, and then utilized here. */
    double emissivity = 1.0 - albedo;
    double inv_albedo = 1.0 / albedo;

    /* Variables to hold matricies and the results for the operations perfomed
       on them */
    double X_2x2[4];
    double Xt_2x2[4];
    double Xt_X_2x2[4];
    double Inv_Xt_X_2x2[4];
    double Y_2x1[2];
    double Xt_Y_2x1[4];
    double A_2x1[2];


    lst_data_dir = getenv ("LST_DATA_DIR");
    if (lst_data_dir == NULL)
    {
        RETURN_ERROR ("LST_DATA_DIR environment variable is not set",
                      FUNC_NAME, FAILURE);
    }

    /* Allocate memory for maximum spectral response count */
    spectral_response =
        (double **) allocate_2d_array (2, MAX_SRS_COUNT, sizeof (double));
    if (spectral_response == NULL)
    {
        RETURN_ERROR ("Allocating spectral_response memory",
                      FUNC_NAME, FAILURE);
    }

    /* Determine the spectral response file to read */
    if (input->meta.instrument == INST_TM
        && input->meta.satellite == SAT_LANDSAT_5)
    {
        num_srs = L5_TM_SRS_COUNT;

        snprintf (srs_file_path, sizeof (srs_file_path),
                  "%s/%s", lst_data_dir, "L5_Spectral_Response.rsp");
    }
    else if (input->meta.instrument == INST_ETM
             && input->meta.satellite == SAT_LANDSAT_7)
    {
        num_srs = L7_TM_SRS_COUNT;

        snprintf (srs_file_path, sizeof (srs_file_path),
                  "%s/%s", lst_data_dir, "L7_Spectral_Response.rsp");
    }
    else if (input->meta.instrument == INST_OLI_TIRS
             && input->meta.satellite == SAT_LANDSAT_8)
    {
        num_srs = L8_OLITIRS_SRS_COUNT;

        snprintf (srs_file_path, sizeof (srs_file_path),
                  "%s/%s", lst_data_dir, "L8_B10.rsp");
    }
    else
    {
        RETURN_ERROR ("invalid instrument type", FUNC_NAME, FAILURE);
    }

    snprintf (msg, sizeof (msg),
              "Reading Spectral Response File [%s]", srs_file_path);
    LOG_MESSAGE (msg, FUNC_NAME);
    fd = fopen (srs_file_path, "r");
    if (fd == NULL)
    {
        RETURN_ERROR ("Can't open Spectral Response file", FUNC_NAME, FAILURE);
    }

    for (i = 0; i < num_srs; i++)
    {
        if (fscanf (fd, "%lf %lf%*c", &spectral_response[0][i],
                    &spectral_response[1][i]) == EOF)
        {
            RETURN_ERROR ("Failed reading spectral response file",
                          FUNC_NAME, FAILURE);
        }
    }
    fclose (fd);

    /* Calculate Lt for each specific temperature */
    if (calculate_lt (273, spectral_response, num_srs, &temp_radiance_273)
        != SUCCESS)
    {
        RETURN_ERROR ("Calling calculate_lt for 273K", FUNC_NAME, FAILURE);
    }
    if (calculate_lt (310, spectral_response, num_srs, &temp_radiance_310)
        != SUCCESS)
    {
        RETURN_ERROR ("Calling calculate_lt for 310K", FUNC_NAME, FAILURE);
    }

    /* Implement a = INVERT(TRANSPOSE(x)##x)##TRANSPOSE(x)##y
       from the IDL code base.
       Partially implemented here, the variable part is implemented in the
       looping code. */
    X_2x2[0] = 1;
    X_2x2[1] = temp_radiance_273;
    X_2x2[2] = 1;
    X_2x2[3] = temp_radiance_310;

    matrix_transpose_2x2(X_2x2, Xt_2x2);
    matrix_multiply_2x2_2x2(Xt_2x2, X_2x2, Xt_X_2x2);
    matrix_inverse_2x2(Xt_X_2x2, Inv_Xt_X_2x2);

    /* Output information about the used points, primarily usefull for
       plotting them against the scene */
    used_points_fd = fopen ("used_points.txt", "w");
    if (used_points_fd == NULL)
    {
        RETURN_ERROR ("Can't open used_points.txt file",
                      FUNC_NAME, FAILURE);
    }

    /* Iterate through all points and heights */
    counter = 0;
    for (i = 0; i < points->num_points; i++)
    {
        fprintf (used_points_fd, "\"%d\"|\"%f\"|\"%f\"\n",
                 i, points->utm_easting[i], points->utm_northing[i]);

        for (j = 0; j < NUM_ELEVATIONS; j++)
        {
            result_loc = i * NUM_ELEVATIONS + j;

            /* put results into MODTRAN results array */
            modtran_results[result_loc][MGPE_LATITUDE] =
                points->modtran_runs[counter].latitude;
            modtran_results[result_loc][MGPE_LONGITUDE] =
                points->modtran_runs[counter].longitude;
            modtran_results[result_loc][MGPE_HEIGHT] =
                points->modtran_runs[counter].height;

            /* Read the lst_modtran.info file for the 000 execution
               (when MODTRAN is run at 0K)
               We read the zero_temp from this file, and also the record count
               The record count is the same for all three associated runs */
            /* The 000 file is always the "counter+2" element in the array
               at this point in the code */
            snprintf (current_file, sizeof (current_file),
                      "%s/lst_modtran.info",
                      points->modtran_runs[counter+2].path);

            fd = fopen (current_file, "r");
            if (fd == NULL)
            {
                RETURN_ERROR ("Can't open current_file file",
                              FUNC_NAME, FAILURE);
            }
            /* Retrieve the temperature from this lowest atmospheric layer */
            if (fscanf (fd, "%*s %lf%*c", &zero_temp) != 1)
            {
                RETURN_ERROR ("End of file (EOF) is met before"
                              " reading TARGET_PIXEL_SURFACE_TEMPERATURE",
                              FUNC_NAME, FAILURE);
            }
            /* determine number of entries in current file */
            if (fscanf (fd, "%*s %d%*c", &num_entries) != 1)
            {
                RETURN_ERROR ("End of file (EOF) is met before"
                              " reading RADIANCE_RECORD_COUNT",
                              FUNC_NAME, FAILURE);
            }
            fclose (fd);

            /* for each height, read in radiance inforomation for three
               modtran runs, columns of array are organized:
               wavelength | 273,0.0 | 310,0.0 | 000,0.1 */
            current_data =
                (double **) allocate_2d_array (num_entries, 4, sizeof (double));
            if (current_data == NULL)
            {
                RETURN_ERROR ("Allocating current_data memory",
                              FUNC_NAME, FAILURE);
            }

            /* iterate through the three pairs of parameters */
            for (index = 1; index < 4; index++)
            {
                /* define current file */
                snprintf (current_file, sizeof (current_file),
                          "%s/lst_modtran.dat",
                          points->modtran_runs[counter].path);

                fd = fopen (current_file, "r");
                if (fd == NULL)
                {
                    RETURN_ERROR ("Can't open current_file file",
                                  FUNC_NAME, FAILURE);
                }
                for (entry = 0; entry < num_entries; entry++)
                {
                    if (fscanf (fd, "%lf %lf%*c",
                                &modtran_wavelength, &modtran_radiance)
                        != 2)
                    {
                        RETURN_ERROR ("Failed reading lst_modtran.dat lines",
                                      FUNC_NAME, FAILURE);
                    }

                    /* If we are on the first file set the wavelength value
                       for the data array */
                    if (index == 1)
                    {
                        current_data[entry][0] = modtran_wavelength;
                    }
                    /* Place radiance into data array for current point at
                       current height */
                    current_data[entry][index] = modtran_radiance;

                }
                fclose (fd);

                counter++;
            }

            /* parameters from 3 modtran runs
               Lobs = Lt*tau + Lu; m = tau; b = Lu; */
            if (calculate_lobs (current_data, spectral_response,
                                num_entries, num_srs, 1, &y_0)
                != SUCCESS)
            {
                RETURN_ERROR ("Calling calculate_lobs for height y_0",
                              FUNC_NAME, FAILURE);
            }

            if (calculate_lobs (current_data, spectral_response,
                                num_entries, num_srs, 2, &y_1)
                != SUCCESS)
            {
                RETURN_ERROR ("Calling calculate_lobs for height y_1",
                              FUNC_NAME, FAILURE);
            }

            /* Implement a = INVERT(TRANSPOSE(x)##x)##TRANSPOSE(x)##y
               from the IDL code base.
               Partially implemented above and used here. */
            Y_2x1[0] = y_0;
            Y_2x1[1] = y_1;

            matrix_multiply_2x2_2x1(Xt_2x2, Y_2x1, Xt_Y_2x1);
            matrix_multiply_2x2_2x1(Inv_Xt_X_2x2, Xt_Y_2x1, A_2x1);

            tau = A_2x1[1]; /* Transmittance */
            lu = A_2x1[0];  /* Upwelled Radiance */

            /* determine Lobs and Lt when
               modtran was run at 0K - calculate downwelled */
            if (calculate_lt (zero_temp, spectral_response, num_srs,
                              &temp_radiance_0) != SUCCESS)
            {
                RETURN_ERROR ("Calling calculate_lt for zero temp (0Kelvin)",
                              FUNC_NAME, FAILURE);
            }

            if (calculate_lobs (current_data, spectral_response,
                                num_entries, num_srs, 3, &obs_radiance_0)
                != SUCCESS)
            {
                RETURN_ERROR ("Calling calculate_lobs for (0Kelvin)",
                              FUNC_NAME, FAILURE);
            }

            /* Calculate the downwelled radiance */
            /* TODO - These are all equivalent today, but may need to change
                      to use the (1.0 - emissivity) when emissivity is
                      provided. */
            /* Ld = (((Lobs-Lu)/tau) - (Lt*emissivity))/(1.0-emissivity) */
            /* Ld = (((Lobs-Lu)/tau) - (Lt*emissivity))/abledo */
            /* Ld = (((Lobs-Lu)/tau) - (Lt*emissivity))*inv_albedo */
            ld = (((obs_radiance_0 - lu) / tau)
                  - (temp_radiance_0 * emissivity)) * inv_albedo;

            /* Place results into MODTRAN results array */
            modtran_results[result_loc][MGPE_TRANSMISSION] = tau;
            modtran_results[result_loc][MGPE_UPWELLED_RADIANCE] = lu;
            modtran_results[result_loc][MGPE_DOWNWELLED_RADIANCE] = ld;

            /* Free the allocated memory in the loop */
            if (free_2d_array ((void **) current_data) != SUCCESS)
            {
                RETURN_ERROR ("Freeing memory: current_data\n",
                              FUNC_NAME, FAILURE);
            }
            current_data = NULL;
        } /* END - NUM_ELEVATIONS loop */
    } /* END - num_points loop */
    fclose (used_points_fd);

    /* Free allocated memory */
    if (free_2d_array ((void **) spectral_response) != SUCCESS)
    {
        RETURN_ERROR ("Freeing memory: spectral_response\n", FUNC_NAME,
                      FAILURE);
    }
    spectral_response = NULL;

    /* Output the results to a file */
    snprintf (current_file, sizeof (current_file),
              "atmospheric_parameters.txt");
    snprintf (msg, sizeof (msg),
              "Creating Atmospheric Parameters File = [%s]\n", current_file);
    LOG_MESSAGE (msg, FUNC_NAME);
    fd = fopen (current_file, "w");
    if (fd == NULL)
    {
        RETURN_ERROR ("Can't open atmospheric_parameters.txt file",
                      FUNC_NAME, FAILURE);
    }
    for (k = 0; k < points->num_points * NUM_ELEVATIONS; k++)
    {
        fprintf (fd, "%f,%f,%12.9f,%12.9f,%12.9f,%12.9f\n",
                 modtran_results[k][MGPE_LATITUDE],
                 modtran_results[k][MGPE_LONGITUDE],
                 modtran_results[k][MGPE_HEIGHT],
                 modtran_results[k][MGPE_TRANSMISSION],
                 modtran_results[k][MGPE_UPWELLED_RADIANCE],
                 modtran_results[k][MGPE_DOWNWELLED_RADIANCE]);
    }
    fclose (fd);

    return SUCCESS;
}
Exemplo n.º 19
0
/******************************************************************************
METHOD:  lst

PURPOSE:  The main routine for scene based LST (Land Surface Temperature).

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the scene_based_lst
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
          at the USGS EROS
******************************************************************************/
int
main (int argc, char *argv[])
{
    char FUNC_NAME[] = "main";

    Espa_internal_meta_t xml_metadata;  /* XML metadata structure */

    char msg_str[MAX_STR_LEN];
    char xml_filename[PATH_MAX];        /* input XML filename */
    char dem_filename[PATH_MAX];        /* input DEM filename */
    char emissivity_filename[PATH_MAX]; /* input Emissivity filename */
    char command[PATH_MAX];

    Input_t *input = NULL;          /* input data and meta data */
    //    Output_t *output = NULL; /* output structure and metadata */

    bool use_tape6;             /* Use the tape6 output */
    bool verbose;               /* verbose flag for printing messages */
    bool debug;                 /* debug flag for debug output */

    int modtran_run;

    double alb = 0.1;
    double **modtran_results = NULL;

    char *tmp_env = NULL;

    REANALYSIS_POINTS points;

    time_t now;

    /* Display the starting time of the application */
    time (&now);
    snprintf (msg_str, sizeof(msg_str),
              "LST start_time [%s]", ctime (&now));
    LOG_MESSAGE (msg_str, FUNC_NAME);

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    if (get_args (argc, argv, xml_filename, dem_filename, emissivity_filename,
                  &use_tape6, &verbose, &debug) != SUCCESS)
    {
        RETURN_ERROR ("calling get_args", FUNC_NAME, EXIT_FAILURE);
    }

    /* Verify the existence of required environment variables */
    /* Grab the environment path to the LST_DATA_DIR */
    tmp_env = getenv ("LST_DATA_DIR");
    if (tmp_env == NULL)
    {
        RETURN_ERROR ("LST_DATA_DIR environment variable is not set",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Validate the input metadata file */
    if (validate_xml_file (xml_filename) != SUCCESS)
    {
        /* Error messages already written */
        return EXIT_FAILURE;
    }

    /* 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 (xml_filename, &xml_metadata) != SUCCESS)
    {
        /* Error messages already written */
        return EXIT_FAILURE;
    }

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput (&xml_metadata);
    if (input == NULL)
    {
        RETURN_ERROR ("opening input files", FUNC_NAME, EXIT_FAILURE);
    }

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("Satellite: %d\n", input->meta.satellite);
        printf ("Instrument: %d\n", input->meta.instrument);

        printf ("Number of input lines: %d\n", input->thermal.size.l);
        printf ("Number of input samples: %d\n", input->thermal.size.s);

        printf ("Fill value is %d\n", input->thermal.fill_value);

        printf ("Thermal Band -->\n");
        printf ("  therm_gain: %f\n  therm_bias: %f\n",
                input->thermal.rad_gain, input->thermal.rad_bias);

        printf ("Year, Month, Day, Hour, Minute, Second:"
                " %d, %d, %d, %d, %d, %f\n",
                input->meta.acq_date.year, input->meta.acq_date.month,
                input->meta.acq_date.day, input->meta.acq_date.hour,
                input->meta.acq_date.minute, input->meta.acq_date.second);
        printf ("ACQUISITION_DATE.DOY is %d\n",
                input->meta.acq_date.doy);

        printf ("UL_MAP_CORNER: %f, %f\n", input->meta.ul_map_corner.x,
                input->meta.ul_map_corner.y);
        printf ("LR_MAP_CORNER: %f, %f\n", input->meta.lr_map_corner.x,
                input->meta.lr_map_corner.y);
        printf ("UL_GEO_CORNER: %f, %f\n",
                input->meta.ul_geo_corner.lat, input->meta.ul_geo_corner.lon);
        printf ("LR_GEO_CORNER: %f, %f\n",
                input->meta.lr_geo_corner.lat, input->meta.lr_geo_corner.lon);
    }

    /* Build the points that will be used */
    if (build_points (input, &points) != SUCCESS)
    {
        RETURN_ERROR ("Building POINTS input\n", FUNC_NAME, EXIT_FAILURE);
    }

    if (verbose)
    {
        printf ("Number of Points: %d\n", points.num_points);
    }

    /* Call build_modtran_input to generate the tape5 file input and
       the MODTRAN commands for each point and height */
    if (build_modtran_input (input, &points, verbose, debug)
        != SUCCESS)
    {
        RETURN_ERROR ("Building MODTRAN input\n", FUNC_NAME, EXIT_FAILURE);
    }

    /* Perform MODTRAN runs by calling each command */
    for (modtran_run = 0; modtran_run < points.num_modtran_runs; modtran_run++)
    {
        snprintf (msg_str, sizeof(msg_str),
                  "Executing MODTRAN [%s]",
                   points.modtran_runs[modtran_run].command);
        LOG_MESSAGE (msg_str, FUNC_NAME);

#if RUN_MODTRAN
        if (system (points.modtran_runs[modtran_run].command) != SUCCESS)
        {
            RETURN_ERROR ("Error executing MODTRAN", FUNC_NAME,
                          EXIT_FAILURE);
        }
#endif
    }

    /* PARSING MODTRAN RESULTS:
       for each case in caseList (for each modtran run),
       parse wavelength and total radiance from tape6 file into parsed */
    for (modtran_run = 0; modtran_run < points.num_modtran_runs; modtran_run++)
    {
        if (use_tape6)
        {
            /* Use modtran generated tape6 output */
            snprintf (command, sizeof (command),
                      "lst_extract_modtran_results.py"
                      " --tape6"
                      " --input-path %s"
                      " --output-path %s",
                      points.modtran_runs[modtran_run].path,
                      points.modtran_runs[modtran_run].path);
        }
        else
        {
            /* Use modtran generated pltout.asc output */
            snprintf (command, sizeof (command),
                      "lst_extract_modtran_results.py"
                      " --pltout"
                      " --input-path %s"
                      " --output-path %s",
                      points.modtran_runs[modtran_run].path,
                      points.modtran_runs[modtran_run].path);
        }

        snprintf (msg_str, sizeof(msg_str), "Executing [%s]", command);
        LOG_MESSAGE (msg_str, FUNC_NAME);

#if EXTRACT_TAPE6_RESULTS
        if (system (command) != SUCCESS)
        {
            RETURN_ERROR ("Failed executing lst_extract_tape6_results.py",
                          FUNC_NAME, EXIT_FAILURE);
        }
#endif
    }

    /* Allocate memory for MODTRAN results */
    modtran_results =
        (double **) allocate_2d_array (points.num_points * NUM_ELEVATIONS,
                                       MGPE_NUM_ELEMENTS, sizeof (double));
    if (modtran_results == NULL)
    {
        RETURN_ERROR ("Allocating MODTRAN results memory", FUNC_NAME,
                      EXIT_FAILURE);
    }

    /* Generate parameters for each height and NARR point */
    if (calculate_point_atmospheric_parameters (input, &points, alb,
                                                modtran_results, verbose)
        != SUCCESS)
    {
        RETURN_ERROR ("Calculating point atmospheric parameters\n",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Generate parameters for each Landsat pixel */
    if (calculate_pixel_atmospheric_parameters (input, &points,
                                                xml_filename,
                                                dem_filename,
                                                emissivity_filename,
                                                modtran_results, verbose)
        != SUCCESS)
    {
        RETURN_ERROR ("Calculating per/pixel atmospheric parameters\n",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Free memory allocation */
    free_points_memory (&points);

#if NOT_TESTED
    /* Open the output file */
    output = OpenOutput (&xml_metadata, input);
    if (output == NULL)
    {                           /* error message already printed */
        RETURN_ERROR ("Opening output file", FUNC_NAME, EXIT_FAILURE);
    }

    if (!PutOutput (output, pixel_mask))
    {
        RETURN_ERROR ("Writing output LST in HDF files\n", FUNC_NAME,
                      EXIT_FAILURE);
    }

    /* Close the output file */
    if (!CloseOutput (output))
    {
        RETURN_ERROR ("closing output file", FUNC_NAME, EXIT_FAILURE);
    }

    /* Create the ENVI header file this band */
    if (create_envi_struct (&output->metadata.band[0], &xml_metadata.global,
                            &envi_hdr) != SUCCESS)
    {
        RETURN_ERROR ("Creating ENVI header structure.", FUNC_NAME,
                      EXIT_FAILURE);
    }

    /* Write the ENVI header */
    strcpy (envi_file, output->metadata.band[0].file_name);
    cptr = strchr (envi_file, '.');
    if (cptr == NULL)
    {
        RETURN_ERROR ("error in ENVI header filename", FUNC_NAME,
                      EXIT_FAILURE);
    }

    strcpy (cptr, ".hdr");
    if (write_envi_hdr (envi_file, &envi_hdr) != SUCCESS)
    {
        RETURN_ERROR ("Writing ENVI header file.", FUNC_NAME, EXIT_FAILURE);
    }

    /* Append the LST band to the XML file */
    if (append_metadata (output->nband, output->metadata.band, xml_filename)
        != SUCCESS)
    {
        RETURN_ERROR ("Appending spectral index bands to XML file.",
                      FUNC_NAME, EXIT_FAILURE);
    }

    /* Free the structure */
    if (!FreeOutput (output))
    {
        RETURN_ERROR ("freeing output file structure", FUNC_NAME,
                      EXIT_FAILURE);
    }
#endif

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

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    /* Free memory allocations */
    if (free_2d_array ((void **) modtran_results) != SUCCESS)
    {
        RETURN_ERROR ("Freeing memory: MODTRAN results\n", FUNC_NAME,
                      EXIT_FAILURE);
    }

    if (!debug)
    {
        /* Delete temporary file */
        if (unlink ("atmospheric_parameters.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting atmospheric_parameters.txt files\n",
                          FUNC_NAME, EXIT_FAILURE);
        }

        if (unlink ("base_head.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting baseHead.txt files\n", FUNC_NAME,
                          EXIT_FAILURE);
        }

        if (unlink ("new_tail.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting newTail.txt files\n", FUNC_NAME,
                          EXIT_FAILURE);
        }

        if (unlink ("temp_layers.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting tempLayers.txt file\n", FUNC_NAME,
                          EXIT_FAILURE);
        }

        if (unlink ("used_points.txt") != SUCCESS)
        {
            RETURN_ERROR ("Deleting used_points.txt file\n", FUNC_NAME,
                          EXIT_FAILURE);
        }
    }

    time (&now);
    snprintf (msg_str, sizeof(msg_str),
              "scene_based_lst end_time=%s\n", ctime (&now));
    LOG_MESSAGE (msg_str, FUNC_NAME);

    return EXIT_SUCCESS;
}
Exemplo n.º 20
0
/*! \fn static void flux_output_func(MeshS *pM, OutputS *pOut) 
 *  \brief  New output format which outputs y-integrated angular momentum fluxes
 *  Currently can only be used with 1 proc and 1 domain.
 */
static void flux_output_func(MeshS *pM, OutputS *pOut)
{
  GridS *pG=pM->Domain[0][0].Grid;
  int nx1,nx2,nx3, ind, i,j,k, ind_i,ind_k;
  Real lx1, lx2, lx3;
  PrimS W[7],Ws[7],We[7];
  Real x1[7],x2[7],x3[7],xs1[7],xs2[7],xs3[7],xe1[7],xe2[7],xe3[7];
  Real dmin, dmax;
  Real **Fluxx=NULL;
  Real **FluxH=NULL;
  Real **FluxNu=NULL;
  Real **Th=NULL;
  Real **outCoordsx1=NULL;
  Real **outCoordsx3=NULL;
  Real **vx=NULL;
  Real **vy=NULL;
  Real **sigvx=NULL;
  Real **osigx=NULL;
  Real **davg=NULL;
  
  
  FILE *pfile;
  char *fname;
	
  nx1 = pG->Nx[0]; nx3 = pG->Nx[2]; nx2 = pG->Nx[1];
  lx1 = pG->MaxX[0] - pG->MinX[0];
  lx2 = pG->MaxX[1] - pG->MinX[1];
  lx3 = pG->MaxX[2] - pG->MinX[2];
 printf("%d, %d, %d, %g, %g, %g\n",nx1,nx2,nx3,lx1,lx2,lx3);
#ifdef MPI_PARALLEL  
  printf("%d, %d, %d, %g, %g, %g \n", myID_Comm_world,nx1,nx3,lx1,lx2,lx3); 

  printf("IND %d: (%d,%d), (%d,%d)\n",myID_Comm_world,pG->is,pG->js,pG->ie,pG->je);

#endif

  Fluxx=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (Fluxx == NULL) return;
  FluxH=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (FluxH == NULL) return;
  FluxNu=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (FluxNu == NULL) return;
  Th=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (Th == NULL) return;
  outCoordsx1=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (outCoordsx1 == NULL) return;
  outCoordsx3=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (outCoordsx3 == NULL) return;
  vx=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (vx == NULL) return;
  vy=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (vy == NULL) return;
  sigvx=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (sigvx == NULL) return;
  osigx=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (osigx == NULL) return;
  davg=(Real **)calloc_2d_array(nx3,nx1,sizeof(Real));
  if (davg == NULL) return;
/* Open file and write header */  
  
  if((fname = ath_fname(NULL,pM->outfilename,NULL,NULL,num_digit,
            pOut->num,pOut->id,"tab")) == NULL){
          ath_error("[dump_tab]: Error constructing filename\n");
  }
  if((pfile = fopen(fname,"w")) == NULL){
  	ath_error("[dump_tab]: Unable to open ppm file %s\n",fname);
  }
  free(fname);
  
#ifdef MPI_PARALLEL
  if(myID_Comm_world == 0) 
#endif
  	fprintf(pfile,"#t=%12.8e	x,FH,Fx,Fnu,Th,vx,vy,davg,sigvx,omsigx \n", pOut->t);

/* Compute y-integrated fluxes explicitly.
 * For the derivatives use a central difference method.
 * For the integration use a composite trapezoid method.
 * Both of these make use of the ghost cells for the boundary values.
 * 1	FluxH = < d * vx1 *vx2 >
 * 2	Fluxx = < 0.5 * omega * d * x1 * vx1 >
 * 3	FluxNu = - < nu_iso * d/dx vx2 >
 * 4	Th = - < d * d/dy phi >
 * 5	vx = < vx1 >
 * 6	vy = < vx2 >
 * 7	davg = < d >
 * 8	sigvx = < d * vx1 >
 * 9	osigx = < 0.5 * sig * omega * x1 >
 *
 *         
 *		x	4   x
 *      1   0	2
 *		x	3	x
 *
 * The variables are stored up as arrays of primitives W[7].
 * W = ( W[k][j][i], W[k][j][i-1], W[k][j][i+1], W[k][j-1][i], 
 *		 W[k][j+1][i], W[k-1][j][i], W[k+1][j][i] )
 * This is needed for the integration and differentiation.
 *
 * The background shear is taken out of vy when doing computations.
*/

  for(k=pG->ks; k <=pG->ke; k++) {
  	for(i=pG->is; i<= pG->ie; i++) {
		ind_i=i-pG->is; ind_k=k-pG->ks; 
		for(ind=0; ind < 7; ind++) {
			if (ind==0) {
				cc_pos(pG,i,pG->js,k,&xs1[ind],&xs2[ind],&xs3[ind]);
				cc_pos(pG,i,pG->je,k,&xe1[ind],&xe2[ind],&xe3[ind]);
				Ws[ind] = Cons_to_Prim(&(pG->U[k][pG->js][i])); 
				We[ind] = Cons_to_Prim(&(pG->U[k][pG->je][i])); 
			}
			if (ind > 0 && ind < 3) {
				cc_pos(pG,i+2*ind-3,pG->js,k,&xs1[ind],&xs2[ind],&xs3[ind]);
				cc_pos(pG,i+2*ind-3,pG->je,k,&xe1[ind],&xe2[ind],&xe3[ind]);
				Ws[ind] = Cons_to_Prim(&(pG->U[k][pG->js][i+2*ind-3])); 
				We[ind] = Cons_to_Prim(&(pG->U[k][pG->je][i+2*ind-3]));
			}
			if (ind > 2 && ind < 5) {
				cc_pos(pG,i,pG->js+2*ind-7,k,&xs1[ind],&xs2[ind],&xs3[ind]);
				cc_pos(pG,i,pG->je+2*ind-7,k,&xe1[ind],&xe2[ind],&xe3[ind]);
				Ws[ind] = Cons_to_Prim(&(pG->U[k][pG->js+2*ind-7][i])); 
				We[ind] = Cons_to_Prim(&(pG->U[k][pG->je+2*ind-7][i]));
			}
			if (ind > 4 && ind < 7) {
				if (pG->MinX[2] == pG->MaxX[2]) {
					cc_pos(pG,i,pG->js,k,&xs1[ind],&xs2[ind],&xs3[ind]);
					cc_pos(pG,i,pG->je,k,&xe1[ind],&xe2[ind],&xe3[ind]);
					Ws[ind] = Cons_to_Prim(&(pG->U[k][pG->js][i])); 
					We[ind] = Cons_to_Prim(&(pG->U[k][pG->je][i]));
				}
				else {
					cc_pos(pG,i,pG->js,k+2*ind-11,&xs1[ind],&xs2[ind],&xs3[ind]);
					cc_pos(pG,i,pG->je,k+2*ind-11,&xe1[ind],&xe2[ind],&xe3[ind]);
					Ws[ind] = Cons_to_Prim(&(pG->U[k+2*ind-11][pG->js][i])); 
					We[ind] = Cons_to_Prim(&(pG->U[k+2*ind-11][pG->je][i]));
				}
			}
			Ws[ind].V2 = Ws[ind].V2 + qshear*Omega_0*xs1[ind];
			We[ind].V2 = We[ind].V2 + qshear*Omega_0*xe1[ind];
/* If using d' instead of d then put in
 * W[ind] = W[ind]->d - d0;
 */
		}
/* Set initial values for the integration */
		FluxH[ind_k][ind_i] = 0.5*( Ws[0].d * Ws[0].V1 * Ws[0].V2 + 
						    We[0].d * We[0].V1 * We[0].V2 );  	
		
		Fluxx[ind_k][ind_i] = 0.5*( Ws[0].d * Ws[0].V1 * xs1[0] +
							We[0].d * We[0].V1 * xe1[0] );
		
#ifdef VISCOSITY 
		FluxNu[ind_k][ind_i] = 0.5*( Ws[2].V2 - Ws[1].V2 +
							 We[2].V2 - We[1].V2 ); 
		
#else
		FluxNu[ind_k][ind_i] = 0;
#endif   	
  	
  		Th[ind_k][ind_i] = 0.5*( Ws[0].d * dx2PlanetPot(xs1[0],xs2[0],xs3[0]) +
  						 We[0].d * dx2PlanetPot(xe1[0],xe2[0],xe3[0]) );
		
		vx[ind_k][ind_i] = 0.5*( Ws[0].V1 + We[0].V1 );
		vy[ind_k][ind_i] = 0.5*( Ws[0].V2 + We[0].V2 );
		sigvx[ind_k][ind_i] = 0.5*( Ws[0].d * Ws[0].V1 + We[0].d * We[0].V1 );
		osigx[ind_k][ind_i] = 0.5*( Ws[0].d * xs1[0] + We[0].d * xe1[0] );
		davg[ind_k][ind_i] = 0.5*(Ws[0].d + We[0].d);
  	 	for(j=(pG->js)+1; j < pG->je; j++) {
  			for(ind=0; ind < 7; ind++) {
				if (ind==0) {
					cc_pos(pG,i,j,k,&x1[ind],&x2[ind],&x3[ind]);
					W[ind] = Cons_to_Prim(&(pG->U[k][j][i])); 
				}
				if (ind > 0 && ind < 3) {
					cc_pos(pG,i+2*ind-3,j,k,&x1[ind],&x2[ind],&x3[ind]);
					W[ind] = Cons_to_Prim(&(pG->U[k][j][i+2*ind-3])); 
				}
				if (ind > 2 && ind < 5) {
					cc_pos(pG,i,j+2*ind-7,k,&x1[ind],&x2[ind],&x3[ind]);
					W[ind] = Cons_to_Prim(&(pG->U[k][j+2*ind-7][i])); 
				}
				if (ind > 4 && ind < 7) {
					if (pG->MinX[2] == pG->MaxX[2]) {
						cc_pos(pG,i,j,k,&x1[ind],&x2[ind],&x3[ind]);
						W[ind] = Cons_to_Prim(&(pG->U[k][j][i])); 
					}
					else {
						cc_pos(pG,i,j,k+2*ind-11,&x1[ind],&x2[ind],&x3[ind]);
						W[ind] = Cons_to_Prim(&(pG->U[k+2*ind-11][j][i])); 
					}
				}
				W[ind].V2 = W[ind].V2 + qshear*Omega_0*x1[ind];
				
			}	 
			
			FluxH[ind_k][ind_i] += W[0].d * W[0].V1 * W[0].V2;
			Fluxx[ind_k][ind_i] += W[0].d * W[0].V1 * x1[0];
#ifdef VISCOSITY
			FluxNu[ind_k][ind_i] += W[0].d * (W[2].V2 - W[1].V1);
#endif
			Th[ind_k][ind_i] += W[0].d * dx2PlanetPot(x1[0],x2[0],x3[0]);
			vx[ind_k][ind_i] += W[0].V1;
			vy[ind_k][ind_i] += W[0].V2;
			sigvx[ind_k][ind_i] += W[0].d * W[0].V1;
			osigx[ind_k][ind_i] += W[0].d * x1[0];
			davg[ind_k][ind_i] += W[0].d;
			
  		}	
  		FluxH[ind_k][ind_i] *= (pG->dx2)/lx2;
  		Fluxx[ind_k][ind_i] *= .5*Omega_0*(pG->dx2)/lx2;
#ifdef	VISCOSITY
		FluxNu[ind_k][ind_i] *= -nu_iso*(pG->dx2)/(2*lx2*(pG->dx1));
#endif 
  		Th[ind_k][ind_i] *= -1.0*(pG->dx2)/lx2;				
  		vx[ind_k][ind_i] *= (pG->dx2)/lx2;
  		vy[ind_k][ind_i] *= (pG->dx2)/lx2;
  		sigvx[ind_k][ind_i] *= (pG->dx2)/lx2;
  		osigx[ind_k][ind_i] *= (0.5*Omega_0*(pG->dx2))/lx2;
  		davg[ind_k][ind_i] *= (pG->dx2)/lx2;
  	 	outCoordsx1[ind_k][ind_i]=x1[0]; outCoordsx3[ind_k][ind_i]=x3[0];
  	}
  }  

/* Quantities are ready to be written to output file
 * Format (Not outputting x3 at the moment: 
 * x1	(x3)	FluxH	Fluxx	Fluxnu	Th	vx	vy	davg	sigvx	osigx
*/
  
  
  for(k=pG->ks; k<=pG->ke; k++) {
  	for(i=pG->is; i<=pG->ie; i++) {
 		ind_k=k-pG->ks; ind_i=i-pG->is;
  		if (lx3==0) {
  			fprintf(pfile,"%12.8e %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e\n",
  					outCoordsx1[ind_k][ind_i],FluxH[ind_k][ind_i],
  					Fluxx[ind_k][ind_i],FluxNu[ind_k][ind_i],Th[ind_k][ind_i],
  					vx[ind_k][ind_i],vy[ind_k][ind_i],davg[ind_k][ind_i],sigvx[ind_k][ind_i],
  					osigx[ind_k][ind_i]);
  		}
  		else {
  			fprintf(pfile,"%12.8e	%12.8e	%12.8e	%12.8e	%12.8e	%12.8e %12.8e %12.8e %12.8e %12.8e %12.8e\n",
  					outCoordsx1[ind_k][ind_i],outCoordsx3[ind_k][ind_i],FluxH[ind_k][ind_i],
  					Fluxx[ind_k][ind_i],FluxNu[ind_k][ind_i],Th[ind_k][ind_i],
  					vx[ind_k][ind_i],vy[ind_k][ind_i],davg[ind_k][ind_i],sigvx[ind_k][ind_i],
  					osigx[ind_k][ind_i]);
  		}
  	}
  }
  
  fclose(pfile);
  free_2d_array(Fluxx); 
  free_2d_array(FluxH); 
  free_2d_array(FluxNu); 
  free_2d_array(Th); 
  free_2d_array(outCoordsx1);
  free_2d_array(outCoordsx3);
  free_2d_array(vx);
  free_2d_array(vy);
  free_2d_array(sigvx);
  free_2d_array(osigx);
  free_2d_array(davg);
  return;
}
Exemplo n.º 21
0
void problem(DomainS *pDomain)
{
  GridS *pGrid = pDomain->Grid;
  int i,j,k,ks,pt,tsmode;
  long p,q;
  Real ScaleHg,tsmin,tsmax,tscrit,amin,amax,Hparmin,Hparmax;
  Real *ep,*ScaleHpar,epsum,mratio,pwind,rhoaconv,etavk;
  Real *epsilon,*uxNSH,*uyNSH,**wxNSH,**wyNSH;
  Real rhog,h,x1,x2,x3,t,x1p,x2p,x3p,zmin,zmax,dx3_1,b;
  long int iseed = myID_Comm_world; /* Initialize on the first call to ran2 */

  if (pDomain->Nx[2] == 1) {
    ath_error("[par_strat3d]: par_strat3d only works for 3D problem.\n");
  }
  
#ifdef MPI_PARALLEL
  if (pDomain->NGrid[2] > 2) {
    ath_error(   
  "[par_strat3d]: The z-domain can not be decomposed into more than 2 grids\n");
  }
#endif

/* Initialize boxsize */
  x1min = pGrid->MinX[0];
  x1max = pGrid->MaxX[0];
  Lx = x1max - x1min;

  x2min = pGrid->MinX[1];
  x2max = pGrid->MaxX[1];
  Ly = x2max - x2min;

  x3min = par_getd("domain1","x3min");
  x3max = par_getd("domain1","x3max");
  Lz = x3max - x3min;

  Lg = nghost*pGrid->dx3; /* size of the ghost zone */

  ks = pGrid->ks;

/* Read initial conditions */
  Omega_0 = par_getd("problem","omega");
  qshear = par_getd_def("problem","qshear",1.5);
  ipert = par_geti_def("problem","ipert",1);
  vsc1 = par_getd_def("problem","vsc1",0.05); /* in unit of iso_sound (N.B.!) */
  vsc2 = par_getd_def("problem","vsc2",0.0);

  vsc1 = vsc1 * Iso_csound;
  vsc2 = vsc2 * Iso_csound;

  ScaleHg = Iso_csound/Omega_0;

  /* particle number */
  Npar  = (long)(par_geti("particle","parnumgrid"));

  pGrid->nparticle = Npar*npartypes;
  for (i=0; i<npartypes; i++)
    grproperty[i].num = Npar;

  if (pGrid->nparticle+2 > pGrid->arrsize)
    particle_realloc(pGrid, pGrid->nparticle+2);

  ep = (Real*)calloc_1d_array(npartypes, sizeof(Real));
  ScaleHpar = (Real*)calloc_1d_array(npartypes, sizeof(Real));

  epsilon = (Real*)calloc_1d_array(npartypes, sizeof(Real));
  wxNSH   = (Real**)calloc_2d_array(pGrid->Nx[2]+1, npartypes,sizeof(Real));
  wyNSH   = (Real**)calloc_2d_array(pGrid->Nx[2]+1, npartypes,sizeof(Real));
  uxNSH   = (Real*)calloc_1d_array(pGrid->Nx[2]+1, sizeof(Real));
  uyNSH   = (Real*)calloc_1d_array(pGrid->Nx[2]+1, sizeof(Real));

  /* particle stopping time */
  tsmode = par_geti("particle","tsmode");
  if (tsmode == 3) {/* fixed stopping time */
    tsmin = par_getd("problem","tsmin"); /* in code unit */
    tsmax = par_getd("problem","tsmax");
    tscrit= par_getd("problem","tscrit");

    for (i=0; i<npartypes; i++) {
      tstop0[i] = tsmin*exp(i*log(tsmax/tsmin)/MAX(npartypes-1,1.0));
      grproperty[i].rad = tstop0[i];
      /* use fully implicit integrator for well coupled particles */
      if (tstop0[i] < tscrit) grproperty[i].integrator = 3;
    }
  }
  else { 
    amin = par_getd("problem","amin");
    amax = par_getd("problem","amax");

    for (i=0; i<npartypes; i++)
      grproperty[i].rad = amin*exp(i*log(amax/amin)/MAX(npartypes-1,1.0));

    if (tsmode <= 2) {/* Epstein/General regime */
      /* conversion factor for rhoa */
      rhoaconv = par_getd_def("problem","rhoaconv",1.0);

      for (i=0; i<npartypes; i++)
        grrhoa[i]=grproperty[i].rad*rhoaconv;
    }

    if (tsmode == 1)  /* General drag formula */
      alamcoeff = par_getd("problem","alamcoeff");
  }

  /* particle scale height */
  Hparmax = par_getd("problem","hparmax"); /* in unit of gas scale height */
  Hparmin = par_getd("problem","hparmin");
  for (i=0; i<npartypes; i++) 
    ScaleHpar[i] = Hparmax*
                   exp(-i*log(Hparmax/Hparmin)/MAX(npartypes-1,1.0));

#ifdef FEEDBACK
  mratio = par_getd_def("problem","mratio",0.0); /* total mass fraction */
  pwind = par_getd_def("problem","pwind",0.0);   /* power law index */
  if (mratio < 0.0)
    ath_error("[par_strat2d]: mratio must be positive!\n");

  epsum = 0.0;
  for (i=0; i<npartypes; i++)
  {
    ep[i] = pow(grproperty[i].rad,pwind);	epsum += ep[i];
  }

  for (i=0; i<npartypes; i++)
  {
    ep[i] = mratio*ep[i]/epsum;
    grproperty[i].m = sqrt(2.0*PI)*ScaleHg/Lz*ep[i]*
                                   pGrid->Nx[0]*pGrid->Nx[1]*pGrid->Nx[2]/Npar;
  }
#else
  mratio = 0.0;
  for (i=0; i<npartypes; i++)
    ep[i] = 0.0;
#endif

  /* NSH equilibrium */
  for (k=pGrid->ks; k<=pGrid->ke+1; k++) {

    h = pGrid->MinX[2] + (k-pGrid->ks)*pGrid->dx3;
    q = k - ks;
    etavk = fabs(vsc1+vsc2*SQR(h));

    for (i=0; i<npartypes; i++) {
      epsilon[i] = ep[i]/ScaleHpar[i]*exp(-0.5*SQR(h/ScaleHg)
         *(SQR(1.0/ScaleHpar[i])-1.0))/erf(Lz/(sqrt(8.0)*ScaleHpar[i]*ScaleHg));

      if (tsmode != 3)
        tstop0[i] = get_ts(pGrid,i,exp(-0.5*SQR(h/ScaleHg)),Iso_csound,etavk);
    }

    MultiNSH(npartypes, tstop0, epsilon, etavk,
                              &uxNSH[q], &uyNSH[q], wxNSH[q], wyNSH[q]);
  }

/* Now set initial conditions for the gas */
  for (k=pGrid->ks; k<=pGrid->ke; k++) {
  for (j=pGrid->js; j<=pGrid->je; j++) {
  for (i=pGrid->is; i<=pGrid->ie; i++) {
    cc_pos(pGrid,i,j,k,&x1,&x2,&x3);

    rhog = exp(-0.5*SQR(x3/ScaleHg));
    pGrid->U[k][j][i].d = rhog;

    if (ipert != 1) {/* NSH velocity */
      pGrid->U[k][j][i].M1 = 0.5*rhog*(uxNSH[k-ks]+uxNSH[k-ks+1]);
      pGrid->U[k][j][i].M2 = 0.5*rhog*(uyNSH[k-ks]+uyNSH[k-ks+1]);
    } else {
      pGrid->U[k][j][i].M1 = 0.0;
      pGrid->U[k][j][i].M2 = 0.0;
    }

    pGrid->U[k][j][i].M3 = 0.0;
#ifndef FARGO
    pGrid->U[k][j][i].M2 -= qshear*rhog*Omega_0*x1;
#endif

  }}}

/* Now set initial conditions for the particles */
  p = 0;
  dx3_1 = 1.0/pGrid->dx3;
  zmin = pGrid->MinX[2];
  zmax = pGrid->MaxX[2];

  for (q=0; q<Npar; q++) {

    for (pt=0; pt<npartypes; pt++) {

      x1p = x1min + Lx*ran2(&iseed);
      x2p = x2min + Ly*ran2(&iseed);
      x3p = ScaleHpar[pt]*ScaleHg*Normal(&iseed);
      while ((x3p >= zmax) || (x3p < zmin))
        x3p = ScaleHpar[pt]*ScaleHg*Normal(&iseed);

      pGrid->particle[p].property = pt;
      pGrid->particle[p].x1 = x1p;
      pGrid->particle[p].x2 = x2p;
      pGrid->particle[p].x3 = x3p;

      if (ipert != 1) {/* NSH velocity */

        cellk(pGrid, x3p, dx3_1, &k, &b);
        k = k-pGrid->ks;  b = b - pGrid->ks;

        pGrid->particle[p].v1 = (k+1-b)*wxNSH[k][pt]+(b-k)*wxNSH[k+1][pt];
        pGrid->particle[p].v2 = (k+1-b)*wyNSH[k][pt]+(b-k)*wyNSH[k+1][pt];

      } else {

        pGrid->particle[p].v1 = 0.0;
        pGrid->particle[p].v2 = vsc1+vsc2*SQR(x2p);

      }

      pGrid->particle[p].v3 = 0.0;
#ifndef FARGO
      pGrid->particle[p].v2 -= qshear*Omega_0*x1p;
#endif

      pGrid->particle[p].pos = 1; /* grid particle */
      pGrid->particle[p].my_id = p;
#ifdef MPI_PARALLEL
      pGrid->particle[p].init_id = myID_Comm_world;
#endif
      p++;
  }}

/* enroll gravitational potential function, shearing sheet BC functions */
  ShearingBoxPot = StratifiedDisk;

  dump_history_enroll(hst_rho_Vx_dVy, "<rho Vx dVy>");

  /* set the # of the particles in list output
   * (by default, output 1 particle per cell)
   */
  nlis = par_geti_def("problem","nlis",pGrid->Nx[0]*pGrid->Nx[1]*pGrid->Nx[2]);

  /* set the number of particles to keep track of */
  ntrack = par_geti_def("problem","ntrack",2000);

  /* set the threshold particle density */
  dpar_thresh = par_geti_def("problem","dpar_thresh",10.0);

  /* finalize */
  free(ep);  free(ScaleHpar);
  free(epsilon);
  free_2d_array(wxNSH);  free_2d_array(wyNSH);
  free(uxNSH);           free(uyNSH);

  return;
}
Exemplo n.º 22
0
void output_pgm(MeshS *pM, OutputS *pOut)
{
  GridS *pGrid;
  FILE *pfile;
  char *fname,*plev=NULL,*pdom=NULL;
  char levstr[8],domstr[8];
  int nl,nd,nx1,nx2,gray,i,j;
  Real **data, dmin, dmax, max_min, sfact;

/* check output data is 2D (output must be a 2D slice for 3D runs) */
  if (pOut->ndim != 2) {
    ath_error("[output_pgm]: Output must be a 2D slice\n");
    return;
  }

/* Loop over all Domains in Mesh, and output Grid data */

  for (nl=0; nl<(pM->NLevels); nl++){
    for (nd=0; nd<(pM->DomainsPerLevel[nl]); nd++){
      if (pM->Domain[nl][nd].Grid != NULL){

/* write files if domain and level match input, or are not specified (-1) */
      if ((pOut->nlevel == -1 || pOut->nlevel == nl) &&
          (pOut->ndomain == -1 || pOut->ndomain == nd)){
        pGrid = pM->Domain[nl][nd].Grid;

/* Extract 2D data from 3D data,  Can either be slice or average along axis,
 * depending on range of ix1,ix2,ix3 in <ouput> block.  If OutData2 returns
 * NULL pointer, then slice is outside range of data in pGrid, so skip */

        data = OutData2(pGrid,pOut,&nx1,&nx2);
        if (data != NULL) {

/* construct output filename.  pOut->id will either be name of variable,
 * if 'id=...' was included in <ouput> block, or 'outN' where N is number of
 * <output> block.  */
          if (nl>0) {
            plev = &levstr[0];
            sprintf(plev,"lev%d",nl);
          }
          if (nd>0) {
            pdom = &domstr[0];
            sprintf(pdom,"dom%d",nd);
          }

          if((fname = ath_fname(plev,pM->outfilename,plev,pdom,num_digit,
            pOut->num,pOut->id,"pgm")) == NULL){
            ath_error("[output_pgm]: Error constructing filename\n");
          }

/* open output file */
          if((pfile = fopen(fname,"w")) == NULL){
            ath_error("[output_pgm]: Unable to open pgm file %s\n",fname);
            return;
          }
          free(fname);

          fprintf(pfile,"P5\n%d %d\n255\n",nx1,nx2);

/* Store the global min / max, for output at end of run */
          minmax2(data,nx2,nx1,&dmin,&dmax);
          pOut->gmin = MIN(dmin,pOut->gmin);
          pOut->gmax = MAX(dmax,pOut->gmax);

/* Override auto-scale? */
          if (pOut->sdmin != 0) dmin = pOut->dmin;
          if (pOut->sdmax != 0) dmax = pOut->dmax;
  
          max_min = (dmax - dmin)*(1.0 + FLT_EPSILON);

/* map the data which satisfies [min <= data <= max] to the range 
 * [0.0 , 256.0] -- Not inclusive of 256 */

          if(max_min > 0.0) {
            sfact = 256.0/max_min;
            for (j=nx2-1; j>=0; j--) {
              for (i=0; i<nx1; i++) {
/* Map the data to an 8 bit int, i.e. 0 -> 255 */
                gray = (int)(sfact*(data[j][i] - dmin));
/* Out of bounds data is mapped to the min or max integer value */
                gray = gray >   0 ? gray :   0;
                gray = gray < 255 ? gray : 255;

                fputc(gray,pfile);
              }
            }

/* else, if max=min set image to constant */

          } else {
            gray = 0;
            for (j=0; j<nx2; j++) {
              for (i=0; i<nx1; i++) {
                fputc(gray,pfile);
              }
            }
          }

/* Close the file, free memory */

          fclose(pfile); 
          free_2d_array(data);
        }
      }}
    }
  }
}
Exemplo n.º 23
0
void r_learner_free (RLearner *rl)
{
    free_2d_array( rl->q_values, rl->num_states);
    free( rl );
}
Exemplo n.º 24
0
int main(int argc, char* argv[])
{
    /* argument variables */
    int f1=0,f2=0,fi=1;
    char *defdir = ".";
    char *inbase = NULL, *postname = NULL;
    char *indir = defdir;
    /* fild variables */
    FILE *fid;
    char fname[100];
    /* data variables */
    int i,*cpuid,*property,ntype;
    long p,n,*pid,*order;
    float header[20],**data,time[2],*typeinfo=NULL;

    /* Read Arguments */
    for (i=1; i<argc; i++) {
        /* If argv[i] is a 2 character string of the form "-?" then: */
        if(*argv[i] == '-'  && *(argv[i]+1) != '\0' && *(argv[i]+2) == '\0') {
            switch(*(argv[i]+1)) {
            case 'd':                                /* -d <indir> */
                indir = argv[++i];
                break;
            case 'i':                                /* -i <basename>   */
                inbase = argv[++i];
                break;
            case 's':                                /* -s <post-name> */
                postname = argv[++i];
                break;
            case 'f':                                /* -f <# range(f1:f2:fi)>*/
                sscanf(argv[++i],"%d:%d:%d",&f1,&f2,&fi);
                if (f2 == 0) f2 = f1;
                break;
            case 'h':                                /* -h */
                usage(argv[0]);
                break;
            default:
                usage(argv[0]);
                break;
            }
        }
    }

    /* Checkpoints */
    if (inbase == NULL)
        sort_error("Please specify input file basename using -i option!\n");

    if (postname == NULL)
        sort_error("Please specify posterior file name using -s option!\n");

    if ((f1>f2) || (f2<0) || (fi<=0))
        sort_error("Wrong number sequence in the -f option!\n");

    /* ====================================================================== */

    for (i=f1; i<=f2; i+=fi)
    {
        fprintf(stderr,"Processing file number %d...\n",i);

        /* Step 1: Read the file */
        sprintf(fname,"%s/%s.%04d.%s.lis",indir,inbase,i,postname);

        fid = fopen(fname,"rb");
        if (fid == NULL)
            sort_error("Fail to open output file %s!\n",fname);

        /* read header */
        fread(header,sizeof(float),12,fid);
        fread(&ntype,sizeof(int),1,fid);

        if (i == f1)
            typeinfo = (float*)calloc_1d_array(ntype,sizeof(float));

        fread(typeinfo,sizeof(float),ntype,fid);
        fread(&time,sizeof(float),2,fid);
        fread(&n,sizeof(long),1,fid);

        data  = (float**)calloc_2d_array(n,7,sizeof(float));
        property = (int*)calloc_1d_array(n,sizeof(int));
        pid   = (long*)calloc_1d_array(n,sizeof(long));
        cpuid = (int*)calloc_1d_array(n,sizeof(int));
        order = (long*)calloc_1d_array(n,sizeof(long));

        /* read data */
        for (p=0; p<n; p++)
        {
            fread(data[p],sizeof(float),7,fid);
            fread(&(property[p]),sizeof(int),1,fid);
            fread(&(pid[p]),sizeof(long),1,fid);
            fread(&(cpuid[p]),sizeof(int),1,fid);
        }

        fclose(fid);

        /* Step 2: sort the particles */
        for (p=0; p<n; p++)
            order[p] = p;

        quicksort(cpuid, pid, order, 0, n-1);

        /* Step 3: write back the ordered particle list */
        fid = fopen(fname,"wb");

        /* write header */
        fwrite(header,sizeof(float),12,fid);
        fwrite(&ntype,sizeof(int),1,fid);
        fwrite(typeinfo,sizeof(float),ntype,fid);
        fwrite(time,sizeof(float),2,fid);
        fwrite(&n,sizeof(long),1,fid);

        /* write data */
        for (p=0; p<n; p++)
        {
            fwrite(data[order[p]],sizeof(float),7,fid);
            fwrite(&property[p],sizeof(int),1,fid);
            fwrite(&pid[order[p]],sizeof(long),1,fid);
            fwrite(&cpuid[order[p]],sizeof(int),1,fid);
        }

        free_2d_array(data);
        free(property);
        free(pid);
        free(cpuid);
        free(order);

        fclose(fid);
    }

    if (typeinfo != NULL)
        free(typeinfo);

    return 0;
}