コード例 #1
0
ファイル: tsd.c プロジェクト: Milkyway-at-home/nemo
scan_sd(string infile)
{
    int id, sds, n, i, j, k, ret, size, type, na, nd;
    char name[256];

    id = SDstart(infile, DFACC_RDONLY);
    if (id<0) error("%s is probably not an HDF SD (scientific dataset)",
                      infile);

    ret = SDfileinfo(id, &nd, &na);
    
    dprintf(0,"Found %d scientific data set%s in %s with %d attributes\n",
            nd,(nd > 1 ? "s" : ""),infile, na);
    for (k=0;;k++) {
        sds = SDselect(id, nd);
        ret = SDgetinfo(sds, name, &rank, shape, &type, &na);
    	label[0] = unit[0] = fmt[0] = coordsys[0] = 0;
        ret =  SDgetdatastrs(sds, label, unit, fmt, coordsys, 256);
    	dprintf(0,"%d: %s(",k,label);
    	for (i=0, size=1; i<rank; i++) {
    	    if (i==rank-1)
                dprintf(0,"%d)",shape[i]);
            else
                dprintf(0,"%d,",shape[i]);
    	    size *= shape[i];
    	}
    	dprintf(0," %s ",unit);
    	dprintf(0," -> [%d elements of type: %d]\n", size, type);
        /* ret = SDendaccess(sds); */
    }   
    SDend(id);
}
コード例 #2
0
ファイル: hrepack_list.c プロジェクト: schwehr/hdf4
int list_sds(int32 infile_id,
             int32 outfile_id,
             int32 sd_id,
             int32 sd_out,
             list_table_t *list_tbl,
             dim_table_t *td1,
             dim_table_t *td2,
             options_t *options)
{
    int32 sds_id,                 /* dataset identifier */
          n_datasets,             /* number of datasets in the file */
          n_file_attrs,           /* number of file attributes */
          index,                  /* index of a dataset */
          sds_ref,                /* reference number */
          dim_sizes[H4_MAX_VAR_DIMS],/* dimensions of an image */
          data_type,              /* number type  */
          rank,                   /* rank */
          n_attrs;                /* number of attributes */
    char  name[H4_MAX_GR_NAME];      /* name of dataset */
    
    /* determine the number of data sets in the file and the number of file attributes */
    if (SDfileinfo (sd_id, &n_datasets, &n_file_attrs)==FAIL){
        printf("Could not get SDS info\n");
        return FAIL;
    }
    
    for (index = 0; index < n_datasets; index++)
    {
        sds_id  = SDselect (sd_id, index);
        SDgetinfo(sds_id, name, &rank, dim_sizes, &data_type, &n_attrs);
        sds_ref = SDidtoref(sds_id);
        
        /* check if already inserted in Vgroup; search all SDS tags */
        if ( list_table_search(list_tbl,DFTAG_SD,sds_ref)>=0 ||
            list_table_search(list_tbl,DFTAG_SDG,sds_ref)>=0 ||
            list_table_search(list_tbl,DFTAG_NDG,sds_ref)>=0 )
        {
            SDendaccess (sds_id);
            continue;
        }
        
        /* copy SDS  */
        if (copy_sds(sd_id,sd_out,TAG_GRP_DSET,sds_ref,0,NULL,options,list_tbl,td1,td2,
            infile_id,outfile_id)<0) goto out;
        
        /* terminate access to the current dataset */
        SDendaccess (sds_id);
    }
    
    return SUCCEED;
    
out:
    SDendaccess (sds_id);
    return FAIL;
}
コード例 #3
0
ファイル: myhdf.c プロジェクト: HawkyLu/cfmask
bool GetSDSInfo(int32 sds_file_id, Myhdf_sds_t *sds)
/* 
!C******************************************************************************

!Description: 'GetSDSInfo' reads information for a specific SDS.
 
!Input Parameters:
 sds_file_id    SDS file id

!Output Parameters:
 sds            SDS data structure; the following fields are updated:
                  index, name, id, rank, type, nattr
 (returns)      Status:
                  'true' = okay
		  'false' = error reading the SDS information

!Team Unique Header:

 ! Design Notes:
   1. An error status is returned if the SDS rank is greater than 
      'MYHDF_MAX_RANK'.
   2. On normal returns the SDS is selected for access.
   3. The HDF file is assumed to be open for SD (Science Data) access.
   4. Error messages are handled with the 'RETURN_ERROR' macro.

!END****************************************************************************
*/
{
  int32 dims[MYHDF_MAX_RANK];

//  printf("SDS name %s\n",sds->name);
  sds->index = SDnametoindex(sds_file_id, sds->name);
  if (sds->index == HDF_ERROR)
    RETURN_ERROR("getting sds index", "GetSDSInfo", false);

  sds->id = SDselect(sds_file_id, sds->index);
  if (sds->id == HDF_ERROR)
    RETURN_ERROR("getting sds id", "GetSDSInfo", false);

  if (SDgetinfo(sds->id, sds->name, &sds->rank, dims, 
                &sds->type, &sds->nattr) == HDF_ERROR) {
    SDendaccess(sds->id);
    RETURN_ERROR("getting sds information", "GetSDSInfo", false);
  }
  if (sds->rank > MYHDF_MAX_RANK) {
    SDendaccess(sds->id);
    RETURN_ERROR("sds rank too large", "GetSDSInfo", false);
  }
  return true;
}
コード例 #4
0
ファイル: hdf_get_sds.c プロジェクト: durack1/Tcl-NAP
void
hdf_get_sds(
    char                *filename,
    char                *sds_name,
    int                 *start,
    int                 *stride,
    int                 *edge,
    void                *data,                  /* area for input data */ 
    int                 *status)                /* see above */
{   
    int32               edge32[MAX_VAR_DIMS];   
    int32               i;
    char                name[MAX_NC_NAME];
    int32               natts;                  /* # attributes */
    int32               number_type;            /* HDF type */
    int32               rank;                   
    int                 s;                      /* error status */
    int32               sd_id;                  /* file handle */
    int32               sds_id;                 /* SDS handle */
    int32               sds_index;              /* SDS index */
    int32               shape32[MAX_VAR_DIMS];
    int32               start32[MAX_VAR_DIMS];
    int32               stride32[MAX_VAR_DIMS];
    
    sd_id = SDstart(filename, DFACC_RDONLY);
    CHECK(sd_id >= 0, 1);
    sds_index = SDnametoindex(sd_id, sds_name);
    CHECK(sds_index >= 0, 2);
    sds_id = SDselect(sd_id, sds_index);
    CHECK(sds_id >= 0, 3);
    s = SDgetinfo(sds_id, name, &rank, shape32, &number_type, &natts);
    CHECK(s == SUCCEED, 4);
    for (i = 0; i < rank; i++) {
        start32[i] = start[i];
        stride32[i] = stride[i];
        edge32[i] = edge[i];
    }
    s = SDreaddata(sds_id, start32, stride32, edge32, data);
    CHECK(s == SUCCEED, 5);
    s = SDend(sd_id);
    CHECK(s == SUCCEED, 6);
    *status = 0;
}
コード例 #5
0
ファイル: read_sds_name.c プロジェクト: YannChemin/wxGIPE
int main(int argc, char *argv[]){
	char	filename[100], sds_name[100][MAX_NUM_SDS];
	int32	file_ID, sds_ID, num_sds, num_file_attribute;
	int	which_sds=0, i;

	char	name[100], type_name[100];
	int32	num_dim, dim_size[MAX_NUM_DIM];
	int32	data_type, num_sds_attribute;

	strcpy( filename, "2B31.070101.52026.6.HDF");

	/*
	* Read Data
	*/

	file_ID 	= SDstart( filename, DFACC_READ );
	sds_ID		= SDfileinfo( file_ID, &num_sds, &num_file_attribute );

	printf( "\n there are %d SDSs in the file.\n", num_sds );

	for( which_sds=0 ; which_sds<num_sds ; which_sds++ ){
		sds_ID	= SDselect( file_ID, which_sds );
		SDgetinfo( sds_ID, name, &num_dim, dim_size, &data_type, &num_sds_attribute );
		SDendaccess( sds_ID );
	
		switch (data_type){
			case 5: strcpy( type_name, "float32" ); break;
			case 20: strcpy( type_name, "int8" ); break;
			case 22: strcpy( type_name, "int16" ); break;
			case 24: strcpy( type_name, "int32" ); break;
			default: strcpy( type_name, "other" ); break;
		}
		
		strcpy( sds_name[which_sds], name );
		printf( "%3d %10s %20s(", which_sds, type_name, sds_name[which_sds] );
		for(i=0;i<num_dim;i++){
			printf("%5d, ", dim_size[i]);
		}
		printf( ")\n" );
	}
	SDend( file_ID );
	exit(0);
}
コード例 #6
0
ファイル: cdunifhdf.c プロジェクト: MartinDix/cdat_lite_test
/* Retrieve the variable values. Return success (0) if the
 * variable values were obtained sucessfully, otherwise this
 * function returns the failure status (-1).
 */
int cuvarget_hdf(CuFile* file, int varid, const long start[], const long count[], void* values){

	int32 sds_id, i, ndims;
	int32 *startvalues, *edges, natts;
	int32 dim_sizes[H4_MAX_VAR_DIMS];
	char name[H4_MAX_NC_NAME+1];
	hdf_type dtype;

 	/* Get the identifier for the data set. */
        sds_id = SDselect(file->internid1, varid);

	/* Get the number of dimensions. */
        if (SDgetinfo(sds_id, name, &ndims, dim_sizes, &dtype, &natts) == -1) {
             cuerrorreport_hdf();
             return (-1);
	}

	/* Define dimension size */
        startvalues = (int32 *)malloc((ndims)*sizeof(int32));
        edges       = (int32 *)malloc((ndims)*sizeof(int32));

	for (i = 0; i < ndims; ++i) {
	   startvalues[i] = start[i];
	   edges[i] = count[i];
	}

	/* Read the data array. */
        if (SDreaddata(sds_id, startvalues, NULL, edges, (VOIDP)values) == -1) {
             cuerrorreport_hdf();
             return (-1);
        }

	free ((char *) startvalues);
	free ((char *) edges);

	/* Return success ( 0 ). */
        return (CU_SUCCESS);
}
コード例 #7
0
ファイル: cdunifhdf.c プロジェクト: MartinDix/cdat_lite_test
/* Get information about the variable. Return success (0) if the
 * variable information was obtained sucessfully, otherwise this
 * function returns the failure status (-1).
 */
int cuvarinq_hdf(CuFile* file, int varid, char* name, CuType* datatype, int* ndims, int dimids[], int* natts){
	int err, i;
	hdf_type dtype;
	char t_name[H4_MAX_NC_NAME+1];
	int t_ndims, t_natts, numdims;
	int t_dimids[H4_MAX_VAR_DIMS];
	int32 sds_id, index;
	int32 dim_sizes[H4_MAX_VAR_DIMS];

	/* Select the data set corresponding to the returned index. */
	sds_id = SDselect(file->internid1, varid);

	/* Get the variable information. */
	if ((err=SDgetinfo(sds_id, 
                           (name ? name : t_name), 
                           (ndims ? ndims : &t_ndims),
                           dim_sizes,
                           &dtype, 
                           (natts ? natts : &t_natts))) != -1) {
		if(datatype) {
			cumapdatatype_hdf(dtype,datatype);
			if (*datatype==CuInvalidType)
				return -1;
		}
 
         	/* Retrieve dimension IDs. */
		if (dimids) {
		   numdims = (ndims ? *ndims : t_ndims);
		   for (i=0; i < numdims; ++i) /* reverse the order */
		       dimids[i] = SDgetdimid (sds_id, i) - file->internid2;
		}
	}

	/* Return variable ID, or failure ( -1 ). */
	return (err==-1 ? -1 : CU_SUCCESS);
}
コード例 #8
0
ファイル: cdunifhdf.c プロジェクト: MartinDix/cdat_lite_test
/* Get information about the dimension. Return success (0) if the 
 * dimension information  was obtained sucessfully, otherwise this 
 * function returns the failure status (-1).
 */
int cudiminq_hdf(CuFile* file, int dimidx, char* dimname, char* dimunits, CuType* dataType, CuDimType* dimtype, int* varid, long* length){
	char dname[H4_MAX_NC_NAME+1];
	int cdfid;
	int dimvarid; /* HDF ID of variable associated with this dimension. */
	int found;   /* True iff a dimension variable was found. */
	int ndims;
	int get_dimid;
	int saveopts;
	long len;
	hdf_type hdftype, hdfunitstype;
	int natts;
	char varname[H4_MAX_NC_NAME+1];
	int attlen;

	int32 sds_id, attr_index, datatype, nattrs;
	int32 dim_sizes[H4_MAX_VAR_DIMS];
	char attr_name[H4_MAX_NC_NAME];
	int32 dimid;

	cdfid = file->internid1;
	if((dimid = cudimid2hdf(file, dimidx))==-1)
		return -1;

	/* Get information about the selected dimension. */
	if(SDdiminfo(dimid, dname, &len, &datatype, &nattrs)==-1){
           	cuerrorreport_hdf();
                return -1;
        }

	if(dimname) strncpy(dimname,dname,CU_MAX_NAME);
	if(length) *length = len;

        /* HDF dimensions are always global */
	if(varid) *varid = CU_GLOBAL;
	if(dimtype) *dimtype = CuGlobalDim;

        /* Inquire a variable with */
        /* - the same name as dimname, */
        /* - a single dimension, and */
        /* - a dimension name which equals the variable name. */
	if((dimvarid = SDnametoindex(cdfid, dname)) != -1){
                sds_id = SDselect(cdfid, dimvarid);
                if (SDgetinfo(sds_id, varname, &ndims, dim_sizes,
                                   &hdftype, &natts) == -1){
           	        cuerrorreport_hdf();
			return -1;
		}

                /* pass back the dimension id */
                if ((get_dimid = SDgetdimid(sds_id, 0)) == -1){
           	        cuerrorreport_hdf();
                        return -1;
                }

		found = (ndims == 1 && get_dimid == dimid);
	}
	else
		found = 0;

	/* If dimension variable was found, */
	/* inquire the units attribute (if any) */
	if(found){
                sds_id = SDselect(cdfid, dimvarid);

		/* Set the length of an unlimited dimension. */
		if (len==0 && length) *length = dim_sizes[0];

        	/* Find the data set attribute name index. */
        	attr_index = SDfindattr(sds_id, "units");

        	/* Get information about the data set attribute. */
        	if(SDattrinfo(sds_id, attr_index, attr_name, &hdfunitstype, 
                    &attlen) != -1 && hdfunitstype == DFNT_CHAR) {
			if(dimunits && SDreadattr(sds_id, attr_index, dimunits)==-1)
				return -1;
		}
	    /* Dimension variable was found, but no character units string */
		else{
			if(dimunits) strcpy(dimunits,"");
		}
		if(dataType) {
			cumapdatatype_hdf(hdftype, dataType);
			if (*dataType==CuInvalidType)
				return -1;
		}
	}
	else{
		/* The dimension variable was not found: */
		/* return default units and datatype */
		if(dimunits) strcpy(dimunits,"");
		if(dataType) *dataType = CuFloat;
	}

	/* Return success ( 0 ). */
	return CU_SUCCESS;
}
コード例 #9
0
ファイル: tszip.c プロジェクト: Higginbottom/zeus_python
static intn test_szip_SDSfl64bit()
{
    /************************* Variable declaration **************************/

    int32	sd_id, sds_id;
    intn 	status;
    int32	dim_sizes[2], array_rank, num_type, attributes;
    char	name[H4_MAX_NC_NAME];
    comp_info	c_info;
    int32       start[2], edges[2];
    float64     fill_value = 0;   /* Fill value */
    int         i,j;
    int		num_errs = 0;    /* number of errors so far */
    float64	out_data[LENGTH][WIDTH];
    float64	in_data[LENGTH][WIDTH]={
	   			 100.0,100.0,200.0,200.0,300.0,400.0,
	   			 100.0,100.0,200.0,200.0,300.0,400.0,
				 100.0,100.0,200.0,200.0,300.0,400.0,
				 300.0,300.0,  0.0,400.0,300.0,400.0,
				 300.0,300.0,  0.0,400.0,300.0,400.0,
				 300.0,300.0,  0.0,400.0,300.0,400.0,
				   0.0,  0.0,600.0,600.0,300.0,400.0,
				 500.0,500.0,600.0,600.0,300.0,400.0,
				   0.0,  0.0,600.0,600.0,300.0,400.0};

    /********************* End of variable declaration ***********************/

    /* Create the file and initialize SD interface */
    sd_id = SDstart (FILE_NAMEfl64, DFACC_CREATE);
    CHECK(sd_id, FAIL, "SDstart");

    /* Create the SDS */
    dim_sizes[0] = LENGTH;
    dim_sizes[1] = WIDTH;
    sds_id = SDcreate (sd_id, SDS_NAME, DFNT_FLOAT64, RANK, dim_sizes);
    CHECK(sds_id, FAIL, "SDcreate:Failed to create a data set for szip compression testing");

    /* Define the location, pattern, and size of the data set */
    for (i = 0; i < RANK; i++) {
	start[i] = 0;
	edges[i] = dim_sizes[i];
	}

    /* Fill the SDS array with the fill value */
    status = SDsetfillvalue (sds_id, (VOIDP)&fill_value);
    CHECK(status, FAIL, "SDsetfillvalue");

    /* Initialization for SZIP */
    c_info.szip.pixels_per_block = 2;

    c_info.szip.options_mask = SZ_EC_OPTION_MASK;
    c_info.szip.options_mask |= SZ_RAW_OPTION_MASK;
    c_info.szip.bits_per_pixel = 0;
    c_info.szip.pixels = 0;
    c_info.szip.pixels_per_scanline = 0;

    /* Set the compression */
    status = SDsetcompress (sds_id, COMP_CODE_SZIP, &c_info);
    CHECK(status, FAIL, "SDsetcompress");

    /* Write data to the SDS */
    status = SDwritedata(sds_id, start, NULL, edges, (VOIDP)in_data);
    CHECK(status, FAIL, "SDwritedata");

    /* Terminate access to the data set */
    status = SDendaccess (sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Terminate access to the SD interface and close the file to 
       flush the compressed info to the file */
    status = SDend (sd_id);
    CHECK(status, FAIL, "SDend");

    /*
     * Verify the compressed data
     */

    /* Reopen the file and select the first SDS */
    sd_id = SDstart (FILE_NAMEfl64, DFACC_READ);
    CHECK(sd_id, FAIL, "SDstart");

    sds_id = SDselect (sd_id, 0);
    CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for szip compression testing");

    /* Retrieve information of the data set */
    status = SDgetinfo(sds_id, name, &array_rank, dim_sizes, &num_type, &attributes);
    CHECK(status, FAIL, "SDgetinfo");

    /* Wipe out the output buffer */
    HDmemset(&out_data, 0, sizeof(out_data));

    /* Read the data set */
    start[0] = 0;
    start[1] = 0;
    edges[0] = LENGTH;
    edges[1] = WIDTH;
    status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)out_data);
    CHECK(status, FAIL, "SDreaddata");

    /* Compare read data against input data */
    for (j=0; j<LENGTH; j++) 
    {
        for (i=0; i<WIDTH; i++)
	    if (out_data[j][i] != in_data[j][i])
	    {
		fprintf(stderr,"Bogus val in loc [%d][%d] in compressed dset, want %ld got %ld\n", j, i, (long)in_data[j][i], (long)out_data[j][i]);
		num_errs++;
	    }
    }

    /* Terminate access to the data set */
    status = SDendaccess (sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Terminate access to the SD interface and close the file */
    status = SDend (sd_id);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}  /* test_szip_SDSfl64bit */
コード例 #10
0
ファイル: tszip.c プロジェクト: Higginbottom/zeus_python
/********************************************************************
   Name: test_getszipdata() - verifies that SZIP compressed data can be read
		when either SZIP library encoder or only decoder is present

   Description:
	This test function opens the existing file "sds_szipped.dat" that 
	contains a dataset with SZIP compression and verifies that the SZIP 
	compressed data can be read with or without the encoder as long as
	the szlib is available.

	The input file, sds_szipped.dat, is generated by the program
	mfhdf/libsrc/gen_sds_szipped.c

   Return value:
        The number of errors occurred in this routine.

   BMR - Oct 10, 2008
*********************************************************************/

#ifdef H4_HAVE_LIBSZ	/* needed to read data, either decoder or encoder */
static intn test_getszipdata()
{
    /************************* Variable declaration **************************/

    int32	sd_id, sds_id;
    intn 	status;
    int32	dim_sizes[2], array_rank, num_type, attributes;
    char	name[H4_MAX_NC_NAME];
    comp_info	c_info;
    int32       start[2], edges[2];
    int16       fill_value = 0;   /* Fill value */
    int         i,j;
    int		num_errs = 0;    /* number of errors so far */
    int32       out_data[SZ_LENGTH][SZ_WIDTH];
    char	testfile[512] = "";
    char       *basename = "sds_szipped.dat";

    /* data to compare against read data from sds_szipped.dat */
    int32	in_data[SZ_LENGTH][SZ_WIDTH]={
                                100,100,200,200,300,
                                0, 0, 0, 0, 0,
                                100,100,200,200,300,
                                400,300,200,100,0,
                                300,300,  0,400,300,
                                300,300,  0,400,300,
                                300,300,  0,400,300,
                                0,  0,600,600,300,
                                500,500,600,600,300,
                                0,  0,600,600,300,
                                0,  0,600,600,300,
                                0,  0,600,600,300,
                                0,  0,600,600,300,
                                500,500,600,600,300,
                                500,500,600,600,300,
                                500,500,600,600,300 };

    /********************* End of variable declaration ***********************/

    /* Make the name for the test file */
    make_datafilename(basename, testfile, sizeof(testfile));

    /* Open the file */
    sd_id = SDstart (testfile, DFACC_READ);
    CHECK(sd_id, FAIL, "SDstart");

    /* Get the first SDS */
    sds_id = SDselect (sd_id, 0);
    CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for szip compression testing");

    /* Retrieve information of the data set */
    status = SDgetinfo(sds_id, name, &array_rank, dim_sizes, &num_type, &attributes);
    CHECK(status, FAIL, "SDgetinfo");

    /* Prepare for reading */
    start[0] = 0;
    start[1] = 0;
    edges[0] = dim_sizes[0];
    edges[1] = dim_sizes[1];

    /* Wipe out the output buffer */
    HDmemset(&out_data, 0, sizeof(out_data));

    /* Read the data set */
    status = SDreaddata (sds_id, start, NULL, edges, (VOIDP)out_data);
    CHECK(status, FAIL, "SDreaddata");

    /* Compare read data against input data */
    for (j=0; j<SZ_LENGTH; j++) 
    {
        for (i=0; i<SZ_WIDTH; i++)
	     if (out_data[j][i] != in_data[j][i])
	    {
		 fprintf(stderr,"This one: Bogus val in loc [%d][%d] in compressed dset, want %ld got %ld\n", j, i, (long)in_data[j][i], (long)out_data[j][i]);
		num_errs++;
	    }
    }

    /* Terminate access to the data set */
    status = SDendaccess (sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Terminate access to the SD interface and close the file */
    status = SDend (sd_id);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}  /* test_getszipdata */
コード例 #11
0
ファイル: tszip.c プロジェクト: Higginbottom/zeus_python
static intn test_getszipinfo()
{
    /************************* Variable declaration **************************/

    int32	sd_id, sds_id, sds_index;
    intn 	status;
    int32	dim_sizes[2], array_rank, num_type, attributes;
    char	name[H4_MAX_NC_NAME];
    comp_info	c_info;
    int32       start[2], edges[2];
    comp_coder_t comp_type;
    int32	comp_size, uncomp_size, orig_size;
    int16       fill_value = 0;   /* Fill value */
    int         i,j;
    int		num_errs = 0;    /* number of errors so far */
    char	testfile[512] = "";
    int32      *out_data = NULL;
    char *basename = "sds_szipped.dat";

    /********************* End of variable declaration ***********************/

    /* Make the name for the test file */
    make_datafilename(basename, testfile, sizeof(testfile));

    /* Open the file and select dataset SDS1_NAME */
    sd_id = SDstart (testfile, DFACC_RDONLY);
    CHECK(sd_id, FAIL, "SDstart");

    sds_index = SDnametoindex(sd_id, SDS1_NAME);
    CHECK(sds_index, FAIL, "SDnametoindex");

    sds_id = SDselect (sd_id, sds_index);
    CHECK(sds_id, FAIL, "SDselect:Failed to select a data set for szip compression testing");

    /* Retrieve and verify information of the data set */
    status = SDgetinfo(sds_id, name, &array_rank, dim_sizes, &num_type, &attributes);
    CHECK(status, FAIL, "SDgetinfo");
    VERIFY(array_rank, RANK, "SDgetinfo");
    VERIFY(dim_sizes[0], SZ_LENGTH, "SDgetinfo");
    VERIFY(dim_sizes[1], SZ_WIDTH, "SDgetinfo");
    VERIFY(num_type, DFNT_INT32, "SDgetinfo");
    VERIFY(attributes, 0, "SDgetinfo");

    /* Get the compression method and verify it */
    comp_type = COMP_CODE_INVALID;  /* reset variables before retrieving info */
    status = SDgetcomptype(sds_id, &comp_type);
    CHECK(status, FAIL, "SDgetcomptype");
    VERIFY(comp_type, COMP_CODE_SZIP, "SDgetcomptype");

    /* Get the compressed data size and non-compressed data size */
    status = SDgetdatasize(sds_id, &comp_size, &uncomp_size);
    CHECK(status, FAIL, "SDgetdatasize");

    /* Compute the uncompressed/original size of the data for comparision */
    orig_size = SZ_WIDTH * SZ_LENGTH * SIZE_INT32;
    VERIFY(uncomp_size, orig_size, "SDgetdatasize");

    /* In this test, compressed data size should be smaller than non-compressed
       data size */
    if (comp_size >= uncomp_size)
    {
        printf("*** Routine test_getszipinfo: FAILED at line %d ***\n", __LINE__);
        printf("    In this test, compressed data size (%d) should be smaller than non-compressed data size (%d)\n", comp_size, uncomp_size);
        num_errs++;
    }

    /* Terminate access to the data set */
    status = SDendaccess (sds_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Terminate access to the SD interface and close the file */
    status = SDend (sd_id);
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}  /* test_getszipinfo */
コード例 #12
0
ファイル: tcoordvar.c プロジェクト: Higginbottom/zeus_python
static intn test_dim1_SDS2(void)
{
    char  sds_name[20];
    float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9};
    float32 sds2_data[2][3] = {{0.1, 2.3, 4.5}, {4.5, 6.7, 8.9}};
    int32 dimsize[1], dimsize2[2];
    int32 sds1_id, sds2_id, file_id, dim_id, index;
    int32 start=0, stride=1, stat;
    int32 start2[2]={0,0}, stride2[2]={1,1};
    int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5];
    int32 num_type, array_rank, attributes;
    int32 n_datasets, n_file_attrs, n_local_attrs;
    float32 out_data2[2][3];
    intn  datanum, ranknum, status =0, i, idx, idx1, idx2;
    intn  num_errs = 0;         /* number of errors so far */

    file_id = SDstart(FILE2, DFACC_CREATE);
    CHECK(file_id, FAIL, "SDstart");

    dimsize[0] = 5;
    dimsize2[0] = 2;
    dimsize2[1] = 3;
    sds1_id = SDcreate(file_id, VAR1_NAME, DFNT_FLOAT32, 1, dimsize);
    CHECK(sds1_id, FAIL, "SDcreate");

    /* Set the dimension name to be the same as the next dataset (not 
       created yet) */
    dim_id = SDgetdimid(sds1_id, 0);
    CHECK(dim_id, FAIL, "SDgetdimid");

    status = SDsetdimname(dim_id, VAR2_NAME);
    CHECK(status, FAIL, "SDsetdimname");

    /* Get file info and verify that there is 1 dataset in the file */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 1, "SDfileinfo");

    /* Create and write data to the second dataset VAR2_NAME */
    sds2_id = SDcreate(file_id, VAR2_NAME, DFNT_FLOAT32, 2, dimsize2);
    CHECK(sds2_id, FAIL, "SDcreate");

    stat = SDwritedata(sds2_id, start2, stride2, dimsize2, sds2_data);
    CHECK(status, FAIL, "SDwritedata");

    status = SDendaccess(sds2_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Get file info and verify that there are 2 datasets in the file. */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 2, "SDfileinfo");

    /* Write values to the dimension VAR2_NAME (same name as VAR2_NAME) */
    status = SDsetdimscale (dim_id, dimsize[0], DFNT_INT32, scale1);
    CHECK(status, FAIL, "SDsetdimscale");

    /* Get file info and verify that there are 3 datasets in the file:
       2 SDS and 1 coordinate variable */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 3, "SDfileinfo");

    /* Close dataset and file */
    status = SDendaccess(sds1_id);
    CHECK(status, FAIL, "SDendaccess");
    status = SDend(file_id); 
    CHECK(status, FAIL, "SDend");

    /* Open the file again to check its data */
    file_id = SDstart(FILE2, DFACC_RDWR);
    CHECK(file_id, FAIL, "SDstart");

    /* Verify dimension scale of the first dimension of SDS VAR1_NAME */

    /* Get access to dataset VAR1_NAME */
    index = SDnametoindex(file_id, VAR1_NAME);
    CHECK(index, FAIL, "SDnametoindex");

    sds1_id = SDselect(file_id, index);
    CHECK(sds1_id, FAIL, "SDselect");

    /* Get access to its first dimension */
    dim_id = SDgetdimid(sds1_id, 0);
    CHECK(dim_id, FAIL, "SDgetdimid");

    /* Get dimension scale and verify the values */
    status = SDgetdimscale (dim_id, scale1_out);
    CHECK(status, FAIL, "SDgetdimscale");

    for (idx = 0; idx < dimsize[0]; idx++)
        if (scale1_out[idx] != scale1[idx])
	{
	    fprintf(stderr, "Read value (%d) differs from written (%d) at [%d]\n", scale1_out[idx], scale1[idx], idx);
	    num_errs++;
	}
    /* End verifying dimension scale */

    /* Verify dimension scale of the first dimension of SDS VAR1_NAME */

    /* Get access to dataset VAR2_NAME */
    index = SDnametoindex(file_id, VAR2_NAME);
    CHECK(index, FAIL, "SDnametoindex");

    sds2_id = SDselect(file_id, index);
    CHECK(sds2_id, FAIL, "SDselect");

    /* Get dataset's info and verify them */
    status = SDgetinfo(sds2_id, sds_name, &array_rank, dimsize2,
                                        &num_type, &n_local_attrs);
    CHECK(status, FAIL, "SDgetinfo");
    VERIFY(array_rank, 2, "SDfileinfo");
    VERIFY(num_type, DFNT_FLOAT32, "SDfileinfo");
    VERIFY(n_local_attrs, 0, "SDfileinfo");

    /* Read and verify the dataset's data */
    status = SDreaddata (sds2_id, start2, NULL, dimsize2, &out_data2);
    CHECK(status, FAIL, "SDreaddata");

    for (idx1 = 0; idx1 < dimsize2[0]; idx1++)
	for (idx2 = 0; idx2 < dimsize2[1]; idx2++)
	{
            if (out_data2[idx1][idx2] != sds2_data[idx1][idx2])
	    {
		fprintf(stderr, "Read value (%f) differs from written (%f) at [%d][%d]\n", out_data2[idx1][idx2], sds2_data[idx1][idx2], idx1, idx2);
		num_errs++;
	    }
	}

    /* Verify again that the number of datasets in the file is 3, 2 SDSs and
       1 coordinate variable */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 3, "SDfileinfo");

    status = SDendaccess(sds1_id);
    CHECK(status, FAIL, "SDendaccess");
    status = SDendaccess(sds2_id);
    CHECK(status, FAIL, "SDendaccess");

    status = SDend(file_id); 
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}   /* test_dim1_SDS2 */
コード例 #13
0
ファイル: main.c プロジェクト: ahroach/hdf4tohdf5
int main(int ac, char **av)
{

  char input_file_path[256], output_file_path[256];
  char hdf4sds_name[256], hdf4sds_attr_name[256];
  char hdf4sds_str_attr_buff[256];
  
  
  int hdf4sd_id, hdf4access_mode, hdf4status, hdf4n_datasets, hdf4n_file_attrs,
    hdf4sds_id, hdf4sds_index, hdf4sds_rank, hdf4sds_data_type,
    hdf4sds_n_attrs, hdf4sds_attr_data_type, hdf4sds_attr_n_values,
    hdf4sds_num_elements;

  int hdf4sds_dim_sizes[8], hdf4sds_read_start[8], hdf4sds_read_stride[8];

  float *hdf4sds_flt_buff;

  hid_t hdf5file_id, hdf5dataset_id, hdf5space_id, hdf5attr_id,
    hdf5attr_space_id, hdf5str_type;
  herr_t hdf5status;
  hsize_t hdf5dataset_dims[8], hdf5attr_size;


  /* Get input file name from the command line, and create output file name */

  if (ac < 2) {
    fprintf(stderr, "Usage is: hdf4tohdf5 inputfile outputfile\n");
    fprintf(stderr, "If no output file is given, inputfile.h5 will be produced.\n");
    return 1;
  }
  strcpy(input_file_path, av[1]);

  if (ac > 2) {
    strcpy(output_file_path, av[2]);
  } else {
    strcpy(output_file_path, input_file_path);
    strcat(output_file_path, ".h5");
  }

  /* Open hdf4 file */

  hdf4access_mode = DFACC_READ;
  hdf4sd_id = SDstart(input_file_path, hdf4access_mode);

  hdf4status = SDfileinfo(hdf4sd_id, &hdf4n_datasets, &hdf4n_file_attrs);
  if (hdf4status == FAIL) {
    fprintf(stderr, "Reading hdf4 file failed.  Returning.\n");
    return 1;
  }

  
#ifdef DEBUG
  printf("Id: %i, Datasets: %i, File_attrs: %i\n", hdf4sd_id, hdf4n_datasets,
	 hdf4n_file_attrs);
#endif



  /* Open hdf5 file */
 
  hdf5file_id = H5Fcreate(output_file_path, H5F_ACC_TRUNC, H5P_DEFAULT, 
			  H5P_DEFAULT);
  
  if (hdf5file_id < 0) {
    fprintf(stderr, "Failed to open HDF5 file %s for writing.\n",
	    output_file_path);
    return 1;
  }


  /* Loop through datasets */

  for(hdf4sds_index = 0; hdf4sds_index < hdf4n_datasets; hdf4sds_index++) {
    hdf4sds_id = SDselect(hdf4sd_id, hdf4sds_index);
    hdf4status = SDgetinfo(hdf4sds_id, hdf4sds_name, &hdf4sds_rank,
			 hdf4sds_dim_sizes, &hdf4sds_data_type,
			 &hdf4sds_n_attrs);
#ifdef DEBUG    
    printf("\nsds_id: %i, sds_name: %s, sds_rank: %i, sds_data_type: %i,\nn_attrs_: %i, dim_sizes: %i %i %i\n", hdf4sds_id, hdf4sds_name, hdf4sds_rank, hdf4sds_data_type, hdf4sds_n_attrs, hdf4sds_dim_sizes[0], hdf4sds_dim_sizes[1], hdf4sds_dim_sizes[2]);
#endif

    /* Get data from SDS into buffer */
    hdf4sds_num_elements = hdf4sds_dim_sizes[0];
    hdf4sds_read_start[0] = 0;
    hdf4sds_read_stride[0] = 1;
    if (hdf4sds_rank > 1){
      for(int i=1; i < hdf4sds_rank; i++) {
	hdf4sds_num_elements = hdf4sds_num_elements*hdf4sds_dim_sizes[i];
	hdf4sds_read_start[i] = 0;
	hdf4sds_read_stride[i] = 1;
      }
    }
#ifdef DEBUG
    printf("Total number of elements for this sds: %i\n",
	   hdf4sds_num_elements);      
#endif    

    /* Allocate the buffer to read in the data */
    hdf4sds_flt_buff = malloc((sizeof (float))*hdf4sds_num_elements);

    hdf4status = SDreaddata(hdf4sds_id, hdf4sds_read_start,
			    hdf4sds_read_stride, hdf4sds_dim_sizes,
			    (VOIDP)hdf4sds_flt_buff);
    

    /* Have all of the data from the hdf4 file,
       now need to load it into hdf5 */

    for (int i=0; i < hdf4sds_rank; i++) {
          hdf5dataset_dims[i] = (hsize_t)hdf4sds_dim_sizes[i];
    }

    hdf5space_id = H5Screate_simple(hdf4sds_rank, hdf5dataset_dims,
				    hdf5dataset_dims);
    if (hdf4sds_data_type == 5) { //Floats
      hdf5dataset_id = H5Dcreate(hdf5file_id, hdf4sds_name, H5T_IEEE_F32LE,
				 hdf5space_id, H5P_DEFAULT);
      
      hdf5status = H5Dwrite(hdf5dataset_id, H5T_IEEE_F32LE, H5S_ALL, H5S_ALL, 
			    H5P_DEFAULT, hdf4sds_flt_buff);

      /* We've finished reading in the data. Free the hdf4 data buffer. */
      free(hdf4sds_flt_buff);

      /*Assign the HDF5 attributes */
      
      for(int i=0; i < hdf4sds_n_attrs; i++) {
	hdf4status = SDattrinfo(hdf4sds_id, i, hdf4sds_attr_name,
			      &hdf4sds_attr_data_type, &hdf4sds_attr_n_values);
#ifdef DEBUG
	printf("Attribute %i: Name = %s, Data type = %i, N_values = %i\n", 
	       i, hdf4sds_attr_name, hdf4sds_attr_data_type,
	       hdf4sds_attr_n_values);
#endif	
	if(hdf4sds_attr_data_type == 4) { /* Read the string attributes */
	  hdf4status = SDreadattr(hdf4sds_id, i, (VOIDP)hdf4sds_str_attr_buff);
	  hdf4sds_str_attr_buff[hdf4sds_attr_n_values]='\0'; 
#ifdef DEBUG
	  printf("Value: %s\n", hdf4sds_str_attr_buff);
#endif
	  hdf5attr_size = (hsize_t)hdf4sds_attr_n_values;
	  
	  hdf5attr_space_id = H5Screate(H5S_SCALAR);
	  hdf5str_type = H5Tcopy(H5T_C_S1);
	  hdf5status = H5Tset_size(hdf5str_type,
				   (size_t)hdf4sds_attr_n_values);
	  hdf5attr_id = H5Acreate(hdf5dataset_id, hdf4sds_attr_name,
				  hdf5str_type, hdf5attr_space_id,
				  H5P_DEFAULT);
	  hdf5status =  H5Awrite(hdf5attr_id, hdf5str_type,
				 hdf4sds_str_attr_buff);
	  hdf5status = H5Aclose(hdf5attr_id);
	}
      }
      hdf5status = H5Dclose(hdf5dataset_id);        
    }
    hdf5status = H5Sclose(hdf5space_id);				
    hdf4status = SDendaccess(hdf4sds_id);
  }



  /* Close access to hdf4 file */

  hdf4status = SDend(hdf4sd_id);



  /* Close access to HDF5 file */
  
  hdf5status = H5Fclose(hdf5file_id);

  return 0;
}
コード例 #14
0
Databox         *ReadSDS(char *filename, int ds_num,
			 double default_value)
{
  Databox         *v;

  int32           dim[MAX_VAR_DIMS];
  int32           edges[3];
  int32           start[3];
  int             i;
  int             z;

  int32           type;

  char name[MAX_NC_NAME];
  
  int32  sd_id;
  int32  sds_id;


  int32 rank, nt, nattrs;
  
  int             nx, ny, nz;

  int             m;

  double         *double_ptr;
  
  sd_id = SDstart(filename, DFACC_RDONLY);
  
  sds_id = SDselect(sd_id, ds_num);
  
  SDgetinfo(sds_id, name, &rank, dim, &type, &nattrs);

  start[0] = start[1] = start[2] = 0;


  /* create the new databox structure */
  if((v = NewDatabox(dim[2], dim[1], dim[0], 0, 0, 0, 0, 0, 0, default_value)) == NULL)
     return((Databox *)NULL);
     

  double_ptr = DataboxCoeffs(v);

  edges[0] = 1;
  edges[1] = DataboxNy(v);
  edges[2] = DataboxNx(v);

  switch (type) {
  case DFNT_FLOAT32 : 
    {
      float32  *convert_ptr, *data;
      
      if( (data = convert_ptr = (float32 *)malloc(dim[1]*dim[2] * 
						  sizeof(float32))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_FLOAT64 : 
    {
      float64  *convert_ptr, *data;
      
      if( (data = convert_ptr = (float64 *)malloc(dim[1]*dim[2] * 
						  sizeof(float64))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_INT8 : 
    {
      int8  *convert_ptr, *data;
      
      if( (data = convert_ptr = (int8 *)malloc(dim[1]*dim[2] * 
						  sizeof(int8))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_UINT8 : 
    {
      uint8  *convert_ptr, *data;
      
      if( (data = convert_ptr = (uint8 *)malloc(dim[1]*dim[2] * 
						  sizeof(uint8))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_INT16 : 
    {
      int16  *convert_ptr, *data;
      
      if( (data = convert_ptr = (int16 *)malloc(dim[1]*dim[2] * 
						  sizeof(int16))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_UINT16 : 
    {
      uint16  *convert_ptr, *data;
      
      if( (data = convert_ptr = (uint16 *)malloc(dim[1]*dim[2] * 
						  sizeof(uint16))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_INT32 : 
    {
      int32  *convert_ptr, *data;
      
      if( (data = convert_ptr = (int32 *)malloc(dim[1]*dim[2] * 
						  sizeof(int32))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  case DFNT_UINT32 : 
    {
      uint32  *convert_ptr, *data;
      
      if( (data = convert_ptr = (uint32 *)malloc(dim[1]*dim[2] * 
						  sizeof(uint32))) == NULL)
	{
	  exit(1);
	}

      for(z=0; z < dim[0]; z++) 
	{
	  start[0] = z;
	  
	  SDreaddata(sds_id, start, NULL, edges, data);
      
	  convert_ptr = data;
	  for(i=dim[1]*dim[2]; i--;)
	    *double_ptr++ = *convert_ptr++;
	}

      free(data);
      break;
    };
  }
  
  SDendaccess(sds_id);

  SDend(sd_id);

  return v;
}
コード例 #15
0
ファイル: addmeta.c プロジェクト: ajoykayak/opengdp
bool TransferAttributes( Param_t *param, int32 old_fid, int32 new_fid,
  int proc_sds )
{
    int j = 0;
    int curr_band, curr_sds;
    int32 old_sds, new_sds;
    int32 sds_index;
    int32 nattr, attr_index;
    int32 data_type, n_values;
    char attr_name[1024], sds_name[1024], *buffer = NULL;
    int32 rank;
    int32 dims[10];

    /* loop through the remaining SDS's and add them only if their output
       resolution is the same as the current SDSs resolution */
    for ( curr_sds = proc_sds; curr_sds < param->num_input_sds; curr_sds++ )
    {
        /* if this SDS is not the same resolution as proc_sds, then do not
           output metadata attributes for the SDS */
        if ( param->output_pixel_size[curr_sds] !=
             param->output_pixel_size[proc_sds] )
            continue;

        /* get the SDS index */
        sds_index = SDnametoindex( old_fid,
            param->input_sds_name_list[curr_sds] );

        /* open old SDS */
        old_sds = SDselect( old_fid, sds_index );

        /* get various SDS info */
        SDgetinfo( old_sds, sds_name, &rank, dims, &data_type, &nattr );

        /* loop through the bands in this SDS, writing the same info
           to each band that was processed */
        for ( curr_band = 0; curr_band < param->input_sds_nbands[curr_sds];
              curr_band++ )
        {
            /* if this band wasn't processed, skip to the next band */
            if (param->input_sds_bands[curr_sds][curr_band] == 0)
                continue;

            /* open new SDS, for this SDS/band combination */
            new_sds = SDselect( new_fid, j++ );

            /* for each SDS attribute */
            for ( attr_index = 0; attr_index < nattr; attr_index++ )
            {
                /* get SDS attribute info */
                SDattrinfo( old_sds, attr_index, attr_name, &data_type,
                    &n_values );

                /* allocate memory */
                switch (data_type)
                {
                    case 6:
                    case 26:
                    case 27:
                        buffer = calloc(n_values, 8);
                        break;

                    case 5:
                    case 24:
                    case 25:
                        buffer = calloc(n_values, 4);
                        break;

                    case 22:
                    case 23:
                        buffer = calloc(n_values, 2);
                        break;

                    default:
                        buffer = calloc(n_values, 1);
                        break;
                }

                /* make sure the buffer was allocated */
                if ( buffer == NULL )
                    LOG_RETURN_ERROR( "unable to allocate space for buffer",
                        "TransferAttributes", false);

                /* get SDS attribute values */
                if ( SDreadattr( old_sds, attr_index, buffer ) == -1 )
                    LOG_RETURN_ERROR("unable to find attribute in old HDF file",
                        "TransferAttributes", false);

                /* write SDS attribute to output file */
                if (SDsetattr( new_sds, attr_name, data_type, n_values,
                    buffer ) == -1 )
                    LOG_RETURN_ERROR( "unable to write attribute to new HDF "
                        "file", "TransferAttributes", false);
            } /* for attr_index */

            /* free memory */
            free(buffer);

            /* close current SDS */
            SDendaccess( new_sds );
        } /* for curr_band */

        /* close current SDS */
        SDendaccess( old_sds );
    } /* for curr_sds */

    return true;
}
コード例 #16
0
ファイル: sds_hdf4.c プロジェクト: biocycle/SimpleSDS
/* Opens an HDF file and reads all its SDS metadata, returning an SDSInfo
 * structure containing this metadata.  Returns NULL on error.
 */
SDSInfo *open_h4_sds(const char *path)
{
    int i, status;

    int sd_id = SDstart(path, DFACC_READ);
    CHECK_HDF_ERROR(path, sd_id);

    // get dataset and global att counts
    int32 n_datasets, n_global_atts;
    status = SDfileinfo(sd_id, &n_datasets, &n_global_atts);
    CHECK_HDF_ERROR(path, status);

    SDSInfo *sds = NEW0(SDSInfo);
    sds->path = xstrdup(path);
    sds->type = SDS_HDF4_FILE;
    sds->id = sd_id;

    // read global attributes
    sds->gatts = read_attributes(path, sd_id, n_global_atts);

    // read variables ('datasets')
    for (i = 0; i < n_datasets; i++) {
        int sds_id = SDselect(sd_id, i);
        CHECK_HDF_ERROR(path, sds_id);

        char buf[_H4_MAX_SDS_NAME + 1];
        memset(buf, 0, sizeof(buf));
        int32 rank, dim_sizes[H4_MAX_VAR_DIMS], type, natts;
        status = SDgetinfo(sds_id, buf, &rank, dim_sizes, &type, &natts);
        CHECK_HDF_ERROR(path, status);

        SDSVarInfo *var = NEW0(SDSVarInfo);
        var->name = xstrdup(buf);
        var->type = h4_to_sdstype(type);
        var->iscoord = SDiscoordvar(sds_id);
        var->ndims = rank;
        var->dims = read_dimensions(sds, sds_id, rank, dim_sizes);
        var->atts = read_attributes(path, sds_id, natts);
        var->id = i; // actually the sds_index

        comp_coder_t comp_type;
        comp_info c_info;
        status = SDgetcompinfo(sds_id, &comp_type, &c_info);
        CHECK_HDF_ERROR(path, status);
        switch (comp_type) {
        case COMP_CODE_NONE:
            var->compress = 0;
            break;
        case COMP_CODE_DEFLATE:
            var->compress = c_info.deflate.level;
            break;
        default:
            // any other compression method is 'worth' 1 imo *trollface*
            // better than claiming 0 to the user
            var->compress = 1;
            break;
        }

        var->sds = sds;

        var->next = sds->vars;
        sds->vars = var;

        status = SDendaccess(sds_id);
        CHECK_HDF_ERROR(path, status);
    }
    sds->vars = (SDSVarInfo *)list_reverse((List *)sds->vars);
    sds->dims = (SDSDimInfo *)list_reverse((List *)sds->dims);

    sds->funcs = &h4_funcs;
    return sds;
}
コード例 #17
0
ファイル: cdunifhdf.c プロジェクト: MartinDix/cdat_lite_test
/* Get dimension coordinates values. Return success (0) if the
 * dimension values were obtained sucessfully, otherwise this
 * function returns the failure status (-1).
 */
int cudimget_hdf(CuFile* file, int dimidx, void* values){
	char dimname[H4_MAX_NC_NAME+1];
	float *fp;
	int cdfid;
	int get_dimid;
	int dimvarid;
	int found;
	int ndims;
	int saveopts;
	long i;
	long length;
	long start;
	char varname[H4_MAX_NC_NAME+1];
	hdf_type hdftype;
	int natts;
	int32 dimid;

        int32 sds_id, datatype, nattrs, attr_index, num_type, count;
        int32 dim_sizes[H4_MAX_VAR_DIMS];
	char attr_name[H4_MAX_NC_NAME];

	cdfid = file->internid1;
	if((dimid = cudimid2hdf(file, dimidx))==-1)
		return -1;

	/* Get information about the selected dimension. */
        if(SDdiminfo(dimid, attr_name, &length, &datatype, &nattrs)==-1){
		return -1;
	}

  	/* Inquire a variable with */
     	/* - the same name as dimname, */
     	/* - a single dimension, and */
     	/* - a (single) dimension id which equals dimid */
        if((dimvarid = SDnametoindex(cdfid, attr_name)) != -1){
                sds_id = SDselect(cdfid, dimvarid);
                if (SDgetinfo(sds_id, varname, &ndims, dim_sizes,
                                   &hdftype, &natts) == -1){
           	        cuerrorreport_hdf();
			return -1;
		}

                /* pass back the dimension id */
                if ((get_dimid = SDgetdimid(sds_id, 0)) == -1){
           	        cuerrorreport_hdf();
                        return -1;
                }

		found = (ndims == 1 && get_dimid == dimid);
	}
	else
		found = 0;

        /* If the dimension variable was found, read it */
	if(found){
		start = 0;
		if(values && SDgetdimscale(dimid, (VOIDP) values)==-1)
			return -1;
	}
	else{
		/* Otherwise assign the default dimension */
		if(values){
			for(i=0, fp=(float*)values; i<length; i++){
				*fp++ = (float)i;
			}
		}
	}

	/* Return success ( 0 ). */
	return CU_SUCCESS;
}
コード例 #18
0
ファイル: cviirs.c プロジェクト: davidh-ssec/polar2grid
int main(int argc, char *argv[])
{
	char *MOD021KMfile, *MOD02HKMfile, *MOD02QKMfile;
	char *filename;	/* output file */

	FILE *fp;
	int outfile_exists;

	char *ancpath;
	SDS sds[Nitems], outsds[Nbands], dem, height;
	int32 MOD02QKMfile_id, MOD02HKMfile_id, MOD021KMfile_id;
	int32 sd_id, attr_index, count, num_type;

	int ib, j, iscan, Nscans, irow, jcol, idx, crsidx;
	int nbands;

	char *SDSlocatorQKM[Nitems] = {"EV_250_RefSB", "EV_250_RefSB",
		"EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB",
		"EV_500_RefSB", "EV_500_RefSB","EV_1KM_RefSB", "EV_1KM_RefSB",
		"EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB",
		"EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "SolarZenith",
		"SensorZenith", "SolarAzimuth", "SensorAzimuth", "Longitude",
		"Latitude"};

	char *SDSlocatorHKM[Nitems] = {"EV_500_RefSB",
		"EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB",
		"EV_500_RefSB", "EV_500_RefSB", "EV_500_RefSB",
		"Reflectance_Img_I1","Reflectance_Img_I2","Reflectance_Img_I3",
		"EV_1KM_RefSB","EV_1KM_RefSB","EV_1KM_RefSB", "EV_1KM_RefSB",
		"EV_1KM_RefSB", "EV_1KM_RefSB","SolZenAng_Mod",
		"SenZenAng_Mod", "SolAziAng_Mod", "SenAziAng_Mod", "Longitude",
		"Latitude" };

	char *SDSlocator1KM[Nitems] = {"Reflectance_Mod_M5",
		"Reflectance_Mod_M7", "Reflectance_Mod_M3",
		"Reflectance_Mod_M4", "Reflectance_Mod_M8",
		"Reflectance_Mod_M10",  "Reflectance_Mod_M11",
		"EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB",
		"EV_1KM_RefSB", "EV_1KM_RefSB",
		"EV_1KM_RefSB", "EV_1KM_RefSB", "EV_1KM_RefSB", "SolZenAng_Mod",
		"SenZenAng_Mod", "SolAziAng_Mod", "SenAziAng_Mod", "Longitude",
		"Latitude"};

	char indexlocator[Nitems] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 7,
		9, 10, 0, 0, 0, 0, 0, 0};

	char numtypelocator[Nitems] = {DFNT_UINT16, DFNT_UINT16, DFNT_UINT16,
		DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16,
		DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16,
		DFNT_UINT16, DFNT_UINT16, DFNT_UINT16, DFNT_UINT16,
		DFNT_UINT16,DFNT_FLOAT32 ,DFNT_FLOAT32 ,DFNT_FLOAT32 ,DFNT_FLOAT32 ,
		DFNT_FLOAT32, DFNT_FLOAT32};

	uint16 *l1bdata[Nbands];
	float32 *sola, *solz, *sena, *senz, *solzfill;
	float32 *lon, *lat, *lonfill, *latfill;
	char *attr_name;
	float64 scale_factor[Nitems], add_offset[Nitems];

	unsigned char process[Nbands];

	float refl, *mus, muv, phi;
	float *rhoray, *sphalb, *TtotraytH2O, *tOG;
	int aggfactor, crsrow1, crsrow2, crscol1, crscol2;
	int crsidx11, crsidx12, crsidx21, crsidx22;
	float mus0, mus11, mus12, mus21, mus22;
	float fractrow, fractcol, t, u;
	float rhoray0, rhoray11, rhoray12, rhoray21, rhoray22;
	float sphalb0, sphalb11, sphalb12, sphalb21, sphalb22;
	float reflmin=REFLMIN, reflmax=REFLMAX, maxsolz=MAXSOLZ;
	int bad;

	int write_mode = DFACC_CREATE;

	int st;

	size_t nbytes;

	int ftype;

	extern char *optarg;
	extern int optind, opterr;
	int option_index = 0;

	static int verbose, overwrite;
	static int gzip, append;

	static int output500m, output1km;
	static int sealevel, TOA, nearest;

	char dummy[H4_MAX_NC_NAME];

	enum{OPT_BANDS = 1, OPT_RANGE, OPT_OUTFILE, OPT_MAXSOLZ};

	static struct option long_options[] = {
		{"1km",		no_argument,		&output1km, 1},
		{"500m",	no_argument,		&output500m, 1},
		{"append",	no_argument,		&append, 1},
		{"bands",	required_argument,	(int *) NULL, OPT_BANDS},
		{"gzip",	no_argument,		&gzip,	1},
		{"maxsolz",	required_argument,	(int *) NULL, OPT_MAXSOLZ},
		{"nearest",	no_argument,		&nearest, 1},
		{"of",		required_argument,	(int *) NULL, OPT_OUTFILE},
		{"overwrite",	no_argument,		&overwrite, 1},
		{"range",	required_argument,	(int *) NULL, OPT_RANGE},
		{"sealevel",	no_argument,		&sealevel, 1},
		{"toa",		no_argument,		&TOA, 1},
		{"verbose",	no_argument,		&verbose, 1},
		{(char *) NULL, 0, (int *) NULL, 0}
		};

	int c;

	static char dem_filename_buff[MAXNAMELENGTH];


	MOD021KMfile = MOD02HKMfile = MOD02QKMfile = (char *) NULL;
	filename = (char *) NULL;

	for (ib = 0; ib < Nbands; ib++) process[ib] = FALSE;

	/* default settings */
	output500m = output1km = 0;
	append = gzip = nearest = sealevel = TOA = verbose = overwrite = 0;


	while ((c = getopt_long(argc, argv, "", long_options,
		&option_index)) >= 0) {

		switch (c) {
			case 0:
				/* do nothing for options which will have a
				flag set automatically by getopt_long() */
				break;

			case OPT_BANDS:
				if (parse_bands(optarg, process)) {
					fputs("Invalid band(s) specified.\n",
						stderr);
					exit(1);
					}
				break;

			case OPT_RANGE:
				if (sscanf(optarg, "%g,%g", &reflmin, &reflmax) != 2) {
					fputs("Error parsing reflectance range.\n", stderr);
					exit(1);
					}

				if ( range_check(reflmin, 0.0F, 1.0F) ||
					range_check(reflmax, 0.0F, 1.0F) ||
					(reflmin >= reflmax) ) {
					fputs("Invalid reflectance range.\n", stderr);
					exit(1);
					}

				printf("Output reflectance range [%.3f,%.3f] requested.\n",
					reflmin, reflmax);
				break;

			case OPT_MAXSOLZ:
				maxsolz = (float) atof(optarg);
				if (range_check(maxsolz, 0.0F, 90.0F)) {
					fputs("Invalid max. solar zenith angle.\n", stderr);
					exit(1);
					}
				break;

			case OPT_OUTFILE:
				filename = optarg;
				break;

			default:
				usage();
				exit(1);
			}
		}

	if (append) write_mode = DFACC_RDWR;

	/* at least one input file must follow */
	if (optind >= argc) {
		usage();
		exit(1);
		}


	/* check for conflicting options */
	if (overwrite && append) {
		fputs("Options --overwrite and --append are mutually exclusive.\n",
			stderr);
		exit(1);
		}
	if (sealevel && TOA) {
		fputs("Options --sealevel and --toa are mutually exclusive.\n",
			stderr);
		exit(1);
		}

#ifdef DEBUG
printf("append = %d\n", append);
if (filename) printf("output filename = %s\n", filename);
printf("output1km = %d\n", (int) output1km);
printf("output500m = %d\n", (int) output500m);
printf("gzip = %d\n", gzip);
printf("nearest = %d\n", nearest);
printf("sealevel = %d\n", sealevel);
printf("TOA = %d\n", TOA);
printf("Max. solar zenith angle: %g degrees\n", maxsolz);
if (filename) printf("Output file: %s.", filename);
#endif



	if (verbose) puts("Verbose mode requested.");
	if (overwrite) puts("Overwriting existing output file.");
	if (gzip) puts("Gzip compression requested.");
	if (sealevel) puts("Sea-level atmospheric correction requested. Terrain height ignored.");
	if (TOA) puts("Top-of-the-atmosphere reflectance requested. No atmospheric correction.");
	if (output1km) puts("1km-resolution output requested.");
	if (nearest) puts("Interpolation disabled.");



	/* parse input file names */
	for (j = optind; j < argc; j++) {
		ftype = input_file_type(argv[j]);

		switch (ftype) {
			case INPUT_1KM:
				MOD021KMfile = argv[j];
				break;

			case INPUT_500M:
				MOD02HKMfile = argv[j];
				break;

			case INPUT_250M:
				MOD02QKMfile = argv[j];
				break;

			default:
				fprintf(stderr,
					"Unrecognized input file \"%s\".\n",
					argv[j]);
					MOD021KMfile = argv[j];
/*				exit(1); I commented that*/
				break;
			}
		}



	if (verbose && MOD021KMfile)
		printf("Input geolocation file: %s\n", MOD021KMfile);


	/* output file name is mandatory */
	if (!filename) {
		fputs("Missing output file name.\n", stderr);
		exit(1);
		}

#ifdef DEBUG
if (MOD021KMfile) printf("MOD/MYD021KMfile = %s\n", MOD021KMfile);
if (MOD02HKMfile) printf("MOD/MYD02HKMfile = %s\n", MOD02HKMfile);
if (MOD02QKMfile) printf("MOD/MYD02QKMfile = %s\n", MOD02QKMfile);
#endif


	/*
	1KM file is mandatory for angles.
	HKM file is mandatory unless 1-km output is requested.
	QKM file is mandatory unless 500-m or 1-km output is requested.
	*/
/*	if ( (!MOD021KMfile) ||
		(!MOD02HKMfile && !output1km) ||
		(!MOD02QKMfile && !output500m && !output1km) ) {
		fputs("Invalid combination of input files.\n", stderr);
		exit(1);
		}
commented that too Eric*/

	/* count number of bands to process */
	for (ib = nbands = 0; ib < Nbands; ib++) if (process[ib]) nbands++;
	if (nbands < 1) {
		process[BAND1] = process[BAND3] = process[BAND4] = TRUE;
		if (verbose)
			puts("No band(s) specified.  Default is bands 1, 3, and 4.");
		}


	/* open input files */
  if ( MOD02QKMfile && (!output500m)  &&
       !output1km &&
       (MOD02QKMfile_id = SDstart(MOD02QKMfile, DFACC_READ)) == -1 ) {
    fprintf(stderr, "Cannot open input file %s.\n", MOD02QKMfile);
    exit(1);
  }
  if ( MOD02HKMfile && (!output1km) &&
       (MOD02HKMfile_id = SDstart(MOD02HKMfile, DFACC_READ)) == -1 ) {
    fprintf(stderr, "Cannot open input file %s.\n", MOD02HKMfile);
    exit(1);
  }
  if ( MOD021KMfile &&
       (MOD021KMfile_id = SDstart(MOD021KMfile, DFACC_READ)) == -1 ) {
    fprintf(stderr, "Cannot open input file %s.\n", MOD021KMfile);
    exit(1);
  }


	if (!sealevel && !TOA) {
		dem.filename = dem_filename_buff;

		if ((ancpath = getenv("ANCPATH")) == NULL)
			sprintf(dem.filename, "%s/%s", ANCPATH, DEMFILENAME);
		else
			sprintf(dem.filename, "%s/%s", ancpath, DEMFILENAME);

		if ( (dem.file_id = SDstart(dem.filename, DFACC_READ)) == -1 ) {
			fprintf(stderr, "Cannot open file %s.\n", dem.filename);
			exit(1);
			}
		}


	if ( (fp = fopen(filename, "r")) ) {
		(void) fclose(fp);
		outfile_exists = 1;
		}
	else
		outfile_exists = 0;

	if ((write_mode == DFACC_CREATE)  &&  !overwrite  && outfile_exists) {
		fprintf(stderr, "File \"%s\" already exits.\n", filename);
		exit(1);
		}

	if (output500m) {
		sds[BAND10].file_id =sds[BAND8].file_id = sds[BAND9].file_id = MOD02HKMfile_id;
		sds[BAND10].filename =sds[BAND8].filename = sds[BAND9].filename = MOD02HKMfile;
		}
	else {
		if (output1km) {
			sds[BAND1].file_id = sds[BAND2].file_id = MOD021KMfile_id;
			sds[BAND1].filename = sds[BAND2].filename = MOD021KMfile;
			}
		else {
			sds[BAND1].file_id = sds[BAND2].file_id = MOD02QKMfile_id;
			sds[BAND1].filename = sds[BAND2].filename = MOD02QKMfile;
			}
		}

	if (output1km) {
		sds[BAND3].file_id = sds[BAND4].file_id =
			sds[BAND5].file_id = sds[BAND6].file_id =
			sds[BAND7].file_id = MOD021KMfile_id;
		sds[BAND3].filename = sds[BAND4].filename =
			sds[BAND5].filename = sds[BAND6].filename =
			sds[BAND7].filename = MOD021KMfile;
		}
	else {
		sds[BAND3].file_id = sds[BAND4].file_id =
			sds[BAND5].file_id = sds[BAND6].file_id =
			sds[BAND7].file_id = MOD02HKMfile_id;
		sds[BAND3].filename = sds[BAND4].filename =
			sds[BAND5].filename = sds[BAND6].filename =
			sds[BAND7].filename = MOD02HKMfile;
		}

	 sds[SOLZ].file_id = sds[SOLA].file_id =
		sds[SENZ].file_id = sds[SENA].file_id = sds[LON].file_id =
		sds[LAT].file_id = MOD021KMfile_id;
	 sds[SOLZ].filename = sds[SOLA].filename =
		sds[SENZ].filename = sds[SENA].filename = sds[LON].filename =
		sds[LAT].filename = MOD021KMfile;

	 sds[BAND11].file_id =
		sds[BAND12].file_id = sds[BAND13].file_id =
		sds[BAND14].file_id = sds[BAND15].file_id =
		sds[BAND16].file_id = MOD021KMfile_id;
	  sds[BAND11].filename =
		sds[BAND12].filename = sds[BAND13].filename =
		sds[BAND14].filename = sds[BAND15].filename =
		sds[BAND16].filename = MOD021KMfile;

  for (ib=0; ib < Nitems; ib++) {
	/* initializing these fields will simplify releasing memory later */
	sds[ib].data = sds[ib].fillvalue = (void *) NULL;

    if ( ib < Nbands  &&
         ! process[ib] ) {
      sds[ib].id = -1;
      continue;
    }
    if (output500m)
      sds[ib].name = SDSlocatorHKM[ib];
    else if (output1km)
      sds[ib].name = SDSlocator1KM[ib];
    else
      sds[ib].name = SDSlocatorQKM[ib];

    if ( (sds[ib].index = SDnametoindex(sds[ib].file_id, sds[ib].name)) == -1 ) {
      fprintf(stderr, "Cannot find SDS %s in file %s.\n", sds[ib].name, sds[ib].filename);
      continue;
    }
    if ( (sds[ib].id = SDselect(sds[ib].file_id, sds[ib].index)) == -1 ) {
      fprintf(stderr, "Cannot select SDS no. %d\n", sds[ib].index);
      if (ib < Nbands)
        process[ib] = FALSE;
      continue;
    }

    /*
    Original code passed sds[ib].name as destination for SDS name in call to SDgetinfo().
    This was causing a core dump, apparently because SDgetinfo() writes some additional
    characters beyond the terminating null at the end of the SDS name, so I replaced
    the argument with a dummy character array.
    */
    if (SDgetinfo(sds[ib].id, dummy, &sds[ib].rank, sds[ib].dim_sizes, &sds[ib].num_type, &sds[ib].n_attr) == -1) {
      fprintf(stderr, "Can't get info from SDS \"%s\" in file %s.\n", sds[ib].name, sds[ib].filename);
      SDendaccess(sds[ib].id);
      sds[ib].id = -1;
      if (ib < Nbands)
        process[ib] = FALSE;
      continue;
    }


    sds[ib].factor = 1;
    if (ib < 5 ) sds[ib].factor = 2.441742e-05;
    attr_name = "scale_factor";
    printf("band %d \n",ib);
    if ( (attr_index = SDfindattr(sds[ib].id, attr_name)) != -1  &&
         SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1  &&
         SDreadattr(sds[ib].id, attr_index, scale_factor) != -1 ) 
      sds[ib].factor = ((float32 *)scale_factor)[indexlocator[ib]];
    else {
	attr_name = "Scale";
	if ((attr_index = SDfindattr(sds[ib].id, attr_name)) != -1  &&
		SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1  &&
		SDreadattr(sds[ib].id, attr_index, scale_factor) != -1 )
		sds[ib].factor = *scale_factor;
	}

    sds[ib].offset = 0;
    attr_name = "reflectance_offsets";
    if ( (attr_index = SDfindattr(sds[ib].id, attr_name)) != -1  &&
         SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1  &&
         SDreadattr(sds[ib].id, attr_index, add_offset) != -1 )
      sds[ib].offset = ((float32 *)add_offset)[indexlocator[ib]];
    else {
	attr_name = "add_offset";
	if ( (attr_index = SDfindattr(sds[ib].id, attr_name)) != -1  &&
		SDattrinfo(sds[ib].id, attr_index, dummy, &num_type, &count) != -1  &&
		SDreadattr(sds[ib].id, attr_index, add_offset) != -1 )
		sds[ib].offset = *add_offset;
	}


    sds[ib].fillvalue = (void *) malloc(1 * DFKNTsize(sds[ib].num_type));
    if ( SDgetfillvalue(sds[ib].id, sds[ib].fillvalue) != 0 ) {
      fprintf(stderr, "Cannot read fill value of SDS \"%s\".\n", sds[ib].name);
/*      exit(1); commmented that*/
    }

    switch (sds[ib].rank) {
      case 2:
        sds[ib].Nl = sds[ib].dim_sizes[0];
        sds[ib].Np = sds[ib].dim_sizes[1];
        sds[ib].rowsperscan = (int)(NUM1KMROWPERSCAN * sds[ib].Np / (float)NUM1KMCOLPERSCAN + 0.5);
        sds[ib].start[1] = 0;
        sds[ib].edges[0] = sds[ib].rowsperscan;
        sds[ib].edges[1] = sds[ib].Np;
        break;
      case 3:
        sds[ib].Nl = sds[ib].dim_sizes[1];
        sds[ib].Np = sds[ib].dim_sizes[2];
        sds[ib].rowsperscan = (int)(NUM1KMROWPERSCAN * sds[ib].Np / (float)NUM1KMCOLPERSCAN + 0.5);
        sds[ib].start[0] = indexlocator[ib];
        sds[ib].start[2] = 0;
        sds[ib].edges[0] = 1;
        sds[ib].edges[1] = sds[ib].rowsperscan;
        sds[ib].edges[2] = sds[ib].Np;
        break;
      default:
        fprintf(stderr, "SDS rank must be 2 or 3.\n");
        continue;
    }
    if (verbose)
      printf("SDS \"%s\": %dx%d   scale factor: %g  offset: %g\n", sds[ib].name, sds[ib].Np, sds[ib].Nl, sds[ib].factor, sds[ib].offset);
    if (sds[ib].num_type != numtypelocator[ib]) {
      fprintf(stderr, "SDS \"%s\" has not the expected data type.\n", sds[ib].name);
      exit(-1);
    }
    sds[ib].data = malloc(sds[ib].Np * sds[ib].rowsperscan * DFKNTsize(sds[ib].num_type));
    if (!sds[ib].data) {
      (void) fputs("Error allocating memory.\n", stderr);
      exit(1);
      }
  }

	if (sealevel || TOA) {
		dem.id = -1;
		dem.Nl = dem.Np = 0;
		}
	else {
		/* dem.name = strdup(DEMSDSNAME); */
		dem.name = DEMSDSNAME;

		if ( (dem.index = SDnametoindex(dem.file_id, dem.name)) == -1 ) {
			fprintf(stderr, "Cannot find SDS %s in file %s.\n", dem.name, dem.filename);
			exit(1);
			}

		if ( (dem.id = SDselect(dem.file_id, dem.index)) == -1 ) {
			fprintf(stderr, "Cannot select SDS no. %d\n", dem.index);
			exit(1);
			}
		if (SDgetinfo(dem.id, dummy, &dem.rank, dem.dim_sizes, &dem.num_type, &dem.n_attr) == -1) {
			fprintf(stderr, "Can't get info from SDS \"%s\" in file %s.\n", dem.name, dem.filename);
			SDendaccess(dem.id);
			exit(1);
			}

		dem.Nl = dem.dim_sizes[0];
		dem.Np = dem.dim_sizes[1];
		dem.rowsperscan = (int)(NUM1KMROWPERSCAN * dem.Np / (float)NUM1KMCOLPERSCAN + 0.5);
		}

  if ( sds[SOLZ].id == -1 ||
       sds[SOLA].id == -1 ||
       sds[SENZ].id == -1 ||
       sds[SENA].id == -1 ||
       sds[LON].id == -1 ||
       sds[LAT].id == -1 ||
       ((dem.id == -1) && !sealevel && !TOA) ) {
    fprintf(stderr, "Solar and Sensor angles and DEM are necessary to process granule.\n");
    exit(1);
  }

  if ( sds[REFSDS].Np != sds[SOLZ].Np ||
       sds[REFSDS].Np != sds[SOLA].Np ||
       sds[REFSDS].Np != sds[SENZ].Np ||
       sds[REFSDS].Np != sds[SENA].Np ||
       sds[REFSDS].Np != sds[LON].Np ||
       sds[REFSDS].Np != sds[LAT].Np ) {
    fprintf(stderr, "Solar and Sensor angles must have identical dimensions.\n");
    exit(1);
  }

	ib = 0;
	while (sds[ib].id == -1) ib++;
	if (ib >= Nbands) {
		fprintf(stderr, "No L1B SDS can be read successfully.\n");
		exit(1);
		}
 
	Nscans = sds[ib].Nl / sds[ib].rowsperscan;


	/* finally, open output file */
	if ( (sd_id = SDstart(filename, write_mode)) == -1 ) {
		fprintf(stderr, "Cannot open output file %s.\n", filename);
		exit(1);
		}

	if (!append) {
		if (write_global_attributes(sd_id, MOD021KMfile, MOD02HKMfile,
			MOD02QKMfile, maxsolz, sealevel, TOA, nearest)) {
			fputs("Error writing global attributes.\n", stderr);
			exit(1);
			}
		}

	/* create output SDSs and set SDS-specific attributes and dimension names */
	if (init_output_sds(sd_id, process, outsds, sds, gzip, verbose)) exit(1);


	mus = (float *) malloc(sds[REFSDS].rowsperscan * sds[REFSDS].Np * sizeof(float));
	height.data = (int16 *) malloc(sds[REFSDS].rowsperscan * sds[REFSDS].Np * sizeof(int16));
	if (!mus || !height.data) {
		(void) fputs("Error allocating memory.\n", stderr);
		exit(1);
		}

	if (sealevel || TOA)
		dem.data = (void *) NULL;
	else {
		dem.data = (int16 *) malloc(dem.Nl * dem.Np * sizeof(int16));
		if (!dem.data) {
			(void) fputs("Error allocating memory.\n", stderr);
			exit(1);
			}
		}

	if (!TOA) {
		nbytes = Nbands * sds[REFSDS].rowsperscan * sds[REFSDS].Np * sizeof(float);

		rhoray =      (float *) malloc(nbytes);
		sphalb =      (float *) malloc(nbytes);
		TtotraytH2O = (float *) malloc(nbytes);
		tOG =         (float *) malloc(nbytes);

		if (!rhoray || !sphalb || !TtotraytH2O || !tOG) {
			(void) fputs("Error allocating memory.\n", stderr);
			exit(1);
			}
		}

	solz = sds[SOLZ].data;
	sola = sds[SOLA].data;
	senz = sds[SENZ].data;
	sena = sds[SENA].data;
	solzfill = sds[SOLZ].fillvalue;
	lon = sds[LON].data;
	lat = sds[LAT].data;
	lonfill = sds[LON].fillvalue;
	latfill = sds[LAT].fillvalue;
	for (ib = 0; ib < Nbands; ib++) l1bdata[ib] = sds[ib].data;

	/* don't need DEM if --sealevel or --toa specified */
	if (!sealevel && !TOA) {
		dem.start[0] = 0;
		dem.start[1] = 0;
		dem.edges[0] = dem.Nl;
		dem.edges[1] = dem.Np;
		if (SDreaddata(dem.id, dem.start, NULL, dem.edges, dem.data) == -1) {
			fprintf(stderr, "  Can't read DEM SDS \"%s\"\n", dem.name);
			exit(-1);
			}
		(void) SDendaccess(dem.id);
		(void) SDend(dem.file_id);
		}

	/* loop over each MODIS scan */
	for (iscan = 0; iscan < Nscans; iscan++) {
		if ((iscan % NUM1KMROWPERSCAN == 0) && verbose)
			printf("Processing scan %d...\n", iscan);

		/* Fill scan buffer for each band to be processed.
		Exit scan loop if error occurred while reading. */
		if (read_scan(iscan, sds)) break;

		for (idx = 0; idx < sds[REFSDS].rowsperscan*sds[REFSDS].Np; idx++) {
			if (solz[idx] * sds[SOLZ].factor >= maxsolz)
				solz[idx] = *solzfill;

			if (!sealevel &&
				(lon[idx] == *lonfill || lat[idx] == *latfill))
				solz[idx] = *solzfill;

			if (solz[idx] != *solzfill) {
				mus[idx] = cos(solz[idx] * sds[SOLZ].factor * DEG2RAD);

				if (sealevel || TOA)
					((int16 *)height.data)[idx] = 0;
				else
					((int16 *)height.data)[idx] =
						(int16) interp_dem(lat[idx],
						lon[idx], &dem);
				}
			}


		if (!TOA) {
			for (irow=0; irow<sds[REFSDS].rowsperscan; irow++) {
				for (jcol=0; jcol<sds[REFSDS].Np; jcol++) {
					idx = irow * sds[REFSDS].Np + jcol;
					if (solz[idx] == *solzfill) continue;
					phi = sola[idx] * sds[SOLA].factor - sena[idx] * sds[SENA].factor;
					muv = cos(senz[idx] * sds[SENZ].factor * DEG2RAD);
					if ( getatmvariables(mus[idx], muv, phi, ((int16 *)height.data)[idx],
						process,
						&sphalb[idx * Nbands], &rhoray[idx * Nbands],
						&TtotraytH2O[idx * Nbands], &tOG[idx * Nbands]) == -1 )
						solz[idx] = *solzfill;
/*						printf(" some data %f %f %f %f %f \n",senz[idx],phi,mus[idx],rhoray[idx * Nbands],tOG[idx * Nbands]);*/
					}
				}
			}

		for (ib=0; ib<Nbands; ib++) {
			if (! process[ib]) continue;
			aggfactor = outsds[ib].rowsperscan / sds[REFSDS].rowsperscan;
			for (irow=0; irow<outsds[ib].rowsperscan; irow++) {
				if (!nearest) {
					fractrow = (float)irow / aggfactor - 0.5;	/* We want fractrow integer on coarse pixel center */
					crsrow1 = floor(fractrow);
					crsrow2 = crsrow1 + 1;
					if (crsrow1 < 0) crsrow1 = crsrow2 + 1;
					if (crsrow2 > sds[REFSDS].rowsperscan - 1) crsrow2 = crsrow1 - 1;
					t = (fractrow - crsrow1) / (crsrow2 - crsrow1);
					}

				for (jcol=0; jcol<outsds[ib].Np; jcol++) {
					idx = irow * outsds[ib].Np + jcol;
					crsidx = (int)(irow / aggfactor) * sds[REFSDS].Np + (int)(jcol / aggfactor);
					if ( solz[crsidx] == *solzfill  ||	/* Bad geolocation or night pixel */
						l1bdata[ib][idx] >= 65528 ) {	/* VIIRS SDR is read as uint16, fills start at 65528 */
						if (l1bdata[ib][idx] == (65536 + MISSING))
							((int16 *)outsds[ib].data)[idx] = 32768 + MISSING;
						else
							((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue;

						continue;
						}

					if (nearest) {
						mus0 = mus[crsidx];
						if (! TOA) {
							rhoray0 = rhoray[crsidx * Nbands + ib];
							sphalb0 = sphalb[crsidx * Nbands + ib];
							if ( sphalb0 <= 0.0F ) {	/* Atm variables not computed successfully in this band */
								((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue;
								continue;
								}
							}
						}
					else {
						fractcol = ((float) jcol) / aggfactor - 0.5F;	/* We want fractcol integer on coarse pixel center */
						crscol1 = (int) floor(fractcol);
						crscol2 = crscol1 + 1;
						if (crscol1 < 0) crscol1 = crscol2 + 1;
						if (crscol2 > sds[REFSDS].Np - 1) crscol2 = crscol1 - 1;
						u = (fractcol - crscol1) / (crscol2 - crscol1);		/* We want u=0 on coarse pixel center */
						crsidx11 = crsrow1 * sds[REFSDS].Np + crscol1;
						crsidx12 = crsrow1 * sds[REFSDS].Np + crscol2;
						crsidx21 = crsrow2 * sds[REFSDS].Np + crscol1;
						crsidx22 = crsrow2 * sds[REFSDS].Np + crscol2;
/*						mus0 = t * u * mus[crsidx22] + (1.0F - t) * u * mus[crsidx12] + t * (1.0F - u) * mus[crsidx21] + (1.0F - t) * (1.0F - u) * mus[crsidx11];

						bad = (solz[crsidx11] == *solzfill) ||
							(solz[crsidx12] == *solzfill) ||
							(solz[crsidx21] == *solzfill) ||
							(solz[crsidx22] == *solzfill);
commented by eric to handle the viirs fill value hardcoding */ 

						bad = (solz[crsidx11] <-900.) ||
							(solz[crsidx12] <-900.) ||
							(solz[crsidx21] <-900.) ||
							(solz[crsidx22] <-900.);
 
						if (bad) {
							((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue;
							continue;
							}

						if (! TOA) {
							rhoray11 = rhoray[crsidx11 * Nbands + ib];
							rhoray12 = rhoray[crsidx12 * Nbands + ib];
							rhoray21 = rhoray[crsidx21 * Nbands + ib];
							rhoray22 = rhoray[crsidx22 * Nbands + ib];
							rhoray0 = t * u * rhoray22 + (1.0F - t) * u * rhoray12 + t * (1.0F - u) * rhoray21 + (1.0F - t) * (1.0F - u) * rhoray11;
							sphalb11 = sphalb[crsidx11 * Nbands + ib];
							sphalb12 = sphalb[crsidx12 * Nbands + ib];
							sphalb21 = sphalb[crsidx21 * Nbands + ib];
							sphalb22 = sphalb[crsidx22 * Nbands + ib];

							bad = (sphalb11 <= 0.0F) ||
								(sphalb12 <= 0.0F) ||
								(sphalb21 <= 0.0F) ||
								(sphalb22 <= 0.0F);

							if (bad) {
								((int16 *)outsds[ib].data)[idx] = *(int16 *)outsds[ib].fillvalue;
								continue;
								}
							sphalb0 = t * u * sphalb22 + (1.0F - t) * u * sphalb12 + t * (1.0F - u) * sphalb21 + (1.0F - t) * (1.0F - u) * sphalb11;
							}
						}

					/* TOA reflectance */
					/*printf(" mus0 is %f\n",mus0);*/
					refl = (l1bdata[ib][idx] - sds[ib].offset) * sds[ib].factor /*/ mus0 commented by Eric who suspected something*/;

					/* corrected reflectance */
					if (!TOA)
						refl = correctedrefl(refl, TtotraytH2O[crsidx * Nbands + ib],
							tOG[crsidx * Nbands + ib], rhoray0, sphalb0);

					/* reflectance bounds checking */
					if (refl > reflmax) refl = reflmax;
					if (refl < reflmin) refl = reflmin;

					((int16 *)outsds[ib].data)[idx] = (int16) (refl / outsds[ib].factor + 0.5);
					}
				}
			}


		/* write current scan line for all processed bands */
		if (write_scan(iscan, process, outsds)) {
			fprintf(stderr, "Cannot write scan %d of SDS %s\n",
				iscan, outsds[ib].name);
			exit(1);
			}

		} /* end of scan loop */


	for (ib = 0; ib < Nitems; ib++)
		if (sds[ib].id != -1) SDendaccess(sds[ib].id);

	for (ib = 0; ib < Nbands; ib++)
		if (process[ib]) SDendaccess(outsds[ib].id);

	SDend(MOD02QKMfile_id);
	SDend(MOD02HKMfile_id);
	SDend(MOD021KMfile_id);
	SDend(sd_id);


	/* ----- free memory ----- */

	for (ib = 0; ib < Nitems; ib++) {
		if (sds[ib].fillvalue) free(sds[ib].fillvalue);
		if (sds[ib].data) free(sds[ib].data);
		}

	free(height.data);
	free(mus);

	if (!TOA) {
		free(tOG);
		free(TtotraytH2O);
		free(sphalb);
		free(rhoray);
		}

	/* not allocated if --sealevel specified */
	if (dem.data) free(dem.data);


	return 0;
}
コード例 #19
0
ファイル: hdf4dataset.cpp プロジェクト: ryandavid/rotobox
GDALDataset *HDF4Dataset::Open( GDALOpenInfo * poOpenInfo )

{
    if( !Identify( poOpenInfo ) )
        return NULL;

    CPLMutexHolderD(&hHDF4Mutex);

/* -------------------------------------------------------------------- */
/*      Try opening the dataset.                                        */
/* -------------------------------------------------------------------- */

    // Attempt to increase maximum number of opened HDF files.
#ifdef HDF4_HAS_MAXOPENFILES
    intn nCurrMax = 0;
    intn nSysLimit = 0;

    if ( SDget_maxopenfiles(&nCurrMax, &nSysLimit) >= 0
         && nCurrMax < nSysLimit )
    {
        /*intn res = */SDreset_maxopenfiles( nSysLimit );
    }
#endif /* HDF4_HAS_MAXOPENFILES */

    int32 hHDF4 = Hopen(poOpenInfo->pszFilename, DFACC_READ, 0);

    if( hHDF4 <= 0 )
        return NULL;

    Hclose( hHDF4 );

/* -------------------------------------------------------------------- */
/*      Create a corresponding GDALDataset.                             */
/* -------------------------------------------------------------------- */
    // Release mutex otherwise we will deadlock with GDALDataset own mutex.
    CPLReleaseMutex(hHDF4Mutex);
    HDF4Dataset *poDS = new HDF4Dataset();
    CPLAcquireMutex(hHDF4Mutex, 1000.0);

    if( poOpenInfo->fpL != NULL )
    {
        VSIFCloseL(poOpenInfo->fpL);
        poOpenInfo->fpL = NULL;
    }

/* -------------------------------------------------------------------- */
/*          Open HDF SDS Interface.                                     */
/* -------------------------------------------------------------------- */
    poDS->hSD = SDstart( poOpenInfo->pszFilename, DFACC_READ );

    if ( poDS->hSD == -1 )
    {
      // Release mutex otherwise we will deadlock with GDALDataset own mutex.
        CPLReleaseMutex(hHDF4Mutex);
        delete poDS;
        CPLAcquireMutex(hHDF4Mutex, 1000.0);
        CPLError( CE_Failure, CPLE_OpenFailed,
                  "Failed to open HDF4 file \"%s\" for SDS reading.",
                  poOpenInfo->pszFilename );
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*              Now read Global Attributes.                             */
/* -------------------------------------------------------------------- */
    if ( poDS->ReadGlobalAttributes( poDS->hSD ) != CE_None )
    {
        // Release mutex otherwise we will deadlock with GDALDataset own mutex.
        CPLReleaseMutex(hHDF4Mutex);
        delete poDS;
        CPLAcquireMutex(hHDF4Mutex, 1000.0);
        CPLError( CE_Failure, CPLE_OpenFailed,
                  "Failed to read global attributes from HDF4 file \"%s\".",
                  poOpenInfo->pszFilename );
        return NULL;
    }

    poDS->SetMetadata( poDS->papszGlobalMetadata, "" );

/* -------------------------------------------------------------------- */
/*              Determine type of file we read.                         */
/* -------------------------------------------------------------------- */
    const char *pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                             "Signature");

    if ( pszValue != NULL && EQUAL( pszValue, pszGDALSignature ) )
    {
        poDS->iSubdatasetType = H4ST_GDAL;
        poDS->pszSubdatasetType = "GDAL_HDF4";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                            "Title")) != NULL
         && EQUAL( pszValue, "SeaWiFS Level-1A Data" ) )
    {
        poDS->iSubdatasetType = H4ST_SEAWIFS_L1A;
        poDS->pszSubdatasetType = "SEAWIFS_L1A";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                            "Title")) != NULL
        && EQUAL( pszValue, "SeaWiFS Level-2 Data" ) )
    {
        poDS->iSubdatasetType = H4ST_SEAWIFS_L2;
        poDS->pszSubdatasetType = "SEAWIFS_L2";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                            "Title")) != NULL
        && EQUAL( pszValue, "SeaWiFS Level-3 Standard Mapped Image" ) )
    {
        poDS->iSubdatasetType = H4ST_SEAWIFS_L3;
        poDS->pszSubdatasetType = "SEAWIFS_L3";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                            "L1 File Generated By")) != NULL
        && STARTS_WITH_CI(pszValue, "HYP version ") )
    {
        poDS->iSubdatasetType = H4ST_HYPERION_L1;
        poDS->pszSubdatasetType = "HYPERION_L1";
    }

    else
    {
        poDS->iSubdatasetType = H4ST_UNKNOWN;
        poDS->pszSubdatasetType = "UNKNOWN";
    }

/* -------------------------------------------------------------------- */
/*  If we have HDF-EOS dataset, process it here.                        */
/* -------------------------------------------------------------------- */
    int32 aiDimSizes[H4_MAX_VAR_DIMS] = {};  // TODO: Get this off of the stack.
    int32 iRank = 0;
    int32 iNumType = 0;
    int32 nAttrs = 0;
    bool bIsHDF = true;

    // Sometimes "HDFEOSVersion" attribute is not defined and we will
    // determine HDF-EOS datasets using other records
    // (see ReadGlobalAttributes() method).
    if ( poDS->bIsHDFEOS
         || CSLFetchNameValue(poDS->papszGlobalMetadata, "HDFEOSVersion") )
    {
/* -------------------------------------------------------------------- */
/*  Process swath layers.                                               */
/* -------------------------------------------------------------------- */
        hHDF4 = SWopen( poOpenInfo->pszFilename, DFACC_READ );
        if( hHDF4 < 0)
        {
            // Release mutex otherwise we will deadlock with GDALDataset own
            // mutex.
            CPLReleaseMutex(hHDF4Mutex);
            delete poDS;
            CPLAcquireMutex(hHDF4Mutex, 1000.0);
            CPLError( CE_Failure, CPLE_OpenFailed,
                      "Failed to open HDF-EOS file \"%s\" for swath reading.",
                      poOpenInfo->pszFilename );
            return NULL;
        }
        int32 nStrBufSize = 0;
        int32 nSubDatasets =
            SWinqswath(poOpenInfo->pszFilename, NULL, &nStrBufSize);

#ifdef DEBUG
        CPLDebug( "HDF4", "Number of HDF-EOS swaths: %d",
                  static_cast<int>( nSubDatasets ) );
#endif

        if ( nSubDatasets > 0 && nStrBufSize > 0 )
        {
            char *pszSwathList =
                static_cast<char *>( CPLMalloc( nStrBufSize + 1 ) );
            SWinqswath( poOpenInfo->pszFilename, pszSwathList, &nStrBufSize );
            pszSwathList[nStrBufSize] = '\0';

#ifdef DEBUG
            CPLDebug( "HDF4", "List of HDF-EOS swaths: %s", pszSwathList );
#endif

            char **papszSwaths =
                CSLTokenizeString2( pszSwathList, ",", CSLT_HONOURSTRINGS );
            CPLFree( pszSwathList );

            if ( nSubDatasets != CSLCount(papszSwaths) )
            {
                CSLDestroy( papszSwaths );
                // Release mutex otherwise we will deadlock with GDALDataset own
                // mutex.
                CPLReleaseMutex(hHDF4Mutex);
                delete poDS;
                CPLAcquireMutex(hHDF4Mutex, 1000.0);
                CPLDebug( "HDF4", "Cannot parse list of HDF-EOS grids." );
                return NULL;
            }

            for( int32 i = 0; i < nSubDatasets; i++)
            {
                const int32 hSW = SWattach( hHDF4, papszSwaths[i] );

                const int32 nFields
                    = SWnentries( hSW, HDFE_NENTDFLD, &nStrBufSize );
                char *pszFieldList = static_cast<char *>(
                    CPLMalloc( nStrBufSize + 1 ) );
                int32 *paiRank = static_cast<int32 *>(
                    CPLMalloc( nFields * sizeof(int32) ) );
                int32 *paiNumType = static_cast<int32 *>(
                    CPLMalloc( nFields * sizeof(int32) ) );

                SWinqdatafields( hSW, pszFieldList, paiRank, paiNumType );

#ifdef DEBUG
                {
                    char * const pszTmp =
                        SPrintArray( GDT_UInt32, paiRank, nFields, "," );

                    CPLDebug( "HDF4", "Number of data fields in swath %d: %d",
                              static_cast<int>( i ),
                              static_cast<int>( nFields ) );
                    CPLDebug( "HDF4", "List of data fields in swath %d: %s",
                              static_cast<int>( i ), pszFieldList );
                    CPLDebug( "HDF4", "Data fields ranks: %s", pszTmp );

                    CPLFree( pszTmp );
                }
#endif

                char **papszFields = CSLTokenizeString2( pszFieldList, ",",
                                                         CSLT_HONOURSTRINGS );

                char szTemp[256] = {'\0'};  // TODO: Get this off the stack.
                for( int32 j = 0; j < nFields; j++ )
                {
                    SWfieldinfo( hSW, papszFields[j], &iRank, aiDimSizes,
                                 &iNumType, NULL );

                    if ( iRank < 2 )
                        continue;

                    // Add field to the list of GDAL subdatasets.
                    const int nCount = CSLCount( poDS->papszSubDatasets ) / 2;
                    snprintf( szTemp, sizeof(szTemp),
                              "SUBDATASET_%d_NAME", nCount + 1 );
                    // We will use the field index as an identificator.
                    poDS->papszSubDatasets =
                        CSLSetNameValue( poDS->papszSubDatasets, szTemp,
                                CPLSPrintf("HDF4_EOS:EOS_SWATH:\"%s\":%s:%s",
                                           poOpenInfo->pszFilename,
                                           papszSwaths[i], papszFields[j]) );

                    snprintf( szTemp, sizeof(szTemp),
                              "SUBDATASET_%d_DESC", nCount + 1 );
                    char *pszString = SPrintArray( GDT_UInt32, aiDimSizes,
                                                   iRank, "x" );
                    poDS->papszSubDatasets =
                        CSLSetNameValue( poDS->papszSubDatasets, szTemp,
                                         CPLSPrintf( "[%s] %s %s (%s)",
                                                     pszString,
                                         papszFields[j],
                                         papszSwaths[i],
                                         poDS->GetDataTypeName(iNumType) ) );
                    CPLFree( pszString );
                    szTemp[0] = '\0';
                }

                CSLDestroy( papszFields );
                CPLFree( paiNumType );
                CPLFree( paiRank );
                CPLFree( pszFieldList );
                SWdetach( hSW );
            }

            CSLDestroy( papszSwaths );
        }
        SWclose( hHDF4 );

/* -------------------------------------------------------------------- */
/*  Process grid layers.                                                */
/* -------------------------------------------------------------------- */
        hHDF4 = GDopen( poOpenInfo->pszFilename, DFACC_READ );
        nSubDatasets = GDinqgrid( poOpenInfo->pszFilename, NULL, &nStrBufSize );

#ifdef DEBUG
        CPLDebug( "HDF4", "Number of HDF-EOS grids: %d",
                  static_cast<int>( nSubDatasets ) );
#endif

        if ( nSubDatasets > 0 && nStrBufSize > 0 )
        {
            char *pszGridList
                = static_cast<char *>( CPLMalloc( nStrBufSize + 1 ) );
            GDinqgrid( poOpenInfo->pszFilename, pszGridList, &nStrBufSize );

#ifdef DEBUG
            CPLDebug( "HDF4", "List of HDF-EOS grids: %s", pszGridList );
#endif

            char **papszGrids =
                CSLTokenizeString2( pszGridList, ",", CSLT_HONOURSTRINGS );
            CPLFree( pszGridList );

            if ( nSubDatasets != CSLCount(papszGrids) )
            {
                CSLDestroy( papszGrids );
                GDclose( hHDF4 );
                // Release mutex otherwise we will deadlock with GDALDataset own
                // mutex.
                CPLReleaseMutex(hHDF4Mutex);
                delete poDS;
                CPLAcquireMutex(hHDF4Mutex, 1000.0);
                CPLDebug( "HDF4", "Cannot parse list of HDF-EOS grids." );
                return NULL;
            }

            for( int32 i = 0; i < nSubDatasets; i++)
            {
                const int32 hGD = GDattach( hHDF4, papszGrids[i] );

                const int32 nFields
                    = GDnentries( hGD, HDFE_NENTDFLD, &nStrBufSize );
                char *pszFieldList = static_cast<char *>(
                    CPLMalloc( nStrBufSize + 1 ) );
                int32 *paiRank = static_cast<int32 *>(
                    CPLMalloc( nFields * sizeof(int32) ) );
                int32 *paiNumType = static_cast<int32 *>(
                    CPLMalloc( nFields * sizeof(int32) ) );

                GDinqfields( hGD, pszFieldList, paiRank, paiNumType );

#ifdef DEBUG
                {
                    char* pszTmp =
                            SPrintArray( GDT_UInt32, paiRank, nFields, "," );
                    CPLDebug( "HDF4", "Number of fields in grid %d: %d",
                              static_cast<int>( i ),
                              static_cast<int>( nFields ) );
                    CPLDebug( "HDF4", "List of fields in grid %d: %s",
                              static_cast<int>( i ), pszFieldList );
                    CPLDebug( "HDF4", "Fields ranks: %s", pszTmp );
                    CPLFree( pszTmp );
                }
#endif

                char **papszFields = CSLTokenizeString2( pszFieldList, ",",
                                                  CSLT_HONOURSTRINGS );

                char szTemp[256];
                for( int32 j = 0; j < nFields; j++ )
                {
                    GDfieldinfo( hGD, papszFields[j], &iRank, aiDimSizes,
                                 &iNumType, NULL );

                    if ( iRank < 2 )
                        continue;

                    // Add field to the list of GDAL subdatasets
                    const int nCount = CSLCount( poDS->papszSubDatasets ) / 2;
                    snprintf( szTemp, sizeof(szTemp),
                              "SUBDATASET_%d_NAME", nCount + 1 );
                    // We will use the field index as an identificator.
                    poDS->papszSubDatasets =
                        CSLSetNameValue(poDS->papszSubDatasets, szTemp,
                                CPLSPrintf( "HDF4_EOS:EOS_GRID:\"%s\":%s:%s",
                                            poOpenInfo->pszFilename,
                                            papszGrids[i], papszFields[j]));

                    snprintf( szTemp, sizeof(szTemp),
                              "SUBDATASET_%d_DESC", nCount + 1 );
                    char *pszString = SPrintArray( GDT_UInt32, aiDimSizes,
                                                   iRank, "x" );
                    poDS->papszSubDatasets =
                        CSLSetNameValue( poDS->papszSubDatasets, szTemp,
                                         CPLSPrintf("[%s] %s %s (%s)",
                                                    pszString,
                                             papszFields[j],
                                             papszGrids[i],
                                             poDS->GetDataTypeName(iNumType)) );
                    CPLFree( pszString );
                }

                CSLDestroy( papszFields );
                CPLFree( paiNumType );
                CPLFree( paiRank );
                CPLFree( pszFieldList );
                GDdetach( hGD );
            }

            CSLDestroy( papszGrids );
        }
        GDclose( hHDF4 );

        bIsHDF = ( nSubDatasets == 0 ); // Try to read as HDF
    }

    char szName[VSNAMELENMAX + 1];

    if( bIsHDF )
    {

/* -------------------------------------------------------------------- */
/*  Make a list of subdatasets from SDSs contained in input HDF file.   */
/* -------------------------------------------------------------------- */
        int32 nDatasets = 0;

        if ( SDfileinfo( poDS->hSD, &nDatasets, &nAttrs ) != 0 )
            return NULL;

        char szTemp[256] = {'\0'};  // TODO: Get this off the stack.
        const char *pszName = NULL;

        for( int32 i = 0; i < nDatasets; i++ )
        {
            const int32 iSDS = SDselect( poDS->hSD, i );
            if ( SDgetinfo( iSDS, szName, &iRank, aiDimSizes, &iNumType,
                            &nAttrs) != 0 )
                return NULL;

            if ( iRank == 1 )  // Skip 1D datsets
                    continue;

            // Do sort of known datasets. We will display only image bands
            if ( (poDS->iSubdatasetType == H4ST_SEAWIFS_L1A ) &&
                      !STARTS_WITH_CI(szName, "l1a_data") )
                    continue;
            else
                pszName = szName;

            // Add datasets with multiple dimensions to the list of GDAL
            // subdatasets.
            const int nCount = CSLCount( poDS->papszSubDatasets ) / 2;
            snprintf( szTemp, sizeof(szTemp),
                      "SUBDATASET_%d_NAME", nCount + 1 );
            // We will use SDS index as an identificator, because SDS names
            // are not unique. Filename also needed for further file opening
            poDS->papszSubDatasets = CSLSetNameValue(
                  poDS->papszSubDatasets,
                  szTemp,
                  CPLSPrintf( "HDF4_SDS:%s:\"%s\":%ld", poDS->pszSubdatasetType,
                              poOpenInfo->pszFilename,
                              static_cast<long>( i ) ) );
            snprintf( szTemp, sizeof(szTemp),
                      "SUBDATASET_%d_DESC", nCount + 1 );
            char *pszString = SPrintArray( GDT_UInt32, aiDimSizes, iRank, "x" );
            poDS->papszSubDatasets = CSLSetNameValue(
                poDS->papszSubDatasets,
                szTemp,
                CPLSPrintf( "[%s] %s (%s)", pszString,
                            pszName, poDS->GetDataTypeName(iNumType)) );
            CPLFree( pszString );

            SDendaccess( iSDS );
            szTemp[0] = '\0';
        }

        SDend( poDS->hSD );
        poDS->hSD = 0;
    }

/* -------------------------------------------------------------------- */
/*      Build a list of raster images. Note, that HDF-EOS dataset may   */
/*      contain a raster image as well.                                 */
/* -------------------------------------------------------------------- */

    hHDF4 = Hopen(poOpenInfo->pszFilename, DFACC_READ, 0);
    poDS->hGR = GRstart( hHDF4 );

    if ( poDS->hGR != -1 )
    {
        if ( GRfileinfo( poDS->hGR, &poDS->nImages, &nAttrs ) == -1 )
        {
            // Release mutex otherwise we will deadlock with GDALDataset own
            // mutex.
            CPLReleaseMutex(hHDF4Mutex);
            GRend( poDS->hGR );
            poDS->hGR = 0;
            Hclose( hHDF4 );
            delete poDS;
            CPLAcquireMutex(hHDF4Mutex, 1000.0);
            return NULL;
        }

        char szTemp[256] = {'\0'};  // TODO: Get this off the stack.
        for( int32 i = 0; i < poDS->nImages; i++ )
        {
            const int32 iGR = GRselect( poDS->hGR, i );

            // iRank in GR interface has another meaning. It represents number
            // of samples per pixel. aiDimSizes has only two dimensions.
            int32 iInterlaceMode = 0;
            if ( GRgetiminfo( iGR, szName, &iRank, &iNumType, &iInterlaceMode,
                              aiDimSizes, &nAttrs ) != 0 )
            {
                // Release mutex otherwise we will deadlock with GDALDataset
                // own mutex.
                CPLReleaseMutex(hHDF4Mutex);
                GRend( poDS->hGR );
                poDS->hGR = 0;
                Hclose( hHDF4 );
                delete poDS;
                CPLAcquireMutex(hHDF4Mutex, 1000.0);
                return NULL;
            }
            const int nCount = CSLCount( poDS->papszSubDatasets ) / 2;
            snprintf( szTemp, sizeof(szTemp),
                      "SUBDATASET_%d_NAME", nCount + 1 );
            poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
                szTemp,CPLSPrintf( "HDF4_GR:UNKNOWN:\"%s\":%ld",
                                   poOpenInfo->pszFilename,
                                   static_cast<long>( i ) ) );
            snprintf( szTemp, sizeof(szTemp),
                      "SUBDATASET_%d_DESC", nCount + 1 );
            char *pszString = SPrintArray( GDT_UInt32, aiDimSizes, 2, "x" );
            poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
                szTemp, CPLSPrintf( "[%sx%ld] %s (%s)", pszString,
                                    static_cast<long>( iRank ),
                                    szName, poDS->GetDataTypeName(iNumType)) );
            CPLFree( pszString );

            GRendaccess( iGR );
            szTemp[0] = '\0';
        }

        GRend( poDS->hGR );
        poDS->hGR = 0;
    }

    Hclose( hHDF4 );

    poDS->nRasterXSize = poDS->nRasterYSize = 512; // XXX: bogus values

    // Make sure we don't try to do any pam stuff with this dataset.
    poDS->nPamFlags |= GPF_NOSAVE;

/* -------------------------------------------------------------------- */
/*      If we have single subdataset only, open it immediately          */
/* -------------------------------------------------------------------- */
    if ( CSLCount( poDS->papszSubDatasets ) / 2 == 1 )
    {
        char *pszSDSName = CPLStrdup( CSLFetchNameValue( poDS->papszSubDatasets,
                                                         "SUBDATASET_1_NAME" ));
        // Release mutex otherwise we will deadlock with GDALDataset own mutex.
        CPLReleaseMutex(hHDF4Mutex);
        delete poDS;
        poDS = NULL;

        GDALDataset* poRetDS = reinterpret_cast<GDALDataset*>(
            GDALOpen( pszSDSName, poOpenInfo->eAccess ) );
        CPLFree( pszSDSName );

        CPLAcquireMutex(hHDF4Mutex, 1000.0);

        if (poRetDS)
        {
            poRetDS->SetDescription(poOpenInfo->pszFilename);
        }

        return poRetDS;
    }
    else
    {
/* -------------------------------------------------------------------- */
/*      Confirm the requested access is supported.                      */
/* -------------------------------------------------------------------- */
        if( poOpenInfo->eAccess == GA_Update )
        {
            // Release mutex otherwise we will deadlock with GDALDataset own
            // mutex.
            CPLReleaseMutex(hHDF4Mutex);
            delete poDS;
            CPLAcquireMutex(hHDF4Mutex, 1000.0);

            CPLError( CE_Failure, CPLE_NotSupported,
                      "The HDF4 driver does not support update access to "
                      "existing datasets." );
            return NULL;
        }

    }

    return poDS;
}
コード例 #20
0
ファイル: tcoordvar.c プロジェクト: Higginbottom/zeus_python
static intn test_named_vars(void)
{
    char  sds_name[20];
    float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9};
    float32 sds2_data[2][3] = {{0.1, 2.3, 4.5}, {4.5, 6.7, 8.9}};
    int32 dimsize[1], dimsize2[2];
    int32 sds_id, sds1_id, sds2_id, sds3_id, sds4_id, sds5_id;
    int32 file_id, dim_id, index;
    int32 start=0, stride=1, stat;
    int32 start2[2]={0,0}, stride2[2]={1,1};
    int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5];
    int32 array_rank;
    int32 n_datasets, n_file_attrs, n_local_attrs, n_vars=0;
    float32 out_data2[2][3];
    intn  datanum, ranknum, status =0, idx, idx1, idx2;
    intn  is_coordvar=FALSE;
    hdf_varlist_t *allvars, *varlistp;
    intn  num_errs = 0;         /* number of errors so far */
    char  line[40];
    char  contents[7][40]={
          "#0 SDS        2-dim 'Common Name'",
          "#1 SDS        2-dim 'Common Name'",
          "#2 SDS        1-dim 'One Dimension'",
          "#3 Coordinate 1-dim 'Common Name'",
          "#4 SDS        1-dim 'One Dimension'",
          "#5 SDS        1-dim 'Another Name'",
          "#6 Coordinate 1-dim 'Another Name'"};

    file_id = SDstart(FILE3, DFACC_CREATE);
    CHECK(file_id, FAIL, "SDstart");

    dimsize2[0] = 2;
    dimsize2[1] = 3;

    /* Create first COMMON_NAME data set. */
    sds1_id = SDcreate(file_id, COMMON_NAME, DFNT_FLOAT32, 2, dimsize2);
    CHECK(sds1_id, FAIL, "SDcreate");
    status = SDendaccess(sds1_id);
    CHECK(status, FAIL, "SDendaccess");

    /* Create second COMMON_NAME data set. */
    sds2_id = SDcreate(file_id, COMMON_NAME, DFNT_FLOAT32, 2, dimsize2);
    CHECK(sds2_id, FAIL, "SDcreate");
    status = SDendaccess(sds2_id);
    CHECK(status, FAIL, "SDendaccess");

    dimsize[0] = 5;
    sds3_id = SDcreate(file_id, ONEDIM_NAME, DFNT_FLOAT32, 1, dimsize);
    CHECK(sds3_id, FAIL, "SDcreate");

    /* Set the dimension name to be the same as the previous 2 datasets */
    dim_id = SDgetdimid(sds3_id, 0);
    CHECK(dim_id, FAIL, "SDgetdimid");
    status = SDsetdimname(dim_id, COMMON_NAME);
    CHECK(status, FAIL, "SDsetdimname");

    /* Get file info and verify that there are 3 datasets in the file */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 3, "SDfileinfo");

    /* Write values to the dimension COMMON_NAME (same name as first 2 datasets) */
    status = SDsetdimscale (dim_id, dimsize[0], DFNT_INT32, scale1);
    CHECK(status, FAIL, "SDsetdimscale");

    /* Get file info and verify that there are 4 datasets in the file */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 4, "SDfileinfo");

    dimsize[0] = 8;
    sds4_id = SDcreate(file_id, ONEDIM_NAME, DFNT_FLOAT32, 1, dimsize);
    CHECK(sds4_id, FAIL, "SDcreate");

    /* Set the dimension name to be the same as the previous 2 datasets */
    dim_id = SDgetdimid(sds4_id, 0);
    CHECK(dim_id, FAIL, "SDgetdimid");
    status = SDsetdimname(dim_id, ANOTHER_NAME);
    CHECK(status, FAIL, "SDsetdimname");

    sds5_id = SDcreate(file_id, ANOTHER_NAME, DFNT_FLOAT32, 1, dimsize);
    CHECK(sds5_id, FAIL, "SDcreate");

    /* Get file info and verify that there are 6 datasets in the file */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 6, "SDfileinfo");

    status = SDsetattr(dim_id, ATTR1_NAME, DFNT_CHAR8, ATTR1_LEN, ATTR1_VAL);
    CHECK(status, FAIL, "SDsetattr");

    /* Get file info and verify that there are 7 datasets in the file */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 7, "SDfileinfo");

    /* Verify again that the number of datasets in the file is 7 */
    status = SDfileinfo(file_id, &n_datasets, &n_file_attrs);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_datasets, 7, "SDfileinfo");

    /* There are 3 variables of name COMMON_NAME */
    status = SDgetnumvars_byname(file_id, COMMON_NAME, &n_vars);
    CHECK(status, FAIL, "SDfileinfo");
    VERIFY(n_vars, 3, "SDfileinfo");

    allvars = (hdf_varlist_t *)HDmalloc(n_vars * sizeof(hdf_varlist_t));
    status = SDnametoindices(file_id, COMMON_NAME, allvars);
    CHECK(status, FAIL, "SDfileinfo");

    /* Compare file contents with predefined text to verify */
    for (idx = 0; idx < n_datasets; idx++)
    {

	sds_id = SDselect(file_id, idx);
	CHECK(sds_id, FAIL, "SDselect");
	status = SDgetinfo(sds_id, sds_name, &array_rank, NULL, NULL, NULL);
	CHECK(status, FAIL, "SDgetinfo");
	is_coordvar = SDiscoordvar(sds_id);
	if (is_coordvar)
	    sprintf(line,"#%d Coordinate %d-dim '%s'\n", idx, array_rank, 
								sds_name);
	else
	    sprintf(line,"#%d SDS        %d-dim '%s'\n", idx, array_rank, 
								sds_name);

	if (strncmp(contents[idx], line, strlen(contents[idx])) != 0)
	{
	    fprintf(stderr, "File contents are incorrect in testing variable types at variable of index %d\n", idx);
	}
    }

    status = SDend(file_id); 
    CHECK(status, FAIL, "SDend");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
}   /* test_named_vars */
コード例 #21
0
void ossimHdfGridModel::setGridNodes(ossimDblGrid& grid, int32 sds_id, const ossimIpt& spacing)
{
   int x=0, y=0;
   ossim_uint32 index = 0;
   if (m_isHdf4)
   {
      int32 dim_sizes[MAX_VAR_DIMS];
      int32 rank, data_type, n_attrs;
      char  name[MAX_NC_NAME];

      int32 status = SDgetinfo(sds_id, name, &rank, dim_sizes, &data_type, &n_attrs);
      if (status == -1)
         return;

      int32 origin[2] = {0, 0}; 
      int32 num_rows = dim_sizes[0]; 
      int32 num_cols = dim_sizes[1]; 

      ossimDpt grid_origin(0,0); // The grid as used in base class, has UV-space always at 0,0 origin
      ossimIpt grid_size (num_cols, num_rows);
      ossimDpt dspacing (spacing);
      grid.initialize(grid_size, grid_origin, dspacing);

      float32* values = new float32 [num_rows * num_cols]; 
      intn statusN = SDreaddata(sds_id, origin, NULL, dim_sizes, (VOIDP)values);
      if (statusN > -1)
      {
         for (y = 0; y < num_rows; y++)
         {
            for (x = 0; x < num_cols; x++)
            {
               grid.setNode(x, y, values[index++]);
            }
         }
      }
      delete values;
   }
   else
   {
      hsize_t dims_out[2]; //dataset dimensions
      hid_t datatype = H5Dget_type(sds_id);
      hid_t dataspace = H5Dget_space(sds_id);    //dataspace handle
      int rank = H5Sget_simple_extent_ndims(dataspace);
      if (rank == 2)
      {
         herr_t status_n  = H5Sget_simple_extent_dims(dataspace, dims_out, NULL);

         ossim_int32 latGridRows = dims_out[0];
         ossim_int32 lonGridCols = dims_out[1];

         ossim_int32 cols = spacing.x;
         ossim_int32 rows = spacing.y;

         ossim_int32 spacingX = 0;
         ossim_int32 spacingY = 0;

         if (rows % latGridRows == 0 && cols % lonGridCols == 0)
         {
            spacingY = rows/latGridRows; //line increment step
            spacingX = cols/lonGridCols; //pixel increment step
         }
         
         ossimIpt gridSpacing(spacingX, spacingY);

         ossimDpt grid_origin(0,0); // The grid as used in base class, has UV-space always at 0,0 origin
         ossimIpt grid_size (cols, rows);
         ossimDpt dspacing (gridSpacing);
         grid.initialize(grid_size, grid_origin, dspacing);

         if( H5Tequal(H5T_NATIVE_FLOAT,  datatype))
         {
            ossim_float32* data_out = new ossim_float32[dims_out[0] * dims_out[1]];
            herr_t status = H5Dread(sds_id, datatype, dataspace, dataspace,
               H5P_DEFAULT, data_out);

            index = 0;
            for (y = 0; y < rows; y++)
            {
               for (x = 0; x < cols; x++)
               {
                  index = x + y * cols;
                  grid.setNode(x, y, data_out[index]);
               }
            }
            delete[] data_out;
         }
         else if( H5Tequal(H5T_NATIVE_DOUBLE,  datatype))
         {
            ossim_float64* data_out = new ossim_float64[dims_out[0] * dims_out[1]];
            herr_t status = H5Dread(sds_id, datatype, dataspace, dataspace,
               H5P_DEFAULT, data_out);

            index = 0;
            for (y = 0; y < rows; y++)
            {
               for (x = 0; x < cols; x++)
               {
                  index = x + y * cols;
                  grid.setNode(x, y, data_out[index]);
               }
            }
            delete[] data_out;
         }
      }
      H5Tclose(datatype);
      H5Sclose(dataspace);
   }
}
コード例 #22
0
ファイル: hdf4dataset.cpp プロジェクト: AsherBond/MondocosmOS
GDALDataset *HDF4Dataset::Open( GDALOpenInfo * poOpenInfo )

{
    int32	i;

    if( !Identify( poOpenInfo ) )
        return NULL;

/* -------------------------------------------------------------------- */
/*      Try opening the dataset.                                        */
/* -------------------------------------------------------------------- */
    int32	hHDF4;
    
    hHDF4 = Hopen(poOpenInfo->pszFilename, DFACC_READ, 0);
    
    if( hHDF4 <= 0 )
        return( NULL );

    Hclose( hHDF4 );

/* -------------------------------------------------------------------- */
/*      Create a corresponding GDALDataset.                             */
/* -------------------------------------------------------------------- */
    HDF4Dataset *poDS;

    poDS = new HDF4Dataset();

    poDS->fp = poOpenInfo->fp;
    poOpenInfo->fp = NULL;
    
/* -------------------------------------------------------------------- */
/*          Open HDF SDS Interface.                                     */
/* -------------------------------------------------------------------- */
    poDS->hSD = SDstart( poOpenInfo->pszFilename, DFACC_READ );

    if ( poDS->hSD == -1 )
    {
	delete poDS;
        return NULL;
    }
   
/* -------------------------------------------------------------------- */
/*		Now read Global Attributes.				*/
/* -------------------------------------------------------------------- */
    if ( poDS->ReadGlobalAttributes( poDS->hSD ) != CE_None )
    {
	delete poDS;
        return NULL;
    }

    poDS->SetMetadata( poDS->papszGlobalMetadata, "" );

/* -------------------------------------------------------------------- */
/*		Determine type of file we read.				*/
/* -------------------------------------------------------------------- */
    const char	*pszValue;
    
    if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                       "Signature"))
	 && EQUAL( pszValue, pszGDALSignature ) )
    {
	poDS->iSubdatasetType = H4ST_GDAL;
	poDS->pszSubdatasetType = "GDAL_HDF4";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Title"))
	 && EQUAL( pszValue, "SeaWiFS Level-1A Data" ) )
    {
	poDS->iSubdatasetType = H4ST_SEAWIFS_L1A;
	poDS->pszSubdatasetType = "SEAWIFS_L1A";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Title"))
	&& EQUAL( pszValue, "SeaWiFS Level-2 Data" ) )
    {
	poDS->iSubdatasetType = H4ST_SEAWIFS_L2;
	poDS->pszSubdatasetType = "SEAWIFS_L2";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata, "Title"))
	&& EQUAL( pszValue, "SeaWiFS Level-3 Standard Mapped Image" ) )
    {
	poDS->iSubdatasetType = H4ST_SEAWIFS_L3;
	poDS->pszSubdatasetType = "SEAWIFS_L3";
    }

    else if ( (pszValue = CSLFetchNameValue(poDS->papszGlobalMetadata,
                                            "L1 File Generated By"))
	&& EQUALN( pszValue, "HYP version ", 12 ) )
    {
	poDS->iSubdatasetType = H4ST_HYPERION_L1;
	poDS->pszSubdatasetType = "HYPERION_L1";
    }

    else
    {
	poDS->iSubdatasetType = H4ST_UNKNOWN;
	poDS->pszSubdatasetType = "UNKNOWN";
    }

/* -------------------------------------------------------------------- */
/*  If we have HDF-EOS dataset, process it here.	                */
/* -------------------------------------------------------------------- */
    char	szName[VSNAMELENMAX + 1], szTemp[8192];
    char	*pszString;
    const char  *pszName;
    int		nCount;
    int32	aiDimSizes[H4_MAX_VAR_DIMS];
    int32	iRank, iNumType, nAttrs;
    bool        bIsHDF = true;
    
    // Sometimes "HDFEOSVersion" attribute is not defined and we will
    // determine HDF-EOS datasets using other records
    // (see ReadGlobalAttributes() method).
    if ( poDS->bIsHDFEOS
         || CSLFetchNameValue(poDS->papszGlobalMetadata, "HDFEOSVersion") )
    {
        bIsHDF  = false;

        int32   nSubDatasets, nStrBufSize;

/* -------------------------------------------------------------------- */
/*  Process swath layers.                                               */
/* -------------------------------------------------------------------- */
        hHDF4 = SWopen( poOpenInfo->pszFilename, DFACC_READ );
        if( hHDF4 < 0)
        {
            delete poDS;
            CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open HDF4 `%s'.\n", poOpenInfo->pszFilename );
            return NULL;
        } 
        nSubDatasets = SWinqswath(poOpenInfo->pszFilename, NULL, &nStrBufSize);
#if DEBUG
        CPLDebug( "HDF4", "Number of HDF-EOS swaths: %d", (int)nSubDatasets );
#endif
        if ( nSubDatasets > 0 && nStrBufSize > 0 )
        {
            char    *pszSwathList;
            char    **papszSwaths;

            pszSwathList = (char *)CPLMalloc( nStrBufSize + 1 );
            SWinqswath( poOpenInfo->pszFilename, pszSwathList, &nStrBufSize );
            pszSwathList[nStrBufSize] = '\0';

#if DEBUG
            CPLDebug( "HDF4", "List of HDF-EOS swaths: %s", pszSwathList );
#endif

            papszSwaths =
                CSLTokenizeString2( pszSwathList, ",", CSLT_HONOURSTRINGS );
            CPLFree( pszSwathList );

            if ( nSubDatasets != CSLCount(papszSwaths) )
            {
                CSLDestroy( papszSwaths );
                delete poDS;
                CPLDebug( "HDF4", "Can not parse list of HDF-EOS grids." );
                return NULL;
            }

            for ( i = 0; i < nSubDatasets; i++)
            {
                char    *pszFieldList;
                char    **papszFields;
                int32   *paiRank, *paiNumType;
                int32   hSW, nFields, j;

                hSW = SWattach( hHDF4, papszSwaths[i] );

                nFields = SWnentries( hSW, HDFE_NENTDFLD, &nStrBufSize );
                pszFieldList = (char *)CPLMalloc( nStrBufSize + 1 );
                paiRank = (int32 *)CPLMalloc( nFields * sizeof(int32) );
                paiNumType = (int32 *)CPLMalloc( nFields * sizeof(int32) );

                SWinqdatafields( hSW, pszFieldList, paiRank, paiNumType );

#if DEBUG
                {
                    char *pszTmp =
                        SPrintArray( GDT_UInt32, paiRank, nFields, "," );

                    CPLDebug( "HDF4", "Number of data fields in swath %d: %d",
                              (int) i, (int) nFields );
                    CPLDebug( "HDF4", "List of data fields in swath %d: %s",
                              (int) i, pszFieldList );
                    CPLDebug( "HDF4", "Data fields ranks: %s", pszTmp );

                    CPLFree( pszTmp );
                }
#endif

                papszFields = CSLTokenizeString2( pszFieldList, ",",
                                                  CSLT_HONOURSTRINGS );
                
                for ( j = 0; j < nFields; j++ )
                {
                    SWfieldinfo( hSW, papszFields[j], &iRank, aiDimSizes,
                                 &iNumType, NULL );

                    if ( iRank < 2 )
                        continue;

	            // Add field to the list of GDAL subdatasets
                    nCount = CSLCount( poDS->papszSubDatasets ) / 2;
                    sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 );
	            // We will use the field index as an identificator.
                    poDS->papszSubDatasets =
                        CSLSetNameValue( poDS->papszSubDatasets, szTemp,
                                CPLSPrintf("HDF4_EOS:EOS_SWATH:\"%s\":%s:%s",
                                           poOpenInfo->pszFilename,
                                           papszSwaths[i], papszFields[j]) );

                    sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 );
                    pszString = SPrintArray( GDT_UInt32, aiDimSizes,
                                             iRank, "x" );
                    poDS->papszSubDatasets =
                        CSLSetNameValue( poDS->papszSubDatasets, szTemp,
                                         CPLSPrintf( "[%s] %s %s (%s)", pszString,
                                         papszFields[j],
                                         papszSwaths[i],
                                         poDS->GetDataTypeName(iNumType) ) );
                    CPLFree( pszString );
                }

                CSLDestroy( papszFields );
                CPLFree( paiNumType );
                CPLFree( paiRank );
                CPLFree( pszFieldList );
                SWdetach( hSW );
            }

            CSLDestroy( papszSwaths );
        }
        SWclose( hHDF4 );

/* -------------------------------------------------------------------- */
/*  Process grid layers.                                                */
/* -------------------------------------------------------------------- */
        hHDF4 = GDopen( poOpenInfo->pszFilename, DFACC_READ );
        nSubDatasets = GDinqgrid( poOpenInfo->pszFilename, NULL, &nStrBufSize );
#if DEBUG
        CPLDebug( "HDF4", "Number of HDF-EOS grids: %d", (int)nSubDatasets );
#endif
        if ( nSubDatasets > 0 && nStrBufSize > 0 )
        {
            char    *pszGridList;
            char    **papszGrids;

            pszGridList = (char *)CPLMalloc( nStrBufSize + 1 );
            GDinqgrid( poOpenInfo->pszFilename, pszGridList, &nStrBufSize );

#if DEBUG
            CPLDebug( "HDF4", "List of HDF-EOS grids: %s", pszGridList );
#endif

            papszGrids =
                CSLTokenizeString2( pszGridList, ",", CSLT_HONOURSTRINGS );
            CPLFree( pszGridList );

            if ( nSubDatasets != CSLCount(papszGrids) )
            {
                CSLDestroy( papszGrids );
                delete poDS;
                CPLDebug( "HDF4", "Can not parse list of HDF-EOS grids." );
                return NULL;
            }

            for ( i = 0; i < nSubDatasets; i++)
            {
                char    *pszFieldList;
                char    **papszFields;
                int32   *paiRank, *paiNumType;
                int32   hGD, nFields, j;

                hGD = GDattach( hHDF4, papszGrids[i] );

                nFields = GDnentries( hGD, HDFE_NENTDFLD, &nStrBufSize );
                pszFieldList = (char *)CPLMalloc( nStrBufSize + 1 );
                paiRank = (int32 *)CPLMalloc( nFields * sizeof(int32) );
                paiNumType = (int32 *)CPLMalloc( nFields * sizeof(int32) );

                GDinqfields( hGD, pszFieldList, paiRank, paiNumType );

#if DEBUG
                {
                    char* pszTmp =
                            SPrintArray( GDT_UInt32, paiRank, nFields, "," );
                    CPLDebug( "HDF4", "Number of fields in grid %d: %d",
                            (int) i, (int) nFields );
                    CPLDebug( "HDF4", "List of fields in grid %d: %s",
                            (int) i, pszFieldList );
                    CPLDebug( "HDF4", "Fields ranks: %s",
                            pszTmp );
                    CPLFree( pszTmp );
                }
#endif

                papszFields = CSLTokenizeString2( pszFieldList, ",",
                                                  CSLT_HONOURSTRINGS );
                
                for ( j = 0; j < nFields; j++ )
                {
                    GDfieldinfo( hGD, papszFields[j], &iRank, aiDimSizes,
                                 &iNumType, NULL );

                    if ( iRank < 2 )
                        continue;

	            // Add field to the list of GDAL subdatasets
                    nCount = CSLCount( poDS->papszSubDatasets ) / 2;
                    sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 );
	            // We will use the field index as an identificator.
                    poDS->papszSubDatasets =
                        CSLSetNameValue(poDS->papszSubDatasets, szTemp,
                                CPLSPrintf( "HDF4_EOS:EOS_GRID:\"%s\":%s:%s",
                                            poOpenInfo->pszFilename,
                                            papszGrids[i], papszFields[j]));

                    sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 );
                    pszString = SPrintArray( GDT_UInt32, aiDimSizes,
                                             iRank, "x" );
                    poDS->papszSubDatasets =
                        CSLSetNameValue( poDS->papszSubDatasets, szTemp,
                                         CPLSPrintf("[%s] %s %s (%s)", pszString,
                                             papszFields[j],
                                             papszGrids[i],
                                             poDS->GetDataTypeName(iNumType)) );
                    CPLFree( pszString );
                }

                CSLDestroy( papszFields );
                CPLFree( paiNumType );
                CPLFree( paiRank );
                CPLFree( pszFieldList );
                GDdetach( hGD );
            }

            CSLDestroy( papszGrids );
            GDclose( hHDF4 );
        }
        GDclose( hHDF4 );

        bIsHDF = ( nSubDatasets == 0 ); // Try to read as HDF
    }

    if( bIsHDF )
    {

/* -------------------------------------------------------------------- */
/*  Make a list of subdatasets from SDSs contained in input HDF file.	*/
/* -------------------------------------------------------------------- */
        int32   nDatasets;

        if ( SDfileinfo( poDS->hSD, &nDatasets, &nAttrs ) != 0 )
	    return NULL;

        for ( i = 0; i < nDatasets; i++ )
        {
            int32	iSDS;

            iSDS = SDselect( poDS->hSD, i );
            if ( SDgetinfo( iSDS, szName, &iRank, aiDimSizes, &iNumType, &nAttrs) != 0 )
                return NULL;
            
            if ( iRank == 1 )		// Skip 1D datsets
                    continue;

            // Do sort of known datasets. We will display only image bands
            if ( (poDS->iSubdatasetType == H4ST_SEAWIFS_L1A ) &&
                      !EQUALN( szName, "l1a_data", 8 ) )
                    continue;
            else
                pszName = szName;
            
            // Add datasets with multiple dimensions to the list of GDAL subdatasets
            nCount = CSLCount( poDS->papszSubDatasets ) / 2;
            sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 );
            // We will use SDS index as an identificator, because SDS names
            // are not unique. Filename also needed for further file opening
            poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp, 
                  CPLSPrintf( "HDF4_SDS:%s:\"%s\":%ld", poDS->pszSubdatasetType,
                              poOpenInfo->pszFilename, (long)i) );
            sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 );
            pszString = SPrintArray( GDT_UInt32, aiDimSizes, iRank, "x" );
            poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets, szTemp,
                CPLSPrintf( "[%s] %s (%s)", pszString,
                            pszName, poDS->GetDataTypeName(iNumType)) );
            CPLFree( pszString );

            SDendaccess( iSDS );
        }

        SDend( poDS->hSD );
        poDS->hSD = 0;
    }

/* -------------------------------------------------------------------- */
/*      Build a list of raster images. Note, that HDF-EOS dataset may   */
/*      contain a raster image as well.                                 */
/* -------------------------------------------------------------------- */
    hHDF4 = Hopen(poOpenInfo->pszFilename, DFACC_READ, 0);
    poDS->hGR = GRstart( hHDF4 );

    if ( poDS->hGR != -1 )
    {
        if ( GRfileinfo( poDS->hGR, &poDS->nImages, &nAttrs ) == -1 )
            return NULL;
        
        for ( i = 0; i < poDS->nImages; i++ )
        {
            int32   iInterlaceMode; 
            int32   iGR = GRselect( poDS->hGR, i );

            // iRank in GR interface has another meaning. It represents number
            // of samples per pixel. aiDimSizes has only two dimensions.
            if ( GRgetiminfo( iGR, szName, &iRank, &iNumType, &iInterlaceMode,
                              aiDimSizes, &nAttrs ) != 0 )
                return NULL;
            nCount = CSLCount( poDS->papszSubDatasets ) / 2;
            sprintf( szTemp, "SUBDATASET_%d_NAME", nCount + 1 );
            poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
                szTemp,CPLSPrintf( "HDF4_GR:UNKNOWN:\"%s\":%ld",
                                   poOpenInfo->pszFilename, (long)i));
            sprintf( szTemp, "SUBDATASET_%d_DESC", nCount + 1 );
            pszString = SPrintArray( GDT_UInt32, aiDimSizes, 2, "x" );
            poDS->papszSubDatasets = CSLSetNameValue(poDS->papszSubDatasets,
                szTemp, CPLSPrintf( "[%sx%ld] %s (%s)", pszString, (long)iRank,
                                    szName, poDS->GetDataTypeName(iNumType)) );
            CPLFree( pszString );

            GRendaccess( iGR );
        }

        GRend( poDS->hGR );
        poDS->hGR = 0;
    }

    Hclose( hHDF4 );

    poDS->nRasterXSize = poDS->nRasterYSize = 512; // XXX: bogus values

    // Make sure we don't try to do any pam stuff with this dataset.
    poDS->nPamFlags |= GPF_NOSAVE;

/* -------------------------------------------------------------------- */
/*      If we have single subdataset only, open it immediately          */
/* -------------------------------------------------------------------- */
    if ( CSLCount( poDS->papszSubDatasets ) / 2 == 1 )
    {
        char *pszSDSName;
        pszSDSName = CPLStrdup( CSLFetchNameValue( poDS->papszSubDatasets,
                            "SUBDATASET_1_NAME" ));
        delete poDS;
        poDS = NULL;

        GDALDataset* poRetDS = (GDALDataset*) GDALOpen( pszSDSName, poOpenInfo->eAccess );
        CPLFree( pszSDSName );

        if (poRetDS)
        {
            poRetDS->SetDescription(poOpenInfo->pszFilename);
        }

        return poRetDS;
    }
    else
    {
/* -------------------------------------------------------------------- */
/*      Confirm the requested access is supported.                      */
/* -------------------------------------------------------------------- */
        if( poOpenInfo->eAccess == GA_Update )
        {
            delete poDS;
            CPLError( CE_Failure, CPLE_NotSupported, 
                      "The HDF4 driver does not support update access to existing"
                      " datasets.\n" );
            return NULL;
        }
    
    }

    return( poDS );
}
コード例 #23
0
bool
RemapHDF4::setFile(QList<QString> fl)
{
  m_fileName = fl;
  
  // list all image files in the directory
  QStringList imageNameFilter;
  imageNameFilter << "*.hdf";
  QStringList files= QDir(m_fileName[0]).entryList(imageNameFilter,
						   QDir::NoSymLinks|
						   QDir::NoDotAndDotDot|
						   QDir::Readable|
						   QDir::Files);
  m_imageList.clear();
  for(uint i=0; i<files.size(); i++)
    {
      QFileInfo fileInfo(m_fileName[0], files[i]);
      QString imgfl = fileInfo.absoluteFilePath();
      m_imageList.append(imgfl);
    }

  m_depth = m_imageList.size();


  /* Open the file and initiate the SD interface. */
  int32 sd_id = SDstart(strdup(m_imageList[0].toAscii().data()),
		  DFACC_READ);
  if (sd_id < 0) {
    QMessageBox::information(0, 
			     "Error",
			     QString("Failed to open %1").arg(m_imageList[0]));
    return false;
  }
    
  /* Determine the contents of the file. */
  int32 dim_sizes[MAX_VAR_DIMS];
  int32 rank, num_type, attributes, istat;
  char name[64];
  int32 n_datasets, n_file_attrs;

  istat = SDfileinfo(sd_id, &n_datasets, &n_file_attrs);

  /* Access the name of every data set in the file. */
  QStringList varNames;
  for (int32 index = 0; index < n_datasets; index++)
    {
      int32 sds_id = SDselect(sd_id, index);
      
      istat = SDgetinfo(sds_id, name, &rank, dim_sizes,	\
			&num_type, &attributes);
            
      istat = SDendaccess(sds_id);

      if (rank == 2)
	varNames.append(name);
    }
  
  QString var;
  if (varNames.size() == 0) {
    QMessageBox::information(0, 
			     "Error",
			     QString("No variables in file with rank of 2"));
    return false;
  } else if (varNames.size() == 1)
    {
      var = varNames[0];
    }
  else
    {
      var = QInputDialog::getItem(0,
				  "Select Variable to Extract",
				  "Variable Names",
				  varNames,
				  0,
				  false);
    }
  
  m_Index = 0;
  for (int32 index = 0; index < n_datasets; index++)
    {
      int32 sds_id = SDselect(sd_id, index);
      
      istat = SDgetinfo(sds_id, name, &rank, dim_sizes,	\
			&num_type, &attributes);
      
      istat = SDendaccess(sds_id);
      
      if (var == QString(name))
	{
	  m_Index = index;
	  break;
	}
    }

  {    
    int32 sds_id = SDselect(sd_id, m_Index);
    
    istat = SDgetinfo(sds_id,
		      name,
		      &rank,
		      dim_sizes,
		      &num_type,
		      &attributes);
    
    istat = SDendaccess(sds_id);
  }

  /* Terminate access to the SD interface and close the file. */
  istat = SDend(sd_id);


  if (num_type == DFNT_CHAR8)
    m_voxelType = _Char;
  else if (num_type == DFNT_UCHAR8)
    m_voxelType = _UChar;
  else if (num_type == DFNT_INT8)
    m_voxelType = _Char;
  else if (num_type == DFNT_UINT8)
    m_voxelType = _UChar;
  else if (num_type == DFNT_INT16)
    m_voxelType = _Short;
  else if (num_type == DFNT_UINT16)
    m_voxelType = _UShort;
  else if (num_type == DFNT_INT32)
    m_voxelType = _Int;
  else if (num_type == DFNT_FLOAT32)
    m_voxelType = _Float;
  else
    {
      QMessageBox::information(0, "Error",
			       QString("Cannot handle datatype %1").arg(num_type));
      return false;
    }

  m_width = dim_sizes[0];
  m_height = dim_sizes[1];

  m_bytesPerVoxel = 1;
  if (m_voxelType == _UChar) m_bytesPerVoxel = 1;
  else if (m_voxelType == _Char) m_bytesPerVoxel = 1;
  else if (m_voxelType == _UShort) m_bytesPerVoxel = 2;
  else if (m_voxelType == _Short) m_bytesPerVoxel = 2;
  else if (m_voxelType == _Int) m_bytesPerVoxel = 4;
  else if (m_voxelType == _Float) m_bytesPerVoxel = 4;

  m_headerBytes = 0;

  if (m_voxelType == _UChar ||
      m_voxelType == _Char ||
      m_voxelType == _UShort ||
      m_voxelType == _Short)
    {
      findMinMaxandGenerateHistogram();
    }
  else
    {
      findMinMax();
      generateHistogram();
    }

  m_rawMap.append(m_rawMin);
  m_rawMap.append(m_rawMax);
  m_pvlMap.append(0);
  m_pvlMap.append(255);

  return true;
}