/*****************************************************************************

				PRAAT_WRITE.C
				Writes GPK ASCII IMAGE format data files.

Copyright (C) 2002 Greg Kochanski, gpk@pribble.com .

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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*****************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "gpk_std.h"
#include "gpklib.h"
#include "gpklib/image.h"
#include "rw_misc.h"
#include "rbfile.h"
#include "internals.h"



LOCAL void print_properties(FILE *out, C proplist& prop)
{
	C property *p = prop.first;
	for(; p; p=p->next)	{
		if(!*p->attr || !p->val)
			continue;

		/* Drop duplicates and 'covered' attributes. */
		if(prop.get(p->attr) != p)
			continue;

		C char *a = 0;
		if( strcmp(p->attr, "NAXIS2") == 0)
			a = "nx";
		else if( strcmp(p->attr, "CDELT2") == 0)
			a = "dx";
		else if( strcmp(p->attr, "CRPIX1") == 0)
			;
		else if( strcmp(p->attr, "CRVAL1") == 0) {
			C double x1 =
			print_avc(out, "x1", x1, (C char *)0);
			continue;
			}
		... ZZZ
		print_avc(out, p->attr, p->val, (C char *)0);
		putc('\n', out);
	}
}


BOOL praat_writefile(C struct image *p, FILE *out, C proplist& control)
{
 assert(p);
 assert(out);
 assert(p->prop);
 proplist q(*p->prop);
 if( q.geti("NAXIS1", 0) != 1) {
	gpk_warn("Sorry! Can only write single channel data (NAXIS1=1) to Praat format", 0);
 	return FALSE;
 }
 normalize_proplist(&q, p);
 q += control;
 C int bitpix = q.geti("BITPIX", 0);
 double bscale, bzero;
 scale_image_for_bitpix(p, 0, &bscale, &bzero, 0, 0, &q, TRUE);

 q.add("_BITPIX", bitpix);
 q.add("BITPIX", (C char *)0);
 q.add("_FILETYPE", (C char *)0);
 gpkio_date_fix(&q);

 fprintf(out, "File type = \"ooTextFile\"\n");
 fprintf(out, "Object class = \"%s\"\n", q.gets("TTYPE1", "unknown"));
 print_properties(out, q);
 if(fprintf(out, "\n")<0 || ferror(out))
	return 0;

 return write_out_data(out, p, 0, NETWORK_ORDER, bscale, bzero);
}
