aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/immatch/src/linmatch/rgltools.x
blob: 845a0ac49feabf445c71a118b6b1e79cde6333a5 (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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
include "linmatch.h"

#  RG_LINIT -- Initialize the linscale structure. 

procedure rg_linit (ls, max_nregions)

pointer	ls		#I/O pointer to the intensity scaling structure
int	max_nregions	#I the maximum number of regions

begin
	# Allocate the temporary space.
	call malloc (ls, LEN_LSSTRUCT, TY_STRUCT)

	# Set up the regions parameters.
	LS_NREGIONS(ls) = 0
	LS_CNREGION(ls) = 1
	LS_MAXNREGIONS(ls) = max_nregions
		
	# Initialize the pointers.
	LS_RC1(ls) = NULL
	LS_RC2(ls) = NULL
	LS_RL1(ls) = NULL
	LS_RL2(ls) = NULL
	LS_RXSTEP(ls) = NULL
	LS_RYSTEP(ls) = NULL
	LS_XSHIFT(ls) = 0.0
	LS_YSHIFT(ls) = 0.0
	LS_SXSHIFT(ls) = 0.0
	LS_SYSHIFT(ls) = 0.0

	LS_RBUF(ls) = NULL
	LS_RGAIN(ls) = 1.0
	LS_RREADNOISE(ls) = 0.0
	LS_RMEAN(ls) = NULL
	LS_RMEDIAN(ls) = NULL
	LS_RMODE(ls) = NULL
	LS_RSIGMA(ls) = NULL
	LS_RSKY(ls) = NULL
	LS_RSKYERR(ls) = NULL
	LS_RMAG(ls) = NULL
	LS_RMAGERR(ls) = NULL
	LS_RNPTS(ls) = NULL

	LS_IBUF(ls) = NULL
	LS_IGAIN(ls) = 1.0
	LS_IREADNOISE(ls) = 0.0
	LS_IMEAN(ls) = NULL
	LS_IMEDIAN(ls) = NULL
	LS_IMODE(ls) = NULL
	LS_ISIGMA(ls) = NULL
	LS_ISKY(ls) = NULL
	LS_ISKYERR(ls) = NULL
	LS_IMAG(ls) = NULL
	LS_IMAGERR(ls) = NULL
	LS_INPTS(ls) = NULL

	LS_RBSCALE(ls) = NULL
	LS_RBSCALEERR(ls) = NULL
	LS_RBZERO(ls) = NULL
	LS_RBZEROERR(ls) = NULL
	LS_RDELETE(ls) = NULL
	LS_RCHI(ls) = NULL

	# Initialize the scaling algorithm parameters.
	LS_BZALGORITHM(ls) = DEF_BZALGORITHM
	LS_BSALGORITHM(ls) = DEF_BSALGORITHM
	LS_CBZERO(ls) = DEF_CBZERO
	LS_CBSCALE(ls) = DEF_CBSCALE
	LS_DNX(ls) = DEF_DNX
	LS_DNY(ls) = DEF_DNY
	LS_MAXITER(ls) = DEF_MAXITER
	LS_DATAMIN(ls) = DEF_DATAMIN
	LS_DATAMAX(ls) = DEF_DATAMAX
	LS_NREJECT(ls) = DEF_NREJECT
	LS_LOREJECT(ls) = DEF_LOREJECT
	LS_HIREJECT(ls) = DEF_HIREJECT
	LS_GAIN(ls) = DEF_GAIN
	LS_READNOISE(ls) = DEF_READNOISE

	# Initialize the answers
	LS_TBZERO(ls) = 0.0
	LS_TBZEROERR(ls) = INDEFR
	LS_TBSCALE(ls) = 1.0
	LS_TBSCALEERR(ls) = INDEFR

	# Initialize the strings.
	call strcpy ("mean", LS_BSSTRING(ls), SZ_FNAME)
	call strcpy ("mean", LS_BZSTRING(ls), SZ_FNAME)
	LS_CCDGAIN(ls) = EOS
	LS_CCDREAD(ls) = EOS
	LS_IMAGE(ls) = EOS
	LS_REFIMAGE(ls) = EOS
	LS_REGIONS(ls) = EOS
	LS_DATABASE(ls) = EOS
	LS_OUTIMAGE(ls) = EOS
	LS_RECORD(ls) = EOS
	LS_SHIFTSFILE(ls) = EOS
	LS_PHOTFILE(ls) = EOS

	# Initialize the buffers.
	call rg_lrinit (ls)
end


#  RG_LRINIT -- Initialize the region dependent part of the linscale structure. 

procedure rg_lrinit (ls)

pointer	ls		#I pointer to the intensity scaling structure

begin
	# Free up previously defined region pointers.
	call rg_lrfree (ls)

	# Allocate region definition pointers.
	call malloc (LS_RC1(ls), LS_MAXNREGIONS(ls), TY_INT)
	call malloc (LS_RC2(ls), LS_MAXNREGIONS(ls), TY_INT)
	call malloc (LS_RL1(ls), LS_MAXNREGIONS(ls), TY_INT)
	call malloc (LS_RL2(ls), LS_MAXNREGIONS(ls), TY_INT)
	call malloc (LS_RXSTEP(ls), LS_MAXNREGIONS(ls), TY_INT)
	call malloc (LS_RYSTEP(ls), LS_MAXNREGIONS(ls), TY_INT)

	# Allocate region statistics pointers.
	call malloc (LS_RMEAN(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RMEDIAN(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RMODE(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RSIGMA(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RSKY(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RSKYERR(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RMAG(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RMAGERR(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RNPTS(ls), LS_MAXNREGIONS(ls), TY_INT)

	call malloc (LS_IMEAN(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_IMEDIAN(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_IMODE(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_ISIGMA(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_ISKY(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_ISKYERR(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_IMAG(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_IMAGERR(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_INPTS(ls), LS_MAXNREGIONS(ls), TY_INT)

	call malloc (LS_RBSCALE(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RBSCALEERR(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RBZERO(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RBZEROERR(ls), LS_MAXNREGIONS(ls), TY_REAL)
	call malloc (LS_RDELETE(ls), LS_MAXNREGIONS(ls), TY_INT)
	call malloc (LS_RCHI(ls), LS_MAXNREGIONS(ls), TY_REAL)

	# Initialize region definitions.
	call amovki (INDEFI, Memi[LS_RC1(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_RC2(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_RL1(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_RL2(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_RXSTEP(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_RYSTEP(ls)], LS_MAXNREGIONS(ls))

	# Initilaize the statistics.
	call amovkr (INDEFR, Memr[LS_RMEAN(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RMEDIAN(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RMODE(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RSIGMA(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RSKY(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RSKYERR(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RMAG(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RMAGERR(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_RNPTS(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_IMEAN(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_IMEDIAN(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_IMODE(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_ISIGMA(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_ISKY(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_ISKYERR(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_IMAG(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_IMAGERR(ls)], LS_MAXNREGIONS(ls))
	call amovki (INDEFI, Memi[LS_INPTS(ls)], LS_MAXNREGIONS(ls))

	# Initialize the answers.
	call amovkr (INDEFR, Memr[LS_RBSCALE(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RBSCALEERR(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RBZERO(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RBZEROERR(ls)], LS_MAXNREGIONS(ls))
	call amovki (LS_NO, Memi[LS_RDELETE(ls)], LS_MAXNREGIONS(ls))
	call amovkr (INDEFR, Memr[LS_RCHI(ls)], LS_MAXNREGIONS(ls))
end


# RG_LINDEFR -- Re-initialize the regions dependent buffers.

procedure rg_lindefr (ls)

pointer	ls		#I pointer to the intensity scaling structure

int	nregions
int	rg_lstati()

begin
	nregions = rg_lstati (ls, NREGIONS)
	if (nregions > 0) {

	    # Reinitialize the region definition pointers.
	    call amovki (INDEFI, Memi[LS_RC1(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_RC2(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_RL1(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_RL2(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_RXSTEP(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_RYSTEP(ls)], nregions)

	    # Reinitialize the statistics pointers.
	    call amovkr (INDEFR, Memr[LS_RMEAN(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RMEDIAN(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RMODE(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RSIGMA(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RSKY(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RSKYERR(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RMAG(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RMAGERR(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_RNPTS(ls)], nregions)

	    call amovkr (INDEFR, Memr[LS_IMEAN(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_IMEDIAN(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_IMODE(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_ISIGMA(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_ISKY(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_ISKYERR(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_IMAG(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_IMAGERR(ls)], nregions)
	    call amovki (INDEFI, Memi[LS_INPTS(ls)], nregions)

	    # Reinitialize the answers pointers.
	    call amovkr (INDEFR, Memr[LS_RBSCALE(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RBSCALEERR(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RBZERO(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RBZEROERR(ls)], nregions)
	    call amovki (LS_NO, Memi[LS_RDELETE(ls)], nregions)
	    call amovkr (INDEFR, Memr[LS_RCHI(ls)], nregions)

	}
end


#  RG_LREALLOC -- Reallocate the regions dependent buffers.

procedure rg_lrealloc (ls, nregions)

pointer	ls		#I pointer to the intensity scaling structure
int	nregions	#I the number of regions

int	nr
int	rg_lstati()

begin
	nr = rg_lstati (ls, NREGIONS)

	# Resize the region definition buffers.
	call realloc (LS_RC1(ls), nregions, TY_INT)
	call realloc (LS_RC2(ls), nregions, TY_INT)
	call realloc (LS_RL1(ls), nregions, TY_INT)
	call realloc (LS_RL2(ls), nregions, TY_INT)
	call realloc (LS_RXSTEP(ls), nregions, TY_INT)
	call realloc (LS_RYSTEP(ls), nregions, TY_INT)

	# Resize the statistics buffers.
	call realloc (LS_RMEAN(ls), nregions, TY_REAL)
	call realloc (LS_RMEDIAN(ls), nregions, TY_REAL)
	call realloc (LS_RMODE(ls), nregions, TY_REAL)
	call realloc (LS_RSIGMA(ls), nregions, TY_REAL)
	call realloc (LS_RSKY(ls), nregions, TY_REAL)
	call realloc (LS_RSKYERR(ls), nregions, TY_REAL)
	call realloc (LS_RMAG(ls), nregions, TY_REAL)
	call realloc (LS_RMAGERR(ls), nregions, TY_REAL)
	call realloc (LS_RNPTS(ls), nregions, TY_INT)

	call realloc (LS_IMEAN(ls), nregions, TY_REAL)
	call realloc (LS_IMEDIAN(ls), nregions, TY_REAL)
	call realloc (LS_IMODE(ls), nregions, TY_REAL)
	call realloc (LS_ISIGMA(ls), nregions, TY_REAL)
	call realloc (LS_ISKY(ls), nregions, TY_REAL)
	call realloc (LS_ISKYERR(ls), nregions, TY_REAL)
	call realloc (LS_IMAG(ls), nregions, TY_REAL)
	call realloc (LS_IMAGERR(ls), nregions, TY_REAL)
	call realloc (LS_INPTS(ls), nregions, TY_INT)

	# Resize the answers buffers.
	call realloc (LS_RBSCALE(ls), nregions, TY_REAL)
	call realloc (LS_RBSCALEERR(ls), nregions, TY_REAL)
	call realloc (LS_RBZERO(ls), nregions, TY_REAL)
	call realloc (LS_RBZEROERR(ls), nregions, TY_REAL)
	call realloc (LS_RDELETE(ls), nregions, TY_INT)
	call realloc (LS_RCHI(ls), nregions, TY_REAL)

	# Reinitialize the region defintions.
	call amovki (INDEFI, Memi[LS_RC1(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_RC2(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_RL1(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_RL2(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_RXSTEP(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_RYSTEP(ls)+nr], nregions - nr)

	# Reinitialize the statistics buffers.
	call amovkr (INDEFR, Memr[LS_RMEAN(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RMEDIAN(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RMODE(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RSIGMA(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RSKY(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RSKYERR(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RMAG(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RMAGERR(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_RNPTS(ls)+nr], nregions - nr)

	call amovkr (INDEFR, Memr[LS_IMEAN(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_IMEDIAN(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_IMODE(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_ISIGMA(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_ISKY(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_ISKYERR(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_IMAG(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_IMAGERR(ls)+nr], nregions - nr)
	call amovki (INDEFI, Memi[LS_INPTS(ls)+nr], nregions - nr)

	# Reinitialize the answers buffers.
	call amovkr (INDEFR, Memr[LS_RBSCALE(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RBSCALEERR(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RBZERO(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RBZEROERR(ls)+nr], nregions - nr)
	call amovki (LS_NO, Memi[LS_RDELETE(ls)+nr], nregions - nr)
	call amovkr (INDEFR, Memr[LS_RCHI(ls)+nr], nregions - nr)
end


# RG_LRFREE -- Free the regions portion of the linscale structure.

procedure rg_lrfree (ls)

pointer	ls		#I pointer to the intensity scaling structure

begin
	LS_NREGIONS(ls) = 0

	# Free the regions definitions buffers.
	if (LS_RC1(ls) != NULL)
	    call mfree (LS_RC1(ls), TY_INT)
	LS_RC1(ls) = NULL
	if (LS_RC2(ls) != NULL)
	    call mfree (LS_RC2(ls), TY_INT)
	LS_RC2(ls) = NULL
	if (LS_RL1(ls) != NULL)
	    call mfree (LS_RL1(ls), TY_INT)
	LS_RL1(ls) = NULL
	if (LS_RL2(ls) != NULL)
	    call mfree (LS_RL2(ls), TY_INT)
	LS_RL2(ls) = NULL
	if (LS_RXSTEP(ls) != NULL)
	    call mfree (LS_RXSTEP(ls), TY_INT)
	LS_RXSTEP(ls) = NULL
	if (LS_RYSTEP(ls) != NULL)
	    call mfree (LS_RYSTEP(ls), TY_INT)
	LS_RYSTEP(ls) = NULL

	# Free the statistics buffers.
	if (LS_RBUF(ls) != NULL)
	    call mfree (LS_RBUF(ls), TY_REAL)
	if (LS_RMEAN(ls) != NULL)
	    call mfree (LS_RMEAN(ls), TY_REAL)
	LS_RMEAN(ls) = NULL
	if (LS_RMEDIAN(ls) != NULL)
	    call mfree (LS_RMEDIAN(ls), TY_REAL)
	LS_RMEDIAN(ls) = NULL
	if (LS_RMODE(ls) != NULL)
	    call mfree (LS_RMODE(ls), TY_REAL)
	LS_RMODE(ls) = NULL
	if (LS_RSIGMA(ls) != NULL)
	    call mfree (LS_RSIGMA(ls), TY_REAL)
	LS_RSIGMA(ls) = NULL
	if (LS_RSKY(ls) != NULL)
	    call mfree (LS_RSKY(ls), TY_REAL)
	LS_RSKY(ls) = NULL
	if (LS_RSKYERR(ls) != NULL)
	    call mfree (LS_RSKYERR(ls), TY_REAL)
	LS_RSKYERR(ls) = NULL
	if (LS_RMAG(ls) != NULL)
	    call mfree (LS_RMAG(ls), TY_REAL)
	LS_RMAG(ls) = NULL
	if (LS_RMAGERR(ls) != NULL)
	    call mfree (LS_RMAGERR(ls), TY_REAL)
	LS_RMAGERR(ls) = NULL
	if (LS_RNPTS(ls) != NULL)
	    call mfree (LS_RNPTS(ls), TY_INT)
	LS_RNPTS(ls) = NULL

	if (LS_IBUF(ls) != NULL)
	    call mfree (LS_IBUF(ls), TY_REAL)
	if (LS_IMEAN(ls) != NULL)
	    call mfree (LS_IMEAN(ls), TY_REAL)
	LS_IMEAN(ls) = NULL
	if (LS_IMEDIAN(ls) != NULL)
	    call mfree (LS_IMEDIAN(ls), TY_REAL)
	LS_IMEDIAN(ls) = NULL
	if (LS_IMODE(ls) != NULL)
	    call mfree (LS_IMODE(ls), TY_REAL)
	LS_IMODE(ls) = NULL
	if (LS_ISIGMA(ls) != NULL)
	    call mfree (LS_ISIGMA(ls), TY_REAL)
	LS_ISIGMA(ls) = NULL
	if (LS_ISKY(ls) != NULL)
	    call mfree (LS_ISKY(ls), TY_REAL)
	LS_ISKY(ls) = NULL
	if (LS_ISKYERR(ls) != NULL)
	    call mfree (LS_ISKYERR(ls), TY_REAL)
	LS_ISKYERR(ls) = NULL
	if (LS_IMAG(ls) != NULL)
	    call mfree (LS_IMAG(ls), TY_REAL)
	LS_IMAG(ls) = NULL
	if (LS_IMAGERR(ls) != NULL)
	    call mfree (LS_IMAGERR(ls), TY_REAL)
	LS_IMAGERR(ls) = NULL
	if (LS_INPTS(ls) != NULL)
	    call mfree (LS_INPTS(ls), TY_INT)
	LS_INPTS(ls) = NULL

	# Free the answers buffers.
	if (LS_RBSCALE(ls) != NULL)
	    call mfree (LS_RBSCALE(ls), TY_REAL)
	LS_RBSCALE(ls) = NULL
	if (LS_RBSCALEERR(ls) != NULL)
	    call mfree (LS_RBSCALEERR(ls), TY_REAL)
	LS_RBSCALEERR(ls) = NULL
	if (LS_RBZERO(ls) != NULL)
	    call mfree (LS_RBZERO(ls), TY_REAL)
	LS_RBZERO(ls) = NULL
	if (LS_RBZEROERR(ls) != NULL)
	    call mfree (LS_RBZEROERR(ls), TY_REAL)
	LS_RBZEROERR(ls) = NULL
	if (LS_RDELETE(ls) != NULL)
	    call mfree (LS_RDELETE(ls), TY_INT)
	LS_RDELETE(ls) = NULL
	if (LS_RCHI(ls) != NULL)
	    call mfree (LS_RCHI(ls), TY_REAL)
	LS_RCHI(ls) = NULL
end


# RG_LFREE -- Free the linscale structure.

procedure rg_lfree (ls)

pointer	ls		#I/O pointer to the intensity scaling structure

begin
	# Free the regions dependent pointers.
	call rg_lrfree (ls)

	call mfree (ls, TY_STRUCT)
end


# RG_LSTATI -- Fetch the value of an integer parameter.

int procedure rg_lstati (ls, param)

pointer	ls		#I pointer to the intensity scaling structure
int	param		#I parameter to be fetched

begin
	switch (param) {
	case CNREGION:
	    return (LS_CNREGION(ls))
	case NREGIONS:
	    return (LS_NREGIONS(ls))
	case MAXNREGIONS:
	    return (LS_MAXNREGIONS(ls))
	case BZALGORITHM:
	    return (LS_BZALGORITHM(ls))
	case BSALGORITHM:
	    return (LS_BSALGORITHM(ls))
	case DNX:
	    return (LS_DNX(ls))
	case DNY:
	    return (LS_DNY(ls))
	case MAXITER:
	    return (LS_MAXITER(ls))
	case NREJECT:
	    return (LS_NREJECT(ls))
	default:
	    call error (0, "RG_LSTATI: Unknown integer parameter.")
	}
end


# RG_LSTATP -- Fetch the value of a pointer parameter.

pointer procedure rg_lstatp (ls, param)

pointer	ls		#I pointer to the intensity scaling structure
int	param		#I parameter to be fetched

begin
	switch (param) {

	case RC1:
	    return (LS_RC1(ls))
	case RC2:
	    return (LS_RC2(ls))
	case RL1:
	    return (LS_RL1(ls))
	case RL2:
	    return (LS_RL2(ls))
	case RXSTEP:
	    return (LS_RXSTEP(ls))
	case RYSTEP:
	    return (LS_RYSTEP(ls))

	case RBUF:
	    return (LS_RBUF(ls))
	case RMEAN:
	    return (LS_RMEAN(ls))
	case RMEDIAN:
	    return (LS_RMEDIAN(ls))
	case RMODE:
	    return (LS_RMODE(ls))
	case RSIGMA:
	    return (LS_RSIGMA(ls))
	case RSKY:
	    return (LS_RSKY(ls))
	case RSKYERR:
	    return (LS_RSKYERR(ls))
	case RMAG:
	    return (LS_RMAG(ls))
	case RMAGERR:
	    return (LS_RMAGERR(ls))
	case RNPTS:
	    return (LS_RNPTS(ls))

	case IBUF:
	    return (LS_IBUF(ls))
	case IMEAN:
	    return (LS_IMEAN(ls))
	case IMEDIAN:
	    return (LS_IMEDIAN(ls))
	case IMODE:
	    return (LS_IMODE(ls))
	case ISIGMA:
	    return (LS_ISIGMA(ls))
	case ISKY:
	    return (LS_ISKY(ls))
	case ISKYERR:
	    return (LS_ISKYERR(ls))
	case IMAG:
	    return (LS_IMAG(ls))
	case IMAGERR:
	    return (LS_IMAGERR(ls))
	case INPTS:
	    return (LS_INPTS(ls))

	case RBSCALE:
	    return (LS_RBSCALE(ls))
	case RBSCALEERR:
	    return (LS_RBSCALEERR(ls))
	case RBZERO:
	    return (LS_RBZERO(ls))
	case RBZEROERR:
	    return (LS_RBZEROERR(ls))
	case RDELETE:
	    return (LS_RDELETE(ls))
	case RCHI:
	    return (LS_RCHI(ls))

	default:
	    call error (0, "RG_LSTATP: Unknown pointer parameter.")
	}
end


# RG_LSTATR -- Fetch the value of a real parameter.

real procedure rg_lstatr (ls, param)

pointer	ls		#I pointer to the intensity scaling structure
int	param		#I parameter to be fetched

begin
	switch (param) {

	case XSHIFT:
	    return (LS_XSHIFT(ls))
	case YSHIFT:
	    return (LS_YSHIFT(ls))
	case SXSHIFT:
	    return (LS_SXSHIFT(ls))
	case SYSHIFT:
	    return (LS_SYSHIFT(ls))

	case CBZERO:
	    return (LS_CBZERO(ls))
	case CBSCALE:
	    return (LS_CBSCALE(ls))
	case DATAMIN:
	    return (LS_DATAMIN(ls))
	case DATAMAX:
	    return (LS_DATAMAX(ls))
	case LOREJECT:
	    return (LS_LOREJECT(ls))
	case HIREJECT:
	    return (LS_HIREJECT(ls))
	case GAIN:
	    return (LS_GAIN(ls))
	case RGAIN:
	    return (LS_RGAIN(ls))
	case IGAIN:
	    return (LS_IGAIN(ls))
	case READNOISE:
	    return (LS_READNOISE(ls))
	case RREADNOISE:
	    return (LS_RREADNOISE(ls))
	case IREADNOISE:
	    return (LS_IREADNOISE(ls))

	case TBZERO:
	    return (LS_TBZERO(ls))
	case TBZEROERR:
	    return (LS_TBZEROERR(ls))
	case TBSCALE:
	    return (LS_TBSCALE(ls))
	case TBSCALEERR:
	    return (LS_TBSCALEERR(ls))

	default:
	    call error (0, "RG_LSTATR: Unknown real parameter.")
	}
end


# RG_LSTATS -- Fetch the value of a string parameter.

procedure rg_lstats (ls, param, str, maxch)

pointer	ls		#I pointer to the intensity scaling structure
int	param		#I parameter to be fetched
char	str[ARB]	#I the output string
int	maxch		#I maximum number of characters

begin
	switch (param) {
	case BZSTRING:
	    call strcpy (LS_BZSTRING(ls), str, maxch)
	case BSSTRING:
	    call strcpy (LS_BSSTRING(ls), str, maxch)
	case CCDGAIN:
	    call strcpy (LS_CCDGAIN(ls), str, maxch)
	case CCDREAD:
	    call strcpy (LS_CCDREAD(ls), str, maxch)
	case IMAGE:
	    call strcpy (LS_IMAGE(ls), str, maxch)
	case REFIMAGE:
	    call strcpy (LS_REFIMAGE(ls), str, maxch)
	case REGIONS:
	    call strcpy (LS_REGIONS(ls), str, maxch)
	case DATABASE:
	    call strcpy (LS_DATABASE(ls), str, maxch)
	case OUTIMAGE:
	    call strcpy (LS_OUTIMAGE(ls), str, maxch)
	case SHIFTSFILE:
	    call strcpy (LS_SHIFTSFILE(ls), str, maxch)
	case PHOTFILE:
	    call strcpy (LS_PHOTFILE(ls), str, maxch)
	case RECORD:
	    call strcpy (LS_RECORD(ls), str, maxch)
	default:
	    call error (0, "RG_LSTATS: Unknown string parameter.")
	}
end


# RG_LSETI -- Set the value of an integer parameter.

procedure rg_lseti (ls, param, value)

pointer	ls		# pointer to the intensity scaling structure
int	param		# parameter to be fetched
int	value		# value of the integer parameter

begin
	switch (param) {

	case NREGIONS:
	    LS_NREGIONS(ls) = value
	case CNREGION:
	    LS_CNREGION(ls) = value
	case MAXNREGIONS:
	    LS_MAXNREGIONS(ls) = value

	case BZALGORITHM:
	    LS_BZALGORITHM(ls) = value
	    switch (value) {
	    case LS_MEAN:
		call strcpy ("mean", LS_BZSTRING(ls), SZ_FNAME)
	    case LS_MEDIAN:
		call strcpy ("median", LS_BZSTRING(ls), SZ_FNAME)
	    case LS_MODE:
		call strcpy ("mode", LS_BZSTRING(ls), SZ_FNAME)
	    case LS_FIT:
		call strcpy ("fit", LS_BZSTRING(ls), SZ_FNAME)
	    case LS_PHOTOMETRY:
		call strcpy ("photometry", LS_BZSTRING(ls), SZ_FNAME)
	    case LS_NUMBER:
		;
	    case LS_FILE:
		call strcpy ("file", LS_BZSTRING(ls), SZ_FNAME)
	        LS_BSALGORITHM(ls) = value
		call strcpy ("file", LS_BSSTRING(ls), SZ_FNAME)
	    default:
		LS_BZALGORITHM(ls) = LS_NUMBER
		call strcpy ("0.0", LS_BZSTRING(ls), SZ_FNAME)
		LS_CBZERO(ls) = 0.0
	    }

	case BSALGORITHM:
	    LS_BSALGORITHM(ls) = value
	    switch (value) {
	    case LS_MEAN:
		call strcpy ("mean", LS_BSSTRING(ls), SZ_FNAME)
	    case LS_MEDIAN:
		call strcpy ("median", LS_BSSTRING(ls), SZ_FNAME)
	    case LS_MODE:
		call strcpy ("mode", LS_BSSTRING(ls), SZ_FNAME)
	    case LS_FIT:
		call strcpy ("fit", LS_BSSTRING(ls), SZ_FNAME)
	    case LS_PHOTOMETRY:
		call strcpy ("photometry", LS_BSSTRING(ls), SZ_FNAME)
	    case LS_NUMBER:
		;
	    case LS_FILE:
		call strcpy ("file", LS_BSSTRING(ls), SZ_FNAME)
	        LS_BZALGORITHM(ls) = value
		call strcpy ("file", LS_BZSTRING(ls), SZ_FNAME)
	    default:
		LS_BSALGORITHM(ls) = LS_NUMBER
		call strcpy ("1.0", LS_BSSTRING(ls), SZ_FNAME)
		LS_CBSCALE(ls) = 1.0
	    }

	case DNX:
	    LS_DNX(ls) = value
	case DNY:
	    LS_DNY(ls) = value
	case MAXITER:
	    LS_MAXITER(ls) = value
	case NREJECT:
	    LS_NREJECT(ls) = value

	default:
	    call error (0, "RG_LSETI: Unknown integer parameter.")
	}
end


# RG_LSETP -- Set the value of a pointer parameter.

procedure rg_lsetp (ls, param, value)

pointer	ls		#I pointer to the linscale structure
int	param		#I parameter to be fetched
pointer value		#I value of the pointer parameter

begin
	switch (param) {

	case RC1:
	    LS_RC1(ls) = value
	case RC2:
	    LS_RC2(ls) = value
	case RL1:
	    LS_RL1(ls) = value
	case RL2:
	    LS_RL2(ls) = value
	case RXSTEP:
	    LS_RXSTEP(ls) = value
	case RYSTEP:
	    LS_RYSTEP(ls) = value

	case RBUF:
	    LS_RBUF(ls) = value
	case RMEAN:
	    LS_RMEAN(ls) = value
	case RMEDIAN:
	    LS_RMEDIAN(ls) = value
	case RMODE:
	    LS_RMODE(ls) = value
	case RSIGMA:
	    LS_RSIGMA(ls) = value
	case RSKY:
	    LS_RSKY(ls) = value
	case RSKYERR:
	    LS_RSKYERR(ls) = value
	case RMAG:
	    LS_RMAG(ls) = value
	case RMAGERR:
	    LS_RMAGERR(ls) = value
	case RNPTS:
	    LS_RNPTS(ls) = value

	case IBUF:
	    LS_IBUF(ls) = value
	case IMEAN:
	    LS_IMEAN(ls) = value
	case IMEDIAN:
	    LS_IMEDIAN(ls) = value
	case IMODE:
	    LS_IMODE(ls) = value
	case ISIGMA:
	    LS_ISIGMA(ls) = value
	case ISKY:
	    LS_ISKY(ls) = value
	case ISKYERR:
	    LS_ISKYERR(ls) = value
	case IMAG:
	    LS_IMAG(ls) = value
	case IMAGERR:
	    LS_IMAGERR(ls) = value
	case INPTS:
	    LS_INPTS(ls) = value

	case RBSCALE:
	    LS_RBSCALE(ls) = value
	case RBSCALEERR:
	    LS_RBSCALEERR(ls) = value
	case RBZERO:
	    LS_RBZERO(ls) = value
	case RBZEROERR:
	    LS_RBZEROERR(ls) = value
	case RDELETE:
	    LS_RDELETE(ls) = value
	case RCHI:
	    LS_RCHI(ls) = value

	default:
	    call error (0, "RG_LSETP: Unknown pointer parameter.")
	}
end


# RG_LSETR -- Set the value of a real parameter.

procedure rg_lsetr (ls, param, value)

pointer	ls		#I pointer to iscale structure
int	param		#I parameter to be fetched
real	value		#I real parameter

begin
	switch (param) {
	case XSHIFT:
	    LS_XSHIFT(ls) = value
	case YSHIFT:
	    LS_YSHIFT(ls) = value
	case SXSHIFT:
	    LS_SXSHIFT(ls) = value
	case SYSHIFT:
	    LS_SYSHIFT(ls) = value
	case CBZERO:
	    LS_CBZERO(ls) = value
	case CBSCALE:
	    LS_CBSCALE(ls) = value
	case DATAMIN:
	    LS_DATAMIN(ls) = value
	case DATAMAX:
	    LS_DATAMAX(ls) = value
	case LOREJECT:
	    LS_LOREJECT(ls) = value
	case HIREJECT:
	    LS_HIREJECT(ls) = value
	case GAIN:
	    LS_GAIN(ls) = value
	case RGAIN:
	    LS_RGAIN(ls) = value
	case IGAIN:
	    LS_IGAIN(ls) = value
	case READNOISE:
	    LS_READNOISE(ls) = value
	case RREADNOISE:
	    LS_RREADNOISE(ls) = value
	case IREADNOISE:
	    LS_IREADNOISE(ls) = value
	case TBSCALE:
	    LS_TBSCALE(ls) = value
	case TBSCALEERR:
	    LS_TBSCALEERR(ls) = value
	case TBZERO:
	    LS_TBZERO(ls) = value
	case TBZEROERR:
	    LS_TBZEROERR(ls) = value
	default:
	    call error (0, "RG_LSETR: Unknown real parameter.")
	}
end


# RG_LSETS -- Set the value of a string parameter.

procedure rg_lsets (ls, param, str)

pointer	ls		# pointer to the intensity scaling structure
int	param		# parameter to be fetched
char	str[ARB]	# output string

int	index, ip
pointer	sp, temp
real	rval
int	fnldir(), strdic(), ctor(), rg_lstati()

begin
	call smark (sp)
	call salloc (temp, SZ_LINE, TY_CHAR)

	switch (param) {

	case BZSTRING:
	    ip = 1
	    index = strdic (str, str, SZ_LINE, LS_SCALING)
	    if (index > 0) {
		if (rg_lstati (ls, BSALGORITHM) == LS_NUMBER) {
		    call strcpy (str, LS_BZSTRING(ls), SZ_FNAME)
		    call rg_lseti (ls, BZALGORITHM, index)
		} else {
		    call strcpy (LS_BSSTRING(ls), LS_BZSTRING(ls), SZ_FNAME)
		    call rg_lseti (ls, BZALGORITHM, rg_lstati (ls, BSALGORITHM))
		}
	    } else if (ctor (str, ip, rval) > 0) {
		call strcpy (str, LS_BZSTRING(ls), SZ_FNAME)
		call rg_lsetr (ls, CBZERO, rval) 
		call rg_lseti (ls, BZALGORITHM, LS_NUMBER)
	    } else {
		call strcpy ("0.0", LS_BZSTRING(ls), SZ_FNAME)
		call rg_lsetr (ls, CBZERO, 0.0) 
		call rg_lseti (ls, BZALGORITHM, LS_NUMBER)
	    }
	case BSSTRING:
	    ip = 1
	    index = strdic (str, str, SZ_LINE, LS_SCALING)
	    if (index > 0) {
		call strcpy (str, LS_BSSTRING(ls), SZ_FNAME)
		call rg_lseti (ls, BSALGORITHM, index)
	    } else if (ctor (str, ip, rval) > 0) {
		call strcpy (str, LS_BSSTRING(ls), SZ_FNAME)
		call rg_lsetr (ls, CBSCALE, rval) 
		call rg_lseti (ls, BSALGORITHM, LS_NUMBER)
	    } else {
		call strcpy ("1.0", LS_BSSTRING(ls), SZ_FNAME)
		call rg_lsetr (ls, CBSCALE, 1.0) 
		call rg_lseti (ls, BSALGORITHM, LS_NUMBER)
	    }
	case CCDGAIN:
	    ip = 1
	    if (ctor (str, ip, rval) > 0) {
		call strcpy (str, LS_CCDGAIN(ls), SZ_FNAME)
		call rg_lsetr (ls, RGAIN, rval)
	        if (ctor (str, ip, rval) > 0)
		    call rg_lsetr (ls, IGAIN, rval)
		else
		    call rg_lsetr (ls, IGAIN, 1.0)
		call rg_lsetr (ls, GAIN, INDEFR)
	    } else {
		call sscan (str)
		    call gargwrd (Memc[temp], SZ_LINE)
		call strcpy (Memc[temp], LS_CCDGAIN(ls), SZ_FNAME)
		call rg_lsetr (ls, RGAIN, 1.0)
		call rg_lsetr (ls, IGAIN, 1.0)
		call rg_lsetr (ls, GAIN, INDEFR)
	    }
	case CCDREAD:
	    ip = 1
	    if (ctor (str, ip, rval) > 0) {
		call strcpy (str, LS_CCDREAD(ls), SZ_FNAME)
		call rg_lsetr (ls, RREADNOISE, rval)
	        if (ctor (str, ip, rval) > 0)
		    call rg_lsetr (ls, IREADNOISE, rval)
		else
		    call rg_lsetr (ls, IREADNOISE, 0.0)
		call rg_lsetr (ls, READNOISE, INDEFR)
	    } else {
		call sscan (str)
		    call gargwrd (Memc[temp], SZ_LINE)
		call strcpy (Memc[temp], LS_CCDREAD(ls), SZ_FNAME)
		call rg_lsetr (ls, RREADNOISE, 0.0)
		call rg_lsetr (ls, IREADNOISE, 0.0)
		call rg_lsetr (ls, READNOISE, INDEFR)
	    }

	case IMAGE:
	    call imgcluster (str, Memc[temp], SZ_FNAME)
	    index = fnldir (Memc[temp], LS_IMAGE(ls), SZ_FNAME)
	    call strcpy (Memc[temp+index], LS_IMAGE(ls), SZ_FNAME)
	case REFIMAGE:
	    call imgcluster (str, Memc[temp], SZ_FNAME)
	    index = fnldir (Memc[temp], LS_REFIMAGE(ls), SZ_FNAME)
	    call strcpy (Memc[temp+index], LS_REFIMAGE(ls), SZ_FNAME)
	case REGIONS:
	    call strcpy (str, LS_REGIONS(ls), SZ_FNAME)
	case DATABASE:
	    index = fnldir (str, LS_DATABASE(ls), SZ_FNAME)
	    call strcpy (str[index+1], LS_DATABASE(ls), SZ_FNAME)
	case OUTIMAGE:
	    call strcpy (str, LS_OUTIMAGE(ls), SZ_FNAME)
	case SHIFTSFILE:
	    call strcpy (str, LS_SHIFTSFILE(ls), SZ_FNAME)
	case PHOTFILE:
	    call strcpy (str, LS_PHOTFILE(ls), SZ_FNAME)
	case RECORD:
	    call strcpy (str, LS_RECORD(ls), SZ_FNAME)

	default:
	    call error (0, "RG_LSETS: Unknown string parameter.")
	}

	call sfree (sp)
end