/****************************************************************/
/*                                                              */
/* readbsqmetadata - Read the BSQ metadata from the header      */
/*                   and world file.                            */
/*                                                              */
/*              filename <CHAR*> - The BSQ filename.            */
/*              bsqmetadata <BSQMETADATA> - Structure to hold   */
/*                           BSQ metadata.                      */
/*                                                              */
/****************************************************************/

LONG readbsqmetadata(CHAR *filename, BSQMETADATA *bsqmetadata)

{

FILE *fd;
SHORT i;
LFLOAT lvalue[6];
CHAR line[256], value[128], keyword[128], *headerfilename, *worldfilename;

/* create the BSQ header filename */

headerfilename = (CHAR *) malloc(sizeof(CHAR) * (strlen(filename) + 5));
sprintf(headerfilename,"%s.hdr",filename);

/* open the BSQ header file */

fd = fopen(headerfilename, "rt");

if (fd == NULL) {
    printf("error opening BSQ header file \n");
    return(SE_FAILURE);
}

free(headerfilename);

/* Set the pointer to the beginning of the file */

fseek( fd, 0L, SEEK_SET );

/* Read the contents of the BSQ header file into the */
/* appropriate bsqmetadata parameters */

while(fgets (line,80,fd)!=NULL) {
     sscanf (line,"%s%s",keyword,value);
     if (STRCASECMP(keyword,"BYTEORDER")==0)
          bsqmetadata->byteorder = value[0];
     else if(STRCASECMP(keyword,"NROWS")==0)
          sscanf(value,"%d",&bsqmetadata->pixelheight);
     else if(STRCASECMP(keyword,"NCOLS")==0)
          sscanf(value,"%d",&bsqmetadata->pixelwidth);
     else if(STRCASECMP(keyword,"NBANDS")==0)
          sscanf(value,"%d",&bsqmetadata->numbands);
     else if(STRCASECMP(keyword,"BANDROWBYTES")==0)
          sscanf(value,"%d",&bsqmetadata->bandrowbytes);
     else if(STRCASECMP(keyword,"COLORMAP")==0)
          sscanf(value,"%d",&bsqmetadata->colormapentries);
     else if(STRCASECMP(keyword,"MASK")==0)
          sscanf(value,"%d",&bsqmetadata->mask);
     else if(STRCASECMP(keyword,"PIXELTYPE")==0)
          sscanf(value,"%d",&bsqmetadata->pixelwidth);
}

/* close the header file */

fclose(fd);

/* create the BSQ world filename */

worldfilename = (CHAR *) malloc(sizeof(CHAR) * (strlen(filename) + 5));
sprintf(worldfilename,"%s.bqw",filename);

/* open the BSQ world file */

fd = fopen(worldfilename, "rt");

if (fd == NULL) {
    printf("error opening BSQ world file \n");
    return(SE_FAILURE);
}

/* Set the pointer to the beginning of the file */

fseek( fd, 0L, SEEK_SET );

/* Read the parameters of the world file into the bsqmetadata structure */

/* BSQ worldfile has a standard format. */

for(i=0;i<6;i++) {
    if (fgets(line,80,fd) == NULL) {
         printf ("Error: Invalid world file %s!\n",worldfilename);
         printf ("Line: %d is blank.\n",i+1);
         printf ("Valid world files contain 6 lines of real numbers.\n");
         return(SE_FAILURE);
    }
    if (sscanf(line,"%lf",&lvalue[i]) != 1) {
         printf ("Error: Invalid world file %s!\n",worldfilename);
         printf ("The value at ine %d is not a real number\n",i+1);
         printf ("Value read was %s.\n",value[i]);
         return(SE_FAILURE);
    }
}

if (fgets(line,80,fd) != NULL) {
    printf("Error: Invalid world file %s!\n", worldfilename);
    printf("World file contains more then 6 lines of data.\n");
    return(SE_FAILURE);
}

/* close the BSQ worldfile */

free(worldfilename);

fclose(fd);

bsqmetadata->xcellsize = lvalue[0];
bsqmetadata->ycellsize = lvalue[3];
bsqmetadata->xmin = lvalue[4];
bsqmetadata->ymax = lvalue[5];
bsqmetadata->xmax = bsqmetadata->xmin + bsqmetadata->xcellsize * (LFLOAT) (bsqmetadata->pixelwidth-1);
bsqmetadata->ymin = bsqmetadata->ymax + bsqmetadata->ycellsize * (LFLOAT) (bsqmetadata->pixelheight-1);

return(SE_SUCCESS);

} /* readbsqmetadata */