error for netcdf.putVar when writing data to netcdf file

  • Follow


Hello,
I want to put in 1009 lines of strings from file date_string.txt into a netcdf file.

I get the following error:

??? Error using ==> putVar at 67
The datatype cell is not allowed with putVar.

Error in ==> form_emissions at 13
netcdf.putVar(ncid,TimesID,Times_var);

The script below that leads to the attempt of writing the data to the file.

ncid = netcdf.create('wrflightningchemi_d01','NC_64BIT_OFFSET');

timedimID = netcdf.defDim(ncid, 'Time', 1009);

time_stringdimID = netcdf.defDim(ncid, 'DateStrLen', 19);

nc_char = netcdf.getConstant('NC_CHAR');

TimesID = netcdf.defVar(ncid,'Times',nc_char,[time_stringdimID, timedimID]);

Times_var = textread('date_string.txt','%s');

netcdf.putVar(ncid,TimesID,Times_var);


Thanks,
Jonathan 
0
Reply Jonathan 2/11/2011 9:45:05 AM

TEXTREAD is returning the contents of your text file a a cell array, which isn't a valid input type for the netcdf package.  What you could do instead is to read your text file line-by-line with FGETL and write each text line individually, like 

fid=fopen('date_string.txt');
n = -1;
while 1
        tline = fgetl(fid);
        if ~ischar(tline), break, end
        n = n+1;
        start = [0 n];
        count = [19 1];
        netcdf.putVar(ncid,TimesID,start,count,tline);
end


"Jonathan W Smith" wrote in message <ij30f1$bg7$1@fred.mathworks.com>...
> Hello,
> I want to put in 1009 lines of strings from file date_string.txt into a netcdf file.
> 
> I get the following error:
> 
> ??? Error using ==> putVar at 67
> The datatype cell is not allowed with putVar.
> 
> Error in ==> form_emissions at 13
> netcdf.putVar(ncid,TimesID,Times_var);
> 
> The script below that leads to the attempt of writing the data to the file.
> 
> ncid = netcdf.create('wrflightningchemi_d01','NC_64BIT_OFFSET');
> 
> timedimID = netcdf.defDim(ncid, 'Time', 1009);
> 
> time_stringdimID = netcdf.defDim(ncid, 'DateStrLen', 19);
> 
> nc_char = netcdf.getConstant('NC_CHAR');
> 
> TimesID = netcdf.defVar(ncid,'Times',nc_char,[time_stringdimID, timedimID]);
> 
> Times_var = textread('date_string.txt','%s');
> 
> netcdf.putVar(ncid,TimesID,Times_var);
> 
> 
> Thanks,
> Jonathan 
0
Reply John 2/11/2011 2:05:04 PM


John,

Thanks for your reply.  I used the script you recommended below except I put the netcdf.putVar command outside of the while loop.  Putting it outside allowed all 1009 lines of the text file to write out.  I also closed define mode with netcdf.endDef(ncid) and put the script at the end of the large code (after all dimensions, variables, and attributes were defined).

With these changes and even with the netcdf.putVar command inside the loop, I attained the following error:

??? Error using ==> netcdflib
Size implied by count argument does not match up with size of input
data.

Error in ==> putVar at 75
netcdflib(funcstr,ncid,varid,varargin{:});

Error in ==> form_emissions at 124
    netcdf.putVar(ncid,TimesID,start,count,tline);

Any suggestions on how to get the code working.
Thanks,
Jonathan

"John " <com.works.math@evans.john> wrote in message <ij3fmg$slv$1@fred.mathworks.com>...
> TEXTREAD is returning the contents of your text file a a cell array, which isn't a valid input type for the netcdf package.  What you could do instead is to read your text file line-by-line with FGETL and write each text line individually, like 
> 
> fid=fopen('date_string.txt');
> n = -1;
> while 1
>         tline = fgetl(fid);
>         if ~ischar(tline), break, end
>         n = n+1;
>         start = [0 n];
>         count = [19 1];
>         netcdf.putVar(ncid,TimesID,start,count,tline);
> end
> 
> 
> "Jonathan W Smith" wrote in message <ij30f1$bg7$1@fred.mathworks.com>...
> > Hello,
> > I want to put in 1009 lines of strings from file date_string.txt into a netcdf file.
> > 
> > I get the following error:
> > 
> > ??? Error using ==> putVar at 67
> > The datatype cell is not allowed with putVar.
> > 
> > Error in ==> form_emissions at 13
> > netcdf.putVar(ncid,TimesID,Times_var);
> > 
> > The script below that leads to the attempt of writing the data to the file.
> > 
> > ncid = netcdf.create('wrflightningchemi_d01','NC_64BIT_OFFSET');
> > 
> > timedimID = netcdf.defDim(ncid, 'Time', 1009);
> > 
> > time_stringdimID = netcdf.defDim(ncid, 'DateStrLen', 19);
> > 
> > nc_char = netcdf.getConstant('NC_CHAR');
> > 
> > TimesID = netcdf.defVar(ncid,'Times',nc_char,[time_stringdimID, timedimID]);
> > 
> > Times_var = textread('date_string.txt','%s');
> > 
> > netcdf.putVar(ncid,TimesID,Times_var);
> > 
> > 
> > Thanks,
> > Jonathan 
0
Reply Jonathan 2/11/2011 7:36:03 PM

Hard to be certain, I'm not sure if your "netcdf.putVar" is inside or outside of the loop now.  I would suggest debugging your code by issuing a "dbstop at 124 in form_emissions" and then run that code.  Then compare the size of tline with the size of count to see if they match up.  When provided, the count argument specifies the lengths along the variables extents into when the data is written, so they should match up here.




"Jonathan W Smith" wrote in message <ij4333$8hh$1@fred.mathworks.com>...
> John,
> 
> Thanks for your reply.  I used the script you recommended below except I put the netcdf.putVar command outside of the while loop.  Putting it outside allowed all 1009 lines of the text file to write out.  I also closed define mode with netcdf.endDef(ncid) and put the script at the end of the large code (after all dimensions, variables, and attributes were defined).
> 
> With these changes and even with the netcdf.putVar command inside the loop, I attained the following error:
> 
> ??? Error using ==> netcdflib
> Size implied by count argument does not match up with size of input
> data.
> 
> Error in ==> putVar at 75
> netcdflib(funcstr,ncid,varid,varargin{:});
> 
> Error in ==> form_emissions at 124
>     netcdf.putVar(ncid,TimesID,start,count,tline);
> 
> Any suggestions on how to get the code working.
> Thanks,
> Jonathan
> 
> "John " <com.works.math@evans.john> wrote in message <ij3fmg$slv$1@fred.mathworks.com>...
> > TEXTREAD is returning the contents of your text file a a cell array, which isn't a valid input type for the netcdf package.  What you could do instead is to read your text file line-by-line with FGETL and write each text line individually, like 
> > 
> > fid=fopen('date_string.txt');
> > n = -1;
> > while 1
> >         tline = fgetl(fid);
> >         if ~ischar(tline), break, end
> >         n = n+1;
> >         start = [0 n];
> >         count = [19 1];
> >         netcdf.putVar(ncid,TimesID,start,count,tline);
> > end
> > 
> > 
> > "Jonathan W Smith" wrote in message <ij30f1$bg7$1@fred.mathworks.com>...
> > > Hello,
> > > I want to put in 1009 lines of strings from file date_string.txt into a netcdf file.
> > > 
> > > I get the following error:
> > > 
> > > ??? Error using ==> putVar at 67
> > > The datatype cell is not allowed with putVar.
> > > 
> > > Error in ==> form_emissions at 13
> > > netcdf.putVar(ncid,TimesID,Times_var);
> > > 
> > > The script below that leads to the attempt of writing the data to the file.
> > > 
> > > ncid = netcdf.create('wrflightningchemi_d01','NC_64BIT_OFFSET');
> > > 
> > > timedimID = netcdf.defDim(ncid, 'Time', 1009);
> > > 
> > > time_stringdimID = netcdf.defDim(ncid, 'DateStrLen', 19);
> > > 
> > > nc_char = netcdf.getConstant('NC_CHAR');
> > > 
> > > TimesID = netcdf.defVar(ncid,'Times',nc_char,[time_stringdimID, timedimID]);
> > > 
> > > Times_var = textread('date_string.txt','%s');
> > > 
> > > netcdf.putVar(ncid,TimesID,Times_var);
> > > 
> > > 
> > > Thanks,
> > > Jonathan 
0
Reply com.works.math (28) 2/11/2011 8:18:04 PM

3 Replies
408 Views

(page loaded in 0.055 seconds)

Similiar Articles:












7/25/2012 4:22:30 AM


Reply: