Debian Codegraph @

libsrc/st/scfa.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2010 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 /*+++++++++++++++++++++++ SC interface module SCFA +++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION
31 Module SCFA
32 .AUTHOR
33 K. Banse ESO - Garching
34 .KEYWORDS
35 standard interfaces, bulk data files
36 .ENVIRONMENT
37 VMS and UNIX
38 .COMMENTS
39 holds SCFSAV, SCFMOD, SCFREE, maxi
40 .REMARKS
41
42 .VERSION [1.00] 971015: creation
43
44 101221 last modif
45 ----------------------------------------------------------------------------*/
46
47 #include <stdio.h>
48 #include <stdlib.h>
49
50 #include <fileexts.h>
51 #include <osyparms.h>
52
53 #include <fitsfmt.h>
54 #include <fitsdef.h>
55
56 #define BIT_0 0x1
57
58 /*
59
60 */
61
62 int SCFSAV(imno,flag)
63
64 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
65 .PURPOSE
66 save a Midas file as FITS file on disk
67 .ALGORITHM
68 use Prebens FITS I/O routines
69 .RETURNS
70 return status ( 0 = o.k)
71 -----------------------------------------------------------------------*/
72
73 int imno; /* IN: file no. */
74 int flag; /* IN: 0 = convert Midas image to FITS
75 1 = update internal FITS image/table
76 2 = convert Midas table to FITS */
77
78
79 {
80 char ffmt, cut_opt;
81 char origname[FCT_NAME_LEN];
82
83 int fid, n, m, ftyp, datf, status;
84 int n0, n1, e_c, e_d, e_l;
85 int dscmod_only;
86 int ddcopy();
87
88 void maxi();
89
90 struct FCT_STRUCT *fctpntr;
91
92
93
94
95 fctpntr = FCT.ENTRIES + imno;
96 dscmod_only = fctpntr->KAUX[2]; /* original descr. modified flag */
97
98 if (fctpntr->CATALOG[0] == 'T')
99 {
100 ftyp = F_TBL_TYPE;
101 datf = D_R4_FORMAT;
102 }
103 else
104 {
105 ftyp = F_IMA_TYPE;
106 datf = fctpntr->FORMAT; /* original data type */
107 }
108
109 ffmt = 'O'; /* format flag : Basic FITS, Original */
110 cut_opt = 'N';
111
112 n1 = 1; n0 = 0;
113 SCECNT("GET",&e_c,&e_d,&e_l);
114 SCECNT("PUT",&n1,&n0,&n0); /* disable SC errors */
115
116 if (fctpntr->O_NAMLEN == 0)
117 (void) strcpy(origname,fctpntr->NAME);
118 else /* internal FITS file */
119 {
120 (void) strcpy(origname,fctpntr->NAME+4);
121 m = fctpntr->O_NAMLEN - 4; /* we removed leading "FITZ" */
122 origname[m] = '\0';
123 }
124
125
126 /* write out to FITS file `midFITS.mt' */
127
128 fid = dopen("midFITS.mt",1,'S',6250); /* open disk file for writing */
129 if (fid < 0) return ERR_FILBAD;
130
131
132 dwinit(1); /* init disk writing */
133
134 if (ftyp == F_IMA_TYPE)
135 {
136 if (flag == 0) /* Midas -> FITS */
137 {
138 int unit, null, nv;
139 float lcut, hcut, cuts[2];
140
141 (void) SCDRDR(imno,"LHCUTS",3,2,&nv,cuts,&unit,&null);
142 lcut = cuts[0]; hcut = cuts[1];
143
144 if ((nv!=2) || (hcut<=lcut))
145 { /* compute max/min cuts of data */
146 maxi(imno,fctpntr->SIZE,cuts);
147 (void) SCDWRR(imno,"LHCUTS",cuts,3,2,&unit);
148 }
149
150 status = fitswhd(imno,ftyp,datf,origname,ffmt,cut_opt,BFITS);
151 if (status == 0)
152 status = fitswdm(imno,datf,ffmt); /* also update data */
153 }
154
155 else /* internal FITS -> FITS */
156 {
157 status = fitswhd(imno,ftyp,datf,origname,ffmt,cut_opt,BFITS);
158 if (status == 0)
159 {
160 fctpntr->FITSOUT = 'Y'; /* so SCFGET() will know... */
161 if (dscmod_only == 1) /* only update descr. */
162 { /* open original FITS file again + position it */
163 int gid;
164 MID_fitsin(3,origname,-1,"","",&imno,&gid);
165 if (gid == -99) /* data type changed... */
166 {
167 char *datpntr;
168
169 if (fctpntr->BDADDR[0] == (char *) 0)
170 status = SCFMAP(imno,F_I_MODE,1,-1,&m,&datpntr);
171 else
172 status = ERR_NORMAL;
173 if (status == ERR_NORMAL)
174 status = fitswdm(imno,datf,ffmt); /* also write data */
175 }
176 else
177 {
178 status = ddcopy(); /* copy original FITS data */
179 dclose(gid);
180 }
181 }
182 else
183 status = fitswdm(imno,datf,ffmt); /* also update data */
184 }
185 }
186 }
187
188 else
189 { /* file is a table */
190 status = /* primary header */
191 fitswhd(imno,ftyp,datf,origname,ffmt,cut_opt,BFITSE);
192 if (status == 0)
193 { /* first extension */
194 status = fitswhd(imno,ftyp,datf,origname,ffmt,cut_opt,ATABLE);
195 if (status == 0)
196 {
197 if (ffmt == 'B')
198 status = fitswat(imno); /* write ASCII table */
199 else
200 status = fitswbt(imno); /* write Binary table */
201 }
202 }
203 }
204
205 fctpntr->KAUX[2] = dscmod_only; /* reset, if changed via fitswhd() */
206
207 SCECNT("PUT",&e_c,&e_d,&e_l);
208
209 n = dweof(); /* write EOF */
210 dclose(fid);
211
212 if ((n < 1) || (status != 0))
213 {
214 (void) osfdelete("midFITS.mt");
215 return ERR_FILBAD;
216 }
217 else
218 return ERR_NORMAL;
219 }
220
221 /*
222
223 */
224
225 void maxi(imno,datsize,cuts)
226 int imno;
227 size_t datsize;
228 float cuts[2];
229
230 {
231 int nm, ioff, ns, data_bytes;
232 register int nr;
233
234 char *cpt;
235
236 float *pix, lcut, hcut;
237
238 struct FCT_STRUCT *fctpntr;
239
240
241
242 fctpntr = FCT.ENTRIES + imno;
243 nm = fctpntr->DATTYP;
244 data_bytes = get_byte(nm); /* no. of bytes needed */
245
246
247 ioff = 1;
248 lcut = hcut = 0.0;
249 nm = 100000; /* at most chunks of 100 000 pixels */
250 if (nm > datsize) nm = (int) datsize;
251
252 cpt = (char *) malloc((size_t)(nm*data_bytes));
253 if (cpt == (char *) 0)
254 {
255 (void) printf("Out of memory in SCFSAV...\n");
256 return;
257 }
258
259
260 while (ioff <= (int) datsize)
261 {
262 (void) SCFGET(imno,ioff,nm,&ns,cpt);
263
264 pix = (float *)cpt;
265 if (ioff == 1) lcut = hcut = *pix;
266
267 for (nr=0; nr<ns; nr++)
268 {
269 if (*pix<lcut)
270 lcut = *pix;
271 else if (hcut<*pix)
272 hcut = *pix;
273 pix++;
274 }
275 ioff += ns;
276 }
277
278 cuts[0] = lcut;
279 cuts[1] = hcut;
280
281 free (cpt);
282 }
283
284 /*
285
286 */
287
288 int SCFMOD(imno,dattype,size)
289
290 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
291 .PURPOSE
292 add the data blocks to a Midas file which was created with F_H_MODE
293 i.e. only the header part of the file: FCB + LDBs were created
294 .ALGORITHM
295 if size < 1 we just update the data type info
296 .RETURNS
297 return status ( 0 = o.k)
298 -----------------------------------------------------------------------*/
299
300 int imno; /* IN: file no. */
301 int dattype; /* IN: data type, D_R4_FORMAT, ... */
302 int size; /* IN: no. of data pixels */
303
304 {
305 int status, data_bytes, lpex;
306 unsigned int alq, nblock;
307
308 struct FCB_STRUCT *fcbp;
309
310 struct FCT_STRUCT *fctpntr;
311
312 size_t *sptr;
313
314
315
316
317 if ( (imno < 0) || (imno >= FCT.MAXENT) )
318 {
319 status = ERR_INPINV;
320 goto end_of_it;
321 }
322 fctpntr = FCT.ENTRIES + imno;
323 fcbp = fctpntr->FZP;
324
325 data_bytes = get_byte(dattype);
326 fcbp->NOBYT = data_bytes;
327 fcbp->DFORMAT = dattype;
328 fcbp->PIXPBL = OUR_BLOCK_SIZE / data_bytes;
329
330 fctpntr->NOBYTE = fcbp->NOBYT;
331 fctpntr->FORMAT = fcbp->DFORMAT;
332 fctpntr->PIXPBL = fcbp->PIXPBL;
333 fctpntr->DATTYP = fctpntr->FORMAT;
334
335 fcbp->DATAINFO[4] = fcbp->DFORMAT;
336
337 if (size > 0) /* allocate data blocks */
338 {
339 unsigned int nobyt;
340
341 fcbp->D1BLOCK = fcbp->LEXBDF + 1; /* new start block of data */
342 fcbp->DATAINFO[5] = (fcbp->D1BLOCK-1)*OUR_BLOCK_SIZE;
343 fcbp->NDVAL = size;
344 sptr = (size_t *) fcbp->XNDVAL;
345 *sptr = (size_t) size;
346
347 alq = (unsigned int) (OUR_BLOCK_SIZE - 1);
348 nobyt = (unsigned int)data_bytes * (unsigned int)size;
349 nobyt += alq;
350 nblock = nobyt / OUR_BLOCK_SIZE; /* no. of data blocks */
351
352 #if vms
353 FSY_EXTBDF(fctpntr->FILEIDA,fctpntr->FILEIDB,fctpntr->DEVICE,
354 (int)nblock,&lpex,&status);
355 if ( (status & BIT_0) == 0 )
356 {
357 MID_ERROR("SCFMOD","SCFMOD/FSY_EXTBDF",status,0);
358 return ERR_FRMNAC;
359 }
360 #else
361
362 nobyt = (unsigned int)fcbp->LEXBDF + nblock;
363 nobyt *= OUR_BLOCK_SIZE;
364 status = FSY_MODBDF(fctpntr->IOCHAN,nobyt,&lpex);
365 if (status != ERR_NORMAL) goto end_of_it;
366 #endif
367
368 /*
369 printf("SCFMOD: lpex = %d, status = %x\n",lpex,status);
370 */
371
372 fcbp->PEXBDF = lpex;
373 fcbp->DLBLOCK = lpex;
374
375 fctpntr->STBLOK = fcbp->D1BLOCK;
376 fctpntr->SIZE = (size_t) size;
377 }
378
379 return ERR_NORMAL;
380
381
382 end_of_it:
383 MID_E2(1,imno,"...",status,1);
384 return status;
385 }
386
387 /*
388
389 */
390
391 int SCFREE(type,nampat)
392
393 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
394 .PURPOSE
395 close opened frames
396 frames can be chosen via their iomode/type and/or names
397 .RETURNS
398 return no. of files closed (>= 0)
399 -----------------------------------------------------------------------*/
400
401 char *type; /* IN: type of opened frame,
402 = "X", "I" or "T" or "*"
403 for virtual memory, images, tables or all */
404 char *nampat; /* IN: name pattern
405 "abc*" or "*" for all */
406
407 {
408 int patlen, i, countr;
409
410 char cb;
411 char *ptr, origname[FCT_NAME_LEN];
412
413 struct FCT_STRUCT *fctpntr;
414
415
416
417 cb = *type;
418 if ((cb >= 'a') && (cb <= 'z'))
419 cb += ('A' - 'a');
420 if ((cb != 'X') && (cb != 'T') && (cb != 'I'))
421 cb = '*'; /* that's the default */
422
423 i = (int) strlen(nampat);
424 if ((i > 1) && (nampat[i-1] == '*'))
425 patlen = i - 1;
426 else
427 patlen = 0;
428
429 countr = 0; /* no. of closed frames */
430
431
432 /* loop through FCT and look for relevant entries */
433
434 fctpntr = FCT.ENTRIES;
435 for (i=0; i<FCT.MAXENT; i++)
436 {
437 if (fctpntr->NAME[0] != ' ')
438 {
439 if (fctpntr->FILTYP > 0) /* for FITS files */
440 {
441 if (fctpntr->O_NAMLEN == 0)
442 (void) strcpy(origname,fctpntr->NAME);
443 else /* internal FITS file */
444 {
445 (void) strcpy(origname,fctpntr->NAME+4);
446 i = fctpntr->O_NAMLEN - 4; /* we removed leading "FITZ" */
447 origname[i] = '\0';
448 }
449 ptr = origname;
450 }
451 else
452 ptr = fctpntr->NAME;
453
454 if (patlen > 0)
455 {
456 if (strncmp(nampat,origname,(size_t)patlen) != 0)
457 goto outer_loop;
458 }
459
460 if (cb != '*')
461 {
462 switch (cb)
463 {
464 case 'X':
465 if (fctpntr->ACCESS == 'X') /* virtual memory only */
466 (void) SCFCLO(i);
467 break;
468 case 'I':
469 if (fctpntr->CATALOG[0] != 'T') /* images */
470 (void) SCFCLO(i);
471 break;
472 default: /* only 'T' possible */
473 if (fctpntr->CATALOG[0] == 'T')
474 (void) TCTCLO(i);
475 }
476 }
477 else
478 {
479 if (fctpntr->CATALOG[0] == 'T')
480 (void) TCTCLO(i);
481 else
482 (void) SCFCLO(i);
483 }
484 countr ++;
485 }
486
487 outer_loop:
488 fctpntr ++;
489 }
490
491 return (countr);
492 }
493
494