This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
> Date: Mon, 18 Nov 1996 16:43:22 -0800 > From: address@hidden (P.J.Flatau) > To: address@hidden > Subject: netCDF Hi Piotr, > I just wanted to use NetCDF to write complex array. I have noticed > your recent reply to similar question. > > Clearly, there are applications where complex numbers are important > (take for example spectral GCM models which uses complex FFT). > > Anyway, what is your prescription for writting complex numbers. > > I guess the question about writting complex number reduces to > how do I write 2 real arrays, i.e. how do I skip every second > element. If there was istart, icount, iskip --- it would be easy. Well, one way to do it is to just define an n by 2 netCDF variable of type real: netcdf oct { dimensions: n = 8 ; ri = 2 ; // real (1) or imaginary (2) part variables: float c(n, ri) ; // eighth roots of 1 in complex plane data: c = 1, 0, 0.7071068, 0.7071068, 0, 1, -0.7071068, 0.7071068, -1, 0, -0.7071068, -0.7071068, 0, -1, -0.7071068, 0.7071068 ; } From FORTRAN, you should just be able to access complex arrays directly this way, without any kind of skipping, since the storage order for a complex array is with the real and imaginary parts of each value in successive locations. Thus from FORTRAN with the above structure, you should be able to read all eight complex values into a single complex array with one call to NCVGT: COMPLEX C(8) INTEGER NCID, VARID, RCODE ... CALL NCVGT(NCID, VARID, 1, 8, C, RCODE) and similarly for higher-dimensional arrays. If you needed to deal with many complex arrays this way, it would probably be a good idea to adopt a conventional variable attribute to identify the variables as complex, so that programmers in C or other languages with no complex type could understand your intent, e.g. float:type = "complex"; If you want to skip every second element and just read the real parts, for example, use NCVGTG and specify the stride as 2. > More general comment: why not provide some simple > FORTRAN routines which would elimiminate need to use > "id" variables in the same way as it is done in IDL > (i.e. using character name). It is difficult to use NetCDF > as it is. > > E.g. > > ncdf_varget,idnc,'wavea',wavea Good question. We had thought about doing it this way, and the original HDF SDS interface used variable names instead of variable IDs, for example. The reason we don't do this in the FORTRAN and C interfaces is efficiency. If you use long variable names and have lots of variables, each I/O access would require looking up an offset by variable name, which even in the best case (hashing on the variable string and using the hash index for direct lookup) would require extra time proportional to the length of the variable name string on every data access. With a less sophisticated lookup, it would require additional time proportional to the number of variables, or the log of the number of variables if they are kept sorted. This extra time could be significant lots of small accesses. In the C++ interface and the upcoming Java interface, no variable ID is required, since each variable is just an object and the variable referring to the object can be used directly. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu