aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/ccdred/src/t_ccdinst.x
blob: e98763fd537c6f91a0d8964d927c6f7d09dabfda (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
include	<imhdr.h>
include	<imio.h>
include	<error.h>
include	"ccdtypes.h"

define	HELP1	"noao$imred/ccdred/src/ccdinst1.key"
define	HELP2	"noao$imred/ccdred/src/ccdinst2.key"
define	HELP3	"noao$imred/ccdred/src/ccdinst3.key"

define	LEVELS	"|basic|common|all|"

define	CMDS	"|quit|?|help|show|instrument|imheader|read|write|newimage\
		 |translate|imagetyp|subset|exptime|darktime|fixfile|biassec\
		 |ccdsec|datasec|trimsec|darkcor|fixpix|flatcor|fringcor\
		 |illumcor|overscan|readcor|scancor|trim|zerocor|ccdmean\
		 |fringscl|illumflt|mkfringe|mkillum|skyflat|ncombine\
		 |date-obs|dec|ra|title|next|nscanrow|"

define	QUIT		1	# Quit
define	QUESTION	2	# Help
define	HELP		3	# Help
define	SHOW		4	# Show current translations
define	INST		5	# Show instrument file
define	IMHEADER	6	# Print image header
define	READ		7	# Read instrument file
define	WRITE		8	# Write instrument file
define	NEWIMAGE	9	# Change image
define	TRANSLATE	10	# Translate image type
define	IMAGETYPE	11	# Image type
define	SUBSET		12	# Subset parameter
define	EXPTIME		13	# Exposure time
define	DARKTIME	14	# Dark time
define	FIXFILE		15	# Bad pixel file
define	BIASSEC		16	# Bias section
define	CCDSEC		17	# CCD section
define	DATASEC		18	# Data section
define	TRIMSEC		19	# Trim section
define	DARKCOR		20	# Dark count flag
define	FIXPIX		21	# Bad pixel flag
define	FLATCOR		22	# Flat field flag
define	FRINGCOR	23	# Fringe flag
define	ILLUMCOR	24	# Illumination flag
define	OVERSCAN	25	# Overscan flag
define	READCOR		26	# Readout flag
define	SCANCOR		27	# Scan mode flag
define	NSCANROW	42	# Number of scan rows
define	TRIM		28	# Trim flag
define	ZEROCOR		29	# Zero level flag
define	CCDMEAN		30	# CCD mean value
define	FRINGSCL	31	# Fringe scale value
define	ILLUMFLT	32	# Illumination flat flag
define	MKFRINGE	33	# Illumination flag
define	MKILLUM		34	# Illumination flag
define	SKYFLAT		35	# Sky flat flag
define	NCOMBINE	36	# NCOMBINE parameter
define	DATEOBS		37	# Date
define	DEC		38	# Dec
define	RA		39	# RA
define	TITLE		40	# Title
define	NEXT		41	# Next image

# T_CCDINST -- Check and modify instrument translations

procedure t_ccdinst ()

int	list, level, ncmd, imtopenp(), imtgetim(), scan(), access(), clgwrd()
pointer	sp, image, inst, ssfile, im, immap()
bool	update, clgetb()
errchk	delete, hdmwrite

begin
	call smark (sp)
	call salloc (image, SZ_FNAME, TY_CHAR)
	call salloc (inst, SZ_FNAME, TY_CHAR)
	call salloc (ssfile, SZ_FNAME, TY_CHAR)

	# Get the task parameters, open the translation file, set defaults.
	list = imtopenp ("images")
	call clgstr ("instrument", Memc[inst], SZ_FNAME)
	call clgstr ("ssfile", Memc[ssfile], SZ_FNAME)
	level = clgwrd ("parameters", Memc[image], SZ_FNAME, LEVELS)
        if (Memc[image] == EOS)
            call error (1, "No 'parameters' file value specified.")
	call hdmopen (Memc[inst])
	ncmd = NEXT
	update = false

	# Process each image.
	while (imtgetim (list, Memc[image], SZ_FNAME) != EOF) {
	    iferr (im = immap (Memc[image], READ_ONLY, 0)) {
		call erract (EA_WARN)
		next
	    }

	    if (clgetb ("edit"))
	        call ccdinst_edit (im, Memc[image], Memc[inst], Memc[ssfile],
		    level, ncmd, update)
	    else
		call ccdinst_hdr (im, Memc[image], Memc[inst], Memc[ssfile],
		    level)
	    call imunmap (im)
	    if (ncmd == QUIT)
		break
	}

	# Update instrument file if necessary.
	if (update) {
	    call printf ("Update instrument file %s (%b)? ")
		call pargstr (Memc[inst])
		call pargb (update)
	    call flush (STDOUT)
	    if (scan() != EOF)
		call gargb (update)
	    if (update) {
		iferr {
		    if (access (Memc[inst], 0, 0) == YES)
		        call delete (Memc[inst])
		    call hdmwrite (Memc[inst], NEW_FILE)
		} then
		    call erract (EA_WARN)
	    }
	}

	# Finish up.
	call hdmclose ()
	call imtclose (list)
	call sfree (sp)
end


# CCDINST_EDIT -- Main instrument file editor loop.
# This returns the last command (quit or next) and the update flag.
# The image name may also be changed.

procedure ccdinst_edit (im, image, inst, ssfile, level, ncmd, update)

pointer	im			# Image pointer
char	image[SZ_FNAME]		# Image name
char	inst[SZ_FNAME]		# Instrument file
char	ssfile[SZ_FNAME]	# Subset file
int	level			# Parameter level
int	ncmd			# Last command
bool	update			# Update?

bool	strne()
int	scan(), nscan(), strdic(), access()
pointer	sp, cmd, key, def, imval, im1, immap()
errchk	delete, hdmwrite

begin
	call smark (sp)
	call salloc (cmd, SZ_LINE, TY_CHAR)
	call salloc (key, SZ_FNAME, TY_CHAR)
	call salloc (def, SZ_LINE, TY_CHAR)
	call salloc (imval, SZ_LINE, TY_CHAR)

	call sscan ("show")
	repeat {
	    call gargwrd (Memc[cmd], SZ_LINE)
	    ncmd = strdic (Memc[cmd], Memc[cmd], SZ_LINE, CMDS)
	    switch (ncmd) {
	    case NEXT, QUIT:
		break
	    case QUESTION, HELP:
		if (level == 1)
		    call pagefile (HELP1, "ccdinstrument")
		else if (level == 2)
		    call pagefile (HELP2, "ccdinstrument")
		else if (level == 3)
		    call pagefile (HELP3, "ccdinstrument")
	    case SHOW:
		call ccdinst_hdr (im, image, inst, ssfile, level)
	    case INST:
		call hdmwrite ("STDOUT", APPEND)
		call printf ("\n")
	    case IMHEADER:
		call ccdinst_i (im, image)
	    case READ:
		call gargwrd (Memc[imval], SZ_LINE)
		if (nscan() < 2) 
		    call ccdinst_g ("Instrument file", inst, Memc[imval])
		if (update)
		    call printf ("WARNING: Previous changes lost\n")
		call hdmclose ()
		update = false
		if (strne (inst, Memc[imval])) {
		    iferr (call hdmopen (Memc[imval])) {
			call erract (EA_WARN)
			call hdmopen (inst)
		    } else {
			call ccdinst_hdr (im, image, inst, ssfile, level)
			update = true
		    }
		}
	    case WRITE:
		call gargwrd (Memc[imval], SZ_LINE)
		if (nscan() < 2)
		    call ccdinst_g ("Instrument file", inst, Memc[imval])
		iferr {
		    if (access (Memc[imval], 0, 0) == YES)
		        call delete (Memc[imval])
		    call hdmwrite (Memc[imval], NEW_FILE)
		    update = false
		} then
		    call erract (EA_WARN)
	    case NEWIMAGE:
		call gargwrd (Memc[imval], SZ_LINE)
		if (nscan() < 2)
		    call ccdinst_g ("New image name", image, Memc[imval])
		if (strne (image, Memc[imval])) {
		    iferr (im1 = immap (Memc[imval], READ_ONLY, 0)) {
			call erract (EA_WARN)
			im1 = NULL
		    }
		    if (im1 != NULL) {
			call imunmap (im)
			im = im1
			call strcpy (Memc[imval], image, SZ_FNAME)
			call ccdinst_hdr (im, image, inst, ssfile, level)
		    }
		}
	    case TRANSLATE:
		call ccdtypes (im, Memc[cmd], SZ_LINE)
		call hdmgstr (im, "imagetyp", Memc[imval], SZ_LINE)

		call gargwrd (Memc[def], SZ_FNAME)
		if (nscan() < 2) {
		    call printf ("CCDRED image type for '%s' (%s): ")
			call pargstr (Memc[imval])
			call pargstr (Memc[cmd])
		    call flush (STDOUT)
		    if (scan() != EOF)
			call gargwrd (Memc[def], SZ_FNAME)
		    if (nscan() == 0)
			call strcpy (Memc[cmd], Memc[def], SZ_LINE)
		}
		if (strdic (Memc[def], Memc[def], SZ_LINE, CCDTYPES) == 0) {
		    call printf ("Unknown CCDRED image type\n")
		    call strcpy (Memc[cmd], Memc[def], SZ_LINE)
		}
		if (strne (Memc[def], Memc[cmd])) {
		    call hdmpname (Memc[imval], Memc[def])
		    call ccdinst_p (im, "imagetyp",
			Memc[key], Memc[def], Memc[imval])
		    update = true
		}
	    case IMAGETYPE:
		call ccdinst_e (im, "image type", "imagetyp",
		    Memc[key], Memc[def], Memc[imval], update)
	    case SUBSET:
		call ccdinst_e (im, "subset parameter", "subset",
		    Memc[key], Memc[def], Memc[imval], update)
	    case EXPTIME:
		call ccdinst_e (im, "exposure time", "exptime",
		    Memc[key], Memc[def], Memc[imval], update)
	    case DARKTIME:
		call ccdinst_e (im, "dark time", "darktime",
		    Memc[key], Memc[def], Memc[imval], update)
	    case FIXFILE:
		call ccdinst_e (im, "bad pixel file", "fixfile",
		    Memc[key], Memc[def], Memc[imval], update)
	    case BIASSEC:
		call ccdinst_e (im, "bias section", "biassec",
		    Memc[key], Memc[def], Memc[imval], update)
	    case CCDSEC:
		call ccdinst_e (im, "original CCD section", "ccdsec",
		    Memc[key], Memc[def], Memc[imval], update)
	    case DATASEC:
		call ccdinst_e (im, "data section", "datasec",
		    Memc[key], Memc[def], Memc[imval], update)
	    case TRIMSEC:
		call ccdinst_e (im, "trim section", "trimsec",
		    Memc[key], Memc[def], Memc[imval], update)
	    case DARKCOR:
		call ccdinst_e (im, "dark count flag", "darkcor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case FIXPIX:
		call ccdinst_e (im, "bad pixel flag", "fixpix",
		    Memc[key], Memc[def], Memc[imval], update)
	    case FLATCOR:
		call ccdinst_e (im, "flat field flag", "flatcor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case FRINGCOR:
		call ccdinst_e (im, "fringe flag", "fringcor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case ILLUMCOR:
		call ccdinst_e (im, "illumination flag", "illumcor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case OVERSCAN:
		call ccdinst_e (im, "overscan flag", "overscan",
		    Memc[key], Memc[def], Memc[imval], update)
	    case READCOR:
		call ccdinst_e (im, "read correction flag", "readcor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case SCANCOR:
		call ccdinst_e (im, "scan mode flag", "scancor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case NSCANROW:
		call ccdinst_e (im, "scan mode rows", "nscanrow",
		    Memc[key], Memc[def], Memc[imval], update)
	    case TRIM:
		call ccdinst_e (im, "trim flag", "trim",
		    Memc[key], Memc[def], Memc[imval], update)
	    case ZEROCOR:
		call ccdinst_e (im, "zero level flag", "zerocor",
		    Memc[key], Memc[def], Memc[imval], update)
	    case CCDMEAN:
		call ccdinst_e (im, "mean value", "ccdmean",
		    Memc[key], Memc[def], Memc[imval], update)
	    case FRINGSCL:
		call ccdinst_e (im, "fringe scale", "fringscl",
		    Memc[key], Memc[def], Memc[imval], update)
	    case ILLUMFLT:
		call ccdinst_e (im, "illumination flat image", "illumflt",
		    Memc[key], Memc[def], Memc[imval], update)
	    case MKFRINGE:
		call ccdinst_e (im, "fringe image", "mkfringe",
		    Memc[key], Memc[def], Memc[imval], update)
	    case MKILLUM:
		call ccdinst_e (im, "illumination image", "mkillum",
		    Memc[key], Memc[def], Memc[imval], update)
	    case SKYFLAT:
		call ccdinst_e (im, "sky flat image", "skyflat",
		    Memc[key], Memc[def], Memc[imval], update)
	    case NCOMBINE:
		call ccdinst_e (im, "number of images combined", "ncombine",
		    Memc[key], Memc[def], Memc[imval], update)
	    case DATEOBS:
		call ccdinst_e (im, "date of observation", "date-obs",
		    Memc[key], Memc[def], Memc[imval], update)
	    case DEC:
		call ccdinst_e (im, "declination", "dec",
		    Memc[key], Memc[def], Memc[imval], update)
	    case RA:
		call ccdinst_e (im, "ra", "ra",
		    Memc[key], Memc[def], Memc[imval], update)
	    case TITLE:
		call ccdinst_e (im, "title", "title",
		    Memc[key], Memc[def], Memc[imval], update)
	    default:
	        if (nscan() > 0)
		    call eprintf ("Unrecognized or ambiguous command\007\n")
	    }
	    call printf ("ccdinstrument> ")
	    call flush (STDOUT)
	} until (scan() == EOF)

	call sfree (sp)
end


# CCDINST_HDR -- Print the current instrument translations for an image.

procedure ccdinst_hdr (im, image, inst, ssfile, level)

pointer	im			# Image pointer
char	image[SZ_FNAME]		# Image name
char	inst[SZ_FNAME]		# Instrument file
char	ssfile[SZ_FNAME]	# Subset file
int	level			# Parameter level

pointer	sp, key, def, ccdval, imval

begin
	call smark (sp)
	call salloc (key, SZ_FNAME, TY_CHAR)
	call salloc (def, SZ_LINE, TY_CHAR)
	call salloc (ccdval, SZ_LINE, TY_CHAR)
	call salloc (imval, SZ_LINE, TY_CHAR)

	# General stuff
	call printf ("Image: %s\n")
	    call pargstr (image)
	call printf ("Instrument file: %s\n")
	    call pargstr (inst)
	call printf ("Subset file: %s\n")
	    call pargstr (ssfile)

	# Table labels
	call printf ("\n%-8s  %-8s  %-8s  %-8s  %-8s\n")
	    call pargstr ("CCDRED")
	    call pargstr ("IMAGE")
	    call pargstr ("DEFAULT")
	    call pargstr ("CCDRED")
	    call pargstr ("IMAGE")
	call printf ("%-8s  %-8s  %-8s  %-8s  %-8s\n")
	    call pargstr ("PARAM")
	    call pargstr ("KEYWORD")
	    call pargstr ("VALUE")
	    call pargstr ("VALUE")
	    call pargstr ("VALUE")
	call printf ("---------------------------------------")
	call printf ("---------------------------------------\n")

	# Print translations.  Select those printed only with the all parameter.
	call ccdinst_p (im, "imagetyp", Memc[key], Memc[def], Memc[imval])
	call ccdinst_p (im, "subset", Memc[key], Memc[def], Memc[imval])
	call ccdinst_p (im, "exptime", Memc[key], Memc[def], Memc[imval])
	call ccdinst_p (im, "darktime", Memc[key], Memc[def], Memc[imval])
	if (level > 1) {
	    call printf ("\n")
	    call ccdinst_p (im, "biassec", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "trimsec", Memc[key], Memc[def], Memc[imval])
	    call printf ("\n")
	    call ccdinst_p (im, "fixpix", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "overscan", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "trim", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "zerocor", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "darkcor", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "flatcor", Memc[key], Memc[def], Memc[imval])
	}
	if (level > 2) {
	    call ccdinst_p (im, "datasec", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "ccdsec", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "fixfile", Memc[key], Memc[def], Memc[imval])
	    call printf ("\n")
	    call ccdinst_p (im, "illumcor", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "fringcor", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "readcor", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "scancor", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "nscanrow", Memc[key], Memc[def], Memc[imval])
	    call printf ("\n")
	    call ccdinst_p (im, "illumflt", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "mkfringe", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "mkillum", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "skyflat", Memc[key], Memc[def], Memc[imval])
	    call printf ("\n")
	    call ccdinst_p (im, "ccdmean", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "fringscl", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "ncombine", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "date-obs", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "dec", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "ra", Memc[key], Memc[def], Memc[imval])
	    call ccdinst_p (im, "title", Memc[key], Memc[def], Memc[imval])
	}

	call printf ("\n")
	call flush (STDOUT)
	call sfree (sp)
end


# CCDINST_P -- Print the translation for the specified translation name.

procedure ccdinst_p (im, name, key, def, value)

pointer	im			# Image pointer
char	name[SZ_FNAME]		# CCDRED name
char	key[SZ_FNAME]		# Image header keyword
char	def[SZ_LINE]		# Default value
char	value[SZ_LINE]		# Value

int	i, strdic(), hdmaccf()
bool	bval, ccdflag()

begin
	i = strdic (name, key, SZ_FNAME, CMDS)
	if (i == 0)
	    return

	# Get translaltion image keyword, default, and image value.
	call hdmname (name, key, SZ_FNAME)
	call hdmgdef (name, def, SZ_LINE)
	call hdmgstr (im, name, value, SZ_LINE)
	if (value[1] == EOS)
	    call strcpy ("?", value, SZ_LINE)

	switch (i) {
	case IMAGETYPE: 
	    call printf ("%-8s  %-8s  %-8s")  
		call pargstr (name)
		call pargstr (key)
		call pargstr (def)
	    call ccdtypes (im, def, SZ_LINE)
	    call printf ("  %-8s  %-.39s\n")  
		call pargstr (def)
		call pargstr (value)
	case SUBSET: 
	    call printf ("%-8s  %-8s  %-8s")  
		call pargstr (name)
		call pargstr (key)
		call pargstr (def)
	    call ccdsubset (im, def, SZ_LINE)
	    call printf ("  %-8s  %-.39s\n")  
		call pargstr (def)
		call pargstr (value)
	case FIXPIX, OVERSCAN, TRIM, ZEROCOR, DARKCOR, FLATCOR, ILLUMCOR,
	     FRINGCOR, READCOR, SCANCOR, ILLUMFLT, MKFRINGE, MKILLUM,
	     SKYFLAT:
	    bval = ccdflag (im, name)
	    if (hdmaccf (im, name) == NO)
		call strcpy ("?", value, SZ_LINE)
	    call printf ("%-8s  %-8s  %-8s  %-8b  %-.39s\n")  
		call pargstr (name)
		call pargstr (key)
		call pargstr (def)
		call pargb (bval)
		call pargstr (value)
	default:
	    call printf ("%-8s  %-8s  %-8s  %-8s")  
		call pargstr (name)
		call pargstr (key)
		call pargstr (def)
		call pargstr (value)
	    if (hdmaccf (im, name) == NO)
		call strcpy ("?", value, SZ_LINE)
	    call printf ("  %-.39s\n")  
		call pargstr (value)
	}
end


# CCDINST_E -- Edit a single translation entry.
# This checks for parameters on the command line and if missing queries.
# The default value may only be changed on the command line.

procedure ccdinst_e (im, prompt, name, key, def, imval, update)

pointer	im			# Image pointer
char	prompt[ARB]		# Parameter prompt name
char	name[SZ_FNAME]		# CCDRED name
char	key[SZ_FNAME]		# Image header keyword
char	def[SZ_LINE]		# Default value
char	imval[SZ_LINE]		# Value
bool	update			# Update translation file?

bool	strne()
int	i, scan(), nscan()
pointer	sp, oldkey, olddef

begin
	    call smark (sp)
	    call salloc (oldkey, SZ_FNAME, TY_CHAR)
	    call salloc (olddef, SZ_LINE, TY_CHAR)

	    # Get command line values
	    call gargwrd (key, SZ_FNAME)
	    call gargwrd (def, SZ_LINE)

	    # Get current values
	    call hdmname (name, Memc[oldkey], SZ_FNAME)
	    call hdmgdef (name, Memc[olddef], SZ_LINE)

	    # Query for keyword if needed.
	    i = nscan()
	    if (i < 2) {
		call printf ("Image keyword for %s (%s): ")
		    call pargstr (prompt)
		    call pargstr (Memc[oldkey])
		call flush (STDOUT)
		if (scan() != EOF)
		    call gargwrd (key, SZ_FNAME)
		if (nscan() == 0)
		    call strcpy (Memc[oldkey], key, SZ_FNAME)
	    }
	    if (i < 3) {
		#call printf ("Default %s (%s): ")
		#    call pargstr (prompt)
		#    call pargstr (Memc[olddef])
		#call flush (STDOUT)
		#if (scan() != EOF)
		#    call gargwrd (def, SZ_LINE)
		#if (nscan() == 0)
		    call strcpy (Memc[olddef], def, SZ_LINE)
	    }

	    # Update only if the new value is different from the old value.
	    if (strne (key, Memc[oldkey])) { 
	        call hdmpname (name, key)
		update = true
	    }
	    if (strne (def, Memc[olddef])) {
		call hdmpdef (name, def)
		update = true
	    }

	    # Print the revised translation.
	    call ccdinst_p (im, name, key, def, imval)
	    call sfree (sp)
end


# CCDINST_G -- General procedure to prompt for value.

procedure ccdinst_g (prompt, def, val)

char	prompt[ARB]		# Prompt
char	def[ARB]		# Default value
char	val[SZ_LINE]		# Value

int	scan(), nscan()

begin
	call printf ("%s (%s): ")
	    call pargstr (prompt)
	    call pargstr (def)
	call flush (STDOUT)
	if (scan() != EOF)
	    call gargwrd (val, SZ_FNAME)
	if (nscan() == 0)
	    call strcpy (def, val, SZ_LINE)
end


define	USER_AREA	Memc[($1+IMU-1)*SZ_STRUCT + 1]

# CCDINST_IMH -- Print the user area of the image, if nonzero length
# and it contains only ascii values.  This copied from the code for
# IMHEADER.  It differs in including the OBJECT keyword, using a temporary
# file to page the header, and no leading blanks.

procedure ccdinst_i (im, image)

pointer	im			# image descriptor
char	image[ARB]		# image name

pointer	sp, tmp, lbuf, ip
int	in, out, ncols, min_lenuserarea 
int	open(), stropen(), getline(), envgeti()

begin
	call smark (sp)
	call salloc (tmp, SZ_FNAME, TY_CHAR)
	call salloc (lbuf, SZ_LINE, TY_CHAR)

	# Open user area in header.
	min_lenuserarea = (LEN_IMDES + IM_LENHDRMEM(im) - IMU) * SZ_STRUCT - 1
	in = stropen (USER_AREA(im), min_lenuserarea, READ_ONLY)
	ncols = envgeti ("ttyncols")

	# Open temporary output file.
	call mktemp ("tmp$", Memc[tmp], SZ_FNAME)
	iferr (out = open (Memc[tmp], NEW_FILE, TEXT_FILE)) {
	    call erract (EA_WARN)
	    call sfree (sp)
	    return
	}

	# Copy standard header records.
	call fprintf (out, "OBJECT  = '%s'\n")
	    call pargstr (IM_TITLE(im))

	# Copy header records to the output, stripping any trailing
	# whitespace and clipping at the right margin.

	while (getline (in, Memc[lbuf]) != EOF) {
	    for (ip=lbuf;  Memc[ip] != EOS && Memc[ip] != '\n';  ip=ip+1)
		;
	    while (ip > lbuf && Memc[ip-1] == ' ')
		ip = ip - 1
	    if (ip - lbuf > ncols)
		ip = lbuf + ncols 
	    Memc[ip] = '\n'
	    Memc[ip+1] = EOS
	    
	    call putline (out, Memc[lbuf])
	}
	call putline (out, "\n")

	call close (in)
	call close (out)

	call pagefile (Memc[tmp], image)
	call delete (Memc[tmp])

	call sfree (sp)
end