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

Re: Bug dedected ?



>To: Russ Rew <address@hidden>
>From: Adrianos Lahanas <address@hidden>
>Subject: Bug dedected
>Organization: .
>Keywords: 199707251438.IAA29699

Hi Adrianos,

> I checked again the way you told me but it didn't help.
> The problem is in retreiving data.
> I run the ncdump program to retreive the data and it seems fine.
> With "ncdump -v rhum rhum.79.nc" command I get all data connected with
> rhum variable. But when I try to run my program (in C ) it makes
> a segmentation fault.
> 
> Could you run the below program with this particular file ?
> The file I use is the  "rhum.79.nc". I got it from 
> ftp://ftp.cdc.noaa.gov/Datasets/nmc/ and it is 53MBytes.
> 
> You can retreive it via anonymous ftp.
> 
> Does it work fine in your system ?

I think I can see what is wrong without even trying to run the program.
You are trying to read in more data than you have allocated space for in
your array:

> > #define TIMES 2
> > #define LON 144
> > #define LAT 73
> > #define LEVEL 7
> > 
> > 
> > void handle_error(int status);
> > 
> > main(argc, argv)
> > int argc;
> > char *argv[];
> > {
> >   int status;
> >   int rh_id, ncid;
> >   short rhum_val[LEVEL*TIMES*LON*LAT];
> >   int counter;

So your rhum_val array has enough space to hold data for 2 times
(TIMES==2).

But then later you try to read in the data for all 365 times:

> >         status = nc_get_var_short(ncid, rh_id, rhum_val);

The function nc_get_var_short() is to get *all* the values of a variable
from a netCDF file.  The file you are reading from has time as an
unlimited dimension that currently has size 365:

    $ ncdump -h rhum.79.nc
    netcdf rhum.79 {
    dimensions:
            lon = 144 ;
            lat = 73 ;
            level = 7 ;
            time = UNLIMITED ; // (365 currently)
    variables:
  ...
            short rhum(time, level, lat, lon) ;
  ...

so you are reading 365*7*73*144 values into an array that can only hold
2*7*73*144 values, resulting in a segmentation violation when the array
is overrun.

If you only want to read part of the data, you need to use a different
netCDF function, for example nc_get_vara_short().  The following would
read data for only the first two times into the array:

     size_t start[] = {0, 0, 0, 0};
     size_t count[] = {2, LEVEL, LAT, LON};
     status = nc_get_vara_short(ncid, rh_id, start, count, rhum_val);
     
I hope this helps.

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
address@hidden                     http://www.unidata.ucar.edu