MATLAB: Problem in creating netcdf

netcdf

Hi All,
I need some help. I’m trying to create netcdf file from *.dat file (input.dat). These two matlab code (create_nc.m; create_nc_data.m) had already succeed generate the netcdf file (output.nc). The problem is the NaN data in the input is read differently in the netcdf file output (it becomes zero).
If anyone might know or had the problem before, maybe can suggest the solution for me, thanks.
Here is the information of data input, codes and the netcdf output.
input.dat
5 5 2 NaN 5 5 3 1
create_nc.m
%%Dimension and parameter input
filename='output.nc';
londim=4;
latdim=2;
levdim=1;
%%Call function / subroutine to generate nc file
create_nc_data(filename,londim,latdim,levdim)
%%modify data input
nc=netcdf(filename,'w');
% longitude & latitude
lon=1:1.0:4;
lat=1:1.0:2;
% level
lev=1;
dt=load('input.dat');
nc{'lon'}(:)=lon;
nc{'lat'}(:)=lat;
nc{'lev'}(:)=lev;
nc{'data'}(:,:,:)=dt;
close(nc);
create_nc_data.m
function create_nc_data(filename,londim,latdim,levdim)
%%ncdump('inputdata.nc') %%Generated 08-Jun-2010 13:55:13
nc = netcdf(filename, 'clobber');
if isempty(nc), return, end
%%Global attributes:
nc.title = ncchar('Test_file_dimension_4x2');
%%Dimensions:
nc('lon') = londim;
nc('lat') = latdim;
nc('lev') = levdim;
%%Variables and attributes:
nc{'lon'} = ncfloat('lon'); %%



nc{'lon'}.long_name = ncchar('longitude');
nc{'lon'}.units = ncchar('degrees_east');
nc{'lon'}.missing_value = ncfloat(-9999);
nc{'lat'} = ncfloat('lat'); %%
nc{'lat'}.long_name = ncchar('latitude');
nc{'lat'}.units = ncchar('degrees_north');
nc{'lat'}.missing_value = ncfloat(-9999);
nc{'lev'} = nclong('lev'); %%
nc{'lev'}.long_name = ncchar('lev');
nc{'lev'}.units = ncchar('lev');
nc{'lev'}.missing_value = nclong(-9999);
nc{'data'} = ncshort('lev', 'lat', 'lon'); %%
nc{'data'}.long_name = ncchar('Test Data');
nc{'data'}.units = ncchar('none');
nc{'data'}.missing_value = ncshort(-9999);
endef(nc)
close(nc)
disp('Success..HORAYYYYY');
output.nc
nc_varget('output.nc','data')
ans =
5 2 5 3
5 0 5 1

Best Answer

I suggest experimenting with using an nc double instead of nc float.
NaN is supposed to be storable in netcdf.