1
C @(#)sort.for 19.1 (ES0-DMD) 02/25/03 13:23:50
2
C===========================================================================
3
C Copyright (C) 1995 European Southern Observatory (ESO)
5
C This program is free software; you can redistribute it and/or
6
C modify it under the terms of the GNU General Public License as
7
C published by the Free Software Foundation; either version 2 of
8
C the License, or (at your option) any later version.
10
C This program is distributed in the hope that it will be useful,
11
C but WITHOUT ANY WARRANTY; without even the implied warranty of
12
C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
C GNU General Public License for more details.
15
C You should have received a copy of the GNU General Public
16
C License along with this program; if not, write to the Free
17
C Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
20
C Corresponding concerning ESO-MIDAS should be addressed as follows:
21
C Internet e-mail: midas@eso.org
22
C Postal address: European Southern Observatory
23
C Data Management Division
24
C Karl-Schwarzschild-Strasse 2
25
C D 85748 Garching bei Muenchen
27
C===========================================================================
29
SUBROUTINE SORTER (WORK, MAXWRK, SORT, HIGH, LOW, INDEX,
32
C=======================================================================
34
C Subroutine to read in any of the data files created by DAOPHOT and
35
C sort the stars according to magnitude, position, ID number,
38
C OFFICIAL DAO VERSION: 1991 April 18
42
C WATCH (INPUT) governs whether information relating to the progress
43
C of the reductions is to be typed on the terminal screen
46
C WATCH is a user-definable optional parameter.
48
C=======================================================================
54
C MAX is the number of different sorts which are possible
55
C (currently 5: by ID number, by X, by Y, by magnitude, and
56
C by OTHER [e.g. SHARP, ROUND, CHI, number of iterations]).
58
C MAXSTR is the maximum number of stars permitted in a data file.
59
C It is limited by the size of the WORK array in DAOPHOT:
60
C WORK must contain 282 bytes per star: 4 bytes each for SORT,
61
C INDEX, NLINE1, and, NLINE2, and 133 bytes each for LINE1 and
64
C MAXITM is the maximum number of output data per line of an output.
65
C (currently 15, realized in PHOTOMETRY)
67
INTEGER MAXWRK, MAXSTR, MAXITM
70
REAL DATUM(MAXITM), WORK(MAXWRK), SORT(MAXSTR)
71
INTEGER LOW(MAXSTR), HIGH(MAXSTR), INDEX(MAXSTR)
77
CHARACTER*30 FILE, SWITCH
78
CHARACTER CASE*4, ANSWER*1
79
REAL LOBAD, HIBAD, THRESH, AP1, PHPADU, READNS, FRAD
80
REAL WATCH, WHICH, FLIP
81
INTEGER I, J, K, L, N, NL, NCOL, NROW, ISTAT, ITEMS, NSTAR
82
INTEGER NLO, MODE, AMODE
84
C-----------------------------------------------------------------------
90
C Find out how the user wants to sort.
94
. 11X, ' The following sorts are currently possible:'//
95
. 11X, '+/- 1 By increasing/decreasing star ID number'//
96
. 11X, '+/- 2 By increasing/decreasing X coordinate'//
97
. 11X, '+/- 3 By increasing/decreasing Y coordinate'//
98
. 11X, '+/- 4 By increasing/decreasing magnitude'//
99
. 11X, '+/- n By increasing/decreasing OTHER (n <= ',
101
CALL GETDAT ('Which do you want?', WHICH, 1)
102
IF (WHICH .LT. -1.E38) RETURN ! CTRL-Z was entered
106
IF (MODE.LT.0) AMODE = -MODE
107
IF ((AMODE .LT. 1) .OR. (AMODE .GT. MAXITM))
108
. RETURN ! Invalid response
110
FLIP=FLOAT(MODE/AMODE)
113
C Get input file name, open the file, and read its header.
116
950 CALL GETNAM ('Input file name:', FILE)
117
IF ((FILE .EQ. 'END OF FILE') .OR. (FILE .EQ. 'GIVE UP')) RETURN
118
CALL INFILE (2, FILE, ISTAT)
119
IF (ISTAT .NE. 0) THEN
120
CALL STUPID ('Error opening input file '//FILE)
125
C Get output file name and open the file.
127
FILE = SWITCH(FILE, CASE('.srt'))
128
960 CALL GETNAM ('Output file name:', FILE)
129
IF ((FILE .EQ. 'END OF FILE') .OR. (FILE .EQ. 'GIVE UP')) THEN
134
CALL OUTFIL (3, FILE, ISTAT)
135
IF (ISTAT .NE. 0) THEN
136
CALL STUPID ('Error opening output file '//FILE)
141
CALL GETYN ('Do you want the stars renumbered?', ANSWER)
142
IF (ANSWER .EQ. 'E') THEN
147
CALL RDHEAD (2, NL, NCOL, NROW, LOBAD, HIBAD, THRESH, AP1,
148
. PHPADU, READNS, FRAD)
149
IF (NL .LE. 0) GO TO 1010 ! No header in input
152
C Copy input file header to output file.
155
IF (FRAD .GT. 0.001) ITEMS=7
156
CALL WRHEAD (3, NL, NCOL, NROW, ITEMS, LOBAD, HIBAD, THRESH,
157
. AP1, PHPADU, READNS, FRAD)
160
IF (WATCH .LT. 0.5) GO TO 1020
161
IF (MODE .LE. 4) WRITE (6,611)
162
611 FORMAT (/22X, 'STAR', 6X, 'X', 8X, 'Y', 4X, 'MAG.(1)')
163
IF (MODE .GE. 5) WRITE (6,612) MODE
164
612 FORMAT (/17X, 'STAR', 6X, 'X', 8X, 'Y', 4X, 'MAG.(1)',
168
C-----------------------------------------------------------------------
172
C Read the input file in line by line, verbatim. Pack the contents
173
C of the line into the REAL array WORK, keeping track of the
174
C lower and upper limits in the arrays LINELO and LINEHI. At the
175
C same time, extract the the particular datum
176
C according to which we wish to sort. Sort these data. Then write
177
C the file out again, line by line, verbatim, but in the new order.
182
2000 CALL RDCHAR (2, TEXT, N, ISTAT)
183
IF (ISTAT .EQ. 1) GO TO 2100 ! END OF FILE
184
IF (ISTAT .NE. 0) THEN
185
CALL STUPID ('Unable to read input file.')
190
IF (N .LE. 1) GO TO 2000 ! Blank line encountered
191
READ (TEXT(2:N), 901, IOSTAT=ISTAT)
192
. (DATUM(J), J=1,(N-5)/9+1)
193
901 FORMAT (F5.0, 14F9.0)
194
IF (ISTAT .NE. 0) THEN
195
CALL STUPID ('Unable to read data from input file.')
201
IF (I .GT. MAXSTR) THEN
203
77 FORMAT (I10, ' stars is all I have room for. Sorry!')
204
CALL STUPID (TEXT(1:50))
209
HIGH(I) = NLO + (N-2)/4 + 1
210
IF (HIGH(I) .GT. MAXWRK) THEN
212
CALL STUPID (TEXT(1:50))
218
READ (TEXT(2:N),66) (WORK(J), J=LOW(I),NLO)
222
2012 CALL RDCHAR (2, TEXT, N, ISTAT)
223
IF (ISTAT .NE. 0) THEN
224
CALL STUPID ('Unable to read input file.')
229
IF (N .LE. 1) GO TO 2012
230
READ (TEXT(2:N), 902, IOSTAT=ISTAT)
231
. (DATUM(J), J=16,19+(N-25)/9)
232
902 FORMAT (F12.3, 2F6.3, 12(F8.3, 1X))
233
IF (ISTAT .NE. 0) THEN
234
CALL STUPID ('Unable to read data from input file.')
239
HIGH(I) = HIGH(I) + (N-2)/4+1
240
IF (HIGH(I) .GT. MAXWRK) THEN
242
CALL STUPID (TEXT(1:50))
246
READ (TEXT(2:N),66) (WORK(J), J=NLO+1,HIGH(I))
250
SORT(I)=FLIP*DATUM(MODE)
251
GO TO 2000 ! End of input loop
255
2100 NSTAR=I ! Number of stars
257
IF (NSTAR .LE. 0) THEN
258
CALL STUPID ('No stars in input file.')
261
CALL QUICK (SORT, NSTAR, INDEX)
263
C The vector SORT is now arranged in order of increasing or decreasing
264
C whatever, and the vector INDEX now contains the ordinal position in
265
C the input file of the stars, in order of increasing or decreasing
268
C Now write the data out again.
273
NLO = HIGH(J) - LOW(J) + 1
275
WRITE (TEXT(1:K),67) (WORK(L), L=LOW(J),HIGH(J))
278
C If ID numbers are to be changed, insert the new ID into the text.
280
IF (ANSWER .EQ. 'Y') WRITE (TEXT(2:6),68) I
282
WRITE (3,320) TEXT(1:K)
284
IF (WATCH .LT. 0.5) GO TO 2110
285
IF (MODE .LE. 4) THEN
286
WRITE (6,620) TEXT(2:33)
287
620 FORMAT (21X, A32)
289
WRITE (6,621) TEXT(2:33), FLIP*SORT(I)
290
621 FORMAT (16X, A32, 1X, F9.3)
293
NLO = (HIGH(J) - LOW(J) + 1)/2
295
WRITE (TEXT(1:K),67) (WORK(L), L=LOW(J),LOW(J)+NLO-1)
296
IF (ANSWER .EQ. 'Y') WRITE (TEXT(2:6),68) I
297
WRITE (3,320) TEXT(1:K)
298
IF (WATCH .GE. 0.5) THEN
299
IF (MODE .LE. 4) THEN
300
WRITE (6,620) TEXT(2:33)
302
WRITE (6,621) TEXT(2:33), FLIP*SORT(I)
305
WRITE (TEXT(1:K),67) (WORK(L), L=LOW(J)+NLO,HIGH(J))
306
WRITE (3,320) TEXT(1:K)
310
2110 CONTINUE ! End of output loop
312
C-----------------------------------------------------------------------