~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to prim/dio/libsrc/dataio.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1995-2005 European Southern Observatory (ESO)
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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, 
 
17
  MA 02139, USA.
 
18
 
 
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 
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
29
.COPYRIGHT   (c)  1995-2005  European Southern Observatory
 
30
.IDENT       dataio.c
 
31
.LAUGUAGE    C
 
32
.AUTHOR      P.Grosbol   ESO/IPG
 
33
.KEYWORDS    data I/O routines, tape, stream
 
34
.COMMENT     buffered I/O for data format IHAP/FITS routiens
 
35
.VERSION     1.0  1988-Dec-10 : Creation,   PJG 
 
36
.VERSION     1.1  1989-Jan-17 : Add count of output blocks,   PJG 
 
37
.VERSION     1.2  1989-May-24 : Specify blocksize for disk/tape, PJG 
 
38
.VERSION     1.4  1989-Nov-06 : Upgrade for fixed-block devices , PJG 
 
39
.VERSION     1.5  1990-Feb-04 : Intiate cvb and change call-seq., PJG 
 
40
.VERSION     1.6  1990-Mar-30 : Insert error messages for OS-calls, PJG 
 
41
.VERSION     1.8  1991-Jan-25 : Change include files. FO
 
42
.VERSION     1.9  1991-Nov-19 : Correct error in write with fix-block, PJG
 
43
.VERSION     2.0  1993-Oct-28 : Update to new SC and prototypes, PJG
 
44
.VERSION     2.1  1994-Jun-29 : Check error only in first read, PJG
 
45
.VERSION     2.2  1995-Jan-23 : Skip 'osmsg' for osdopen, PJG
 
46
.VERSION     2.3  1996-Jan-09 : Check read status for disk files, PJG
 
47
 050922         last modif
 
48
 
 
49
---------------------------------------------------------------------*/
 
50
 
 
51
#include   <stdio.h>
 
52
 
 
53
#include   <filedef.h>
 
54
#include   <computer.h>
 
55
#include   <fitsfmt.h>
 
56
#include   <fitsdef.h>
 
57
#include   <midas_def.h>
 
58
 
 
59
#define   MXBUF        30720       /* size of internal buffer       */
 
60
 
 
61
char               *osmsg();       /* OS error message              */
 
62
 
 
63
static    int           fdi = -1;       /* file descriptor of input file */
 
64
static    int           gdi;            /* backup of fdi */
 
65
static    int           fdo = -1;       /* file descriptor of output file */
 
66
static    int           gdo;            /* backup of fdo */
 
67
static    int            fd = -1;       /* file descriptor of tape */
 
68
static    int          apos;       /* absolute position of tape     */
 
69
static    int           fmt;       /* format of data file FITS/IHAP */
 
70
static    int          widx;       /* current write index in buffer */
 
71
static    int          ridx;       /* current read index in buffer  */
 
72
static    int          rlvb;       /* last of valid byte in read buffer  */
 
73
static    int          wlvb;       /* last of valid byte in write buffer  */
 
74
static    int        rbsize;       /* block size of reads           */
 
75
static    int        wbsize;       /* block size of writes          */
 
76
static    int         devbs;       /* device block size             */
 
77
static    int         mxbuf;       /* actual size of buffer         */
 
78
static    int          nopb;       /* no. of output bytes           */
 
79
static    int           lrs;       /* last read status              */
 
80
static    char          dev;       /* type of input device          */
 
81
static    char       *rbptr = (char *) 0;       /* pointer to read buffer  */
 
82
static    char       *wbptr = (char *) 0;       /* pointer to write buffer */
 
83
 
 
84
/*
 
85
 
 
86
*/
 
87
 
 
88
#ifdef __STDC__
 
89
int dopen(char * name , int iomode , char type , int den)
 
90
#else
 
91
int dopen(name,iomode,type,den)
 
92
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
93
.PURPOSE       open device 
 
94
.RETURN        file descriptor - if error status of open call
 
95
---------------------------------------------------------------------*/
 
96
char        *name;             /* IN: name of device to open         */
 
97
int        iomode;             /* IN: I/O open mode 0:read, 1:write  */
 
98
char         type;             /* IN: device type - Block, Stream    */
 
99
int           den;             /* IN: tape density for tapes (B)     */
 
100
#endif
 
101
{
 
102
int  myfd;
 
103
 
 
104
 
 
105
 
 
106
 
 
107
dev = type; apos = 0; devbs = 1;
 
108
mxbuf = (MAXIO1 < MXBUF) ? MAXIO1 : MXBUF;  /* determine buffer size */
 
109
 
 
110
if (dev == 'S') 
 
111
   {                           /* open disk file      */
 
112
   osfop('F',FITSLR);               /* force 2880 byte fix records */
 
113
   myfd = osdopen(name,iomode);
 
114
   if (myfd == -1) return (-1);
 
115
 
 
116
   if (iomode == 0)
 
117
      {
 
118
      gdi = fdi = myfd;
 
119
      }
 
120
   else
 
121
      {
 
122
      gdo = fdo = myfd;
 
123
      }
 
124
   }
 
125
else
 
126
   {                              /* open block dev/tape         */
 
127
   myfd = osuopen(name,iomode,den);
 
128
   if (myfd == -1)
 
129
      {
 
130
      SCTPUT(osmsg()); return (-1);
 
131
      }
 
132
   devbs = osubsize(myfd);
 
133
   mxbuf = (mxbuf/devbs) * devbs;
 
134
   fd = myfd;
 
135
   }
 
136
 
 
137
/* allocate data buffer */
 
138
 
 
139
if (iomode == 0)
 
140
   {
 
141
   if (rbptr == (char *) 0) rbptr = (char *)osmmget(mxbuf);
 
142
   }
 
143
else
 
144
   {
 
145
   if (wbptr == (char *) 0) wbptr = (char *)osmmget(mxbuf);
 
146
   }
 
147
 
 
148
return myfd;
 
149
}
 
150
 
 
151
int drinit()
 
152
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
153
.PURPOSE       initiate read buffer and check data format 
 
154
.RETURN        data format = 1:FITS, 0:Unknown, -1: NO data
 
155
---------------------------------------------------------------------*/
 
156
{
 
157
char   *pc,*str;
 
158
 
 
159
 
 
160
 
 
161
ridx = 0; rlvb = 0; lrs = 1;
 
162
if (dev == 'S') 
 
163
   {
 
164
   rlvb = osdread(fdi,rbptr,mxbuf);
 
165
   if (rlvb<mxbuf) lrs = 0;
 
166
   }
 
167
else
 
168
   rlvb = osuread(fd,rbptr,mxbuf);
 
169
 
 
170
if (rlvb<0)
 
171
   {
 
172
   SCTPUT(osmsg()); 
 
173
   return -1;
 
174
   }
 
175
 
 
176
pc = rbptr; str = "SIMPLE  =";
 
177
while ((*pc) == (*str)) pc++, str++;
 
178
 
 
179
if (*pc==' ' && !(*str))
 
180
   {                                    /* check if FITS format */
 
181
   rbsize = (mxbuf/FITSLR) * FITSLR;    /* define best read size  */
 
182
   if (dev!='S') 
 
183
      {
 
184
      rbsize = (devbs==1) ? rbsize+4 : ((mxbuf-FITSLR)/devbs)*devbs;
 
185
      }
 
186
   cvinit(); fmt = FITS; 
 
187
   return fmt;
 
188
   }
 
189
 
 
190
return 0;
 
191
}
 
192
 
 
193
int dread(ppbuf,no)
 
194
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
195
.PURPOSE       read 'no' bytes from device
 
196
.RETURN        no. of bytes read, -1: EOF, -2:error
 
197
---------------------------------------------------------------------*/
 
198
char    **ppbuf;                /* pointer to buffer pointer         */
 
199
int          no;                /* no. of bytes to read              */
 
200
{
 
201
register char  *pbs,*pbe;
 
202
 
 
203
int      nb,n;
 
204
 
 
205
 
 
206
if (rlvb < ridx+no)
 
207
   {                            /* too few data - read next block    */
 
208
   n = rlvb - ridx; rlvb = n;
 
209
   pbs = rbptr; pbe = &rbptr[ridx];
 
210
   while (n--) *pbs++ = *pbe++;         /* move bytes to start of buffer */
 
211
   ridx = 0;
 
212
   while (lrs && rlvb<no) 
 
213
      {                                 /* read enough data for request  */
 
214
      if (dev == 'S')
 
215
         {
 
216
         nb = rbsize - rlvb;            /* devbs = 1 */
 
217
         n = osdread(fdi,pbs,nb);
 
218
         if (n<nb) lrs = 0;
 
219
         }
 
220
      else
 
221
         {
 
222
         nb = ((rbsize-rlvb-1)/devbs + 1) * devbs;
 
223
         n = osuread(fd,pbs,nb);
 
224
         }
 
225
      
 
226
      if (n > 0)
 
227
         {
 
228
         rlvb += n; pbs += n;
 
229
         }
 
230
      else
 
231
         return -1;
 
232
      }
 
233
   if (rlvb < no) no = rlvb;
 
234
   }
 
235
 
 
236
*ppbuf = &rbptr[ridx];
 
237
ridx += no;
 
238
 
 
239
return  no;
 
240
}
 
241
 
 
242
int dwinit(bf)
 
243
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
244
.PURPOSE       initiate write buffer and define blocking factor
 
245
.RETURN        always 0
 
246
---------------------------------------------------------------------*/
 
247
int          bf;                /* FITS blocking factor              */
 
248
{
 
249
widx = 0; wlvb = 0; nopb = 0;
 
250
 
 
251
if (bf<1)
 
252
   wbsize = FITSLR;
 
253
else
 
254
   wbsize = (10<bf) ? 10*FITSLR : bf*FITSLR;
 
255
 
 
256
if (mxbuf<wbsize) wbsize = (mxbuf/FITSLR) * FITSLR;
 
257
if (dev!='S' && devbs!=1) wbsize = (mxbuf/devbs)*devbs;
 
258
 
 
259
cvinit();
 
260
return 0;
 
261
}
 
262
 
 
263
int dwrite(pbuf,no)
 
264
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
265
.PURPOSE       write 'no' bytes to device
 
266
.RETURN        no. of bytes transfered or <0 error
 
267
---------------------------------------------------------------------*/
 
268
char      *pbuf;                /* pointer to data to be written     */
 
269
int          no;                /* no. of bytes to write             */
 
270
 
 
271
{
 
272
char      *pc;
 
273
 
 
274
int      nn,n;
 
275
 
 
276
if (no<1) return 0;
 
277
 
 
278
pc = &wbptr[widx];
 
279
widx += no; nn = no; nopb += no;
 
280
if (widx<wbsize) 
 
281
   {                                    /* space in buffer - transfer */
 
282
   while (no--) *pc++ = *pbuf++;
 
283
   }
 
284
 
 
285
else 
 
286
   {
 
287
   n = wbsize - widx + no; no -= n;
 
288
   while (n--) *pc++ = *pbuf++;
 
289
 
 
290
   if (dev == 'S')
 
291
      n = osdwrite(fdo,wbptr,wbsize);
 
292
   else
 
293
      n = osuwrite(fd,wbptr,wbsize);
 
294
   if (n != wbsize)
 
295
      {
 
296
      if (n<0) SCTPUT(osmsg());
 
297
      SCTPUT("Error: I/O-error in writting block");
 
298
      widx -= no; return -1;
 
299
      }
 
300
 
 
301
   while (wbsize<no) 
 
302
      {
 
303
      if (dev == 'S')
 
304
         n = osdwrite(fdo,pbuf,wbsize);
 
305
      else
 
306
         n = osuwrite(fd,pbuf,wbsize);
 
307
      if (n != wbsize) 
 
308
         {
 
309
         if (n<0) SCTPUT(osmsg());
 
310
         SCTPUT("Error: I/O-error in writting block");
 
311
         widx -= no; return -1;
 
312
         }
 
313
      pbuf += n; no -= n;
 
314
      }
 
315
 
 
316
   widx = no; pc = wbptr;
 
317
   while (no--) *pc++ = *pbuf++;
 
318
   }
 
319
 
 
320
return nn;
 
321
}
 
322
 
 
323
 
 
324
int ddcopy()
 
325
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
326
.PURPOSE       write 'no' bytes to device
 
327
.RETURN        no. of bytes transfered or <0 error
 
328
---------------------------------------------------------------------*/
 
329
{
 
330
char *pcin, *pcout;
 
331
 
 
332
int  n;
 
333
 
 
334
 
 
335
 
 
336
/*  we need read + write buffer */
 
337
 
 
338
if (rbptr == (char *) 0) rbptr = (char *)osmmget(mxbuf);
 
339
if (wbptr == (char *) 0) wbptr = (char *)osmmget(mxbuf);
 
340
 
 
341
n = 1;                          /* kick off the loop */
 
342
while (n>0)
 
343
   {  
 
344
   n = dread(&pcin,FITSLR);
 
345
   if (n > 0)
 
346
      {
 
347
      pcout = pcin;
 
348
      dwrite(pcout,n);
 
349
      }
 
350
   }
 
351
 
 
352
 
 
353
(void) dweof();
 
354
return 0;
 
355
}
 
356
 
 
357
 
 
358
int dclose(fid)
 
359
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
360
.PURPOSE       spool buffer to device and close it
 
361
.RETURN        status  0: OK, -1: error
 
362
---------------------------------------------------------------------*/
 
363
int  fid;               /* IN: file id returned from `dopen' */
 
364
 
 
365
{
 
366
if (dev != 'S')                 /* for tape ignore fid */
 
367
   {
 
368
   osuclose(fd, 0);
 
369
 
 
370
   if (rbptr != (char *) 0) 
 
371
      {
 
372
      (void) osmmfree(rbptr);
 
373
      rbptr = (char *) 0;
 
374
      }
 
375
   if (wbptr != (char *) 0)
 
376
      {
 
377
      (void) osmmfree(wbptr);
 
378
      wbptr = (char *) 0;
 
379
      }
 
380
   return 0;
 
381
   }
 
382
 
 
383
 
 
384
/* for FITS files on disk */
 
385
 
 
386
if ((fid == fdi) || (fid == gdi))
 
387
   {
 
388
   if (fdi != -1) osdclose(fdi);
 
389
   fdi = -1;
 
390
   if (rbptr != (char *) 0)             /* clear read buffer */
 
391
      {
 
392
      (void) osmmfree(rbptr);
 
393
      rbptr = (char *) 0;
 
394
      }
 
395
   }
 
396
else if ((fid == fdo) || (fid == gdo))
 
397
   {
 
398
   if (fdo != -1) osdclose(fdo);
 
399
   fdo = -1;
 
400
   if (wbptr != (char *) 0)             /* clear write buffer */
 
401
      {
 
402
      (void) osmmfree(wbptr);
 
403
      wbptr = (char *) 0;
 
404
      }
 
405
    }
 
406
else
 
407
   {
 
408
   char  tmp[80];
 
409
 
 
410
   (void) sprintf(tmp,"(FITS) dclose: bad file id (%d) passed...",fid);
 
411
   SCTPUT(tmp);
 
412
   if (rbptr != (char *) 0) 
 
413
      {
 
414
      (void) osmmfree(rbptr);
 
415
      rbptr = (char *) 0;
 
416
      }
 
417
   if (wbptr != (char *) 0)
 
418
      {
 
419
      (void) osmmfree(wbptr);
 
420
      wbptr = (char *) 0;
 
421
      }
 
422
   return (-1);
 
423
   }
 
424
return 0;
 
425
}
 
426
 
 
427
 
 
428
 
 
429
int dapos(no)
 
430
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
431
.PURPOSE       position on an absolute file no. on a magnetic tape
 
432
.RETURN        status  0: OK, -1: error
 
433
---------------------------------------------------------------------*/
 
434
int            no;                 /* absolute file no. on tape      */
 
435
 
 
436
{
 
437
 
 
438
if (dev == 'S') return -1;       /* cannot position Stream         */
 
439
 
 
440
apos = (no<0) ? osufseek(fd,0,FILE_END) : osufseek(fd,no,FILE_START);
 
441
if (apos<0) { SCTPUT(osmsg()); return -1; }
 
442
return 0;
 
443
}
 
444
 
 
445
int dskip(no)
 
446
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
447
.PURPOSE       skip files on a magnetic tape
 
448
.RETURN        status  0: OK, -1: error
 
449
---------------------------------------------------------------------*/
 
450
int            no;                 /* no. of files to be skipped     */
 
451
{
 
452
  if (dev == 'S') return -1;       /* cannot position stream         */
 
453
  if (no) apos = osufseek(fd,no,FILE_CURRENT);
 
454
  if (apos<0) { SCTPUT(osmsg()); return -1; }
 
455
  return 0;
 
456
}
 
457
 
 
458
#ifdef __STDC__
 
459
int dbfill(char val)
 
460
#else
 
461
int dbfill(val)
 
462
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
463
.PURPOSE       fill remaining part of FITS record with 'val'
 
464
.RETURN        status always 0: OK
 
465
---------------------------------------------------------------------*/
 
466
char         val;                  /* IN: value to fill block with   */
 
467
#endif
 
468
{
 
469
  char       *pc;
 
470
  int         nx;
 
471
 
 
472
  nx = ((nopb-1)/FITSLR+1)*FITSLR - nopb;
 
473
  nopb += nx;
 
474
  pc = &wbptr[widx];
 
475
  while (nx--) { *pc++ = val; widx++; }
 
476
 
 
477
  return 0;
 
478
}
 
479
 
 
480
int dweof()
 
481
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
482
.PURPOSE       flush data in internal buffer and close current file
 
483
.RETURN        no. of records written, -1: error
 
484
---------------------------------------------------------------------*/
 
485
{
 
486
int     n,i;
 
487
 
 
488
n = (0<widx) ? (((widx-1)/devbs)+1)*devbs : 0;
 
489
for (i=widx; i<n; i++) wbptr[i] = '\0';
 
490
 
 
491
if (dev == 'S') 
 
492
   {
 
493
   if (fdo != -1) 
 
494
      {
 
495
      n = osdwrite(fdo,wbptr,n);
 
496
      osdclose(fdo);
 
497
      }
 
498
   fdo = -1;
 
499
   } 
 
500
else 
 
501
   {
 
502
   if (fd != -1) 
 
503
      {
 
504
      n = osuwrite(fd,wbptr,n);
 
505
      if (n<0) { SCTPUT(osmsg()); osufclose(fd); return -1; }
 
506
      osufclose(fd);
 
507
      }
 
508
   fd = -1;
 
509
   }
 
510
nopb += n - widx;
 
511
  
 
512
return nopb/FITSLR;
 
513
}
 
514