| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2009 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: [email protected] |
| 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 |
/*+++++++++++++++++++ CGN interfaces part D +++++++++++++++++++++++++++++++ |
| 29 |
.LANGUAGE C |
| 30 |
.IDENTIFICATION Module cgnd.c |
| 31 |
.COMMENTS |
| 32 |
holds CGN_INDEXK, _JNDEXC, _JNDEXS, _CNVT, _xCNVT, _EXIST, _IBUILD |
| 33 |
.AUTHOR K. Banse ESO - Garching |
| 34 |
.KEYWORDS |
| 35 |
name translation, parsing, filling |
| 36 |
.ENVIRONMENT VMS and UNIX |
| 37 |
|
| 38 |
.VERSION |
| 39 |
|
| 40 |
090326 last modif |
| 41 |
-----------------------------------------------------------------------------*/ |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
|
| 45 |
#include <fileexts.h> |
| 46 |
|
| 47 |
#if vms |
| 48 |
#else |
| 49 |
|
| 50 |
#include <sys/types.h> |
| 51 |
#include <dirent.h> |
| 52 |
|
| 53 |
static int nofiles = -1; |
| 54 |
|
| 55 |
#endif |
| 56 |
/* |
| 57 |
|
| 58 |
*/ |
| 59 |
|
| 60 |
#ifdef __STDC__ |
| 61 |
int CGN_INDEXK(char * s , char t , int count) |
| 62 |
#else |
| 63 |
int CGN_INDEXK(s,t,count) |
| 64 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 65 |
.PURPOSE |
| 66 |
returns position of single character in input string |
| 67 |
.RETURN |
| 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 */ |
| 73 |
#endif |
| 74 |
|
| 75 |
{ |
| 76 |
register int i; |
| 77 |
|
| 78 |
register char cc; |
| 79 |
|
| 80 |
|
| 81 |
for (i=0; i<count; i++) |
| 82 |
{ |
| 83 |
cc = s[i]; |
| 84 |
if (cc == t) |
| 85 |
return (i); |
| 86 |
else if (cc == '\0') |
| 87 |
return (-2-i); |
| 88 |
} |
| 89 |
|
| 90 |
return (-1); |
| 91 |
} |
| 92 |
/* |
| 93 |
|
| 94 |
*/ |
| 95 |
|
| 96 |
#ifdef __STDC__ |
| 97 |
int CGN_JNDEXC(char * s , char t) |
| 98 |
#else |
| 99 |
int CGN_JNDEXC(s,t) |
| 100 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 101 |
.PURPOSE |
| 102 |
returns position of single character in input string, running backwards |
| 103 |
.RETURN |
| 104 |
returns position of single character in input string, -1 if not there |
| 105 |
--------------------------------------------------*/ |
| 106 |
|
| 107 |
char *s; /* input string */ |
| 108 |
char t; /* test character */ |
| 109 |
#endif |
| 110 |
|
| 111 |
{ |
| 112 |
int i; |
| 113 |
register int nr; |
| 114 |
|
| 115 |
register char cc; |
| 116 |
|
| 117 |
|
| 118 |
i = -1; |
| 119 |
nr = -1; |
| 120 |
|
| 121 |
while (nr > -2) /* it is assumed, that t != '\0' !! */ |
| 122 |
{ |
| 123 |
cc = s[++nr]; |
| 124 |
if (cc == t) |
| 125 |
i = nr; |
| 126 |
else if (cc == '\0') |
| 127 |
return (i); /* return last index found */ |
| 128 |
} |
| 129 |
|
| 130 |
return (-99); |
| 131 |
} |
| 132 |
/* |
| 133 |
|
| 134 |
*/ |
| 135 |
|
| 136 |
#ifdef __STDC__ |
| 137 |
int CGN_JNDEXS(char * s , char * t) |
| 138 |
#else |
| 139 |
int CGN_JNDEXS(s,t) |
| 140 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 141 |
.PURPOSE |
| 142 |
returns position of substring in input string, running backwards |
| 143 |
.RETURN |
| 144 |
returns position of substring in input string, -1 if not there |
| 145 |
--------------------------------------------------*/ |
| 146 |
char *s; /* input string */ |
| 147 |
char *t; /* substring */ |
| 148 |
#endif |
| 149 |
|
| 150 |
{ |
| 151 |
register int i, j, k, kstrn; |
| 152 |
|
| 153 |
|
| 154 |
kstrn = (int)strlen(s) - (int)strlen(t); |
| 155 |
|
| 156 |
for (i=kstrn; i >= 0; i--) |
| 157 |
{ |
| 158 |
for (j=i,k=0; (t[k] != '\0') && (s[j] == t[k]); j++,k++) |
| 159 |
; |
| 160 |
if (t[k] == '\0') |
| 161 |
return (i); |
| 162 |
} |
| 163 |
|
| 164 |
return (-1); /* substring not found */ |
| 165 |
} |
| 166 |
/* |
| 167 |
|
| 168 |
*/ |
| 169 |
|
| 170 |
int CGN_CNVT(line,type,maxval,ibuf,rbuf,dbuf) |
| 171 |
|
| 172 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 173 |
.PURPOSE |
| 174 |
parse an input line of max. 80 char. and store max. 40 values in integer, |
| 175 |
real or double precision buffer |
| 176 |
.ALGORITHM |
| 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 |
| 181 |
.RETURN |
| 182 |
actval: I*4 actual no. of values in input string |
| 183 |
if < 0, something wrong in input string |
| 184 |
--------------------------------------------------*/ |
| 185 |
|
| 186 |
char *line; /* IN : input line (terminated by \0 ) */ |
| 187 |
int type; /* IN : = 1, for decimal integer |
| 188 |
= 2, for real; |
| 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 */ |
| 195 |
|
| 196 |
{ |
| 197 |
int ic, start, slen, mytype, ml; |
| 198 |
register int kr, ir; |
| 199 |
|
| 200 |
float rr; |
| 201 |
|
| 202 |
char *wp, work[84]; |
| 203 |
|
| 204 |
double dbldum; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
start = 0; |
| 210 |
ml = (int)strlen(line); |
| 211 |
mytype = type; |
| 212 |
wp = work; |
| 213 |
|
| 214 |
|
| 215 |
/* extract substrings from input buffer and use `sscanf' for conversion */ |
| 216 |
|
| 217 |
for (ir=0; ir<maxval; ir++) |
| 218 |
{ |
| 219 |
slen = CGN_EXTRSS(line,ml,',',&start,wp,80); |
| 220 |
if (slen < 1) return (ir); |
| 221 |
|
| 222 |
if ((*wp == '0') && (*(wp+1) == 'x')) /* check for hex input */ |
| 223 |
{ |
| 224 |
mytype = 3; |
| 225 |
wp += 2; |
| 226 |
} |
| 227 |
|
| 228 |
/* decimal integer type */ |
| 229 |
if (mytype == 1) |
| 230 |
{ |
| 231 |
ic = CGN_INDEXC(wp,'.'); |
| 232 |
if (ic < 0) |
| 233 |
ic = sscanf(wp,"%d",&ibuf[ir]); |
| 234 |
else |
| 235 |
{ |
| 236 |
ic = sscanf(wp,"%e",&rr); /* needed for 2.0E+1 */ |
| 237 |
ibuf[ir] = (int) rr; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/* float type */ |
| 242 |
else if (mytype == 2) |
| 243 |
{ |
| 244 |
ic = sscanf(wp,"%le",&dbldum); |
| 245 |
if (ic == 1) rbuf[ir] = (float) dbldum; |
| 246 |
} |
| 247 |
|
| 248 |
/* hex. integer type */ |
| 249 |
else if (mytype == 3) |
| 250 |
{ |
| 251 |
int myint; |
| 252 |
|
| 253 |
ic = sscanf(wp,"%x",&myint); |
| 254 |
if (ic != 1) return (-1); |
| 255 |
|
| 256 |
if (type == 2) |
| 257 |
rbuf[ir] = (float) myint; |
| 258 |
else if (type == 4) |
| 259 |
dbuf[ir] = (double) myint; |
| 260 |
else |
| 261 |
ibuf[ir] = myint; |
| 262 |
|
| 263 |
wp = work; |
| 264 |
mytype = type; /* reset `wp' and `mytype' */ |
| 265 |
} |
| 266 |
|
| 267 |
/* double type */ |
| 268 |
else if (mytype == 4) |
| 269 |
{ /* also type = 3 is treated as double... */ |
| 270 |
for (kr=0; kr<slen; kr++) |
| 271 |
{ |
| 272 |
if ( (work[kr] == 'D') || (work[kr] == 'd') ) |
| 273 |
{ |
| 274 |
work[kr] = 'e'; |
| 275 |
break; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
ic = sscanf(wp,"%le",&dbuf[ir]); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
else |
| 284 |
return (-2); |
| 285 |
|
| 286 |
|
| 287 |
if (ic != 1) return (-1); |
| 288 |
} |
| 289 |
|
| 290 |
return maxval; |
| 291 |
} |
| 292 |
/* |
| 293 |
|
| 294 |
*/ |
| 295 |
|
| 296 |
#ifdef __STDC__ |
| 297 |
int CGN_EXIST(int flag, char *dirname, char *pattrn, char *file_buff) |
| 298 |
#else |
| 299 |
int CGN_EXIST(flag,dirname,pattrn,file_buff) |
| 300 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 301 |
.PURPOSE |
| 302 |
check if a filename matching the `pattrn' exists in given directory |
| 303 |
on subsequent calls return full filenames (128 chars long at most) |
| 304 |
.RETURN |
| 305 |
returns no. of files which match the pattern in given directory |
| 306 |
or |
| 307 |
just 0/-1 |
| 308 |
--------------------------------------------------*/ |
| 309 |
|
| 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) */ |
| 316 |
#endif |
| 317 |
|
| 318 |
|
| 319 |
{ |
| 320 |
#if vms |
| 321 |
return (-1); |
| 322 |
#else |
| 323 |
|
| 324 |
|
| 325 |
/* we want only the no. of files matching the pattern */ |
| 326 |
|
| 327 |
if (flag == 0) |
| 328 |
{ |
| 329 |
if (nofiles > -1) oslclose(); |
| 330 |
|
| 331 |
nofiles = oslopen(dirname,pattrn); |
| 332 |
|
| 333 |
if (nofiles < 0) |
| 334 |
nofiles = -1; /* problems with directory */ |
| 335 |
|
| 336 |
else if (nofiles == 0) |
| 337 |
{ |
| 338 |
(void) oslclose(); |
| 339 |
nofiles = -1; |
| 340 |
return (0); |
| 341 |
} /* no matching file found */ |
| 342 |
|
| 343 |
return (nofiles); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/* now we want the file names matching the pattern */ |
| 348 |
|
| 349 |
else |
| 350 |
{ |
| 351 |
struct dirent *dirp, *oslread(); |
| 352 |
|
| 353 |
if (nofiles > 0) |
| 354 |
{ |
| 355 |
dirp = oslread(); |
| 356 |
(void) strcpy(file_buff,dirp->d_name); /* get file name */ |
| 357 |
nofiles --; |
| 358 |
return (1); |
| 359 |
} |
| 360 |
|
| 361 |
else if (nofiles == 0) |
| 362 |
{ |
| 363 |
(void) oslclose(); |
| 364 |
nofiles = -1; |
| 365 |
return (0); |
| 366 |
} |
| 367 |
|
| 368 |
else |
| 369 |
return (-1); |
| 370 |
} |
| 371 |
|
| 372 |
#endif |
| 373 |
} |
| 374 |
/* |
| 375 |
|
| 376 |
*/ |
| 377 |
|
| 378 |
#ifdef __STDC__ |
| 379 |
int CGN_IBUILD(int simno, char *name, int datform, int sizze, |
| 380 |
int *imno, int *cloned) |
| 381 |
#else |
| 382 |
int CGN_IBUILD(simno,name,datform,sizze,imno,cloned) |
| 383 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 384 |
.PURPOSE |
| 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...) |
| 390 |
.RETURN |
| 391 |
0 if successful, else Midas error code |
| 392 |
--------------------------------------------------*/ |
| 393 |
|
| 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 */ |
| 400 |
#endif |
| 401 |
|
| 402 |
{ |
| 403 |
int uni, nulo, iav, dsc_format, stat; |
| 404 |
int optflg[2]; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
/* check, if cloning is possible */ |
| 410 |
|
| 411 |
dsc_format = 456; /* default to new descr. format */ |
| 412 |
(void) SCKRDI("AUX_MODE",10,1,&iav,&dsc_format,&uni,&nulo); |
| 413 |
|
| 414 |
if (dsc_format == 123) |
| 415 |
stat = 0; /* no cloning with old descr. format */ |
| 416 |
else |
| 417 |
stat = MID_TDCLON(simno,datform,sizze); /* check header of source */ |
| 418 |
|
| 419 |
if (stat == 1) |
| 420 |
{ /* Yes. */ |
| 421 |
optflg[0] = 1; /* clone all descriptors of source */ |
| 422 |
optflg[1] = simno; |
| 423 |
stat = SCFXCR(name,datform,F_O_MODE,F_IMA_TYPE,sizze,optflg,imno); |
| 424 |
*cloned = 1; |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 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 */ |
| 432 |
*cloned = 0; |
| 433 |
} |
| 434 |
|
| 435 |
return stat; |
| 436 |
} |
| 437 |
/* |
| 438 |
|
| 439 |
*/ |
| 440 |
|
| 441 |
int CGN_xCNVT(line,type,maxval,ibuf,rbuf,dbuf,sbuf) |
| 442 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 443 |
.PURPOSE |
| 444 |
parse an input line of max. 80 char. and store max. 40 values in integer, |
| 445 |
real or double precision buffer |
| 446 |
.ALGORITHM |
| 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 |
| 451 |
.RETURN |
| 452 |
actval: I*4 actual no. of values in input string |
| 453 |
if < 0, something wrong in input string |
| 454 |
--------------------------------------------------*/ |
| 455 |
|
| 456 |
char *line; /* IN : input line (terminated by \0 ) */ |
| 457 |
int type; /* IN : = 1, for decimal integer |
| 458 |
= 2, for real; |
| 459 |
= 3, for hex. integer; |
| 460 |
= 4, for double prec. |
| 461 |
= 5, for size_t */ |
| 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 */ |
| 467 |
|
| 468 |
{ |
| 469 |
int ic, start, slen, mytype, ml; |
| 470 |
register int kr, ir; |
| 471 |
|
| 472 |
float rr; |
| 473 |
|
| 474 |
char *wp, work[84]; |
| 475 |
|
| 476 |
double dbldum; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
start = 0; |
| 482 |
ml = (int)strlen(line); |
| 483 |
mytype = type; |
| 484 |
wp = work; |
| 485 |
|
| 486 |
|
| 487 |
/* extract substrings from input buffer and use `sscanf' for conversion */ |
| 488 |
|
| 489 |
for (ir=0; ir<maxval; ir++) |
| 490 |
{ |
| 491 |
slen = CGN_EXTRSS(line,ml,',',&start,wp,80); |
| 492 |
if (slen < 1) return (ir); |
| 493 |
|
| 494 |
if ((*wp == '0') && (*(wp+1) == 'x')) /* check for hex input */ |
| 495 |
{ |
| 496 |
mytype = 3; |
| 497 |
wp += 2; |
| 498 |
} |
| 499 |
|
| 500 |
/* decimal integer type */ |
| 501 |
if (mytype == 1) |
| 502 |
{ |
| 503 |
ic = CGN_INDEXC(wp,'.'); |
| 504 |
if (ic < 0) |
| 505 |
ic = sscanf(wp,"%d",&ibuf[ir]); |
| 506 |
else |
| 507 |
{ |
| 508 |
ic = sscanf(wp,"%e",&rr); /* needed for 2.0E+1 */ |
| 509 |
ibuf[ir] = (int) rr; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
/* float type */ |
| 514 |
else if (mytype == 2) |
| 515 |
{ |
| 516 |
ic = sscanf(wp,"%le",&dbldum); |
| 517 |
if (ic == 1) rbuf[ir] = (float) dbldum; |
| 518 |
} |
| 519 |
|
| 520 |
/* hex. integer type */ |
| 521 |
else if (mytype == 3) |
| 522 |
{ |
| 523 |
int myint; |
| 524 |
|
| 525 |
ic = sscanf(wp,"%x",&myint); |
| 526 |
if (ic != 1) return (-1); |
| 527 |
|
| 528 |
if (type == 2) |
| 529 |
rbuf[ir] = (float) myint; |
| 530 |
else if (type == 4) |
| 531 |
dbuf[ir] = (double) myint; |
| 532 |
else |
| 533 |
ibuf[ir] = myint; |
| 534 |
|
| 535 |
wp = work; |
| 536 |
mytype = type; /* reset `wp' and `mytype' */ |
| 537 |
} |
| 538 |
|
| 539 |
/* double type */ |
| 540 |
else if (mytype == 4) |
| 541 |
{ /* also type = 3 is treated as double... */ |
| 542 |
for (kr=0; kr<slen; kr++) |
| 543 |
{ |
| 544 |
if ( (work[kr] == 'D') || (work[kr] == 'd') ) |
| 545 |
{ |
| 546 |
work[kr] = 'e'; |
| 547 |
break; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
ic = sscanf(wp,"%le",&dbuf[ir]); |
| 552 |
} |
| 553 |
|
| 554 |
/* size_t type */ |
| 555 |
else if (mytype == 5) |
| 556 |
{ |
| 557 |
ic = sscanf(wp,"%zd",&sbuf[ir]); |
| 558 |
} |
| 559 |
|
| 560 |
else |
| 561 |
return (-2); |
| 562 |
|
| 563 |
|
| 564 |
if (ic != 1) return (-1); |
| 565 |
} |
| 566 |
|
| 567 |
return maxval; |
| 568 |
} |
| 569 |
|