📄 param.c
字号:
/*
!C****************************************************************************
!File: param.c
!Description: Functions for accepting parameters from the command line or
a file.
!Revision History:
Revision 1.0 2000/11/07
Robert Wolfe
Original Version.
Revision 1.1 2000/12/13
Sadashiva Devadiga
Modified to accept parameters from command line or file.
Revision 1.2 2001/05/08
Sadashiva Devadiga
Added checks for required parameters.
Revision 1.3 2002/03/02
Robert Wolfe
Added special handling for input ISINUS case.
Revision 1.4 2002/05/10
Robert Wolfe
Added separate output SDS name.
Revision 1.5 06/02
Gail Schmidt
Changed to allow the user to specify input lat/long for the UL and LR
output corners, rather than specifying UL in x/y output space. Since
the UL and LR are specified, the output_image_size is no longer needed.
Revision 1.5 08/02
Gail Schmidt
Changed to allow the user to specify the lat/long values in the output
projection parameters as decimal degrees. This software will convert
the decimal degrees to DMS.
Revision 2.0 Nov-Dec/03
Gail Schmidt
Modified the software to process all the SDSs (of nominal MODIS scan size),
if an SDS was not specified in the parameter file. And, support multiple
SDSs for input by the user.
Also, GRIDs will not be used by MRTSwath, so don't allow GRID processing.
Modified the software to read the NSEW BOUNDING COORDS in the metadata,
if the UL and LR corners were not specified.
Default kernel_type is NN (instead of CC) to match the MRT.
Support output to raw binary.
Add support for multiple output pixel sizes. This also means multiple
lines and samples.
Added support to automatically determine the input resolution, and use
that as the default output pixel size ... if not specified by the user.
Allow the user to specify the UL/LR corner in line/sample units.
Revision 2.0 May/04
Modified ISINUS to be a value of 31 instead of 99 to match GCTP.
!Team Unique Header:
This software was developed by the MODIS Land Science Team Support
Group for the Laboratory for Terrestrial Physics (Code 922) at the
National Aeronautics and Space Administration, Goddard Space Flight
Center, under NASA Task 92-012-00.
! References and Credits:
! MODIS Science Team Member:
Christopher O. Justice
MODIS Land Science Team University of Maryland
justice@hermes.geog.umd.edu Dept. of Geography
phone: 301-405-1600 1113 LeFrak Hall
College Park, MD, 20742
! Developers:
Robert E. Wolfe (Code 922)
MODIS Land Team Support Group Raytheon ITSS
robert.e.wolfe.1@gsfc.nasa.gov 4400 Forbes Blvd.
phone: 301-614-5508 Lanham, MD 20770
Sadashiva Devadiga (Code 922)
MODIS Land Team Support Group SSAI
devadiga@ltpmail.gsfc.nasa.gov 5900 Princess Garden Pkway, #300
phone: 301-614-5549 Lanham, MD 20706
! Design Notes:
1. The following public functions handle the input data:
GetParam - Setup 'param' data structure and populate with user
parameters.
FreeParam - Free the 'param' data structure memory.
2. 'GetParam' must be called before 'FreeParam'.
3. 'FreeParam' should be used to free the 'param' data structure.
!END****************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <values.h> /* MAXFLOAT */
#include "hlimits.h" /* MAX_VAR_DIMS, MAX_NC_NAME */
#include "param.h"
#include "geoloc.h"
#include "parser.h"
#include "usage.h"
#ifndef MY_PROJS
#include "myproj.h"
#endif
#include "const.h"
#include "error.h"
#include "deg2dms.h"
/* External arrays */
extern Proj_sphere_t Proj_sphere[PROJ_NSPHERE];
extern Proj_type_t Proj_type[PROJ_NPROJ];
/* Functions */
Param_t *GetParam(int argc, const char **argv)
/*
!C******************************************************************************
!Description: 'GetParam' sets up the 'param' data structure and populate with user
parameters, either from the command line or from a parameter file.
!Input Parameters:
argc number of command line arguments
argv command line argument list
!Output Parameters:
(returns) 'param' data structure or NULL when an error occurs
!Team Unique Header:
! Design Notes:
1. An error status is returned when:
a. memory allocation is not successful
b. an error is returned from the ReadCmdLine function
c. certain required parameters are invalid or not entered:
input file name, output file name, geolocation file name,
SDS name, output space projection number,
output space pixel size, output space upper left corner,
output space image size, either output space sphere or projection
parameter number 0
output space zone number not given for UTM
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;
Input_t *input = NULL;
int i, j, ip, jp;
char tmp_sds_name[MAX_STR_LEN];
char errstr[1024]; /* error string for OpenInput */
char msg[256];
char *extptr; /* ptr to the output file extension */
float tmp_pixel_size;
Geo_coord_t ul_corner;
Geo_coord_t lr_corner;
int copy_dim[MYHDF_MAX_RANK];
/* Create the Param data structure */
this = (Param_t *)malloc(sizeof(Param_t));
if (this == (Param_t *)NULL)
RETURN_ERROR("allocating Input structure", "GetParam", (Param_t *)NULL);
/* set default parameters */
this->multires = false;
this->input_file_name = (char *)NULL;
this->output_file_name = (char *)NULL;
this->geoloc_file_name = (char *)NULL;
this->input_space_type = SWATH_SPACE; /* Default is input swath */
this->output_file_format = HDF_FMT; /* Default is HDF output */
this->num_input_sds = 0; /* Default is no SDSs specified */
this->output_space_def.pixel_size = -1.0;
for (ip = 0; ip < MAX_SDS_DIMS; ip++)
{
this->input_sds_nbands[ip] = 0;
this->output_pixel_size[ip] = -1.0;
this->output_img_size[ip].l = -1;
this->output_img_size[ip].s = -1;
this->output_dt_arr[ip] = -1;
this->ires[ip] = -1;
this->fill_value[ip] = -1.0;
for (jp = 0; jp < MAX_VAR_DIMS; jp++)
this->input_sds_bands[ip][jp] = 0; /* Default is no bands processed */
this->create_output[ip] = true;
this->rank[ip] = 2;
for (jp = 0; jp < MYHDF_MAX_RANK; jp++)
this->dim[ip][jp] = 0;
this->dim[ip][0] = -1;
this->dim[ip][1] = -2;
}
this->input_sds_name = (char *)NULL;
this->output_sds_name = (char *)NULL;
this->iband = -1;
this->kernel_type = NN;
this->output_space_def.proj_num = -1;
for (ip = 0; ip < NPROJ_PARAM; ip++)
this->output_space_def.proj_param[ip] = 0.0;
for (ip = 0; ip < NPROJ_PARAM; ip++)
this->output_space_def.orig_proj_param[ip] = 0.0;
this->output_space_def.ul_corner.x = -1.0;
this->output_space_def.ul_corner.y = -1.0;
this->output_space_def.ul_corner_geo.lat = -1.0;
this->output_space_def.ul_corner_geo.lon = -1.0;
this->output_space_def.ul_corner_set = false;
this->output_space_def.lr_corner.x = -1.0;
this->output_space_def.lr_corner.y = -1.0;
this->output_space_def.lr_corner_geo.lat = -1.0;
this->output_space_def.lr_corner_geo.lon = -1.0;
this->output_space_def.lr_corner_set = false;
this->output_space_def.img_size.l = -1;
this->output_space_def.img_size.s = -1;
this->output_space_def.zone = 0;
this->output_space_def.zone_set = false;
this->output_space_def.sphere = -1;
this->output_space_def.isin_type = SPACE_NOT_ISIN;
this->output_spatial_subset_type = LAT_LONG;
/* Input space is not really used, since the MRTSwath will not
work with Grids (only swath) */
this->input_space_def.proj_num = -1;
for (ip = 0; ip < NPROJ_PARAM; ip++)
this->input_space_def.proj_param[ip] = 0.0;
this->input_space_def.pixel_size = -1.0;
this->input_space_def.ul_corner.x = -1.0;
this->input_space_def.ul_corner.y = -1.0;
this->input_space_def.ul_corner_set = false;
this->input_space_def.img_size.l = -1;
this->input_space_def.img_size.s = -1;
this->input_space_def.zone = 0;
this->input_space_def.zone_set = false;
this->input_space_def.sphere = -1;
this->input_space_def.isin_type = SPACE_NOT_ISIN;
this->output_data_type = -1;
this->patches_file_name = "patches.tmp";
/* Read the command-line and parameter file parameters */
if (!ReadCmdLine(argc, argv, this)) {
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
/* Check to see that all of the parameters are entered */
if ((this->input_file_name == (char *)NULL) ||
(strlen(this->input_file_name) < 1)) {
sprintf(msg, "resamp: input file name not given\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
/* Check the output filename */
if ((this->output_file_name == (char *)NULL) ||
(strlen(this->output_file_name) < 1)) {
sprintf(msg, "resamp: output file name not given\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
/* Check to see if a .hdf, .hdr. or .tif extension was provided in the
filename. If so, remove it since the output file format will specify
the output extension. */
extptr = strstr (this->output_file_name, ".hdf");
if (extptr)
extptr[0] = '\0';
extptr = strstr (this->output_file_name, ".HDF");
if (extptr)
extptr[0] = '\0';
extptr = strstr (this->output_file_name, ".hdr");
if (extptr)
extptr[0] = '\0';
extptr = strstr (this->output_file_name, ".HDR");
if (extptr)
extptr[0] = '\0';
extptr = strstr (this->output_file_name, ".tif");
if (extptr)
extptr[0] = '\0';
extptr = strstr (this->output_file_name, ".TIF");
if (extptr)
extptr[0] = '\0';
/* Check the output file format */
if ((this->output_file_format != HDF_FMT) &&
(this->output_file_format != GEOTIFF_FMT) &&
(this->output_file_format != RB_FMT) &&
(this->output_file_format != BOTH)) {
sprintf(msg, "resamp: unsupported output file format\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
if ((this->input_space_type == SWATH_SPACE) &&
((this->geoloc_file_name == (char *)NULL) ||
(strlen(this->input_file_name) < 1))) {
sprintf(msg, "resamp: geolocation file name not given\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
/* If no SDS names were specified then process all of them in the file,
otherwise fill in the rest of the SDS information. */
if (this->num_input_sds == 0) {
/* Process all the SDS names, by default */
#ifdef DEBUG
printf ("Reading default SDSs\n");
#endif
this->num_input_sds = ReadSDS(this);
if (this->num_input_sds == 0) {
sprintf(msg, "resamp: error reading default SDS names\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
}
else {
/* Read the SDSs and determine the number of bands in each */
if (!SDSInfo(this)) {
sprintf(msg, "resamp: error reading SDS information\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
}
/* Check output space definition */
if (this->output_space_def.proj_num < 0) {
sprintf(msg, "resamp: output space projection number not given\n");
Infomsg(msg);
FreeParam(this);
sprintf(msg, "%s\n", USAGE);
Infomsg(msg);
return (Param_t *)NULL;
}
/* Loop through all the SDSs and determine their resolution */
for (i = 0; i < this->num_input_sds; i++)
{
/* Loop through all the bands in the current SDS until we find one
that will be processed. Use that band to get the resolution of the
SDS, since all the bands in the SDS will be the same resolution. */
for (j = 0; j < this->input_sds_nbands[i]; j++)
{
/* Is this band one that should be processed? */
if (!this->input_sds_bands[i][j])
continue;
/* Create the input_sds_name which is "SDSname, band" */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -