1
/*===========================================================================
2
Copyright (C) 1988-2009 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
.AUTHOR P.Grosbol ESO/IPG
32
.KEYWORDS Encode data to FITS
33
.PURPOSE Convert disk files in internal MIDAS format to FITS
34
and write them to either tape or disk.
35
.COMMENT Formats supported are : FITS 32 and tables
36
.VERSION 1.0 1988-Dec-01 : Creation, PJG
37
.VERSION 1.1 1989-Jan-25 : Modify input and add print, PJG
38
.VERSION 1.2 1989-May-03 : Open tape in READ_WRITE mode, PJG
39
.VERSION 1.3 1989-Jun-12 : Change definition of unit+kunit, PJG
40
.VERSION 1.4 1989-Oct-08 : Check both 'tape' and 'TAPE', PJG
41
.VERSION 1.5 1990-Feb-19 : Search all catalog, PJG
42
.VERSION 1.65 1990-Mar-30 : Check output file extension, PJG
43
.VERSION 1.7 1990-Jul-04 : Filenames in catalog + U/L-case, PJG
44
.VERSION 1.71 1990-Aug-31 : Force tables to ASCII, PJG
45
.VERSION 1.75 1990-Sep-14 : Correct U/L-case + list no, PJG
46
.VERSION 2.00 1991-Mar-07 : Change structures, PJG
47
.VERSION 2.10 1991-Jul-02 : Check disk-file open status, PJG
48
.VERSION 2.18 1991-Sep-26 : Correct catalog access, PJG
49
.VERSION 2.20 1992-Mar-17 : Correct '.cat' + add physical dev., PJG
50
.VERSION 2.35 1992-Jun-17 : Add Rewind option + default no rewind, PJG
51
.VERSION 2.40 1992-Jul-23 : Give position error + total size, PJG
52
.VERSION 2.45 1992-Aug-31 : Skip deleted CAT-entries correctly, PJG
53
.VERSION 2.46 1992-Dec-02 : Translating environment variables, CG
54
.VERSION 2.50 1993-Oct-29 : Update to new SC + prototypes, PJG
55
.VERSION 2.55 1993-Nov-12 : Replace SCKRDC with SCKGETC, PJG
56
.VERSION 2.65 1996-Jul-08 : Check for "" in input file name, PJG
57
.VERSION 2.70 1996-Jul-11 : Use SCETER for exit, test "" in output, PJG
58
.VERSION 2.80 1997-Jan-08 : Improve file-check for ASCII-catalogs, PJG
59
.VERSION 2.85 1997-Jan-22 : Change test of output file/dev, PJG
62
---------------------------------------------------------------------*/
65
#define _XOPEN_SOURCE 1
76
#include <midas_def.h>
78
#define MXCAT 101 /* Max. size of all catalogue names */
79
#define NOFF 1000 /* File no. increment between cat's */
83
char file[129], name[129], tpname[129], line[81], fin[129];
84
char xname[32], devn[5], opt[4];
85
char cat[MXCAT], cname[129], fext[32];
86
char *pc, *pfile, *pe, *fout, *flist, *ext, *wext, *nfn;
87
char devt, ftype, ffmt, ctype;
88
int iparm[5], cinfo[11], unit[4];
89
int did, err, mfd, mft, mff, den, bf, catno, noff, fid;
90
int no, n, nval, list, null, ncat, nof, mr;
92
struct filestatus status;
94
SCSPRO("OUTTAPE"); /* initiate MIDAS */
96
*cname = '\0'; wext = ".mt"; ext = "cat";
97
catno = 5; noff = 0; ctype = '\0';
98
nof = 0; mr = 0; fout = (char *) 0; mff = 0;
101
SCKGETC("P1",1,128,&nval,file); /* read file list */
103
list = 1; ftype = 'C';
104
if (isalpha(*pc) || *pc=='"') { /* is first char. a letter */
107
while (*pc && *pc!='"') pc++;
110
else while (*pc && *pc!=',') pc++;
112
if (!(*pc) || *pc==',') { /* copy file name to 'cname' */
113
if (*pc) *pc++ = '\0';
117
if ((pe=strrchr(cname,'.'))) { /* check if with extension */
118
*pe++ = '\0'; ext = pe;
119
ftype = '\0'; list = 0;
120
if (!strcmp(ext,"cat")) {
121
ftype = 'C'; list = 1;
125
SCCSHO(cname,&n,&ncat);
126
strcpy(line,cname); strcat(line,".cat");
127
fid = osaopen(line,READ); /* find catalog type */
128
osaread(fid, line, 5);
135
SCKRDI("CATALINF",1,11,&nval,cinfo,unit,&null);
136
SCKRDC("CATALOGS",1,1,MXCAT,&nval,cat,unit,&null);
138
for (n=5; n<10; n++) nval = (nval || 0<cinfo[n]);
140
SCETER(1,"Error: Active catalog must be SET if input starts with digit");
143
flist = (*pc) ? pc : "*"; /* default to all 1-9999 */
144
if (deflist(flist)) /* initiate file list */
145
SCETER(2,"Error: bad number list of catalog entries");
147
pc = ext; n = 0; /* copy full extension */
149
while ((fext[n++] = *pc++));
151
SCKRDI("INPUTI",1,2,&nval,iparm,unit,&null); /* density and block */
152
den = (nval<1) ? 6250 : iparm[0];
153
bf = (nval<2) ? 10 : iparm[1];
154
if (bf<1) bf = 1; else if (10<bf) bf = 10;
156
SCKGETC("P2",1,128,&nval,name); /* read device/file */
159
while (*pc) if (*pc=='"') pc++; else *pe++ = *pc++;
163
devn[n] = (('A'<=name[n]) && (name[n]<='Z')) ? name[n]+'a'-'A' : name[n];
166
if (strncmp(devn,"tape",4)) {
167
CGN_LOGNAM(name,tpname,128);
168
strcpy(name,tpname); /* copy actual name */
169
if (osfinfo(name,&status) == 0) /* if name exists */
170
devt = (S_ISCHR(status.protection)) ? 'B' : 'S'; /* block:stream */
171
else /* check if remote tape */
172
devt = (((pc=strchr(name,':')) || (pc=strchr(name,'!'))) &&
173
(*(pc+1)=='/')) ? 'B' : 'S';
177
while (*pc) { if (('A'<=*pc) && (*pc<='Z')) *pc += 'a'-'A'; pc++; }
178
if (osfphname(name,tpname)) { /* get physical name of device */
180
while (*pc) { if (('a'<=*pc) && (*pc<='z')) *pc += 'A'-'a'; pc++; }
181
if (osfphname(name,tpname))
182
SCETER(3,"Error: device not defined");
184
devt = 'B'; /* block type device */
185
strcpy(name,tpname); /* copy physical name */
189
did = dopen(name,READ_WRITE,devt,den);
190
if (did < 0) { /* open block device */
192
SCETER(4,"Error: cannot open tape-device");
195
else { /* else stream device */
197
while(*pc && *pc!='.') pc++;
199
*pc++ = '\0'; n = 0; xname[n++] = '.';
200
while(*pc && *pc!='.') xname[n++] = *pc++;
201
xname[n] = '\0'; if (1<n) wext = xname;
205
opt[0] = 'N'; /* append flag : No append,Append,Rewind */
206
opt[1] = 'S'; /* print flag : No, Short, Full */
207
opt[2] = 'N'; /* cut flag : No cuts, Cuts used */
208
SCKGETC("P3",1,3,&nval,opt); /* read options */
210
ffmt = 'O'; /* format flag is fixed to: Original */
212
if (devt=='B') { /* position tape */
214
if (opt[0]=='A') err = dapos(-1);
215
else if (opt[0]=='R') err = dapos(0);
217
SCETER(5,"Error: cannot position tape device");
220
SCECNT("PUT",&no,&nval,&nval); /* disable SC errors */
222
do { /* go through all files */
223
if (ftype=='C') { /* files from catalog */
225
while (catno<5 && (cinfo[catno]<=0 || cinfo[catno+5]<=0)) catno++;
226
if (5<=catno) break; /* no more cat's - finish */
228
for (n=cinfo[catno++]-1; cat[n]!=' ' && no<cinfo[10];)
229
cname[no++] = cat[n++];
232
SCCSHO(cname,&n,&ncat); /* get last entry in cat. */
233
fid = osaopen(cname,READ); /* find catalog type */
234
osaread(fid, line, 5);
237
sprintf(line,"Start catalogue(%c) : >%s< with %d entries"
242
if (!getlist(&no) || ncat<no) { /* get new file no. */
244
noff += NOFF * (ncat/NOFF + 1);
248
if (no<1) continue; /* zero and negative entries */
250
if (SCCFND(cname,no,fin)) {
251
sprintf(line,"Warning: Cannot open catalog file: %s, %d",cname,no);
255
if (*fin==' ') continue; /* file does not exist */
257
else { /* explicit files */
259
if (!getlist(&no)) break; /* no more files - exit */
260
outname(cname,no,'o'); /* 'o' like 'original version' */
261
nfn = newfn('I',fext);
263
else { nfn = cname; pc = ext; *(--pc) = '.'; }
268
err = SCFINF(fin,1,iparm); /* get type of MIDAS file */
269
if (err == ERR_NORMAL) {
272
err = SCFINF(fin,4,iparm); /* check if FITS format */
274
sprintf(line,"Warning: >%s< skipped as already in FITS format",fin);
275
SCTPUT(line); continue;
278
else if (ctype=='A') {
279
fid = osaopen(fin,READ); /* open text file */
280
n = osaread(fid,line,80);
282
if (!strncmp(line,"SIMPLE =",9)) {
283
sprintf(line,"Warning: >%s< skipped as already in FITS format",fin);
284
SCTPUT(line); continue;
289
sprintf(line,"Warning: Cannot open or find type of file >%s<",fin);
290
SCTPUT(line); continue;
293
if (mft==F_IMA_TYPE) { /* if image get data format */
298
if (devt=='S') { /* output file open it */
300
outname(name,no+noff,'o');
301
fout = newfn('I',wext);
304
fout = name; pc = name;
305
while (*pc || *pc=='.') pc++;
308
did = dopen(fout,WRITE,devt,den);
309
if (did < 0) { /* open disk file */
311
SCETER(6,"Error: cannot open disk-file");
318
switch (mft) { /* check which format */
319
case F_IMA_TYPE : /* file is an image */
320
SCFOPN(fin,mff,0,mft,&mfd); /* open image file */
321
err = fitswhd(mfd,mft,mff,fin,ffmt,opt[2],BFITS); /* prime hdr */
323
err = fitswdm(mfd,mff,ffmt); /* write prime data */
324
SCFCLO(mfd); /* close image file */
326
case F_TBL_TYPE : /* file is a table */
327
TCTOPN(fin,F_I_MODE,&mfd); /* open table file */
328
err = fitswhd(mfd,mft,mff,fin,ffmt,opt[2],BFITSE); /* prime hdr */
330
err = fitswhd(mfd,mft,mff,fin,ffmt,opt[2],ATABLE); /* extension */
332
else err = fitswbt(mfd); /* write Binary table */
333
TCTCLO(mfd); /* close table file */
335
case F_FIT_TYPE : /* file is a fit-format */
336
SCFOPN(fin,mff,0,mft,&mfd); /* open fit file */
337
err = fitswhd(mfd,mft,mff,fin,ffmt,opt[2],BFITSE);
338
SCFCLO(mfd); /* close fit file */
340
case F_ASC_TYPE : /* file is ASCII char. */
342
err = fitswhd(mfd,mft,mff,fin,ffmt,opt[2],BFITSE); /* prime hdr. */
345
default : sprintf(line,"Warning: file %-16s unknown type>%d<",
352
sprintf(line,"Warning: file %-16s NOT completed - device error",fin);
357
sprintf(line,"Warning: file %-16s NOT written",fin);
358
SCTPUT(line); continue;
364
sprintf(line,"File %-16s written to disk> %s",fin,fout);
367
sprintf(line,"File %-16s written to tape with %5d records",fin,n);
373
tmbyte = (2880.0/1048576.0) * mr;
374
if (tmbyte<0.01) tmbyte = 0.01;
375
sprintf(line,"Total %d files written with %8.2f Mbytes",nof,tmbyte);
377
if (devt!='S') dclose(did);