File: rgnwrite.c

package info (click to toggle)
saoimage 1.29.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,952 kB
  • ctags: 4,265
  • sloc: ansic: 50,317; makefile: 243; sh: 35
file content (196 lines) | stat: -rw-r--r-- 6,296 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	rgnwrite.c (Region Write)
 * Purpose:	Write descriptions of saved regions to a file
 * Subroutine:	write_regions()		returns: void
 * Subroutine:	timestamp()		returns: void
 * UNIX calls:	time(), asctime(), localtime(), fclose()
 * Copyright:	1989, 1990, 1995 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		  9 July 1989
 *		{1} MVH removed comma from list output		   7 Dec 1989
 *              {2} MVH BSDonly strings.h compatability           19 Feb 1990
 *              {3} Doug Mink  cast strlen to int for max          4 May 1995
 *              {4} Doug Mink  fix time() declaration for VMS      8 Sep 1995
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/* stderr, NULL, FILE, etc. */

#ifndef VMS
#ifdef SYSV
#include <string.h>		/* strlen, strcat, strcpy, strrchr */
#else
#include <strings.h>		/* strlen, strcat, strcpy, rindex */
#endif
#else
#include <string.h>		/* strlen, strcat, strcpy, strrchr */
#endif

#include <time.h>		/* needed for asctime() */
#include <X11/Xlib.h>		/* get X types and constants */
#include <X11/Xutil.h>		/* X window manager, visual stuff */
#include "hfiles/constant.h"	/* define codes */
#include "hfiles/color.h"	/* colors used by cursor */
#include "hfiles/cursor.h"	/* define cursor parameter structures */
#include "hfiles/define.h"	/* define SZ_FNAME, MIN, MAX, etc. */
#include "hfiles/image.h"	/* image description structure */
#include "hfiles/edit.h"	/* define EditStruct */
#include "hfiles/extern.h"	/* pass common structures */

#ifndef VMS
#define	IRAFFILE	"frame.%d.%d"
#else
#define	IRAFFILE	"frame.%d_%d"
#endif

char *ProsName = "saoimage.reg";

EditStruct *region_edit;

/*
 * Subroutine:	write_regions
 * Purpose:	Write region info to a file
 */
void write_regions ( cursor, image, output_type )
     struct cursorRec *cursor;
     struct imageRec *image;
     int output_type;		/* i: SOP_Imtool or SOP_PROS */
{
  FILE *fd;
  static int last_output_type = 0;
  char fname[SZ_FNAME];
  static void write_region_imtool();
  int open_output_file();
  EditStruct *init_edit_popup();
  void set_path_iraf(), write_region_pros(), load_edit_struct(), timestamp();

  /* return if there is nothing to write */
  if( cursor->next_region == NULL ) {
    (void)fprintf(stderr, "WARNING: no saved cursors to write!\n");
    return;
  }
  /* imtool type output is different */
  if( output_type == SOP_Imtool ) {
    if( last_output_type != SOP_Imtool ) {
      /* get name of coordinate output file */
      strcpy (fname,img.filename);
      strcat (fname,".plist");
      /* sprintf(fname, IRAFFILE, 1, 1); */
      /* set_path_iraf(fname); */
      if( region_edit == NULL )
	region_edit = init_edit_popup(fname, SZ_FNAME);
      else
	load_edit_struct(region_edit, fname, strlen(fname));
      last_output_type = SOP_Imtool;
    }
    /* open coordinate output file for writing */
    if( open_output_file(&fd, region_edit, 0,
			 "Enter file name for IRAF list:") <= 0 )
      return;
    /* timestamp the first entry in the output file. */
    /* timestamp(fd, image->filename); */
    /* put in a line to explain parameter list */
    /* (void)fprintf(fd, "# (x y)\n"); */
    write_region_imtool(fd, cursor->next_region, image);
  } else {
    if( last_output_type != SOP_PROS ) {
      if( region_edit == NULL )
	region_edit = init_edit_popup(ProsName, SZ_FNAME);
      else
	load_edit_struct(region_edit, ProsName, strlen(fname));
      last_output_type = SOP_PROS;
    }
    /* open region output file for writing */
    if( open_output_file(&fd, region_edit, 0,
			 "Enter file name for regions:") <= 0 )
      return;
    /* timestamp the first entry in the output file. */
    timestamp(fd, image->filename);
    /* put in a line to explain parameter list */
    (void)fprintf(fd,"# shape x, y, [x dimension, y dimension], [angle]\n");
    /* write the pros region specifications */
    write_region_pros(fd, cursor->next_region);
  }
  (void)fclose(fd);
}

/*
 * Subroutine:	write_region_imtool
 * Purpose:	Write region info in imtool's format (iraf "list")
 * Note:	Output is a list of ascii pairs giving only center coords
 */
static void write_region_imtool ( fd, region, image )
     FILE *fd;
     struct cursorRec *region;
     struct imageRec *image;
{
  if( region != NULL ) {
    /* recurse first to reverse order of regions */
    if( region->next_region != NULL )
      write_region_imtool(fd, region->next_region, image);
    if( image->index_base )
      (void)fprintf(fd, "%5d %5d\n", RND(region->file.X), RND(region->file.Y));
    else
      (void)fprintf(fd, "%5d %5d\n", (int)region->file.X, (int)region->file.Y);
  }
}

/*
 * Subroutine:	timestamp
 * Purpose:	Write date and filename (if known) to region file
 */
void timestamp ( fd, imagename )
     FILE *fd;
     char *imagename;
{
  char  line[SZ_LINE];
  int len;
  static void set_time_string();

  /* timestamp the first entry in the output file. */
  set_time_string (line);
  /* get rid of any trailing new line */
  len = MAX(0, (int)strlen(line) - 1);
  if (line[len] == '\n')
    line[len] = '\0';
  /* print file name if one is given */
  if ((imagename != NULL) && (strlen(imagename)) != 0) {
    (void)fprintf(fd, "# %s\n", imagename);
  }
  /* print date and time */
  (void)fprintf(fd, "# %s\n", line);
}

/*
 * Subroutine:	set_time_string (time)
 * Purpose:	Print the time in the passed string
 * UNIX calls:	time(), asctime(), localtime()
 */
static void set_time_string ( time_string )
     char *time_string;
{
  long timeofday;
#
  /* I don't know where this should be defined, it's not in <time.h> */
  /* it's a time_t which is defined as a long in <sys/types.h> */
#ifdef LONG64
  int time();
#else
#ifndef VMS
  long time();
#else
  time_t time();
#endif
#endif

  timeofday = time((long *)0);
  /* note asctime(localtime()) can be replaced by ctime() in newer OS's */
  (void)strcpy(time_string, asctime(localtime(&timeofday)));
}