Пример #1
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);

}
Пример #2
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;
}