Problem in creating netcdf
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2012-7-19
I suggest experimenting with using an nc double instead of nc float.
NaN is supposed to be storable in netcdf.
0 个评论
更多回答(3 个)
Saeid Norouzi
2012-10-19
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 个评论
Walter Roberson
2012-10-19
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.)
John
2012-10-19
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 个评论
Walter Roberson
2012-10-19
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
Carlos Batista
2014-6-9
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?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!