Пример #1
0
/******************************************************************************
!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;
}
Пример #2
0
/******************************************************************************
!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;
}