NetCDF reader and export to ASCII

Easy to use functions that import NetCDF data to matlab and convert them to ASCII.
966 Downloads
Updated 5 May 2015

View License

This set of functions aims for those that are not necessarily familiarized with either Matlab or NetCDF handling.
The first function imports data (NetCDFreader) into matlab and the second (NetCDF2ASCII) converts these variables to ASCII.
Both functions have been tested with the WMO SDS-WAS products, however they may be used with any NetCDF fie.
How to use:
1. Assuming that all NetCDF files are located in a folder located in “C:\Users\admin\Desktop\MyNetCDF_FOLDER”, this location must be assigned as a variable (Note: Matlab commands are case sensitive).
>> fpath=' C:\Users\admin\Desktop\MyNetCDF_FOLDER ';
2. The folder where the two functions have been saved must both appear in the “Current Folder” window of Matlab.
3. >> [header] = NetCDFreader( fpath)
If the run is successful it will yield a report in the command window similar to the one found below.
-----------------------------------------------------------------------------------------------------
Files found: 2
File 1 20140907_BSC_DREAM8b_V2.nc
(1) longitude: longitude (500)
(2) latitude: latitude (250)
(3) level: level (24)
(4) time: time (25)
(5) OD550_DUST: longitude-latitude-time (500x250x25)
(6) SCONC_DUST: longitude-latitude-time (500x250x25)
(7) LOAD_DUST: longitude-latitude-time (500x250x25)
(8) EMI_DUST: longitude-latitude-time (500x250x25)
File 2 netcdf-atls03-20140915102130-63602-0557.nc
(1) longitude: longitude (320)
(2) latitude: latitude (161)
(3) time: time (14)
(4) aod550: longitude-latitude-time (320x161x14)
-----------------------------------------------------------------------------------------------------
This report indicates that two files were identified; 20140907_BSC_DREAM8b_V2.nc (file #1) and netcdf-atls03-20140915102130-63602-0557.nc (file #2).
The variables of each file are also displayed. The first file includes 8 variables and the second 4.

4. Depending on which variable the user wants to export as ASCII he may use the NetCDF2ASCII function as shown below
>> NetCDF2ASCII( header,PositionOfFile,PositionOfVariable)
Which will a create a file named NetcdfFileName_VariableName.txt
Following the example above, if someone wants to export dust load (variable # 7) located in file #1 then the command will be
>> NetCDF2ASCII( header,1,7)
This will create a file in the current directory named
“20140907_BSC_DREAM8b_V2_LOAD_DUST.txt”
The output contains the corresponding latitude, longitude, and time for each dust load. The order is dictated by the NetCDF file itself. However, the user does not need to guess since it is indicated in the report above by the numbers in the parenthesis.

Cite As

Michael Pikridas (2024). NetCDF reader and export to ASCII (https://www.mathworks.com/matlabcentral/fileexchange/48724-netcdf-reader-and-export-to-ascii), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Bug fix
Can read files from provided path (before only from current folder)
Can write to ASCII a variable that has one dimension. Previously only variables with at least 2 dimension would be written.

1.0.0.0