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


so the grid points are


and the value of f on these grid points are


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

netcdf foo {

       x = 2, y = 3;

       int x(x), y(y);
       int f(x,y);
       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 {
    xDim1 = 2 ;
    yDim1 = 3 ;
    fDim1 = 6 ;
    int x(xDim1) ;
    int y(yDim1) ;
    int f(fDim1) ;

 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.


Confirmed by Wolfram Technical Support as a bug.

  • $\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.