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

Re: 20030409:How to extract a slice from a netCDF file



>To: address@hidden
>From: "Hongfei Tian" <address@hidden>
>Subject: Re: 20030428: How to extract a slice from a netCDF file
>Organization: MIT
>Keywords: 200304091902.h39J2V7U024167

Hongfei,

> Thank you very much, Mr. Rew,
> 
> I am a beginner of C program and don't know how to compile. Do you know
> where I can get a compiled NCO?

No, sorry, you might have to inquire of the NCO author (Charlie Zender
<address@hidden>) whether he knows where an already-compiled version
is.  If you want it compiled for Windows, he may not have one, since I
believe he does his development on Unix.

As an alternative, if the file you want to slice data from is not too
large, you could use the ncdump utility that comes with netCDF to
create an ASCII version of the file that is annotated, and use a
different tool to extract the data values you want by matching the
text in the annotations.  Compiled versions of ncdump are available
with any binary version of netCDF.

For example, in the netcdf file represented by

 netcdf rr {
 dimensions:
         lon = 4 ;
         lat = 3 ;
         time = 2 ;
 variables:
         float var(time, lat, lon) ;
 data:
         var = 
           1, 2, 3, 4, 
           5, 6, 7, 8,
           9, 10, 11, 12,
           13, 14, 15, 16,
           17, 18, 19, 20,
           21, 22, 23, 24;      
 }

there is one 3-dimensional variable, var.  The above is the normal
output of ncdump for the netCDF file rr.nc.  However, if you specify
"full annotations using C subscripting", you get a comment on each
value that shows the indices, using

 ncdump -fc rr.c
 netcdf rr {
 dimensions:
         lon = 4 ;
         lat = 3 ;
         time = 2 ;
 variables:
         float var(time, lat, lon) ;
 data:

  var =
   1,  // var(0,0,0)
     2,  // var(0,0,1)
     3,  // var(0,0,2)
     4,  // var(0,0,3)
     5,  // var(0,1,0)
     6,  // var(0,1,1)
     7,  // var(0,1,2)
     8,  // var(0,1,3)
     9,  // var(0,2,0)
     10,  // var(0,2,1)
     11,  // var(0,2,2)
     12,  // var(0,2,3)
     13,  // var(1,0,0)
     14,  // var(1,0,1)
     15,  // var(1,0,2)
     16,  // var(1,0,3)
     17,  // var(1,1,0)
     18,  // var(1,1,1)
     19,  // var(1,1,2)
     20,  // var(1,1,3)
     21,  // var(1,2,0)
     22,  // var(1,2,1)
     23,  // var(1,2,2)
     24;  // var(1,2,3)
     }

If you stored the above text file in rr.cdl, you could get a slice of
values using text matching on the annotations.  For example if you
wanted the var(*,1,*) slice, you could use a pattern matching program
like grep:

 grep "var(.*,1,.*)" rr.cdl
    5,  // var(0,1,0)
    6,  // var(0,1,1)
    7,  // var(0,1,2)
    8,  // var(0,1,3)
    17,  // var(1,1,0)
    18,  // var(1,1,1)
    19,  // var(1,1,2)
    20,  // var(1,1,3)

to get the 8 values in that slice.

This is pretty hokey, and would be impractical for very large
datasets.  The practicality threshold would be somewhat higher if you
used "brief annotations using C subscripting", as in

 ncdump -bc rr.nc
 netcdf rr {
 dimensions:
         lon = 4 ;
         lat = 3 ;
         time = 2 ;
 variables:
         float var(time, lat, lon) ;
 data:

  var =
   // var(0,0, 0-3)
     1, 2, 3, 4,
   // var(0,1, 0-3)
     5, 6, 7, 8,
   // var(0,2, 0-3)
     9, 10, 11, 12,
   // var(1,0, 0-3)
     13, 14, 15, 16,
   // var(1,1, 0-3)
     17, 18, 19, 20,
   // var(1,2, 0-3)
     21, 22, 23, 24 ;
 }

--Russ