Problem in creating netcdf

3 views (last 30 days)
dee
dee on 19 Jul 2012
Answered: Carlos Batista on 9 Jun 2014
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

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jul 2012
I suggest experimenting with using an nc double instead of nc float.
NaN is supposed to be storable in netcdf.

More Answers (3)

Saeid  Norouzi
Saeid Norouzi on 19 Oct 2012
Dear dee,
I was going to create .nc file from .dat file and I saw your script here. I tried to run it for simple input file (your input file without NaN). but I got this error first:
??? Undefined function or method 'netcdf' for input arguments of type 'char'.
Error in ==> create_nc_data at 4 nc = netcdf(filename, 'clobber');
Error in ==> create_nc at 8 create_nc_data(filename,londim,latdim,levdim)
I changed netcdf to netcdf.create and run again and I got this error:
??? Undefined function or method 'ncchar' for input arguments of type 'char'.
Error in ==> create_nc_data at 9 nc.title = ncchar('Test_file_dimension_4x2');
Error in ==> create_nc at 8 create_nc_data(filename,londim,latdim,levdim)
Would you please advise me on that?
Thanks in advance, saeid
  1 Comment
Walter Roberson
Walter Roberson on 19 Oct 2012
Which MATLAB version are you using? netcdf has only been part of MATLAB for a small number of releases; before that you had to install the netcdf package (if my memory is correct.)

Sign in to comment.


John
John on 19 Oct 2012
Hi Dee, you are using the "netcdf toolbox" to create your netcdf file, which isn't MathWorks-supported software. It's not supported by anyone as far as I know. I would guess that it may not be minding the missing_value attribute when you write the data. Replacing NaN in your input data stream with your missing value before writing to file might help.
  1 Comment
Walter Roberson
Walter Roberson on 19 Oct 2012
netcdf itself is supported these days, but with slightly different routine names than Dee is using; see http://www.mathworks.com/help/matlab/ref/netcdf.html

Sign in to comment.


Carlos Batista
Carlos Batista on 9 Jun 2014
I'm having a trouble almost equal! I'm trying save my data in netcdf file, but give erro (??? undefined function or method 'nccreate' for input arguments of type 'cell')
What happens? Can be lack of some function in my sistem?

Tags

Community Treasure Hunt

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

Start Hunting!