1
/*===========================================================================
2
Copyright (C) 1995-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
/*+++++++++++++++++++ CGN interfaces part D +++++++++++++++++++++++++++++++
30
.IDENTIFICATION Module cgnd.c
32
holds CGN_INDEXK, _JNDEXC, _JNDEXS, _CNVT, _xCNVT, _EXIST, _IBUILD
33
.AUTHOR K. Banse ESO - Garching
35
name translation, parsing, filling
36
.ENVIRONMENT VMS and UNIX
41
-----------------------------------------------------------------------------*/
50
#include <sys/types.h>
53
static int nofiles = -1;
61
int CGN_INDEXK(char * s , char t , int count)
63
int CGN_INDEXK(s,t,count)
64
/*++++++++++++++++++++++++++++++++++++++++++++++++++
66
returns position of single character in input string
68
returns position of single character in input string, -1 if not there
69
--------------------------------------------------*/
70
char *s; /* IN: input string */
71
char t; /* IN: test character */
72
int count; /* IN: counter */
81
for (i=0; i<count; i++)
97
int CGN_JNDEXC(char * s , char t)
100
/*++++++++++++++++++++++++++++++++++++++++++++++++++
102
returns position of single character in input string, running backwards
104
returns position of single character in input string, -1 if not there
105
--------------------------------------------------*/
107
char *s; /* input string */
108
char t; /* test character */
121
while (nr > -2) /* it is assumed, that t != '\0' !! */
127
return (i); /* return last index found */
137
int CGN_JNDEXS(char * s , char * t)
140
/*++++++++++++++++++++++++++++++++++++++++++++++++++
142
returns position of substring in input string, running backwards
144
returns position of substring in input string, -1 if not there
145
--------------------------------------------------*/
146
char *s; /* input string */
147
char *t; /* substring */
151
register int i, j, k, kstrn;
154
kstrn = (int)strlen(s) - (int)strlen(t);
156
for (i=kstrn; i >= 0; i--)
158
for (j=i,k=0; (t[k] != '\0') && (s[j] == t[k]); j++,k++)
164
return (-1); /* substring not found */
170
int CGN_CNVT(line,type,maxval,ibuf,rbuf,dbuf)
172
/*++++++++++++++++++++++++++++++++++++++++++++++++++
174
parse an input line of max. 80 char. and store max. 40 values in integer,
175
real or double precision buffer
177
use C-functions atoi + atof for the actual conversion
178
the actual no. of values, 'actval' is returned as the function return value
179
if input line is empty, 'actval' = 0
180
if input line contains non-numeric characters, 'actval' < 0
182
actval: I*4 actual no. of values in input string
183
if < 0, something wrong in input string
184
--------------------------------------------------*/
186
char *line; /* IN : input line (terminated by \0 ) */
187
int type; /* IN : = 1, for decimal integer
189
= 3, for hex. integer;
190
= 4, for double prec. */
191
int maxval; /* IN : maximal no. of output values */
192
int *ibuf; /* OUT: integer output buffer */
193
float *rbuf; /* OUT: real output buffer */
194
double *dbuf; /* OUT: double precision output buffer */
197
int ic, start, slen, mytype, ml;
210
ml = (int)strlen(line);
215
/* extract substrings from input buffer and use `sscanf' for conversion */
217
for (ir=0; ir<maxval; ir++)
219
slen = CGN_EXTRSS(line,ml,',',&start,wp,80);
220
if (slen < 1) return (ir);
222
if ((*wp == '0') && (*(wp+1) == 'x')) /* check for hex input */
228
/* decimal integer type */
231
ic = CGN_INDEXC(wp,'.');
233
ic = sscanf(wp,"%d",&ibuf[ir]);
236
ic = sscanf(wp,"%e",&rr); /* needed for 2.0E+1 */
242
else if (mytype == 2)
244
ic = sscanf(wp,"%le",&dbldum);
245
if (ic == 1) rbuf[ir] = (float) dbldum;
248
/* hex. integer type */
249
else if (mytype == 3)
253
ic = sscanf(wp,"%x",&myint);
254
if (ic != 1) return (-1);
257
rbuf[ir] = (float) myint;
259
dbuf[ir] = (double) myint;
264
mytype = type; /* reset `wp' and `mytype' */
268
else if (mytype == 4)
269
{ /* also type = 3 is treated as double... */
270
for (kr=0; kr<slen; kr++)
272
if ( (work[kr] == 'D') || (work[kr] == 'd') )
279
ic = sscanf(wp,"%le",&dbuf[ir]);
287
if (ic != 1) return (-1);
297
int CGN_EXIST(int flag, char *dirname, char *pattrn, char *file_buff)
299
int CGN_EXIST(flag,dirname,pattrn,file_buff)
300
/*++++++++++++++++++++++++++++++++++++++++++++++++++
302
check if a filename matching the `pattrn' exists in given directory
303
on subsequent calls return full filenames (128 chars long at most)
305
returns no. of files which match the pattern in given directory
308
--------------------------------------------------*/
310
int flag; /* IN: = 0 -> only return no. of matching names
311
= 1 -> return matching names one after
312
the other in `file_buff' */
313
char *dirname; /* IN: directory where to look */
314
char *pattrn; /* IN: pattern to match, e.g. abc*.bdf */
315
char *file_buff; /* OUT: matching filename (max 128 chars) */
325
/* we want only the no. of files matching the pattern */
329
if (nofiles > -1) oslclose();
331
nofiles = oslopen(dirname,pattrn);
334
nofiles = -1; /* problems with directory */
336
else if (nofiles == 0)
341
} /* no matching file found */
347
/* now we want the file names matching the pattern */
351
struct dirent *dirp, *oslread();
356
(void) strcpy(file_buff,dirp->d_name); /* get file name */
361
else if (nofiles == 0)
379
int CGN_IBUILD(int simno, char *name, int datform, int sizze,
380
int *imno, int *cloned)
382
int CGN_IBUILD(simno,name,datform,sizze,imno,cloned)
383
/*++++++++++++++++++++++++++++++++++++++++++++++++++
385
using as source an opened image, create a new image with size `size'
386
and data format `datform'
387
either all descriptors of the source image are cloned to the new image,
388
or just the standard descriptors are copied to the new image
389
(the rest will have to be copied later...)
391
0 if successful, else Midas error code
392
--------------------------------------------------*/
394
int simno; /* IN: id of source image */
395
char *name; /* IN: name of new image */
396
int datform; /* IN: format for new image, e.g. D_R4_FORMAT */
397
int sizze; /* IN: no. of pixel of new image */
398
int *imno; /* OUT: id of new image */
399
int *cloned; /* OUT: cloned_flag, 1 = yes; 0 = no descr cloning done */
403
int uni, nulo, iav, dsc_format, stat;
409
/* check, if cloning is possible */
411
dsc_format = 456; /* default to new descr. format */
412
(void) SCKRDI("AUX_MODE",10,1,&iav,&dsc_format,&uni,&nulo);
414
if (dsc_format == 123)
415
stat = 0; /* no cloning with old descr. format */
417
stat = MID_TDCLON(simno,datform,sizze); /* check header of source */
421
optflg[0] = 1; /* clone all descriptors of source */
423
stat = SCFXCR(name,datform,F_O_MODE,F_IMA_TYPE,sizze,optflg,imno);
428
optflg[0] = 0; /* just create new image */
429
stat = SCFXCR(name,datform,F_O_MODE,F_IMA_TYPE,sizze,optflg,imno);
430
if (stat == ERR_NORMAL)
431
stat = SCDCOP(simno,*imno,2," "); /* only copy standard descr's */
441
int CGN_xCNVT(line,type,maxval,ibuf,rbuf,dbuf,sbuf)
442
/*++++++++++++++++++++++++++++++++++++++++++++++++++
444
parse an input line of max. 80 char. and store max. 40 values in integer,
445
real or double precision buffer
447
use C-functions atoi + atof for the actual conversion
448
the actual no. of values, 'actval' is returned as the function return value
449
if input line is empty, 'actval' = 0
450
if input line contains non-numeric characters, 'actval' < 0
452
actval: I*4 actual no. of values in input string
453
if < 0, something wrong in input string
454
--------------------------------------------------*/
456
char *line; /* IN : input line (terminated by \0 ) */
457
int type; /* IN : = 1, for decimal integer
459
= 3, for hex. integer;
460
= 4, for double prec.
462
int maxval; /* IN : maximal no. of output values */
463
int *ibuf; /* OUT: integer output buffer */
464
float *rbuf; /* OUT: real output buffer */
465
double *dbuf; /* OUT: double precision output buffer */
466
size_t *sbuf; /* OUT: size_t output buffer */
469
int ic, start, slen, mytype, ml;
482
ml = (int)strlen(line);
487
/* extract substrings from input buffer and use `sscanf' for conversion */
489
for (ir=0; ir<maxval; ir++)
491
slen = CGN_EXTRSS(line,ml,',',&start,wp,80);
492
if (slen < 1) return (ir);
494
if ((*wp == '0') && (*(wp+1) == 'x')) /* check for hex input */
500
/* decimal integer type */
503
ic = CGN_INDEXC(wp,'.');
505
ic = sscanf(wp,"%d",&ibuf[ir]);
508
ic = sscanf(wp,"%e",&rr); /* needed for 2.0E+1 */
514
else if (mytype == 2)
516
ic = sscanf(wp,"%le",&dbldum);
517
if (ic == 1) rbuf[ir] = (float) dbldum;
520
/* hex. integer type */
521
else if (mytype == 3)
525
ic = sscanf(wp,"%x",&myint);
526
if (ic != 1) return (-1);
529
rbuf[ir] = (float) myint;
531
dbuf[ir] = (double) myint;
536
mytype = type; /* reset `wp' and `mytype' */
540
else if (mytype == 4)
541
{ /* also type = 3 is treated as double... */
542
for (kr=0; kr<slen; kr++)
544
if ( (work[kr] == 'D') || (work[kr] == 'd') )
551
ic = sscanf(wp,"%le",&dbuf[ir]);
555
else if (mytype == 5)
557
ic = sscanf(wp,"%zd",&sbuf[ir]);
564
if (ic != 1) return (-1);