#include #include #include /* Read a 2-D arrays variable from a netcdf file. Command to compile this extension: mkoctfile ncrdvar.cc -lnetcdf If the data in the netcdf file is 1-D use a column vector. If the data is 2-D the columns size must match that in the netcdf file, or the returned data will be scrambled. Mark P. Esplin Stewart Radiance Laboratory Oct 25, 2001 */ DEFUN_DLD (ncrdvar, args, , "Read a 2-D arrays variable from a netcdf file.\n\ Returns [data, status]\n\n\ \narg1: netcdf file name\narg2: variable name\narg3: number of rows\ \narg4: number of columns") { int ncid,varid,nc_status; size_t start[3],count[3]; int irow,icol,iadd; string fname,varname; octave_value_list retval; int nargin = args.length(); if (nargin != 4 || !args(2).is_real_scalar() || !args(3).is_real_scalar() ){ print_usage ("ncrdvar"); return retval; } int nrow = args(2).int_value(); int ncol = args(3).int_value(); double TempStore[nrow*ncol]; Matrix y (nrow,ncol); fname = args(0).string_value(); varname = args(1).string_value(); retval(0) = "0"; nc_status = nc_open(fname.c_str(),NC_NOWRITE,&ncid); if(nc_status != NC_NOERR){ retval(1) = "couldn't open file"; return retval; } nc_status = nc_inq_varid(ncid, varname.c_str(), &varid); if(nc_status != NC_NOERR){ retval(1) = "variable not defined"; nc_close(ncid); return retval; } start[0]=0; start[1]=0; start[2]=0; count[0]=nrow; count[1]=ncol; count[2]=0; nc_status = nc_get_vara_double(ncid, varid, start, count, TempStore); if(nc_status != NC_NOERR) { retval(1) = "Error reading data"; nc_close(ncid); return retval; } for(irow=0;irow