4
$\begingroup$

Consider I have a scalar field on a grid f(x,y), where x and y can take values

x={1,2}
y={1,2,3}

so the grid points are

{{1,1},{1,2},{1,3},{2,1},{2,2},{2,3}}

and the value of f on these grid points are

f={1,2,3,2,4,6}

How can I export this into a NetCDF file that has a structure similar to

netcdf foo {

     dimensions:
       x = 2, y = 3;

     variables:
       int x(x), y(y);
       int f(x,y);
     data:
       x   = 1,2;
       y   = 1,2,3;
       f   = 1,2,3,2,4,6;
     }

I tried to follow the examples in the NetCDF format documentation page, but not able to make it work.

For example, if we try

path = Export["test.nc", {"x" -> x, "y" -> y, "f" -> f}];
Import["!ncdump " <> path, "Text"]

The exported NetCDF file looks like:

netcdf test {
dimensions:
    xDim1 = 2 ;
    yDim1 = 3 ;
    fDim1 = 6 ;
variables:
    int x(xDim1) ;
    int y(yDim1) ;
    int f(fDim1) ;
data:

 x = 1, 2 ;
 y = 1, 2, 3 ;
 f = 1, 2, 3, 2, 4, 6 ;
}

We can see that Mathematica exports all three variables independently, rather than keep f as a dependent variable on x and y. The dependence of the variables will be very important because it reflects the structure of the data.

So how can I export the data correctly so that it generate the linked variables, as in the first example?

Note that the NetCDF file can be viewed using ncdump.


update

Confirmed by Wolfram Technical Support as a bug.

$\endgroup$
  • $\begingroup$ I wonder, should your definition of the $y$ range in the second to last line be y = 1, 2, 3 in keeping with your example? Could you explain where the 10 comes from otherwise? $\endgroup$ – MarcoB Aug 27 '15 at 4:16
  • $\begingroup$ @MarcoB Good catch! I correct that typo. Thanks. $\endgroup$ – xslittlegrass Aug 27 '15 at 4:18
  • $\begingroup$ I've deleted my answer as it didn't address the dependence of the dimensions of f on those of x and y. Perhaps you should make this requirement more explicit in your question. $\endgroup$ – MikeLimaOscar Aug 27 '15 at 16:28
  • $\begingroup$ @MikeLimaOscar Thanks! I made the question more clearly now. $\endgroup$ – xslittlegrass Aug 27 '15 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.