Debian Codegraph @

libsrc/st/scfaux.c

raw
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: [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 /*+++++++++++++++++++++++ extension module SCFAUX +++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module SCFAUX
31 .AUTHOR K. Banse ESO - Garching
32 .KEYWORDS standard interfaces, bulk data files
33 .ENVIRONMENT VMS and UNIX
34 .COMMENTS
35 holds auxiliary routines Fextcor, Fcnvsng, Fextfr, Finsfr
36
37 .VERSION [1.00] 890929: creation
38 051021 last modif
39
40 ----------------------------------------------------------------------------*/
41
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 #include <fileexts.h>
46 #include <osyparms.h>
47
48
49
50 static int naxis, npix[3];
51 static int newnax, newpix[3];
52
53 static double newstart[3];
54 static double start[3], step[3];
55
56 static struct FCT_STRUCT *fctpntr;
57
58 /*
59
60 */
61
62 int Fextcor(fno,string,sta,end,size)
63 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
64 .PURPOSE
65 from string [xa,ya,za:xe,ye,ze] or @no extract the start + end frame pixels
66 .ALGORITHM
67 use Fcnvcor + CGN_CNVT to do the job
68 .RETURNS
69 return status ( 0 = o.k )
70 -----------------------------------------------------------------------*/
71
72 int fno; /* IN: input file id */
73 char *string; /* IN: string with coords: [xa,ya,za:xe,ye,ze] */
74 int *sta; /* OUT: start frame pixels */
75 int *end; /* OUT: end frame pixels */
76 int *size; /* OUT: size of subframe */
77
78
79 {
80 int nout, off, n1, k, iav;
81 int knul = 0, kuni = 0;
82 int ibuf;
83
84 float rbuf;
85
86 double dbuf;
87
88 char work[80], test;
89
90
91 for (k=0; k<3; k++)
92 {
93 npix[k] = 1;
94 sta[k] = 1;
95 end[k] = 1;
96 }
97
98 /* obtain necessary info from input frame */
99
100 (void) SCDRDI(fno,"NAXIS",1,1,&iav,&naxis,&kuni,&knul);
101 (void) SCDRDI(fno,"NPIX",1,naxis,&iav,npix,&kuni,&knul);
102 (void) SCDRDD(fno,"START",1,naxis,&iav,start,&kuni,&knul);
103 (void) SCDRDD(fno,"STEP",1,naxis,&iav,step,&kuni,&knul);
104
105
106 nout = 1; /* determine true NAXIS */
107 for (n1=naxis-1; n1>=0; n1--)
108 {
109 if (npix[n1] > 1)
110 {
111 nout = n1 + 1;
112 break;
113 }
114 }
115 newnax = nout;
116
117
118 if (string[0] == '@') /* handle [...:...] and @... differently */
119 {
120 if (newnax == 3)
121 {
122 end[0] = npix[0];
123 end[1] = npix[1];
124 }
125 else if (newnax == 2)
126 end[0] = npix[0];
127 else
128 goto inv_syntax;
129
130 k = CGN_CNVT(&string[1],1,1,&ibuf,&rbuf,&dbuf);
131 if (k != 1) goto inv_syntax;
132
133 sta[newnax-1] = ibuf;
134 end[newnax-1] = ibuf;
135 }
136
137 else
138 {
139 (void) strcpy(work,string); /* copy string, could be a constant */
140 nout = 0; off = 1; n1 = naxis - 1;
141
142 st_loop: /* loop over start coords */
143 if (nout == n1)
144 test = ':';
145 else
146 test= ',';
147
148 k = CGN_INDEXC(&work[off],test);
149 if (k <= 0) goto inv_syntax;
150
151 work[off+k] = '\0';
152 sta[nout] = Fcnvsng(&work[off],nout);
153 if (sta[nout] < 1) goto inv_syntax;
154
155 off += (k+1);
156 if (++nout < naxis) goto st_loop;
157
158 nout = 0;
159
160 end_loop: /* loop over end coords */
161 if (nout == n1)
162 test = ']';
163 else
164 test= ',';
165
166 k = CGN_INDEXC(&work[off],test);
167 if (k <= 0) goto inv_syntax;
168
169 work[off+k] = '\0';
170 end[nout] = Fcnvsng(&work[off],nout);
171 if (end[nout] < 1) goto inv_syntax;
172
173 if (++nout < naxis)
174 {
175 off += (k+1);
176 goto end_loop;
177 }
178 }
179
180 *size = 1;
181 newnax = 1;
182 for (k=0; k<3; k++)
183 {
184 newpix[k] = end[k] - sta[k] + 1;
185 if (newpix[k] > 1) newnax = k + 1;
186 *size *= newpix[k];
187 }
188
189 return ERR_NORMAL;
190
191 inv_syntax:
192 return ERR_INPINV;
193 }
194
195 /*
196
197 */
198
199 int Fcnvsng(stri,no)
200 char *stri;
201 int no;
202
203 {
204 int m, nn;
205
206 float rbuf, rm;
207
208 double dbuf;
209
210
211 if (*stri == '<')
212 m = 1;
213
214 else if (*stri == '>')
215 m = npix[no];
216
217 else if (*stri == '@')
218 {
219 nn = CGN_CNVT(&stri[1],1,1,&m,&rbuf,&dbuf);
220 if (nn != 1) m = -1;
221 }
222
223 else
224 {
225 nn = CGN_CNVT(stri,2,1,&m,&rbuf,&dbuf);
226 if (nn != 1)
227 m = -1;
228 else
229 {
230 rm = (float) ((rbuf - start[no]) / step[no]);
231 m = (int) rm;
232 m++;
233 }
234 }
235
236 return (m);
237 }
238
239 /*
240
241 */
242
243 int Fextfr(fno,sta,end,tno)
244 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
245 .PURPOSE
246 extract subframe (tno) from father (fno)
247 .ALGORITHM
248 use conv_dat or rddisk to get stuff in
249 then write compact subframe out via wrdisk
250 .RETURNS
251 return status ( 0 = o.k )
252 -----------------------------------------------------------------------*/
253 int fno; /* IN: father file id */
254 int *sta; /* IN: start coords */
255 int *end; /* IN: end coords */
256 int tno; /* IN: son file id */
257
258 {
259 register int nr;
260 int ysize, felemz;
261 int xsize, felemi, felemo, status;
262 int savlink, dattyp, forma, nbyt, kpix, totsize, actsz;
263 int kuni = 0;
264
265 char *wpntra, *wp[2];
266
267
268 /* update descriptors for subframe */
269
270 totsize = 1;
271 for (nr=0; nr<naxis; nr++)
272 {
273 totsize *= newpix[nr];
274 newstart[nr] = start[nr] + ((sta[nr]-1) * step[nr]);
275 }
276
277 /* and write descriptors */
278
279 fctpntr = FCT.ENTRIES + tno; /* son: clear LINK first, */
280 savlink = fctpntr->LINK[0]; /* so we can */
281 fctpntr->LINK[0] = 0; /* write into the extracted frame */
282 (void) SCDWRI(tno,"NAXIS",&newnax,1,1,&kuni);
283 (void) SCDWRI(tno,"NPIX",newpix,1,naxis,&kuni);
284 (void) SCDWRD(tno,"START",newstart,1,naxis,&kuni);
285 (void) SCDWRD(tno,"STEP",step,1,naxis,&kuni);
286 (void) SCDWRI(tno,"__NAXIS",&naxis,1,1,&kuni);
287 (void) SCDWRI(tno,"__NPIX",npix,1,3,&kuni);
288 (void) SCDWRI(tno,"__STPIX",sta,1,3,&kuni);
289 (void) SCDWRI(tno,"__ENPIX",end,1,3,&kuni);
290 fctpntr->LINK[0] = savlink; /* reset LINK */
291 if (savlink == 3) return ERR_NORMAL; /* no data extraction */
292
293
294 felemz = sta[0] + (sta[1]-1)*npix[0] + (sta[2]-1)*npix[0]*npix[1];
295 felemi = felemz;
296 felemo = 1;
297 xsize = newpix[0];
298 ysize = newpix[1];
299
300 fctpntr = FCT.ENTRIES + fno; /* use DATTYP of father */
301 dattyp = fctpntr->DATTYP;
302 nbyt = get_byte(dattyp);
303 forma = fctpntr->FORMAT;
304
305 fctpntr = FCT.ENTRIES + tno;
306 fctpntr->DATTYP = fctpntr->FORMAT = dattyp; /* that's equal for the son */
307 fctpntr->NOBYTE = nbyt;
308 fctpntr->PIXPBL = OUR_BLOCK_SIZE / fctpntr->NOBYTE;
309 fctpntr = FCT.ENTRIES + fno;
310
311
312 kpix = xsize * ysize; /* allocate virtual memory for file copy */
313 wpntra = malloc((size_t)(kpix*nbyt));
314 if (wpntra == (char *) 0) return ERR_INPINV;
315
316
317 if (fctpntr->FILTYP > 0) /* FITS file */
318 {
319 zf_loop: /* reading loop for planes */
320 wp[0] = wpntra;
321 for(nr=0; nr<ysize; nr++)
322 { /* must pass via SCFGET for FITS files ... */
323 status = SCFGET(fno,felemi,xsize,&actsz,wp[0]);
324 if (status != ERR_NORMAL) return status;
325
326 wp[0] += xsize*nbyt;
327 felemi += npix[0]; /* offset in father */
328 }
329
330 status = wrdisk(tno,felemo,kpix,wpntra); /* write plane to disk */
331 if (status != ERR_NORMAL) return status; /* because it's bdf */
332 felemo += kpix;
333
334 if (felemo <= totsize)
335 {
336 felemz += (npix[0] * npix[1]);
337 felemi = felemz;
338 goto zf_loop;
339 }
340 }
341 else
342 {
343 z_loop: /* reading loop for planes */
344 wp[0] = wpntra;
345 for(nr=0; nr<ysize; nr++)
346 {
347 if (dattyp != forma)
348 status = conv_dat(3,fno,felemi,xsize,&actsz,wp);
349 else
350 status = rddisk(fno,felemi,xsize,&actsz,wp[0]);
351 if (status != ERR_NORMAL) return status;
352
353 wp[0] += xsize*nbyt;
354 felemi += npix[0]; /* offset in father */
355 }
356
357 status = wrdisk(tno,felemo,kpix,wpntra); /* write plane to disk */
358 if (status != ERR_NORMAL) return status;
359 felemo += kpix;
360
361 if (felemo <= totsize)
362 {
363 felemz += (npix[0] * npix[1]);
364 felemi = felemz;
365 goto z_loop;
366 }
367 }
368
369
370 free(wpntra);
371 return ERR_NORMAL;
372 }
373
374 /*
375
376 */
377
378 int Finsfr(fno,tno)
379 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
380 .PURPOSE
381 reinsert subframe (tno) into father (fno)
382 .ALGORITHM
383 get compact subframe in via rddisk
384 then write it out to father via conv_dat or wrdisk
385 .RETURNS
386 return status ( 0 = o.k )
387 -----------------------------------------------------------------------*/
388 int fno; /* IN: father file id */
389 int tno; /* IN: son file id */
390
391 {
392 register int nr;
393 int xsize, felemi, felemo, status;
394 int sta[3], end[3];
395 int savlink, iav, kpix, nbyt, totsize, actsz;
396 int felemz, ysize, dattyp, forma;
397 int knul = 0, kuni = 0;
398
399 char *wpntra, *wp[2];
400
401
402 /* get descriptors with values of father */
403
404 fctpntr = FCT.ENTRIES + tno; /* son: clear LINK first, */
405 savlink = fctpntr->LINK[0]; /* so we can read */
406 fctpntr->LINK[0] = 0; /* descr.s from extracted frame */
407 (void) SCDRDI(tno,"__NPIX",1,3,&iav,npix,&kuni,&knul);
408 (void) SCDRDI(tno,"__STPIX",1,3,&iav,sta,&kuni,&knul);
409 (void) SCDRDI(tno,"__ENPIX",1,3,&iav,end,&kuni,&knul);
410 fctpntr->LINK[0] = savlink; /* reset LINK */
411
412 felemz = sta[0] + (sta[1]-1)*npix[0] + (sta[2]-1)*npix[0]*npix[1];
413 felemo = felemz;
414 felemi = 1;
415 xsize = end[0] - sta[0] + 1;
416 ysize = end[1] - sta[1] + 1;
417 totsize = xsize * ysize * (end[2] - sta[2] + 1);
418
419
420 fctpntr = FCT.ENTRIES + fno; /* allocate virtual memory for file copy */
421 dattyp = fctpntr->DATTYP; /* same for father + son */
422 nbyt = get_byte(dattyp);
423 forma = fctpntr->FORMAT;
424
425 kpix = xsize * ysize; /* pull in a full squeezed plane */
426 wpntra = malloc((size_t)(kpix*nbyt));
427 if (wpntra == (char *) 0) return ERR_INPINV;
428
429
430 if (fctpntr->FILTYP > 0) /* FITS file */
431 {
432 zf_loop: /* reading loop for planes */
433 wp[0] = wpntra;
434 status = rddisk(tno,felemi,kpix,&actsz,wpntra);
435 if (status != ERR_NORMAL) return status;
436
437 for(nr=0; nr<ysize; nr++)
438 { /* must pass via SCFGET for FITS files ... */
439 status = SCFPUT(fno,felemo,xsize,wp[0]);
440 if (status != ERR_NORMAL) return status;
441
442 wp[0] += xsize*nbyt;
443 felemo += npix[0]; /* offset in father */
444 }
445
446 felemi += kpix;
447 if (felemi <= totsize)
448 {
449 felemz += (npix[0] * npix[1]);
450 felemo = felemz;
451 goto zf_loop;
452 }
453 }
454 else
455 {
456 z_loop: /* reading loop for planes */
457 wp[0] = wpntra;
458 status = rddisk(tno,felemi,kpix,&actsz,wpntra);
459 if (status != ERR_NORMAL) return status;
460
461 for (nr=0; nr<ysize; nr++)
462 {
463 if (dattyp != forma)
464 status = conv_dat(2,fno,felemo,xsize,&actsz,wp);
465 else
466 status = wrdisk(fno,felemo,xsize,wp[0]);
467 if (status != ERR_NORMAL) return status;
468
469 wp[0] += xsize*nbyt;
470 felemo += npix[0];
471 }
472
473 felemi += kpix;
474 if (felemi <= totsize)
475 {
476 felemz += (npix[0] * npix[1]);
477 felemo = felemz;
478 goto z_loop; /* get next plane */
479 }
480 }
481
482
483 free(wpntra);
484 return ERR_NORMAL;
485 }
486