⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 param.c

📁 对于图象处理,参数设置是一个非常重要的环节.这个代码可以借鉴一下
💻 C
📖 第 1 页 / 共 3 页
字号:
      if (this->input_sds_nbands[i] == 1)
      {
        /* 2D product so the band number is not needed */
        sprintf(tmp_sds_name, "%s", this->input_sds_name_list[i]);
      }
      else
      {
        /* 3D product so the band number is required */
        sprintf(tmp_sds_name, "%s, %d", this->input_sds_name_list[i], j);
      }

      this->input_sds_name = strdup (tmp_sds_name);
      if (this->input_sds_name == NULL) {
        sprintf(msg, "resamp: error creating input SDS band name");
	Infomsg(msg);
        FreeParam(this);
        return (Param_t *)NULL;
      }

#ifdef DEBUG
      printf ("Getting param %s ...\n", this->input_sds_name);
#endif

      /* Update the system to process the current SDS and band */
      if (!update_sds_info(i, this)) {
        sprintf(msg, "resamp: error updating SDS information");
	Infomsg(msg);
        FreeParam(this);
        return (Param_t *)NULL;
      }

      /* Make a copy of the dim parameters, since they get modified */
      for (ip = 0; ip < MYHDF_MAX_RANK; ip++) {
        copy_dim[ip] = this->dim[i][ip];
      }

      /* Open input file for the specified SDS and band */
      input = OpenInput(this->input_file_name, this->input_sds_name,
                        this->iband, this->rank[i], copy_dim, errstr);
      if (input == (Input_t *)NULL) {
        /* This is an invalid SDS for our processing so skip to the next
           SDS. We will only process SDSs that are at the 1km, 500m, or
           250m resolution (i.e. a factor of 1, 2, or 4 compared to the
           1km geolocation lat/long data). We also only process CHAR8,
           INT8, UINT8, INT16, and UINT16 data types. */
#ifdef DEBUG
        printf("%s\n", errstr);
        printf("%s %ld: param, not processing SDS/band\n\n", (__FILE__),
          (long)(__LINE__));
#endif
        break;
      }

      /* Determine the resolution of each of the input SDSs */
      if (!DetermineResolution(&input->sds, &input->dim, &this->ires[i])) {
        sprintf(msg, "resamp: error determining input resolution\n");
	Infomsg(msg);
        CloseInput(input);
        FreeInput(input);
        FreeParam(this);
        return (Param_t *)NULL; 
      }

      /* Close input file */
      if (!CloseInput(input)) {
        sprintf(msg, "resamp: closing input file");
	Infomsg(msg);
        FreeInput(input);
        FreeParam(this);
        return (Param_t *)NULL; 
      }

      /* Free the input structure */
      if (!FreeInput(input)) {
        sprintf(msg, "resamp: freeing input file stucture");
	Infomsg(msg);
        FreeParam(this);
        return (Param_t *)NULL;
      }

      /* We only need one band in this SDS, so break out of the loop */
      break;
    }  /* for (j = 0; j < this->input_sds_nbands[i]; j++) */
  }  /* for (i = 0; i < this->num_input_sds; i++) */

  /* Verify that at least one output pixel size value is defined */
  if (this->output_pixel_size[0] < 0.0) {
    /* No pixel size was specified, so try to determine the resolution of
       the input SDSs and use that for the pixel size. It is assumed that
       the input swath product will have the same resolution for all SDSs. */
    if (!DeterminePixelSize(this->geoloc_file_name, this->num_input_sds,
      this->ires, this->output_space_def.proj_num,
      this->output_pixel_size)) {
      sprintf(msg, "resamp: error determining output pixel size. "
        "Therefore, in order to process this data, the output pixel size "
        "must be specified.\n");
      Infomsg(msg);
      FreeParam(this);
      sprintf(msg, "%s\n", USAGE);
      Infomsg(msg);
      return (Param_t *)NULL; 
    }

    /* Set multires to FALSE since the output product will have the same
       resolution for all SDSs */
    this->multires = false;
  }

  /* If not enough pixel sizes were provided, then fill the pixel sizes
     in with the very last valid pixel size */
  for (ip = 0; ip < this->num_input_sds; ip++) {
    if (this->output_pixel_size[ip] < 0.0) {
      /* This is the first undefined pixel size, so grab the previous
         pixel size since it was the last valid pixel size value */
      tmp_pixel_size = this->output_pixel_size[ip-1];
      break;
    }
  }

  /* Fill in the rest of the values with the saved pixel size value */
  for (; ip < this->num_input_sds; ip++) {
    this->output_pixel_size[ip] = tmp_pixel_size;
  }

  /* Check the user-specified pixel sizes to see if the output product
     is multiple resolutions */
  for (ip = 0; ip < this->num_input_sds; ip++) {
    if (this->output_pixel_size[ip] != this->output_pixel_size[0])
      this->multires = true;
  }

  /* If the UL or LR corner was not specified, then use the bounding coords */
  if (!this->output_space_def.ul_corner_set ||
      !this->output_space_def.lr_corner_set) {
    /* Read the BOUNDING coords, by default */
    if (!ReadBoundCoords(this->input_file_name, &ul_corner, &lr_corner)) {
      sprintf(msg, "resamp: error reading BOUNDING COORDS from metadata. "
        "Therefore, in order to process this data, the output spatial "
        "subsetting will need to be specified.\n");
      Infomsg(msg);
      sprintf(msg, "%s\n", USAGE);
      Infomsg(msg);
      FreeParam(this);
      return (Param_t *)NULL; 
    }
    else {
      /* Store all initial corner points in the x/y corner structure.
         The call to convert_corners will handle moving to the lat/long
         structure location. */
      this->output_space_def.ul_corner_set = true;
      this->output_space_def.lr_corner_set = true;
      this->output_space_def.ul_corner.x = ul_corner.lon;
      this->output_space_def.ul_corner.y = ul_corner.lat;
      this->output_space_def.lr_corner.x = lr_corner.lon;
      this->output_space_def.lr_corner.y = lr_corner.lat;
      this->output_spatial_subset_type = LAT_LONG;
    }
  }

  if ((this->output_space_def.proj_param[0] <= 0.0) && 
      (this->output_space_def.sphere < 0)) {
    sprintf(msg, "resamp: either output space sphere or projection "
            "parameter number 0 must be given\n");
    Infomsg(msg);
    sprintf(msg, "%s\n", USAGE);
    Infomsg(msg);
    FreeParam(this);
    return (Param_t *)NULL; 
  }

  if ((this->output_space_def.proj_num == 1)  &&  /* UTM => proj_num = 1 */
       !this->output_space_def.zone_set) { 
    sprintf(msg, "resamp: output space zone number not given for UTM\n");
    Infomsg(msg);
    sprintf(msg, "%s\n", USAGE);
    Infomsg(msg);
    FreeParam(this);
    return (Param_t *)NULL; 
  }

  if (this->output_space_def.proj_num == 31) /* ISINUS => proj_num = 31 */
    this->output_space_def.isin_type= SPACE_ISIN_NEST_1;

  /* Copy the projection parameters to orig_proj_param to use the decimal
     degree values later (GeoTiff output) */
  for (i = 0; i < NPROJ_PARAM; i++) {
    this->output_space_def.orig_proj_param[i] =
      this->output_space_def.proj_param[i];
  }

  /* Convert the output projection parameter lat/long values from decimal
     degrees to DMS */
  if (!Deg2DMS (this->output_space_def.proj_num,
                this->output_space_def.proj_param)) {
    sprintf(msg, "resamp: error converting projection parameters from"
            "decimal degrees to DMS\n");
    Infomsg(msg);
    FreeParam(this);
    return (Param_t *)NULL; 
  }

  /* Use the UL and LR corner points to get the UL corner in output
     space and the number of lines/samples in the output image. This must
     be done for each SDS, since the pixel size might be different. */
  if (!ConvertCorners (this)) {
    sprintf(msg, "resamp: error determining UL and lines/samples from "
            "the input UL and LR corners\n");
    Infomsg(msg);
    FreeParam(this);
    return (Param_t *)NULL; 
  }

  /* Make sure the lat/long values are between +-180 and +-90 */
  if (this->output_space_def.ul_corner_geo.lat < -90.0 ||
      this->output_space_def.ul_corner_geo.lat > 90.0  ||
      this->output_space_def.lr_corner_geo.lat < -90.0 ||
      this->output_space_def.lr_corner_geo.lat > 90.0  ||
      this->output_space_def.ul_corner_geo.lon < -180.0 ||
      this->output_space_def.ul_corner_geo.lon > 180.0  ||
      this->output_space_def.lr_corner_geo.lon < -180.0 ||
      this->output_space_def.lr_corner_geo.lon > 180.0) {
    sprintf(msg, "resamp: invalid output lat/lon corners\n");
    Infomsg(msg);
    FreeParam(this);
    return (Param_t *)NULL; 
  }

  /* MRTSwath will only process swath data */
  if (this->input_space_type != SWATH_SPACE) {
    sprintf(msg, "resamp: grid or point data detected. MRTSwath will "
            "only process swath data\n");
    Infomsg(msg);
    FreeParam(this);
    return (Param_t *)NULL; 
  }

  return this;
}


Param_t *CopyParam(Param_t *param)
/* 
!C******************************************************************************

!Description: 'CopyParam' sets up the 'param' data structure and populates
 it with the values in hte input 'param' data structure.
 
!Input Parameters:
 param          valid 'param' data structure

!Output Parameters:
 (returns)      copy of input 'param' data structure or NULL if an error occurs

!Team Unique Header:

 ! Design Notes:
   1. An error status is returned when:
       a. memory allocation is not successful
   2. Error of type 'a' are handled with the 'RETURN_ERROR' macro and 
      the others are handled by writting the error messages to 'stderr' and 
      then printing the usage information.
   3. 'FreeParam' should be called to deallocate memory used by the 
      'param' data structures.

!END****************************************************************************
*/
{
  Param_t *this;
  int ip, jp;

  /* create the Param data structure */

  this = (Param_t *)malloc(sizeof(Param_t));
  if (this == (Param_t *)NULL)
    RETURN_ERROR("allocating Input structure", "CopyParam", (Param_t *)NULL);

  /* copy the parameters */

  this->input_file_name = strdup(param->input_file_name);
  this->output_file_name = strdup(param->output_file_name);
  this->geoloc_file_name = strdup(param->geoloc_file_name);
  this->output_file_format = param->output_file_format;
  this->input_space_type = param->input_space_type;
  this->num_input_sds = param->num_input_sds;
  this->multires = param->multires;

  for (ip = 0; ip < this->num_input_sds; ip++)
  {
    if (param->input_sds_name_list[ip] != NULL)
      strcpy (this->input_sds_name_list[ip], param->input_sds_name_list[ip]);

    this->input_sds_nbands[ip] = param->input_sds_nbands[ip];
    for (jp = 0; jp < this->input_sds_nbands[ip]; jp++)
      this->input_sds_bands[ip][jp] = param->input_sds_bands[ip][jp];

    this->rank[ip] = param->rank[ip];
    for (jp = 0; jp < MYHDF_MAX_RANK; jp++)
      this->dim[ip][jp] = param->dim[ip][jp];

    this->output_pixel_size[ip] = param->output_pixel_size[ip];
    this->output_img_size[ip] = param->output_img_size[ip];
    this->output_dt_arr[ip] = param->output_dt_arr[ip];
    this->ires[ip] = param->ires[ip];
    this->fill_value[ip] = param->fill_value[ip];
    this->create_output[ip] = param->create_output[ip];
  }

  if (param->input_sds_name != NULL)
    this->input_sds_name = strdup(param->input_sds_name);
  else
    this->input_sds_name = (char *)NULL;

  if (param->output_sds_name != NULL)
    this->output_sds_name = strdup(param->output_sds_name);
  else
    this->output_sds_name = (char *)NULL;

  this->iband = param->iband;
  this->kernel_type = param->kernel_type;

  /* Space_def_t doesn't contain any pointers, so its ok to make an
     exact copy */
  this->output_space_def = param->output_space_def;
  this->input_space_def = param->input_space_def;

  this->output_spatial_subset_type = param->output_spatial_subset_type;
  this->output_data_type = param->output_data_type;
  this->patches_file_name = strdup(param->patches_file_name);

  return this;
}


bool FreeParam(Param_t *this)
/* 
!C******************************************************************************

!Description: 'FreeParam' frees the 'param' data structure memory.
 
!Input Parameters:
 this           'param' data structure; the following fields are input:
                   input_file_name, output_file_name, geoloc_file_name, 
		   input_sds_name, output_sds_name

!Output Parameters:
 (returns)      status:
                  'true' = okay (always returned)

!Team Unique Header:

 ! Design Notes:
   1. 'GetParam' must be called before this routine is called.
   2. An error status is never returned.

!END****************************************************************************
*/
{
  if (this != (Param_t *)NULL) {
    if (this->input_file_name  != (char *)NULL) free(this->input_file_name);
    if (this->output_file_name != (char *)NULL) free(this->output_file_name);
    if (this->geoloc_file_name != (char *)NULL) free(this->geoloc_file_name);
    if (this->input_sds_name   != (char *)NULL) free(this->input_sds_name);
    if (this->output_sds_name  != (char *)NULL) free(this->output_sds_name);
    free(this);
  }
  return true;
}                            


int ReadSDS(Param_t *this)
/*
!C******************************************************************************
!Description: 'ReadSDS' reads the input file for available swath SDSs.

!Input Parameters:
 this           'input' data structure; the following fields are input:
                   input_file_name

!Output Parameters:
 this           'input' data structure; the following fields are modified:
                   input_sds_name_list, input_sds_nbands, input_sds_bands

 (returns)      int: number of SDSs

!Team Unique Header:

 ! Design Notes:
   1. The file is open for HDF access but closed before returning from the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -