/***************************************************************************** NAME: open_input PURPOSE: Open all the input files and allocate associated memory for the filenames that reside in the data structure. RETURN VALUE: Type = Input_Data_t * Value Description ------- --------------------------------------------------------------- NULL An error was encountered. * A pointer to the populated Input_Data_t structure. *****************************************************************************/ Input_Data_t * open_input ( Espa_internal_meta_t *metadata, /* I: input metadata */ bool use_toa_flag, /* I: use TOA or SR data */ char *dem_filename /* I: the name of the DEM file */ ) { int index; Input_Data_t *input_data = NULL; input_data = (Input_Data_t *) malloc (sizeof (Input_Data_t)); if (input_data == NULL) { ERROR_MESSAGE ("Error allocating memory for input data structure", MODULE_NAME); return NULL; } /* Initialize the band fields */ for (index = 0; index < MAX_INPUT_BANDS; index++) { input_data->band_name[index] = NULL; input_data->band_fd[index] = NULL; } input_data->lines = 0; input_data->samples = 0; /* Open the input images from the XML file */ if (GetXMLInput (metadata, use_toa_flag, dem_filename, input_data) != SUCCESS) { /* error messages provided by GetXMLInput */ close_input (input_data); return NULL; } return input_data; }
/****************************************************************************** !Description: 'OpenInput' sets up the 'input' data structure, opens the input file for read access, allocates space, and stores some of the metadata. !Input Parameters: file_name input file name !Output Parameters: (returns) populated 'input' data structure or NULL when an error occurs HISTORY: Date Programmer Reason -------- --------------- ------------------------------------- 2/13/2014 Gail Schmidt Modified to work with ESPA internal raw binary file format !Design Notes: ******************************************************************************/ Input_t *OpenInput ( Espa_internal_meta_t *metadata /* I: input metadata */ ) { Input_t *this = NULL; char *error_string = NULL; int i; /* looping variable */ int ib; /* band looping variable */ int16 *buf = NULL; FILE *dsun_in = NULL; /* EarthSunDistance.txt file pointer */ char *path = NULL; char full_path[MAX_STR_LEN]; /* Create the Input data structure */ this = (Input_t *) malloc (sizeof (Input_t)); if (this == NULL) RETURN_ERROR ("allocating Input data structure", "OpenInput", NULL); /* Initialize and get input from header file */ if (!GetXMLInput (this, metadata)) { free (this); this = NULL; RETURN_ERROR ("getting input from header file", "OpenInput", NULL); } /* Open TOA reflectance files for access */ for (ib = 0; ib < this->nband; ib++) { printf ("DEBUG: band %d filename: %s\n", ib, this->file_name[ib]); this->fp_bin[ib] = open_raw_binary (this->file_name[ib], "r"); if (this->fp_bin[ib] == NULL) { RETURN_ERROR ("opening input TOA binary file", "OpenInput", NULL); } this->open[ib] = true; } /* Open thermal file for access */ printf ("DEBUG: thermal band filename: %s\n", this->file_name_therm); this->fp_bin_therm = open_raw_binary (this->file_name_therm, "r"); if (this->fp_bin_therm == NULL) error_string = "opening thermal binary file"; else this->open_therm = true; /* Allocate input buffers. Thermal band only has one band. Image and QA buffers have multiple bands. */ buf = calloc ((size_t) (this->size.s * this->nband), sizeof (int16)); if (buf == NULL) error_string = "allocating input buffer"; else { this->buf[0] = buf; for (ib = 1; ib < this->nband; ib++) this->buf[ib] = this->buf[ib - 1] + this->size.s; } this->therm_buf = calloc ((size_t) (this->size.s), sizeof (int16)); if (this->therm_buf == NULL) error_string = "allocating input thermal buffer"; if (error_string != NULL) { FreeInput (this); CloseInput (this); RETURN_ERROR (error_string, "OpenInput", NULL); } path = getenv ("ESUN"); if (path == NULL) { error_string = "ESUN environment variable is not set"; RETURN_ERROR (error_string, "OpenInput", NULL); } sprintf (full_path, "%s/%s", path, "EarthSunDistance.txt"); dsun_in = fopen (full_path, "r"); if (dsun_in == NULL) { error_string = "Can't open EarthSunDistance.txt file"; RETURN_ERROR (error_string, "OpenInput", NULL); } for (i = 0; i < 366; i++) { if (fscanf (dsun_in, "%f", &this->dsun_doy[i]) == EOF) { error_string = "End of file (EOF) is met before 336 lines"; RETURN_ERROR (error_string, "OpenInput", NULL); } } fclose (dsun_in); /* Calculate maximum TOA reflectance values and put them in metadata */ dn_to_toa_saturation (this); /* Calculate maximum BT values and put them in metadata */ dn_to_bt_saturation (this); return this; }
/* Functions */ Input_t *OpenInput(Espa_internal_meta_t *metadata) /* !C****************************************************************************** !Description: 'OpenInput' sets up the 'input' data structure, opens the input raw binary files for read access. !Input Parameters: metadata 'Espa_internal_meta_t' data structure with XML info !Output Parameters: (returns) 'input' data structure or NULL when an error occurs !Team Unique Header: !END**************************************************************************** */ { Input_t *this = NULL; char *error_string = NULL; int ib; /* Create the Input data structure */ this = (Input_t *)malloc(sizeof(Input_t)); if (this == NULL) RETURN_ERROR("allocating Input data structure", "OpenInput", NULL); /* Initialize and get input from header file */ if (!GetXMLInput (this, metadata)) { free(this); this = NULL; RETURN_ERROR("getting input from header file", "OpenInput", NULL); } /* Open files for access */ if (this->file_type == INPUT_TYPE_BINARY) { for (ib = 0; ib < this->nband; ib++) { this->fp_bin[ib] = fopen(this->file_name[ib], "r"); if (this->fp_bin[ib] == NULL) { error_string = "opening binary file"; break; } this->open[ib] = true; } if ( this->nband_th == 1 ) { this->fp_bin_th = fopen(this->file_name_th, "r"); if (this->fp_bin_th == NULL) error_string = "opening thermal binary file"; else this->open_th = true; } } else error_string = "invalid file type"; if (error_string != NULL) { for (ib = 0; ib < this->nband; ib++) { free(this->file_name[ib]); this->file_name[ib] = NULL; if (this->open[ib]) { if ( this->file_type == INPUT_TYPE_BINARY ) fclose(this->fp_bin[ib]); this->open[ib] = false; } } free(this->file_name_th); this->file_name_th = NULL; if ( this->file_type == INPUT_TYPE_BINARY ) fclose(this->fp_bin_th); this->open_th = false; free(this); this = NULL; RETURN_ERROR(error_string, "OpenInput", NULL); } return this; }
/****************************************************************************** !Description: 'OpenInput' sets up the 'input' data structure, opens the input file for read access, allocates space, and stores some of the metadata. !Input Parameters: file_name input file name !Output Parameters: (returns) populated 'input' data structure or NULL when an error occurs HISTORY: Date Programmer Reason -------- --------------- ------------------------------------- Oct/2014 Ron Dilley Modified to work with ESPA internal raw binary file format !Design Notes: ******************************************************************************/ Input_t *OpenInput ( Espa_internal_meta_t *metadata /* I: input metadata */ ) { Input_t *this = NULL; char *error_string = NULL; int ib; /* band looping variable */ int16 *buf = NULL; char *path = NULL; /* Create the Input data structure */ this = (Input_t *) malloc (sizeof (Input_t)); if (this == NULL) RETURN_ERROR ("allocating Input data structure", "OpenInput", NULL); /* Initialize and get input from header file */ if (!GetXMLInput (this, metadata)) { free (this); this = NULL; RETURN_ERROR ("getting input from header file", "OpenInput", NULL); } /* Open TOA reflectance files for access */ for (ib = 0; ib < this->nband; ib++) { printf ("DEBUG: band %d filename: %s\n", ib, this->file_name[ib]); this->fp_bin[ib] = open_raw_binary (this->file_name[ib], "r"); if (this->fp_bin[ib] == NULL) { RETURN_ERROR ("opening input TOA binary file", "OpenInput", NULL); } this->open[ib] = true; } /* Open thermal file for access */ printf ("DEBUG: thermal band filename: %s\n", this->file_name_therm); this->fp_bin_therm = open_raw_binary (this->file_name_therm, "r"); if (this->fp_bin_therm == NULL) error_string = "opening thermal binary file"; else this->open_therm = true; /* Allocate input buffers. Thermal band only has one band. Image and QA buffers have multiple bands. */ buf = calloc ((size_t) (this->size.s * this->nband), sizeof (int16)); if (buf == NULL) error_string = "allocating input buffer"; else { this->buf[0] = buf; for (ib = 1; ib < this->nband; ib++) this->buf[ib] = this->buf[ib - 1] + this->size.s; } this->therm_buf = calloc ((size_t) (this->size.s), sizeof (int16)); if (this->therm_buf == NULL) error_string = "allocating input thermal buffer"; if (error_string != NULL) { FreeInput (this); CloseInput (this); RETURN_ERROR (error_string, "OpenInput", NULL); } path = getenv ("ESUN"); if (path == NULL) { error_string = "ESUN environment variable is not set"; RETURN_ERROR (error_string, "OpenInput", NULL); } /* Calculate maximum TOA reflectance values and put them in metadata */ dn_to_toa_saturation (this); /* Calculate maximum BT values and put them in metadata */ dn_to_bt_saturation (this); return this; }