[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re[2]: 960716: extracting data from a netcdf file



>To: "support(a)unidata.ucar.edu" <address@hidden>
>Keywords: 199607161033.AA03373

Rahul,

>  Thanks a zillion for the quick reply. I do have the NetCDF users guide
> with me and have gone through the chapters on "Reading a NetCDF File with
> Known Names" and "Reading and Writing Character String Values". However I
> still could not figure out the method of initialing the 'start' and
> 'count' parameters in the 'ncvarget' function to read character values.
> 
> I have attached a 'C' test file and the .cdf file for your perusal. The
> peak.c file is a test file to help extract the "peak_name" values from
> the 000210-1.cdf file.
> 
> Please let me know where I gone wrong in this code.

First, please send questions to "address@hidden" rather than my
email address, as that way they have a better chance of getting answered.

To look at your program, I had to first undo the MIME encoding, then remove
control characters at the end of each line and insert new-lines in place of
null-characters.  Whatever was used to encode the ASCII source of the
program required non-trivial decoding.  The netCDF file also did not come
through the encoding process unscathed; it had some sort of header
preceding the actual netCDF binary file.

I've added comments (prefixed by "rkr:") to the statements I had to change
to get it to run.  I hope this gets you started down the right track.  We
don't have the resources to provide this much support for every netCDF
problem.

I also suspect you may be using an older version of netCDF than we
currently support, since you used an "int*" argument where the current
version requires a "long*" in ncdiminq, but if you are still using the old
version, the "int*" will probably work OK.

--Russ

/* include files */
/* ------------- */
#include <stdio.h>
#include <ctype.h>
#include <limits.h>

#include <float.h>
#include <netcdf.h>
 
int main(int argc,char *argv[]){
  int i;        
  int ii;
  int ncid;                 
  int peak_dim_id;
  int peak_name_varid;
  long peak_dim_size;         /* rkr: in netCDF version 2.x, dim sizes are 
longs */
  int peak_dim_size_peak_name;
  int ndims_peak_name;
  int dimids_peak_name[2];      /* rkr: peak_name only has 2 dimensions */
  int natts_peak_name;

  long start_peak_name[2]= {0, 0}; /* rkr: peak_name only has 2 dimensions  */
  long count_peak_name[2]= {17,32}; /* rkr: want all peak_number, 
_32_byte_string bytes */
  char peak_name_values[17][32]; /* rkr: peak_number by _32_byte_string */

   /* Open the CDF file */
  /* ------------------*/
    ncid = ncopen("000210-1.CDF",NC_NOWRITE);
    if (ncid == -1){
        printf("ERROR OPENING FILE");
        exit(1);}

  /* Get the Dimension size for peak number */
 /*  --------------------------------------*/
    peak_dim_id = ncdimid(ncid,"peak_number");
    ncdiminq(ncid,peak_dim_id,0,&peak_dim_size);

/*  Here's where I have the probelem with initializing */
/*  --------------------------------------------------*/

   /* rkr: These for loops are unnecessary, so commented out
      for(i=0;i<17;i++){
        for(ii=0;ii<17;ii++)
            start_peak_name[i][ii]=0;}
    rkr: */
    peak_name_varid = ncvarid(ncid,"peak_name");
    /* rkr: not sure why this ncvarinq call is here, since you aren't using the
       info it returns */
    
ncvarinq(ncid,peak_name_varid,0,0,&ndims_peak_name,dimids_peak_name,&natts_peak_name);

    ncvarget(ncid,peak_name_varid,start_peak_name,count_peak_name,
             &peak_name_values[0][0]); /* rkr: address of beginning of array */

    /* Output the Peak name values */
    /* ---------------------------*/ 
    printf("\n\nPEAK NAMES VALUES ARE:");
    for(i=0;i<peak_dim_size;i++)
        printf("\n%s",peak_name_values[i]);

    ncclose(ncid);
}