1
$\begingroup$

I previously asked for help with accessing metadata in .HDR (version 4) files. I am obliged to work also with NetCDF files, with which I am only briefly acquainted. (I am aware that my question touches on previous queries but requires further clarification.) I could read data from these files, every element, attribute and all metadata appear correct as in example [1], which lists all the imported elements. Elements read from a NetCDF file.

The process of importing data here is broken apart for clarity. On line 4 I have limited data import to 3 specific records, the first of which is shown at the end of the listing (netCDFdatasets...). The problem I am having is in exporting this reduced dataset back to NetCDF format. When I import the file that I just exported (NetCDF_Testing-Output.nc), annotations are missing ({{},{},{}}), the data format is incorrect (Real64 instead of Real32) and the metadata are missing. The data and dimensions are exported correctly, as shown in example [2]. Exported NetCDF formatted data

When I attempt to add sophistication (likely quite naive but which doesn't generate any export errors that are annunciated) as in example [3], the situation is no better. NetCDF data export attempt with explicit directions for annotations etc.

My question therefore, is whether Mathematica (version 11.1.1.0) is capable of exporting those data that I have so far been unable to include in my NetCDF file?

$\endgroup$
  • $\begingroup$ if you want anyone to try running your code better post it as text, not images. $\endgroup$ – High Performance Mark Feb 17 at 14:03
  • $\begingroup$ Thank you. I guessed that due to the lack of sophistication in my code and the base nature of my query, someone familiar with the issue would have the answer to hand. Also, I am not at liberty to share the source file - not an issue of national security though (lol). $\endgroup$ – David H. Feb 17 at 14:10
  • $\begingroup$ You misunderstand. The images of code you provided in your question is not code that can be copied and pasted into a Mathematica notebook for testing. $\endgroup$ – Somos Feb 17 at 22:59
  • $\begingroup$ I didn't intend the code to be copied into Mathematica for testing because the source file is unavailable. If it would help significantly, I could investigate but the NetCDF file is about 150 MB in size. Thanks. $\endgroup$ – David H. Feb 17 at 23:02
2
$\begingroup$

Your Export call is almost correct - you are trying to use "rules syntax" but forgot the "Rules" keyword as the third argument to Export. Since your code is non-copyable, I will use a simple example below:

Export["test.nc", {
    "Datasets" -> { 
     (* list of rules "dataset name" -> data *)
        "myDataset" -> Range[10], 
        "dset2" -> Range[5]
    },
    "Annotations" -> { 
    (* annotations per dataset *)
        {"long_name" -> "xxxxx", "fillValue" -> 5}, 
        "someKey" -> "someValue"
    }, 
    "Metadata" -> { 
    (* any number of rules with String keys *)
        "title" -> "Sample NetCDF file", 
        "My favourite number" -> 17
    }, 
    "DataFormat" -> { 
    (* one format per dataset *)
        "Integer8", 
        "Real32"
    }

}, "Rules"] (* finally, the "Rules" keyword as third argument *)

"Rules" can also be used as Import element. For the file exported above

In[65]:= Import["test.nc", "Rules"]

Out[65]= 
{"Annotations" -> {{"long_name" -> "xxxxx", "fillValue" -> 5}, {"someKey" -> "someValue"}}, 
 "Data" -> {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1., 2., 3., 4., 5.}}, 
 "DataFormat" -> {"Integer8", "Real32"}, 
 "Datasets" -> {"myDataset", "dset2"}, "Dimensions" -> {{10}, {5}}, 
 "Metadata" -> {"title" -> "Sample NetCDF file", 
   "My_favourite_number" -> 17}}
$\endgroup$
  • $\begingroup$ This looks amazing, thank you very much. I've been away from my desk and so haven't coded my version yet but will be onto it and will let you know how it goes. Thanks again. $\endgroup$ – David H. Feb 18 at 8:55
  • $\begingroup$ Thank you so much @rafalc for your solution, which works completely. I had a later version of the code in image (3) that included the keyword "Rules" but I had missed the keyword "Datasets" at the beginning, so it had failed also. Thanks too, to the community for viewing this query. Best wishes all. Very, very happy to close this thread. $\endgroup$ – David H. Feb 18 at 13:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.