Пример #1
0
static uint32 vdata_cmp(int32  vs1, 
                        int32  vs2, 
                        char   *gname, 
                        char   *cname, 
                        diff_opt_t * opt)
{
 int32   i, j, k, iflag;
 uint32  err_cnt;
 int32   nv1, interlace1, vsize1;
 int32   vsotag1;
 char    fields1[VSFIELDMAX*FIELDNAMELENMAX];
 char    vsclass1[VSNAMELENMAX], vsname1[VSNAMELENMAX];
 int32   nv2, interlace2, vsize2;
 int32   vsotag2;
 char    fields2[VSFIELDMAX*FIELDNAMELENMAX];
 char    vsclass2[VSNAMELENMAX], vsname2[VSNAMELENMAX];
 uint8   *buf1, *buf2, *b1, *b2;
 int32   off1[60], off2[60];
 DYN_VWRITELIST *w1, *w2;
 uint32  nfound=0;
 uint32  max_err_cnt = opt->max_err_cnt; 
 
 VSinquire(vs1, &nv1, &interlace1, fields1, &vsize1, vsname1);
 VSinquire(vs2, &nv2, &interlace2, fields2, &vsize2, vsname2);
 
 vsotag1 = VSQuerytag(vs1);
 VSgetclass(vs1,vsclass1);
 
 vsotag2 = VSQuerytag(vs2);
 VSgetclass(vs2,vsclass2);
 
 if (vsotag1 != vsotag2 || nv1 != nv2 || interlace1 != interlace2 ||
  strcmp(fields1, fields2) != 0 || strcmp(vsclass1, vsclass2) != 0 ||
  (strcmp(vsclass1, "Attr0.0") != 0 && vsize1 != vsize2))
 {
  printf("\n---------------------------\n");
  printf("Vdata Name: %s <%s/%s> (Different attributes)\n",
   vsname1, gname, cname);
  printf("> <%d> nrec=%d interlace=%d fld=[%s] vsize=%d class={%s})\n",
   vsotag1, nv1, interlace1, fields1, vsize1, vsclass1);
  printf("< <%d> nrec=%d interlace=%d fld=[%s] vsize=%d class={%s})\n",
   vsotag2, nv2, interlace2, fields2, vsize2, vsclass2);
  return 0;
 }
 
 
 /* compare the data */
 
 buf1 = (uint8 *) malloc((unsigned) (nv1 * vsize1));
 buf2 = (uint8 *) malloc((unsigned) (nv2 * vsize2));
 if (!buf1 || !buf2) 
 {
  printf("Out of memory!");
  opt->err_stat = 1;
  return 0;
 }
 
 VSsetfields(vs1, fields1);
 VSread(vs1, buf1, nv1, interlace1);
 w1 = (DYN_VWRITELIST*) vswritelist(vs1);
 
 VSsetfields(vs2, fields2);
 VSread(vs2, buf2, nv2, interlace2);
 w2 = (DYN_VWRITELIST*) vswritelist(vs2);
 
 b1 = buf1;
 b2 = buf2;
 
 for (j=0; j < w1->n; j++)
  off1[j] = DFKNTsize(w1->type[j] | DFNT_NATIVE);
 
 for (j=0; j < w2->n; j++)
  off2[j] = DFKNTsize(w2->type[j] | DFNT_NATIVE);
 
 iflag = 0;
 
 err_cnt = 0;
 
 if (vsize1 == vsize2)
 {
  for (i=0; i<nv1; i++)
  {
   if (memcmp(b1, b2, (size_t)vsize1) == 0)
   {
    b1 += vsize1;   
    b2 += vsize2;
    continue;
   }
   if (iflag == 0)
   {
    iflag = 1;         /* there is a difference */
    printf("\n---------------------------\n");
    printf("Vdata Name: %s (Data record comparison)\n", 
     vsname1);
    nfound ++;
   }
   
   printf("> %d: ", i);
   for (j=0; j<w1->n; j++)
   {
    for (k=0; k<w1->order[j]; k++)
    {
     fmt_print(b1, w1->type[j]);
     b1 += off1[j];
     if (w1->type[j] != DFNT_CHAR)
      putchar(' ');
    }
   }        
   putchar('\n');
   printf("< %d: ", i);
   for (j=0; j<w2->n; j++)
   {
    for (k=0; k<w2->order[j]; k++)
    {
     fmt_print(b2, w2->type[j]);
     b2 += off2[j];
     if (w2->type[j] != DFNT_CHAR)
      putchar(' ');
    }
   }        
   putchar('\n');
   
   if (max_err_cnt > 0)
   {
    err_cnt++;
    if (err_cnt >= max_err_cnt)
     break;
   }
  }
 }
 else
 {
  printf("****....\n");
  for (i=0; i<nv1; i++)
  {
   if (iflag == 0)
   {
    iflag = 1;         /* there is a difference */
    printf("\n---------------------------\n");
    printf("Vdata Name: %s (Data record comparison)\n", 
     vsname1);
    nfound ++;
   }
   printf("> %d: ", i);
   for (j=0; j<w1->n; j++)
   {
    for (k=0; k<w1->order[j]; k++)
    {
     fmt_print(b1, w1->type[j]);
     b1 += off1[j];
     if (w1->type[j] != DFNT_CHAR)
      putchar(' ');
    }
   }  
   putchar('\n');
   printf("< %d: ", i);
   for (j=0; j<w2->n; j++)
   {
    for (k=0; k<w2->order[j]; k++)
    {
     fmt_print(b2, w2->type[j]);
     b1 += off2[j];
     if (w2->type[j] != DFNT_CHAR)
      putchar(' ');
    }
   }  
   putchar('\n');
   
   if (max_err_cnt > 0)
   {
    err_cnt++;
    if (err_cnt >= max_err_cnt)
     break;
   }
  }
  
 }
 
 if (buf1)free((char *) buf1);
 if (buf2)free((char *) buf2);

 return nfound;
}
Пример #2
0
void main(void)
{

    char    vdata_name[MAX_NC_NAME], vdata_class[MAX_NC_NAME];
    char    fields[60];
    int32   file_id, vdata_id, istat;
    int32   n_records, interlace, vdata_size, vdata_ref;
    int     bufsz = (2 * sizeof(float32) + sizeof(char) \
                     + sizeof(int16)) * NRECORDS;
    int    i;
    uint8  databuf[((2 * sizeof(float32)) + sizeof(char) \
                    + sizeof(int16)) * NRECORDS];
    VOIDP fldbufptrs[4];
    float32 itemp[NRECORDS], ispeed[NRECORDS];
    int16   iheight[NRECORDS];
    char    idents[NRECORDS];

    /* Open the HDF file. */
    file_id = Hopen("VD_Ex4.hdf", DFACC_READ, 0);

    /* Initialize the Vset interface. */
    istat = Vstart(file_id);

    /*
    * Get the reference number for the first Vdata in
    * the file.
    */
    vdata_ref = -1;
    vdata_ref = VSgetid(file_id, vdata_ref);

    /* Attach to the first Vdata in read mode. */
    vdata_id = VSattach(file_id, vdata_ref, "r");
    for (i=0; i<60; i++)
        fields[i] = '\0';
    /* Get the list of field names. */
    istat =VSinquire(vdata_id, &n_records, &interlace,
                     fields, &vdata_size, vdata_name);
    printf("files: %s, n_records: %d, vdata_size: %d\n",
           fields, n_records, vdata_size);
    /* Get the class. */
    istat = VSgetclass(vdata_id, vdata_class);

    /* Determine the fields that will be read. */
    istat = VSsetfields(vdata_id, fields);

    /* Print the Vdata information. */
    printf("Current Vdata name: %s \nCurrent Vdata class: %s.\n",
           vdata_name, vdata_class);

    /* Read the data. */
    istat = VSread(vdata_id, (VOIDP)databuf, n_records,
                   FULL_INTERLACE);
    /* set fldbufptrs and unpack field values */
    fldbufptrs[0] = &idents[0];
    fldbufptrs[1] = &ispeed[0];
    fldbufptrs[2] = &iheight[0];
    fldbufptrs[3] = &itemp[0];

    istat = VSfpack(vdata_id, _HDF_VSUNPACK, fields, databuf,
                    bufsz, n_records, "Ident,Speed,Height,Temp",
                    fldbufptrs);
    printf("     Temp      Height     Speed      Ident\n");
    for (i=0; i < n_records; i++) {
        printf("   %6.2f    %6d      %6.2f       %c\n",
               itemp[i],iheight[i],ispeed[i],idents[i]);
    }
    /* Detach from the Vdata, close the interface and the file. */
    istat = VSdetach(vdata_id);
    istat = Vend(file_id);
    istat = Hclose(file_id);

}
Пример #3
0
int32
dumpvd(int32       vd, 
       file_format_t ff, 
       int         data_only, 
       FILE       *fp, 
       char        separater[2],
       int32       flds_indices[VSFIELDMAX], 
       int         dumpallfields)
{
    char        vdname[VSNAMELENMAX+1];
    int32       j, i, t, interlace, nv, vsize;
    uint8      *bb = NULL;
    uint8      *b = NULL;
    DYN_VWRITELIST *w = NULL;
    intn       (*vfmtfn[VSFIELDMAX]) (VOIDP ,  file_format_t ff,  FILE *);
    int32       off[VSFIELDMAX];
    int32       order[VSFIELDMAX];
    int32       nattrs[VSFIELDMAX];
    int32       bufsize;		/* size of the buffer we are using */
    int32       chunk;			/* number of rows that will fit in the buffer */
    int32       done;			/* number of rows we have done */
    int32       count;          /* number of rows to do this time through 
                                           the loop */
    int32       nf; 	       	/* number of fields in this Vdata */
    int32       x, display;
    int32       temp;
    int32       addr_width = 0;
    int32       num_digits;
    int32       address = 0;
    int32       nfields;
    int32       cnt1, cnt2;
    int32       cn = 0;
    int32       ret_value = SUCCEED;
    char        fields[VSFIELDMAX*FIELDNAMELENMAX];
    char        flds[VSFIELDMAX*FIELDNAMELENMAX];

    /* inquire about vdata */
    if (FAIL == VSinquire(vd, &nv, &interlace, fields, &vsize, vdname))
      {
          ret_value = FAIL;
          goto done;
      }

    if (nv * vsize > BUFFER)	/* If the number of records in the vdata is 
                                   bigger than the buffer size, then divide
                                   the records into chunks. */
      {
          bufsize = BUFFER;
          chunk = BUFFER / vsize;
      }
    else
        /* Otherwise, all the records will be read in at one time. */
      {
          bufsize = nv * vsize;
          chunk = nv;
      }

    done = 0;
    /* Allocate space for the buffer and terminate hdp if allocation fails. */
    bb = (uint8 *) HDmalloc(bufsize);
    CHECK_ALLOC( fields, "fields", "dumpvd" );
    
    if (FAIL == VSsetfields(vd, fields))
      {
          fprintf(stderr,"dumpvd: VSsetfields failed for vd = %d \n",(int)vd);
          ret_value = FAIL;
          goto done;
      }

    w = vswritelist(vd);

    nf = w->n;
    x = 0;	/* Used for accessing the array storing the indices of the 
                   selected fields. */
    for (i = 0; i < nf; i++)	/* Read in data of all the fields. */
      {
          order[i] = w->order[i];

          /* Set offset for the next element. */
          off[i] = DFKNTsize(w->type[i] | DFNT_NATIVE);
          nattrs[i] = VSfnattrs(vd, i);
          if (FAIL == nattrs[i])
            {
                fprintf(stderr,"dumpvd: VSfnattrs failed for vd = %d \n",(int)vd);
                ret_value = FAIL;
                goto done;
            }

          /* Display the header of a vdata if the user didn't specify the
             data-only option. */
          if (!data_only)
          { 
             if(ff==DASCII)
             {
                if ((dumpallfields) || (flds_indices[x] == i))
                {
                   fprintf(fp, "- field index %d: [%s], type=%d, order=%d\n",
                          (int) i, w->name[i], w->type[i], w->order[i]);
                   x++;
                }
             }
             /* display attributes - BMR moved this block inside if(!data_only) 
		to keep the attributes from being printed - bug #231*/
             if (FAIL == dumpattr(vd, i, 1, ff, fp))
             {
                fprintf(stderr,"dumpvd: dumpattr() failed for vd = %d \n",(int)vd);
                ret_value = FAIL;
                goto done;
             }

          }	/* if !data_only */

          /* Choose a function for displaying a piece of data of a 
             particular type. */
          switch (w->type[i] & 0xff)
            {
            case DFNT_CHAR:
	    case DFNT_UCHAR:
                vfmtfn[i] = fmtchar;
                break;

            case DFNT_UINT8:
                vfmtfn[i] = fmtuint8;
                break;

            case DFNT_INT8:
                vfmtfn[i] = fmtint8;
                break;

            case DFNT_UINT16:
                vfmtfn[i] = fmtuint16;
                break;

            case DFNT_INT16:
                vfmtfn[i] = fmtint16;
                break;

            case DFNT_UINT32:
                vfmtfn[i] = fmtuint32;
                break;

            case DFNT_INT32:
                vfmtfn[i] = fmtint32;
                break;

            case DFNT_FLOAT32:
                vfmtfn[i] = fmtfloat32;
                break;

            case DFNT_FLOAT64:
                vfmtfn[i] = fmtfloat64;
                break;

            default:
                fprintf(stderr, "sorry, type [%d] not supported\n", (int) w->type[i]);
                ret_value = FAIL;
                goto done;

            }	/* switch */
      }		/* for */

    cn = 0;
    done = count = 0;
    
    if(ff==DASCII)
      { 

          /* If not just the data will be dumped out, then put an address-type
             column on the left so that the user can recognize which record 
             he/she is looking at. */
          if (!data_only)
            {
                temp = nv / 10;
                address = 0;
                addr_width = num_digits = 1;
                while (temp != 0)
                  {
                      if (temp != 1)
                          addr_width++;
                      temp = temp / 10;
                  }
                fprintf(fp, "Loc.");
                for (j = 0; j < addr_width - 3; j++)
                    fprintf(fp, " ");
                fprintf(fp, "     Data\n");

                /* The address of the first record is 0. Also, fill in the extra 
                   space on the left with 0's. */
                while (num_digits <= addr_width)
                  {
                      fprintf(fp, "0");
                      num_digits++;
                      cn++;
                  }
                fprintf(fp, "      ");
                cn += 6;
                if (addr_width == 2)
                  {
                      fprintf(fp, " ");
                      cn++;
                  }
                else if (addr_width == 1)
                  {
                      fprintf(fp, "  ");
                      cn += 2;
                  }
            }		/* while */

          nfields = VSgetfields(vd, flds);
          if (FAIL == nfields )
            {
                fprintf(stderr,"dumpvd: VSgetfields failed for vd = %d \n",(int)vd);
                ret_value = FAIL;
                goto done;
            }

          cnt1 = 0;
          cnt2 = 0;
          while (done != nv)
          {
              /* Determine the amount of data to be read this time. */
              if ((nv - done) > chunk)
                    count = chunk;
              else
                    count = nv - done;

              /* read and update bookkeeping */
              if (FAIL == VSread(vd, bb, count, interlace))
              {
		 /* If the data set has external element, get the external file
		 name to provide information */
		 intn extfile_namelen = VSgetexternalfile(vd, 0, NULL, NULL);
		 if (extfile_namelen > 0)
		 {
		    char *extfile_name = NULL;
		    extfile_name = (char *)HDmalloc(sizeof(char *)*(extfile_namelen+1));
		    CHECK_ALLOC(extfile_name, "extfile_name", "dumpvd" );

		    /* Get the external file info, we don't need offset here */
		    extfile_namelen = VSgetexternalfile(vd, extfile_namelen+1, extfile_name, NULL);
		    ERROR_GOTO_3( "in %s: VSread failed for vd(%d) with external file %s.  Please verify the file exists in the same directory.",
                        "dumpvd", (int)vd, extfile_name);
		 }
		 else
		    ERROR_GOTO_2( "in %s: VSread failed for vd(%d)",
                        "dumpvd", (int)vd );
                }

                done += count;
                b = bb;

                /* Display the data. */
                for (j = 0; j < count; j++)	/* each iteration causes one record 
                                               to be printed */
                  {
                      cnt1++;
                      x = 0;
                      for (i = 0; i < nf; i++)	/* display all fields in one record */
                        {
                            if ((!dumpallfields) && (flds_indices[x] != i))
                                display = 0;
                            else
                              {
                                  display = 1;
                                  x++;
                              }

                            for (t = 0; t < order[i]; t++)
                              {
                                  if(display)
                                      cn+=(vfmtfn[i]) (b, ff, fp);
                                  b += off[i];
                                  if (display)
                                    {
                                        fprintf(fp, " ");
                                        cn++;
                                        cnt2++;
                                    }
                              }
                            if (display)
                              {
                                  fprintf(fp, " ");
                                  cn++;
                                  cnt2++;
                              }
                        }		/* for i to nf-1 */

	   
                      if (cnt2 > 0)
                        {
                            address++;
                            /* "separator" is the symbol used for separating 
                               different records. */
                            fprintf(fp, "%s ", separater);
                        }

                      if (!data_only)
                        {
                            if ((cnt1 * cnt2) > 30)
                              {
                                  cnt1 = 0;
                                  cnt2 = 0;
                                  fprintf(fp, "\n");
                                  cn = 0;

                                  /* As long as there is data to be displayed,
                                     fill in the extra space with 0's on the left
                                     of each address. */
                                  if (j < (count - 1))
                                    {
                                        temp = address;
                                        num_digits = 1;
                                        while ((temp = temp / 10) != 0)
                                            num_digits++;
                                        while (num_digits < addr_width)
                                          {
                                              fprintf(fp, "0");
                                              num_digits++;
                                              cn++;
                                          }
                                        fprintf(fp, "%d      ", (int)address);
                                        cn += 6 + num_digits;
                                        if (addr_width == 2)
                                          {
                                              fprintf(fp, " ");
                                              cn++;
                                          }
                                        else if (addr_width == 1)
                                          {
                                              fprintf(fp, "  ");
                                              cn += 2;
                                          }
                                    }		/* if (!data_only) */
                              }
                        }
                      else
                          fprintf(fp, "\n");
                  }	/* for (j=0; j<count; j++) */
            }		/* while (done != nv) */

          /* ============================================ */

          HDfree((VOIDP) bb);
          bb = NULL;

          fprintf(fp, "\n\n");
      }  /*  for DASCII  */

    else
      {       /*  binary file  */
          nfields = VSgetfields(vd, flds);
          if (FAIL == nfields )
            {
                fprintf(stderr,"dumpvd: VSgetfields failed for vd = %d \n",(int)vd);
                ret_value = FAIL;
                goto done;
            }

          cnt1 = 0;
          cnt2 = 0; 
          while (done != nv)
            {
                /* Determine the amount of data to be read this time. */
                if ((nv - done) > chunk)
                    count = chunk;
                else
                    count = nv - done;

                /* read and update bookkeeping */
                if (FAIL == VSread(vd, bb, count, interlace))
                {
		   /* If the data set has external element, get the external
		      file name to provide information */
		   intn extfile_namelen = VSgetexternalfile(vd, 0, NULL, NULL);
		   if (extfile_namelen > 0)
		   {
		      char *extfile_name = NULL;
		      extfile_name = (char *)HDmalloc(sizeof(char *)*(extfile_namelen+1));
		      CHECK_ALLOC(extfile_name, "extfile_name", "dumpvd" );

		      /* Get the external file info, we don't need offset here */
		      extfile_namelen = VSgetexternalfile(vd, extfile_namelen+1, extfile_name, NULL);
		      ERROR_GOTO_3( "in %s: VSread failed for vd(%d) with external file %s.  Please verify the file exists in the same directory",
                        "dumpvd", (int)vd, extfile_name);
		   }
		   else
		      ERROR_GOTO_2( "in %s: VSread failed for vd(%d)",
                        "dumpvd", (int)vd );
                }

                done += count;
                b = bb;

                /* Display the data. */
                for (j = 0; j < count; j++)	/* each iteration causes one record 
                                               to be printed */
                  {
                      cnt1++;
                      x = 0;
                      for (i = 0; i < nf; i++)	/* display all fields in one record */
                        {
                            if ((!dumpallfields) && (flds_indices[x] != i))
                                display = 0;
                            else
                              {
                                  display = 1;
                                  x++;
                              }

                            for (t = 0; t < order[i]; t++)
                              {
                                  if(display)
                                      cn+=(vfmtfn[i]) (b, ff, fp);
                                  b += off[i];
                                  if (display)
                                    {
                           
                                        cn++;
                                        cnt2++;
                                    }
                              }
                            if (display)
                              {
                           
                                  cn++;
                                  cnt2++;
                              }
                        }		/* for i to nf-1 */      
                      if (cnt2 > 0)
                        {
                            address++;
                            /* "separator" is the symbol used for separating
                               different records. */
                        }

                  }	/* for (j=0; j<count; j++) */
            }		/* while (done != nv) */

          /* ============================================ */

          HDfree((VOIDP) bb);     
          bb = NULL;
      }   /* binary file */

done:
    if (ret_value == FAIL)
      { /* Failure cleanup */
          if (bb != NULL)
              HDfree((VOIDP)bb);
      }
    /* Normal cleanup */
    
    return ret_value;
}	/* dumpvd */
Пример #4
0
static int read_basic_type(const coda_cursor *cursor, void *dst)
{
    int32 start[MAX_HDF4_VAR_DIMS];
    int32 stride[MAX_HDF4_VAR_DIMS];
    int32 edge[MAX_HDF4_VAR_DIMS];
    long index;
    long i;

    index = cursor->stack[cursor->n - 1].index;

    assert(cursor->n > 1);
    switch (((coda_hdf4_type *)cursor->stack[cursor->n - 2].type)->tag)
    {
        case tag_hdf4_basic_type_array:
            {
                coda_cursor array_cursor;
                char *buffer;
                int native_type_size;
                long num_elements;

                /* we first read the whole array and then return only the requested element */

                array_cursor = *cursor;
                array_cursor.n--;

                if (coda_cursor_get_num_elements(&array_cursor, &num_elements) != 0)
                {
                    return -1;
                }
                assert(index < num_elements);
                native_type_size =
                    get_native_type_size(((coda_hdf4_type *)cursor->stack[cursor->n - 1].type)->definition->read_type);
                buffer = malloc(num_elements * native_type_size);
                if (buffer == NULL)
                {
                    coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
                                   (long)(num_elements * native_type_size), __FILE__, __LINE__);
                    return -1;
                }

                if (read_attribute(&array_cursor, buffer, -1) != 0)
                {
                    free(buffer);
                    return -1;
                }

                memcpy(dst, &buffer[index * native_type_size], native_type_size);
                free(buffer);
            }
            break;
        case tag_hdf4_attributes:
        case tag_hdf4_file_attributes:
            if (read_attribute(cursor, dst, -1) != 0)
            {
                return -1;
            }
            break;
        case tag_hdf4_GRImage:
            {
                coda_hdf4_GRImage *type;

                stride[0] = 1;
                stride[1] = 1;
                edge[0] = 1;
                edge[1] = 1;
                type = (coda_hdf4_GRImage *)cursor->stack[cursor->n - 2].type;
                if (type->ncomp != 1)
                {
                    uint8 *buffer;
                    int component_size;
                    int component_index;

                    component_size = get_native_type_size(type->basic_type->definition->read_type);

                    /* HDF4 does not allow reading a single component of a GRImage, so we have to first read all
                     * components and then return only the data item that was requested */
                    buffer = malloc(component_size * type->ncomp);
                    if (buffer == NULL)
                    {
                        coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
                                       (long)(component_size * type->ncomp), __FILE__, __LINE__);
                        return -1;
                    }
                    component_index = index % type->ncomp;
                    index /= type->ncomp;
                    /* For GRImage data the first dimension is the fastest running */
                    start[0] = index % type->dim_sizes[0];
                    start[1] = index / type->dim_sizes[0];
                    if (GRreadimage(type->ri_id, start, stride, edge, buffer) != 0)
                    {
                        coda_set_error(CODA_ERROR_HDF4, NULL);
                        free(buffer);
                        return -1;
                    }
                    memcpy(dst, &buffer[component_index * component_size], component_size);
                    free(buffer);
                }
                else
                {
                    /* For GRImage data the first dimension is the fastest running */
                    start[0] = index % type->dim_sizes[0];
                    start[1] = index / type->dim_sizes[0];
                    if (GRreadimage(type->ri_id, start, stride, edge, dst) != 0)
                    {
                        coda_set_error(CODA_ERROR_HDF4, NULL);
                        return -1;
                    }
                }
            }
            break;
        case tag_hdf4_SDS:
            {
                coda_hdf4_SDS *type;

                type = (coda_hdf4_SDS *)cursor->stack[cursor->n - 2].type;
                if (type->rank == 0)
                {
                    start[0] = 0;
                    edge[1] = 1;
                }
                else
                {
                    for (i = type->rank - 1; i >= 0; i--)
                    {
                        start[i] = index % type->dimsizes[i];
                        index /= type->dimsizes[i];
                        edge[i] = 1;
                    }
                }
                if (SDreaddata(type->sds_id, start, NULL, edge, dst) != 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
            }
            break;
        case tag_hdf4_Vdata_field:
            {
                coda_hdf4_Vdata *type;
                coda_hdf4_Vdata_field *field_type;
                int record_pos;
                int order_pos;

                assert(cursor->n > 2);
                type = (coda_hdf4_Vdata *)cursor->stack[cursor->n - 3].type;
                field_type = (coda_hdf4_Vdata_field *)cursor->stack[cursor->n - 2].type;
                order_pos = index % field_type->order;
                record_pos = index / field_type->order;
                if (VSseek(type->vdata_id, record_pos) < 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
                if (VSsetfields(type->vdata_id, field_type->field_name) != 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
                if (field_type->order > 1)
                {
                    /* HDF4 does not allow reading part of a vdata field, so we have to first read the full field and
                     * then return only the data item that was requested */
                    uint8 *buffer;
                    int element_size;
                    int size;

                    size = VSsizeof(type->vdata_id, field_type->field_name);
                    if (size < 0)
                    {
                        coda_set_error(CODA_ERROR_HDF4, NULL);
                        return -1;
                    }
                    buffer = malloc(size);
                    if (buffer == NULL)
                    {
                        coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)",
                                       (long)size, __FILE__, __LINE__);
                        return -1;
                    }
                    if (VSread(type->vdata_id, buffer, 1, FULL_INTERLACE) < 0)
                    {
                        coda_set_error(CODA_ERROR_HDF4, NULL);
                        free(buffer);
                        return -1;
                    }
                    /* the size of a field element is the field size divided by the order of the field */
                    element_size = size / field_type->order;
                    memcpy(dst, &buffer[order_pos * element_size], element_size);
                    free(buffer);
                }
                else
                {
                    if (VSread(type->vdata_id, (uint8 *)dst, 1, FULL_INTERLACE) < 0)
                    {
                        coda_set_error(CODA_ERROR_HDF4, NULL);
                        return -1;
                    }
                }
            }
            break;
        default:
            assert(0);
            exit(1);
    }

    return 0;
}
Пример #5
0
static int read_array(const coda_cursor *cursor, void *dst)
{
    int32 start[MAX_HDF4_VAR_DIMS];
    int32 stride[MAX_HDF4_VAR_DIMS];
    int32 edge[MAX_HDF4_VAR_DIMS];
    long num_elements;
    long i;

    if (coda_hdf4_cursor_get_num_elements(cursor, &num_elements) != 0)
    {
        return -1;
    }
    if (num_elements <= 0)
    {
        /* no data to be read */
        return 0;
    }

    switch (((coda_hdf4_type *)cursor->stack[cursor->n - 1].type)->tag)
    {
        case tag_hdf4_basic_type_array:
            if (read_attribute(cursor, dst, -1) != 0)
            {
                return -1;
            }
            break;
        case tag_hdf4_GRImage:
            {
                coda_hdf4_GRImage *type;

                type = (coda_hdf4_GRImage *)cursor->stack[cursor->n - 1].type;
                start[0] = 0;
                start[1] = 0;
                stride[0] = 1;
                stride[1] = 1;
                edge[0] = type->dim_sizes[0];
                edge[1] = type->dim_sizes[1];
                if (GRreadimage(type->ri_id, start, stride, edge, dst) != 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
            }
            break;
        case tag_hdf4_SDS:
            {
                coda_hdf4_SDS *type;

                type = (coda_hdf4_SDS *)cursor->stack[cursor->n - 1].type;
                if (type->rank == 0)
                {
                    start[0] = 0;
                    edge[0] = 1;
                }
                else
                {
                    for (i = 0; i < type->rank; i++)
                    {
                        start[i] = 0;
                        edge[i] = type->dimsizes[i];
                    }
                }
                if (SDreaddata(type->sds_id, start, NULL, edge, dst) != 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
            }
            break;
        case tag_hdf4_Vdata_field:
            {
                coda_hdf4_Vdata *type;
                coda_hdf4_Vdata_field *field_type;

                assert(cursor->n > 1);
                type = (coda_hdf4_Vdata *)cursor->stack[cursor->n - 2].type;
                field_type = (coda_hdf4_Vdata_field *)cursor->stack[cursor->n - 1].type;
                if (VSseek(type->vdata_id, 0) < 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
                if (VSsetfields(type->vdata_id, field_type->field_name) != 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
                if (VSread(type->vdata_id, (uint8 *)dst, field_type->num_records, FULL_INTERLACE) < 0)
                {
                    coda_set_error(CODA_ERROR_HDF4, NULL);
                    return -1;
                }
            }
            break;
        default:
            assert(0);
            exit(1);
    }

    return 0;
}
Пример #6
0
//// [[Rcpp::export]]
int binlist(int x) {
 int     numbins = 0, *binnums = NULL;
  int     i;
  int     file_id,vdata_ref, vdata_id,numrecs,pvd_id;
  char PARAM[] = "chlor_a";
  initbin();
  
  /* Open the HDF file. */
  file_id = Hopen("S1998001.L3b_DAY_CHL.main", DFACC_READ, 0);
  if(file_id == FAIL){
    fprintf(stderr,"-E- %s line %d: Hopen(%s,DFACC_READ,0) failed.\n",
      __FILE__,__LINE__,"S1998001.L3b_DAY_CHL.main");
    return(EXIT_FAILURE);
  }
  /* Initialize the Vdata interface. */
  if(Vstart(file_id) == FAIL){
    fprintf(stderr,"-E- %s line %d: Vstart(%d) failed.\n",
	    __FILE__,__LINE__,file_id);
    return(EXIT_FAILURE);
  }
  
  /* Open the "BinList" Vdata. */
  vdata_ref = VSfind(file_id,"BinList");
  if(vdata_ref == FAIL){
    fprintf(stderr,"-E- %s line %d: VSfind(%d,\"BinList\") failed.\n",
	    __FILE__,__LINE__,file_id);
    return(EXIT_FAILURE);
  }
  
  vdata_id = VSattach(file_id, vdata_ref, "r");
  if(vdata_id == FAIL){
    fprintf(stderr,"-E- %s line %d: VSattach(%d,%d,\"r\") failed.\n",
      __FILE__,__LINE__,file_id,vdata_ref);
    return(EXIT_FAILURE);
  }
  /* Find out how many bins are stored in this file. */
  numrecs = VSelts(vdata_id);
  if(numrecs == FAIL){
    fprintf(stderr,"-E- %s line %d: VSelts(%d) failed.\n",
	    __FILE__,__LINE__,vdata_id);
    return(EXIT_FAILURE);
  }
  
  // this doesn't seem to be necessary?
  /* Set up to read the fields in the BinList Vdata records. */
//  if(VSsetfields(vdata_id,BLIST_FIELDS) == FAIL){
//    fprintf(stderr,"-E- %s line %d: VSsetfields(%d,%s) failed.\n",
//	    __FILE__,__LINE__,vdata_id,BLIST_FIELDS);
//    return(EXIT_FAILURE);
//  }

  /* Open the parameter-specific Vdata. */
  vdata_ref = VSfind(file_id,PARAM);
  if(vdata_ref == 0){
    fprintf(stderr,"-E- %s line %d: VSfind(%d,\"%s\") failed.\n",
	    __FILE__,__LINE__,file_id,PARAM);
    return(EXIT_FAILURE);
  }
  pvd_id = VSattach(file_id, vdata_ref, "r");
  if(pvd_id == FAIL){
    fprintf(stderr,"-E- %s line %d: VSattach(%d,%d,\"r\") failed.\n",
	    __FILE__,__LINE__,file_id,vdata_ref);
    return(EXIT_FAILURE);
  }
    /* Set up to read the fields in the parameter-specific Vdata records. */
  {
    int len;
    len = 2*strlen(PARAM) + strlen("_sum,") + strlen("_sum_sq") + 1;
    param_fields = (char *)malloc(len);
    if(param_fields == NULL){
      fprintf(stderr,"-E- %s line %d: Memory allocation failed.\n",
        __FILE__,__LINE__);
      return(EXIT_FAILURE);
    }
    strcpy(param_fields,PARAM);
    strcat(param_fields,"_sum,");
    strcat(param_fields,PARAM);
    strcat(param_fields,"_sum_sq");
  }
  if(VSsetfields(pvd_id,param_fields) == FAIL){
    fprintf(stderr,"-E- %s line %d: VSsetfields(%d,%s) failed.\n",
	    __FILE__,__LINE__,pvd_id,param_fields);
    return(EXIT_FAILURE);
  }

  printf(param_fields);
  printf("\n");





  /* Output a header record to identify the fields written out below. */
  printf("%80s%15.15s %15.15s\n"," ",PARAM,PARAM);
  printf("    bin centerlat  centerlon");
  printf("     north     south       west       east");
  printf("    n   N         sum_obs sum_squared_obs          weight");
  printf("  time_trend_bits                     l2_flag_bits sel\n");
  printf("------- --------- ----------");
  printf(" --------- --------- ---------- ----------");
  printf(" ---- --- --------------- --------------- ---------------");
  printf(" ---------------- -------------------------------- ---\n");
 
 // for(i = 0; i < numbins; i++){
  for(i = 0; i < 10; i++){
    int   recno;

   // recno = binsearch(binnums[i],vdata_id,numrecs);
    recno = i;
    if(recno >= 0){
      double    n,s,w,e,clat,clon;

      /*
      Read the sum and sum-of-squares for the
      the specified parameter for this bin.
      */
      if(VSseek(pvd_id,recno) == FAIL){
        fprintf(stderr,"-E- %s line %d: VSseek(%d,%d) failed.\n",
  	__FILE__,__LINE__,pvd_id,recno);
	return(EXIT_FAILURE);
      }
      if(VSread(pvd_id,paramrec,1,FULL_INTERLACE) != 1){
        fprintf(stderr,"-E- %s line %d: ",__FILE__,__LINE__);
	fprintf(stderr,"VSread(%d,paramrec,1,FULL_INTERLACE) failed.\n",
		pvd_id);
        return(EXIT_FAILURE);
      }
      /*
      VSfpack() sets the global sum and sum_sq variables
      via the paramptrs pointer array.
      */
      if(
	 VSfpack(
		 pvd_id,_HDF_VSUNPACK,param_fields,paramrec,PREC_SIZE,1,NULL,paramptrs
		 )
	 == FAIL){
        fprintf(stderr,"-E- %s line %d: ",__FILE__,__LINE__);
	fprintf(stderr,"VSfpack(%d, ...) failed.\n", pvd_id);
	return(EXIT_FAILURE);
      }

      /* Get the geographical coordinates associated with this bin. */
    //  bin2latlon(binnums[i],&clat,&clon);
    //  bin2bounds(binnums[i],&n,&s,&w,&e);

      /* Output the results. */
    //  printf("%7d %9.5f %10.5f %9.5f %9.5f %10.5f %10.5f ",
	  //   binnums[i],clat,clon,n,s,w,e);
    //  printf("%4d %3d ",nobs,nscenes);
    //  printf("% .8e % .8e % .8e ",sum,sum_sq,weights);
      printf("% .8e % .8e % .8e ",sum,sum_sq,0.0);
   //   printf("%.16s %.32s ",bitstr16(time_rec),bitstr32(flags_set));
   //   printf("%3d",sel_cat);
      printf("\n");
    }

  }

  if(VSdetach(pvd_id) == FAIL){
    fprintf(stderr,"-E- %s line %d: VSdetach(%d) failed.\n",
	    __FILE__,__LINE__,pvd_id);
    return(EXIT_FAILURE);
  }
  if(VSdetach(vdata_id) == FAIL){
    fprintf(stderr,"-E- %s line %d: VSdetach(%d) failed.\n",
	    __FILE__,__LINE__,vdata_id);
    return(EXIT_FAILURE);
  }
  if(Vend(file_id) == FAIL){
    fprintf(stderr,"-E- %s line %d: Vend(%d) failed.\n",
	    __FILE__,__LINE__,file_id);
    return(EXIT_FAILURE);
  }
  if(Hclose(file_id) == FAIL){
    fprintf(stderr,"-E- %s line %d: Hclose(%d) failed.\n",
	    __FILE__,__LINE__,file_id);
    return(EXIT_FAILURE);
  }

  free(param_fields);
  free(binnums);






   return 2;
}
int main( )
{
   /************************* Variable declaration **************************/

   intn   status_n;     /* returned status for functions returning an intn  */
   int32  status_32,    /* returned status for functions returning an int32 */
          file_id, vgroup_id, vgroup_ref, vdata_id, vdata_ref,
	  num_of_records, num_of_vals_per_rec, record_pos,
          obj_index,    /* index of an object within a vgroup */
          num_of_pairs, /* number of tag/ref number pairs, i.e., objects */
          obj_tag, obj_ref,     /* tag/ref number of an HDF object */
          vgroup_pos = 0;       /* position of a vgroup in the file */
   float32 databuf[N_RECORDS][N_VALS_PER_REC];
   int16 rec_num;

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

   char FIELDNAME_LIST[MAX_FIELDNAME_LIST_LENGTH];
   memset(FIELDNAME_LIST, 0x00, MAX_FIELDNAME_LIST_LENGTH);
   int i;
   for (i = 0; i < N_VALS_PER_REC; i++) {
	   char buffer[256];
	   if (i == 0) {
		   sprintf(buffer, "%d", i);
	   } else {
		   sprintf(buffer, " %d", i);
	   }
	   strcat(FIELDNAME_LIST, buffer);
   }
   file_id = Hopen (FILE_NAME, DFACC_READ, 0);

   status_n = Vstart (file_id);

   vgroup_ref = -1;

   vgroup_ref = Vgetid (file_id, vgroup_ref);
   if (vgroup_ref == -1) exit(1);
   vgroup_ref = Vgetid (file_id, vgroup_ref);
   if (vgroup_ref == -1) exit(1);

   vgroup_id = Vattach (file_id, vgroup_ref, "r"); 

   num_of_pairs = Vntagrefs (vgroup_id);

   if (num_of_pairs > 0)
   {
	   printf ("\nVgroup #%d contains:\n", vgroup_pos);
	   for (obj_index = 0; obj_index < num_of_pairs; obj_index++)
	   {
		   status_n = Vgettagref (vgroup_id, obj_index, &obj_tag, &obj_ref);
		   printf ("tag = %d, ref = %d", obj_tag, obj_ref);


		   if (Visvg (vgroup_id, obj_ref))
			   printf ("  <-- is a vgroup\n");
		   else if (Visvs (vgroup_id, obj_ref)) {
			   printf ("  <-- is a vdata\n");
			   vdata_ref = VSfind (file_id, VDATA_NAME);
			   vdata_id = VSattach (file_id, vdata_ref, "r");
			   status_n = VSsetfields (vdata_id, FIELDNAME_LIST);
			   record_pos = VSseek (vdata_id, RECORD_INDEX);
			   num_of_records =
				   VSread (vdata_id, (uint8 *)databuf, N_RECORDS, FULL_INTERLACE);
			   for (rec_num = 0; rec_num < num_of_records; rec_num++)
			   {
				   for (num_of_vals_per_rec = 0; num_of_vals_per_rec < N_VALS_PER_REC; num_of_vals_per_rec++) {
					   printf ("%6.2f\n", databuf[rec_num][num_of_vals_per_rec]);

				   }
			   }
		   }
		   else {
			   printf ("  <-- neither vdata nor vgroup\n");
		   }
	   } /* for */
   } /* if */

   else
	   printf ("Vgroup #%d contains no HDF objects\n", vgroup_pos);

   /*
    * Terminate access to the current vgroup.
    */
   status_32 = Vdetach (vgroup_id);

   /*
    * Move to the next vgroup position.
    */
   vgroup_pos++;

   /*
   * Terminate access to the V interface and close the file.
   */
   status_n = Vend (file_id);
   status_n = Hclose (file_id);
   return 0;
}