what's the best method to compute weight value for arbitrary data format?

Hi group, sometimes there is a need to compute a weight array when 
averaging netCDF result. For example, for data(z,y,x)

weight(i,j,k) = wx(i)*wy(j)*wz(k)

but for data(z,x), it's
weight(i,k) = wx(i)*wz(k)

This variable weight has to be constructed during run time without any 
assumption of the underlying data organization (data(t,x), data(t,z) 
etc.). I have an algorithm to compute this variable dynamicsally but it's 
not very efficient. What suggestions do you have?

Fei Liu, PhD.                    phone: 609-4526551
RSIS/GFDL/NOAA                   fax:   609-9875063
P.O. Box 308, 201 Forrestal Rd.  http://www.gfdl.noaa.gov/~fil
Princeton, NJ 08542-0308         email: Fei.Liu@xxxxxxxx