<Oops, sent to octave-maintaners rather than octave-dev (SF), sorry for
that.
Here's for the list:>
Nov. 17, 2010 Levente Torok wrote:
> Dear Philip, Carlo and octave-dev,
>
> I had a chance to polish things according to your commands.
> However I have a few questions.
A while later, Levente Torok wrote:
<snip>
>
> I don't fix myself to ML compatibility at all.
> IO package will be just fine place to have these functions but
> according to our concept FPL would be even more appropriate.
> So if you can adjust it to the standards I would be with happy with this.
Hi Levente,
I went through your functions, they look very good. Thank you very much!
Yet I added and changed a few things. Please check if you find this
acceptable. See below...
Carlo, if Levente agrees with my fixes/additions, will you put them in
fpl pkg or shall I do it?
But first, a question to Levente:
(I know nothing about vtk, so apologies if this is a stupid question)
The array X in the example in savevtk's texinfo header is a real array,
yet the .vtk file that is produced only contains integer values (as the
fprintf statements invoke %d format specifiers rather than %f).
Is that intended?
savevtk:
========
- Fixed texinfo
- Added "See also" (savevtkvector)
- Fixed small typo (adoptatoin)
- Added a return value to the function statement (so one can test in
scripts if savevtk did its job OK). If you don't want to see "ans = 1"
you need to put a semicolon after the savevtk() statement.
- Added a default filename "vtkout.vtk". Just remove the '=vtkout.vtk'
if you don't like this
- Added basic input argument checking (incl. filename type - should be
character)
- Added a try-catch around the fprintf statements to catch disk full errors
- and you don't need to count dimensions yourself, Octave has a function
for that: ndims()
- Minor style changes (spaces etc).
savevtkvector
=============
- Fixed texinfo
- Added "See also" (savevtkvector)
- Fixed small typo (adoptatoin)
- Added a return value to the function statement (so one can test in
scripts if savevtk did its job OK)
- Added a default filename "vtkvectorout.vtk". Just remove the
'=vtkvectorout.vtk' if you don't like this
- Added basic input argument checking
- Added a try-catch around the fprintf statements to catch disk full errors
- I had to replace wrong accents (a.o., ASCII 226, skewed) with '
(straight) in almost all fprintf statements - this may be caused by
editing witha word processor rather than a program editor
- Minor style changes (spaces etc).
See attached files.
Please tell us if you're OK with the changes.
Philip
## Copyright (C) 2010 Kurnia Wano, Levente Torok
##.
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##.
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>.
##
## -*- texinfo -*-
## @deftypefn {Function File} [ @var{status} ] = savevtkvector ( @var{X}, @var{Y}, @var{Z}, @var{filename} )
## savevtkvector Save a 3-D vector array in VTK format
##
## savevtkvector (X, Y, Z, filename) saves a 3-D vector of any size to
## filename in VTK format. This file format is used by Mayavi2 for example.
## X, Y and Z should be arrays of the same
## size, each storing speeds in the a single Cartesian directions.
##
## If no write errors occurred, output argument status is set to 1, otherwise 0.
##
## @end deftypefn
##
## Author: Kurnia Wano, Levente Torok <torok...@gmail.com>
## Created: 2010-08-02 matlab version
## Updates: 2010-11-03 octave adoptatoin
##
function [ status ] = savevtkvector (X, Y, Z, filename)
status = 0;
if (nargin < 4), usage ("[status] = savevtkvector (X, Y, Z, filename='vtkvector.out')"); endif
if ((size(X) ~= size(Y)) | (size(X) ~= size(Z)))
error ("Error: velocity arrays of unequal size\n");
# return; return not needed here, error() breaks out already
end
[ny, nx, nz] = size (datax);
xx = 1:size (datax, 2);
yy = 1:size (datax, 1);
zz = 1:size (datax, 3);
datax = datax(:)';
datay = datay(:)';
dataz = dataz(:)';
## Header
if (ischar (filename))
[ fid, msg ] = fopen (fullfile (outputd, filename), 'w');
if ( fid < 1 )
error ("Cannot open the file for saving: %s", msg);
endif
else
usage ("Filename expected for arg # 2");
endif
try
fprintf (fid, '%s\n', '# vtk DataFile Version 3.0');
fprintf (fid, '%s\n', '3D LFF extrapolation');
fprintf (fid, '%s\n', 'ASCII' );
fprintf (fid, '%s\n', 'DATASET RECTILINEAR_GRID');
fprintf (fid, '%s %1.0i %1.0i %1.0i\n', 'DIMENSIONS',nx, ny, nz);
fprintf (fid, '%s %1.0i %s\n', 'X_COORDINATES', nx, 'float');
fprintf (fid, '%1.0i ', xx);
fprintf (fid, '\n%s %1.0i %s\n', 'Y_COORDINATES', ny, 'float');
fprintf (fid, '%1.0i ', yy);
fprintf (fid, '\n%s %1.0i %s\n', 'Z_COORDINATES', nz, 'float');
fprintf (fid, '%1.0i ', zz);
## Data
fprintf (fid, '\n%s %1.0i','POINT_DATA', nx*ny*nz );
fprintf (fid, '\n%s\n', 'VECTORS BFIELD float');
fprintf (fid, '%6.2f %6.2f %6.2f\n', [datax;datay;dataz]);
fclose (fid);
status = 1;
catch
error ("Error writing file %s - disk full?", filename)
end_try_catch
endfunction
## Copyright (C) 2010 Kurnia Wano, Levente Torok
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} [ @var{status} ] = savevtk ( @var{X}, @var{Y}, @var{Z}, @var{filename} )
## savevtk Save a 3-D scalar array in VTK format.
##
## savevtk( array, filename ) saves a 3-D array of any size to
## filename in VTK format. This file format is used by Mayavi2 for example.
##
## If no write errors occurred, output argument status is set to 1, otherwise 0.
##
## Example:
##
## @example
## n = 30;
## X = zeros(n,n,n);
## for x = 1:n
## for y = 1:n
## for z = 1:n
## X(x, y, z) = 1/sqrt ( x*x + y*y + z*z );
## endfor
## endfor
## endfor
## X = X * 200 / max(max(max(X)));
## savevtk( X, "spherical.vtk");
## @end example
##
## @seealso savevtkvector
##
## @end deftypefn
##
## Author: Kurnia Wano, Levente Torok <torok...@gmail.com>
## Created: 2010-08-02 matlab version
## Updates: 2010-11-03 octave adoptation
##
function [status ] = savevtk (array, filename="vtkout.vtk")
status = 0;
if (nargin < 1), usage ("[status] = savevtk (array, filename)"); endif
dims = size (array)
if ( size (dims) != 3)
# if (ndims (A) != 3)
error ("Save Vtk requires a 3 dimensional array");
endif
[nx, ny, nz] = size (array);
if (ischar (filename))
[fid, msg] = fopen (filename, 'wt');
if (fid < 0)
error ("Cannot open file for saving file. %s", msg);
endif
else
usage ("Filename expected for arg # 2");
endif
try
fprintf (fid, '# vtk DataFile Version 2.0\n');
fprintf (fid, 'Comment goes here\n');
fprintf (fid, 'ASCII\n');
fprintf (fid, '\n');
fprintf (fid, 'DATASET STRUCTURED_POINTS\n');
fprintf (fid, 'DIMENSIONS %d %d %d\n', nx, ny, nz);
fprintf (fid, '\n');
fprintf (fid, 'ORIGIN 0.000 0.000 0.000\n');
fprintf (fid, 'SPACING 1.000 1.000 1.000\n');
fprintf (fid, '\n');
fprintf (fid, 'POINT_DATA %d\n', nx*ny*nz);
fprintf (fid, 'SCALARS scalars double\n');
fprintf (fid, 'LOOKUP_TABLE default\n');
fprintf (fid, '\n');
for a=1:nz
for b=1:ny
for c=1:nx
fprintf (fid, '%d ', array(c, b, a));
endfor
fprintf (fid, '\n');
endfor
endfor
fclose (fid);
status = 1;
catch
error ("Error writing file %s - disk full?", filename);
end_try_catch
endfunction
------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3.
Spend less time writing and rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev