]> Pileus Git - ~andy/rsl/blob - toga.c
Changes from Bart (2011-02-01)
[~andy/rsl] / toga.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1992  Dennis F. Flanigan Jr. of Applied Research Corporation,
5                         Landover, Maryland, a NASA/GSFC on-site contractor.
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16
17     You should have received a copy of the GNU Library General Public
18     License along with this library; if not, write to the Free
19     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20         */
21 /* toga/old sigmet/Darwin access routines
22  * V1.0 12/15/93   by John Merritt.
23  *
24  *  1. Use CFLAGS = -DUSE_PLOG if you want to use the PLOG library.
25  *----------------------------------------------------------------------
26  *
27  * Dennis Flanigan, Jr.
28  * Applied Research Corp.
29  * NASA GSFC Code 910.1
30  * 
31  *
32  * 15 Jul 93
33  * Added tg_prt_head function
34  *
35  * 28 Jun 93
36  * tg_file_str added
37  *
38  * 09 Jun 93
39  * Added tg_open function    ...Mike
40  *
41  * 5/10/93
42  * Modified tg_read_ray and created tg_decode_ray_data .
43  * tg_read_ray now returns decoded real-valued ray data in a 
44  * tg_ray_data structure instead of toga-format-encoded ray data
45  * in a rp_ray structure.      Mike
46  *
47  * 8/13/92
48  * Made changes so that code can be generated in library routines.
49  * Library will be called libtg.a
50  *
51  * 7/10/92
52  * Routines to access toga data from Darwin.   These routines work
53  * with the old sigmet data format that was used in Darwin from 87 
54  * to 91.
55  *
56  */
57
58 #include <stdio.h>
59 #include <stdarg.h>
60 #include <unistd.h>
61 #include <string.h>
62 #include <errno.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <fcntl.h>
66 #include <stdlib.h>
67
68 #include "toga.h"
69
70 #ifdef USE_PLOG
71 #include "plog.h"
72 #endif
73
74 #if defined(__linux)
75 void swab(const void *from, void *to, size_t n);
76 #endif
77 int tg_open(char *,tg_file_str *);
78 int tg_read_map_head(tg_file_str *);
79 float tg_make_ang(unsigned short);
80 int tg_read_map_bytes(tg_file_str *,void *,int);
81 int tg_read_rec_bytes(tg_file_str *,char *,int);
82 int tg_read_map_rec(tg_file_str *);
83 void tg_decode_ray_data(tg_file_str *,short *);
84 int tg_read_ray(tg_file_str *);
85 void tg_prt_head(tg_map_head_str *,int);
86
87 FILE *uncompress_pipe (FILE *fp);
88
89
90 int tg_open(char *filename,tg_file_str *tg_file)
91    {
92    /* open the toga data file */
93    if (filename == NULL) tg_file->fd = STDIN_FILENO; /* Stdin */
94    else 
95          if ((tg_file->fd=open(filename,O_RDONLY)) == -1)
96            {
97 #ifdef USE_PLOG
98                  plog("tg_open: Error opening toga data file\n",PLOG_P);
99 #endif
100                  return(TG_SYS_ERR);
101            }
102    /* Unfortunately, there is no tg_close to modularize the following
103     * pipe close.  Shouldn't be any problems anyway.
104     */
105    (void) uncompress_pipe(fdopen(tg_file->fd, "r")); /* Redirect through gunzip. */
106    /* initialize buffer pointers, flags */
107    tg_file->buf_ind = 32769;
108    tg_file->buf_end = 32769;
109    tg_file->first_rec = TRUE;
110    tg_file->data_ind = 2044;
111
112    /* read the map header from the toga file into the tg_file 
113           map_head str */
114    if (tg_read_map_head(tg_file) < 0)
115           {
116           return(-1);  /* Can't read toga map header */
117           }
118
119    return(TG_OK);
120    }
121
122
123
124 int tg_read_map_head(tg_file_str *tg_file)
125    {
126    int n;
127    tg_map_head_str buf;
128
129    if((n = read(tg_file->fd,&buf,TG_HDSIZE)) != TG_HDSIZE)
130           {
131           if (n < 0)
132                  {
133                  fprintf(stderr,"tg_read_map_head: (%d)%s \n",errno,strerror(errno));
134                  }
135           else
136                  {
137                  fprintf(stderr,"tg_read_map_head: Didn't read entire file header.\n\007");
138                  fprintf(stderr,"tg_read_map_head: Bytes read: %d \n",n);
139                  }
140           return (-1);
141           }
142    
143    /* Do we need to swap bytes ? 
144     * Test for byte swapping is done by checking the storm year.
145     * If less then 2050 then bytes are in correct order, otherwise
146     * swap bytes.
147     */
148    if((buf.strm_year < 2050) && (buf.strm_year > 1960))
149           {
150           memcpy(&(tg_file->map_head),&buf,sizeof(tg_map_head_str));
151           tg_file->swap_bytes = FALSE;
152           }
153    else
154           {
155           swab(&buf,&(tg_file->map_head),sizeof(tg_map_head_str));
156           tg_file->swap_bytes = TRUE;
157           }
158
159    /* The file header has now been written into tg_file->map_head .
160           Check for reasonable strm_year and strm_mon.  If not reasonable,
161           we assume the file is garbled beyond legibility, or perhaps this
162           is not a TOGA format data file. */
163    if ((tg_file->map_head.strm_year < 2050) && 
164           (tg_file->map_head.strm_year > 1960))
165           {
166           if ((tg_file->map_head.strm_mon > 0) &&
167                   (tg_file->map_head.strm_mon < 13))
168                  {
169                  /* file header OK, reset pointers into buffer */
170                  tg_file->buf_ind = 32769;
171                  tg_file->buf_end = 32769;
172                  return(0);
173                  }
174           }
175
176    /* If we've reached this point, we can't read the file header. */
177 #ifdef USE_PLOG
178    plog("tg_read_map_head: Can't read TOGA file header\n",PLOG_P);
179 #endif
180    return(-1);
181    
182    }
183
184 float tg_make_ang(unsigned short binang)
185    {
186    float maxval = 65536.0;
187    
188    return(360.0 * ((float)binang/maxval));
189    }
190
191 int tg_read_map_bytes(tg_file_str *tg_file,void *buf,int size)
192    {
193    int m,n,wsize;
194    short dec_key;
195    int ret_val = 0;
196    char *wbuf;
197    
198    /* Copy the pointer to buf to wbuf.  wbuf stands for working buf */
199    wbuf = buf;
200    
201    /* size is size in bytes. wsize is size in words. */
202    wsize = size / 2;
203    
204    while(wsize > 0)
205           {
206           /* Do we need to decompress more data? */
207           if((tg_file->buf_ind + wsize - 1) > tg_file->buf_end)
208                  {
209                  /* Yes we do, but first make sure that we don't need any data
210                   * already in buffer.
211                   */
212                  if((tg_file->buf_end - tg_file->buf_ind) > 0)
213                         {
214                         /* There is data in buffer that we need, before we decompress
215                          * next string of data. 
216                          */
217                         memcpy(wbuf,&(tg_file->dec_buf[tg_file->buf_ind]),
218                                    (tg_file->buf_end - tg_file->buf_ind + 1) * 2);
219                         wsize = wsize - (tg_file->buf_end - tg_file->buf_ind + 1);
220                         wbuf = (wbuf + ((tg_file->buf_end - tg_file->buf_ind + 1) * 2));
221                         ret_val = ret_val + ((tg_file->buf_end - tg_file->buf_ind + 1)*2);
222                         }
223                  
224                  /* Is this data or is this length of zeros */
225                  if((n = tg_read_rec_bytes(tg_file,(char *)&dec_key,2)) <= 0)
226                         {
227                         tg_file->buf_ind = tg_file->buf_end;
228                         return(n);
229                         }
230                  
231                  n = (0x1 & (dec_key >> 15));
232                  if (n == 1)
233                         {
234                         /* No, it is not length of zeros.
235                          */
236                         n = dec_key & 0x7FFF;
237                         
238                         if((m = tg_read_rec_bytes(tg_file,(char *)&(tg_file->dec_buf),
239                                                                           n*2)) <= 0)
240                            {
241                            return(m);
242                            }
243                         tg_file->buf_ind = 0;
244                         tg_file->buf_end = n - 1;
245                         }
246                  else
247                         {
248                         /* Is this end of data ? */
249                         if(dec_key == 0)
250                            {
251                            /* End of Data */
252                            tg_file->buf_ind = 32769;
253                            tg_file->buf_end = 32769;
254                            return(TG_END_DATA);
255                            }
256                         /* Is this end of ray? */
257                         else if(dec_key == 1)
258                            {
259                            /* plog("End of ray\n",LOG); */
260                            tg_file->buf_ind = tg_file->buf_end;
261                            return(TG_END_RAY);
262                            }
263                         /* Fill decompress buffer with 0's */
264                         else
265                            {
266                            memset(&(tg_file->dec_buf),(char)0,dec_key * 2);
267                            tg_file->buf_ind = 0;
268                            tg_file->buf_end = dec_key - 1;
269                            }
270                         }
271                  }
272           
273           /* Is decompressed data enough to fill request ? */
274           if((tg_file->buf_end - tg_file->buf_ind + 1) < wsize)
275                  {
276                  /* Will need to decompress more data */
277                  memcpy(wbuf,&(tg_file->dec_buf[tg_file->buf_ind]),
278                                 (tg_file->buf_end - tg_file->buf_ind + 1) * 2);
279                  wsize = wsize - (tg_file->buf_end - tg_file->buf_ind + 1);
280                  
281                  wbuf = (wbuf + ((tg_file->buf_end - tg_file->buf_ind + 1) * 2));
282                  
283                  ret_val = ret_val + ((tg_file->buf_end - tg_file->buf_ind + 1) * 2);
284                  tg_file->buf_ind = tg_file->buf_end;
285                  }
286           else
287                  {
288                  /* There is enough decompressed data for request */
289                  memcpy(wbuf,&(tg_file->dec_buf[tg_file->buf_ind]),wsize * 2);
290                  tg_file->buf_ind = tg_file->buf_ind + wsize;
291                  ret_val = ret_val + (wsize * 2);
292                  wsize = 0;
293                  }
294           }
295    return(ret_val);
296    }   
297
298 int tg_read_rec_bytes(tg_file_str *tg_file,char *buf,int size)
299    {
300    /* Return size number of bytes in buf.  Read in new record
301     * if needed.  Check to make sure missing records with
302     * record number variable found in record header.
303         */
304    int wsize,n;
305    char *wbuf;
306    
307    wsize = size/2;
308    wbuf = buf;
309    
310    /* Is there enough data in recbuf for request */
311    if((tg_file->data_ind + wsize - 1) > 2043)
312           {
313           /* No there is not enough data for this request, but before
314            * we read the next buffer, we should copy what we have into
315            * the buffer pointed to by buf.
316            */
317           if(tg_file->data_ind < 2044)
318                  {
319                  memcpy(wbuf,&(tg_file->recbuf.data[tg_file->data_ind]),
320                                 (2043 - tg_file->data_ind + 1) * 2);
321                  wsize = wsize - (2043 - tg_file->data_ind + 1);
322                  wbuf = (wbuf + ((2043 - tg_file->data_ind + 1) * 2));
323                  }
324           
325           /* New record has to be read in */
326           tg_file->data_ind = 0;
327           if((n = tg_read_map_rec(tg_file)) < TG_RECSIZE)
328                  {
329                  if(n == 0)
330                         {
331                         return(n);
332                         }
333                  else
334                         {
335                         fprintf(stderr,"tg_read_map_rec: %d \n",n);
336                         return(-1);
337                         }
338                  }
339           if(tg_file->first_rec)
340                  {
341                  tg_file->recnum = tg_file->recbuf.rec_num;
342                  tg_file->first_rec = FALSE;
343                  }
344           else
345                  {
346                  if((tg_file->recnum + 1) != tg_file->recbuf.rec_num)
347                         {
348                         tg_file->recnum = tg_file->recbuf.rec_num;
349                         /* Set index to next ray */
350                         tg_file->data_ind = tg_file->recbuf.first_ray - 5;
351                         return(TG_REC_NOSEQ);
352                         }
353                  else
354                         {
355                         tg_file->recnum = tg_file->recbuf.rec_num;
356                         }
357                  }                         
358           tg_file->data_ind = 0;
359           }
360    
361    memcpy(wbuf,&(tg_file->recbuf.data[tg_file->data_ind]),wsize*2);
362    tg_file->data_ind = tg_file->data_ind + wsize;
363    
364    return(size);
365    }
366
367 int tg_read_map_rec(tg_file_str *tg_file)
368    {
369    int n;
370    static char buf[TG_RECSIZE];
371    
372    if((n = read(tg_file->fd,buf,TG_RECSIZE)) < 0)
373           {
374           fprintf(stderr,"tg_read_map_rec: Error while reading data record.\n");
375           fprintf(stderr,"tg_read_map_rec: (%d)%s\n",errno,strerror(errno));
376           }
377    else if(n == 0)
378           {
379           /* assume end of file */
380           }
381    else if(n != TG_RECSIZE)
382           {
383           fprintf(stderr,"tg_read_map_rec: Did not read all of data record.\n\007");
384           fprintf(stderr,"tg_read_map_rec: Bytes read: %d \n",n);
385           }
386    else
387           {   
388           if(tg_file->swap_bytes)
389                  {
390                  /* record read in correctly */
391                  swab(buf,&(tg_file->recbuf),TG_RECSIZE);
392                  }
393           else
394                  {
395                  memcpy(&(tg_file->recbuf),buf,TG_RECSIZE);
396                  }
397           }
398
399    return(n);
400    }
401
402
403 void tg_decode_ray_data(tg_file_str *tg_file,short ray_buf[])
404    {
405    int j,k;
406    short dbval,vel,temp1,temp2;
407    float nyq_vel;
408    
409
410    k = 0;  /* intialize ray->data index */
411    switch (tg_file->ray_head.type)
412           {
413         case 1:
414           /* TOGA record type 1 contains uncorrected & corrected 
415                  reflectivity data, velocity, and spectrum width values. */ 
416           tg_file->ray.da_inv[TG_DM_IND] = TRUE;
417           tg_file->ray.da_inv[TG_DZ_IND] = TRUE;
418           tg_file->ray.da_inv[TG_VR_IND] = TRUE;
419           tg_file->ray.da_inv[TG_SW_IND] = TRUE;
420
421           tg_file->ray.num_bins[TG_DM_IND] = tg_file->ray_head.srngkill - 1;
422           tg_file->ray.num_bins[TG_DZ_IND] = tg_file->ray_head.srngkill - 1;
423           tg_file->ray.num_bins[TG_VR_IND] = tg_file->ray_head.srngkill - 1;
424           tg_file->ray.num_bins[TG_SW_IND] = tg_file->ray_head.srngkill - 1;
425
426           tg_file->ray.start_km[TG_DM_IND] = tg_file->ray_head.strt_rng/40.0;
427           tg_file->ray.start_km[TG_DZ_IND] = tg_file->ray_head.strt_rng/40.0;
428           tg_file->ray.start_km[TG_VR_IND] = tg_file->ray_head.strt_rng/40.0;
429           tg_file->ray.start_km[TG_SW_IND] = tg_file->ray_head.strt_rng/40.0;
430           
431           tg_file->ray.interval_km[TG_DM_IND] = tg_file->map_head.rnginc/1000.0;
432           tg_file->ray.interval_km[TG_DZ_IND] = tg_file->map_head.rnginc/1000.0;
433           tg_file->ray.interval_km[TG_VR_IND] = tg_file->map_head.rnginc/1000.0;
434           tg_file->ray.interval_km[TG_SW_IND] = tg_file->map_head.rnginc/1000.0;
435           
436           for (j=0; j<tg_file->ray.num_bins[TG_DM_IND]; j++)
437                  {
438                  /* check OK flag bit to see if this is valid data */
439                  if ((ray_buf[j*3] & 0x8000) == 0)
440                         {
441                         /* bad data, store no_data flag into each field */
442                         tg_file->ray.data[TG_DM_IND][k] = TG_NO_DATA;
443                         tg_file->ray.data[TG_DZ_IND][k] = TG_NO_DATA;
444                         tg_file->ray.data[TG_VR_IND][k] = TG_NO_DATA;
445                         tg_file->ray.data[TG_SW_IND][k] = TG_NO_DATA;
446                         }
447                  else   /* good data, uncode and store into tg_ray_data struct */
448                         {
449                         /******* do corrected dbz value *********/
450                         dbval = ray_buf[j*3 + 1] & 0x0FFF;
451                         /* The dbz data are signed 12 bit values. Check sign bit
452                            of dbz value */
453                         if ((dbval & 0x0800) == 0)
454                            {
455                            /* store unscaled positive dbz value */
456                            tg_file->ray.data[TG_DZ_IND][k] = dbval/16.0;
457                            }
458                         else  /* 12 bit negative value */
459                            {
460                            /* make 12 bit value a 16 bit word by extending sign bit */
461                            dbval = (dbval | ~0x0FFF); 
462                            if (dbval == -2048)  /* -2048 indicates bad data */
463                                   {
464                                   tg_file->ray.data[TG_DZ_IND][k] = TG_NO_DATA;  /* bad data */
465                                   }
466                            else
467                                   {
468                                   /* store unscaled negative dbz value */
469                                   tg_file->ray.data[TG_DZ_IND][k] = dbval/16.0;
470                                   }
471                            }
472                         
473                         
474                         /******* do uncorrected dbz value *********/
475                         dbval = ray_buf[j*3 + 2] & 0x0FFF;
476                         /* The dbz data are signed 12 bit values. Check sign bit
477                            of dbz value */
478                         if ((dbval & 0x0800) == 0)
479                            {
480                            /* store unscaled positive dbz value */
481                            tg_file->ray.data[TG_DM_IND][k] = dbval/16.0;
482                            }
483                         else  /* 12 bit negative value */
484                            {
485                            /* make 12 bit value a 16 bit word by extending sign bit */
486                            dbval = (dbval | ~0x0FFF); 
487                            if (dbval == -2048)  /* -2048 indicates bad data */
488                                   {
489                                   tg_file->ray.data[TG_DM_IND][k] = TG_NO_DATA;  /* bad data */
490                                   }
491                            else
492                                   {
493                                   /* store unscaled negative dbz value */
494                                   tg_file->ray.data[TG_DM_IND][k] = dbval/16.0;
495                                   }
496                            }
497                         
498                         /* compute the nyquist velocity for subsequent scaling
499                            of velocity and spectrum width values. */
500                         /* nyquist velocity = wavelength/(4*pulse repetition period) */
501                         nyq_vel = (tg_file->map_head.wavelen/10000.0) /
502                                    (4.0*(1.0/tg_file->map_head.prf));        /*m/s*/
503                         
504                         /******** do velocity value **********/
505                         /* toga velocity values are 10 bits long and range from
506                            -512 to 511 */
507                         vel = ray_buf[j*3] & 0x03FF; /* strip off leading 6 bits */
508                         /* This is a signed 10 bit value. Check sign bit */
509                         if ((vel & 0x0200) == 0)  /* sign bit set? */
510                            {
511                            /* no, store positive velocity value */
512                            tg_file->ray.data[TG_VR_IND][k] = vel/511.0*nyq_vel;
513                            }
514                         else  /* 10 bit negative value */
515                            {
516                            /* make 10 bit value a 16 bit word by extending sign bit */
517                            vel = vel | ~0x03FF;
518                            /* store negative velocity value */
519                            tg_file->ray.data[TG_VR_IND][k] = vel/512.0*nyq_vel;
520                            }
521                         
522                         /******** do spectrum width ***********/
523                         /* toga spectrum width values are 8 bits and range from
524                            zero to 1/2 . see toga documentation */
525                         /* get the low order 4 bits into their correct positions */
526                         temp1 = (ray_buf[j*3 + 1] >> 12) & 0x000F;
527                         /* get the high order 4 bits into correct positions */
528                         temp2 = (ray_buf[j*3 + 2] >> 8) & 0x00F0;
529                         /* store the spectrum width value into tg_ray_data struct */
530                         tg_file->ray.data[TG_SW_IND][k] = ((temp1 | temp2)/512.0)*nyq_vel;
531                         
532                         }
533                  k += 1;
534                  }     /* end for */
535           break;   /* case 1: */
536           
537         case 19:   /* uncorrected reflectivity data */
538           /* TOGA record type 19 contains only uncorrected reflectivity values */
539           tg_file->ray.da_inv[TG_DM_IND] = TRUE;
540           tg_file->ray.da_inv[TG_DZ_IND] = FALSE;
541           tg_file->ray.da_inv[TG_VR_IND] = FALSE;
542           tg_file->ray.da_inv[TG_SW_IND] = FALSE;
543           tg_file->ray.num_bins[TG_DM_IND] = tg_file->ray_head.srngkill - 1;
544           tg_file->ray.start_km[TG_DM_IND] = tg_file->ray_head.strt_rng/40.0;
545           tg_file->ray.interval_km[TG_DM_IND] = tg_file->map_head.rnginc/1000.0;
546
547           for (j=0; j<tg_file->ray.num_bins[TG_DM_IND]; j++)
548                  {
549                  /* check OK flag bit to see if this is valid data */
550                  if ((ray_buf[j] & 0x8000) == 0)
551                         {
552                         tg_file->ray.data[TG_DM_IND][k] = TG_NO_DATA;  /* bad data, store flag */
553                         }
554                  else   /* good data, uncode it and store into ray->data */
555                         {
556                         dbval = ray_buf[j] & 0x7FFF; /* strip off OK flag */
557                         /* This is a signed 15 bit dbz value. Check sign bit */
558                         if ((dbval & 0x4000) == 0)  /* sign bit set? */
559                            {
560                            /* no, positive db value. Unscale by factor of 16 and store */
561                            tg_file->ray.data[TG_DM_IND][k] = dbval/16.0;
562                            }
563                         else  /* 15 bit negative value */
564                            {
565                            /* make 15 bit value a 16 bit word by extending sign bit,
566                                   unscale by factor of 16 and store. */
567                            tg_file->ray.data[TG_DM_IND][k] = (dbval | ~0x7FFF)/16.0;
568                            }
569                         }
570                  
571                  k += 1;
572                  }    /* end for */
573           break;   /* case 19: */
574
575         default:
576           fprintf(stderr,"tg_decode_ray_data: found unknown toga data type\n");
577           fprintf(stderr,"tg_decode_ray_data: ignore this ray\n");
578           /* return TG_RAY_NOTYPE; */
579           break;
580           
581           }     /* end switch */
582
583    }
584
585
586
587 int tg_read_ray(tg_file_str *tg_file)
588    {
589    int n,a,o;
590    int bin_size;   
591    short ray_buf[1800];
592    
593    /* bin_size is size in bytes of data from one bin */
594
595    n = tg_read_map_bytes(tg_file,&(tg_file->ray_head),sizeof(tg_ray_head_str));
596    switch(n)
597           {
598         case TG_END_DATA:
599           /* plog("tg_read_ray: TG_END_DATA for ray_head\n",LOG); */
600           break;
601         case TG_REC_NOSEQ:
602           /* plog("tg_read_ray: TG_REC_NOSEQ for ray_head\n",LOG); */
603           break;
604         case 0:
605           n = TG_RAY_READ_ERR;
606           /* plog("tg_read_ray: zero bytes for ray_head\n",LOG); */
607           break;
608         case -1:
609           /* plog("tg_read_ray: error for ray_head\n",LOG); */
610           break;
611         default:
612           tg_file->ray.azm = tg_make_ang(tg_file->ray_head.azm);
613           tg_file->ray.elev = tg_make_ang(tg_file->ray_head.elev);
614
615           switch(tg_file->ray_head.type)
616                  {
617            case 1:
618                  /* plog("tg_read_ray:doppler data\n",LOG); */
619                  bin_size = 6 * (tg_file->map_head.numbin);
620                  break;
621            case 19:
622                  /* plog("tg_read_ray:reflectivity data\n",LOG); */
623                  bin_size = 2 * (tg_file->map_head.numbin);
624                  break;
625            default:
626                  /* plog("tg_read_ray:undefined data type\n",PRINT); */
627                  return TG_RAY_NOTYPE;
628                  break;
629                  }
630
631           /* read ray contents from disk file into ray_buf */
632           if((o = tg_read_map_bytes(tg_file,ray_buf,bin_size)) == bin_size)
633                  {
634                  /* decode the raw ray data in ray_buf[] and place into
635                         the tg_file ray structure */
636                  tg_decode_ray_data(tg_file,ray_buf);
637                  }
638           else
639                  {
640                  fprintf(stderr,"*** wrong number of bytes read from ray ***\n");
641                  fprintf(stderr,"\n,number_bytes read:%d\n",o);
642                  return TG_RAY_READ_ERR;
643                  }
644
645           /* read end_of_ray flag from disk file */
646           o = 0;
647           a = 0;
648           while((o != TG_END_RAY) && (a < 100))
649                  {
650              o=tg_read_map_bytes(tg_file,ray_buf,2);
651                  a++;
652                  }
653           if(a >= 100)
654                  {
655                  fprintf(stderr,"tg_read_ray: Could not find TG_END_RAY\n");
656                  return TG_RAY_READ_ERR;
657                  }
658           }
659    return n;
660    }
661
662
663
664 void tg_prt_head(tg_map_head_str *head,int verbose)
665    {
666    /* print out the contents of a file map header */
667
668    
669 #ifdef USE_PLOG
670    plog("strm_year:     %d\n",PLOG_L,head->strm_year);
671    plog("strm_mon:      %d\n",PLOG_L,head->strm_mon);
672    plog("strm_day:      %d\n",PLOG_L,head->strm_day);
673    plog("strm_num:      %d\n",PLOG_L,head->strm_num);
674    plog("map_num:       %d\n",PLOG_L,head->map_num);
675    plog("\n",PLOG_L);
676    plog("scan_year:     %d\n",PLOG_L,head->scan_year);
677    plog("scan_mon:      %d\n",PLOG_L,head->scan_mon);
678    plog("scan_day:      %d\n",PLOG_L,head->scan_day);
679    plog("scan_hour:     %d\n",PLOG_L,head->scan_hour);
680    plog("scan_min:      %d\n",PLOG_L,head->scan_min);
681    plog("scan_sec:      %d\n",PLOG_L,head->scan_sec);
682    plog("data_set:      %d\n",PLOG_L,head->data_set);
683    plog("\n",PLOG_L);
684    if(verbose)
685           {
686           plog("tp1_ar:        %d\n",PLOG_L,head->tp1_ar);
687           plog("tp1_occw:      %d\n",PLOG_L,head->tp1_occw);
688           plog("tp1_dibit:     %d\n",PLOG_L,head->tp1_dibit);
689           plog("tp1_debit:     %d\n",PLOG_L,head->tp1_debit);
690           plog("\n",PLOG_L);
691           plog("tp2_ar:        %d\n",PLOG_L,head->tp2_ar);
692           plog("tp2_occw:      %d\n",PLOG_L,head->tp2_occw);
693           plog("tp2_dibit:     %d\n",PLOG_L,head->tp2_dibit);
694           plog("tp2_debit:     %d\n",PLOG_L,head->tp2_debit);
695           plog("\n",PLOG_L);
696           plog("status:        %d\n",PLOG_L,head->status);
697           plog("strng:         %d\n",PLOG_L,head->strng);
698           plog("numbin:        %d\n",PLOG_L,head->numbin);
699           plog("rnginc:        %d\n",PLOG_L,head->rnginc);
700           plog("rngjit:        %d\n",PLOG_L,head->rngjit);
701           plog("numcbin:       %d\n",PLOG_L,head->numcbin);
702           plog("\n",PLOG_L);
703           plog("strtcal1:      %d\n",PLOG_L,head->strtcal1);
704           plog("strtcal2:      %d\n",PLOG_L,head->strtcal2);
705           plog("stepcal:       %d\n",PLOG_L,head->stepcal);
706           }
707    plog("azmleft:       %d\n",PLOG_L,head->azmleft);
708    plog("azmrght:       %d\n",PLOG_L,head->azmrght);
709    plog("elev_low:      %d\n",PLOG_L,head->elev_low);
710    plog("elev_hgh:      %d\n",PLOG_L,head->elev_hgh);
711    plog("\n",PLOG_L);
712    plog("at_angres:     %d\n",PLOG_L,head->at_angres);
713    plog("numfix_ang:    %d\n",PLOG_L,head->numfix_ang);
714    if(verbose)
715           {
716           for(a=0;a<20;a++)
717                  {
718                  plog("angfix[%2d]:    %d\n",PLOG_L,a,head->angfix[a]);
719                  }
720           plog("\n",PLOG_L);
721           plog("rlparm:        %d\n",PLOG_L,head->rlparm);
722           plog("signois:       %d\n",PLOG_L,head->signois);
723           plog("sigcltr:       %d\n",PLOG_L,head->sigcltr);
724           plog("thrsh_flg:     %d\n",PLOG_L,head->thrsh_flg);
725           plog("\n",PLOG_L);
726           plog("numdsp:        %d\n",PLOG_L,head->numdsp);
727           plog("numwrd:        %d\n",PLOG_L,head->numwrd);
728           plog("\n",PLOG_L);
729           plog("scanmod:       %d\n",PLOG_L,head->scanmod);
730           plog("filename:      %s\n",PLOG_L,head->filename);
731           plog("\n",PLOG_L);
732           plog("prf:           %d\n",PLOG_L,head->prf);
733           plog("transiz:       %d\n",PLOG_L,head->transiz);
734           plog("spconf:        %d\n",PLOG_L,head->spconf);
735           plog("sufchar:       %d\n",PLOG_L,head->sufchar);
736           plog("recsat1:       %d\n",PLOG_L,head->recsat1);
737           plog("recsat2:       %d\n",PLOG_L,head->recsat2);
738           plog("\n",PLOG_L);
739           plog("dsp1cor_log:   %d\n",PLOG_L,head->dsp1cor_log);
740           plog("dsp1cor_iad:   %d\n",PLOG_L,head->dsp1cor_iad);
741           plog("dsp1cor_qad:   %d\n",PLOG_L,head->dsp1cor_qad);
742           plog("dsp1crr_log:   %d\n",PLOG_L,head->dsp1crr_log);
743           plog("dsp1crr_iad:   %d\n",PLOG_L,head->dsp1crr_iad);
744           plog("dsp1crr_qad:   %d\n",PLOG_L,head->dsp1crr_qad);
745           plog("\n",PLOG_L);
746           plog("dsp2cor_log:   %d\n",PLOG_L,head->dsp2cor_log);
747           plog("dsp2cor_iad:   %d\n",PLOG_L,head->dsp2cor_iad);
748           plog("dsp2cor_qad:   %d\n",PLOG_L,head->dsp2cor_qad);
749           plog("dsp2crr_log:   %d\n",PLOG_L,head->dsp2crr_log);
750           plog("dsp2crr_iad:   %d\n",PLOG_L,head->dsp2crr_iad);
751           plog("dsp2crr_qad:   %d\n",PLOG_L,head->dsp2crr_qad);
752           plog("\n",PLOG_L);
753           }
754    plog("wavelen:       %d\n",PLOG_L,head->wavelen);
755    plog("pulsewd:       %d\n",PLOG_L,head->pulsewd);
756    plog("hortran_pow:   %d\n",PLOG_L,head->hortran_pow);
757    plog("vertran_pow:   %d\n",PLOG_L,head->vertran_pow);
758    plog("\n",PLOG_L);
759    if(verbose)
760           {
761           plog("high_zero:     %d\n",PLOG_L,head->high_zero);
762           }
763    plog("sitelat:       %d\n",PLOG_L,head->sitelat);
764    plog("sitelong:      %d\n",PLOG_L,head->sitelong);
765    plog("time_zone:     %d\n",PLOG_L,head->time_zone);
766    plog("\n",PLOG_L);
767    if(verbose)
768           {
769           plog("zm_dsp1_mas:   %d\n",PLOG_L,head->zm_dsp1_mas);
770           plog("zm_dsp1_slv:   %d\n",PLOG_L,head->zm_dsp1_slv);
771           plog("zm_dsp2_mas:   %d\n",PLOG_L,head->zm_dsp2_mas);
772           plog("zm_dsp2_slv:   %d\n",PLOG_L,head->zm_dsp2_slv);
773           plog("\n",PLOG_L);
774           plog("minz_dsp1_mas: %d\n",PLOG_L,head->minz_dsp1_mas);
775           plog("minz_dsp1_slv: %d\n",PLOG_L,head->minz_dsp1_slv);
776           plog("minz_dsp2_mas: %d\n",PLOG_L,head->minz_dsp2_mas);
777           plog("minz_dsp2_slv: %d\n",PLOG_L,head->minz_dsp2_slv);
778           plog("\n",PLOG_L);
779           plog("num_pol:       %d\n",PLOG_L,head->num_pol);
780           plog("exinfo_rayhd:  %d\n",PLOG_L,head->exinfo_rayhd);
781           plog("len_exhd:      %d\n",PLOG_L,head->len_exhd);
782           plog("\n",PLOG_L);
783           }
784    plog("lat_deg:       %d\n",PLOG_L,head->lat_deg);
785    plog("lat_hun_min:   %d\n",PLOG_L,head->lat_hun_min);
786    plog("lon_deg:       %d\n",PLOG_L,head->lon_deg);
787    plog("lon_hun_min:   %d\n",PLOG_L,head->lon_hun_min);
788    plog("\n",PLOG_L);
789    if(verbose)
790           {
791           plog("alt_atn:       %d\n",PLOG_L,head->alt_atn);
792           plog("alt_grn:       %d\n",PLOG_L,head->alt_grn);
793           plog("\n",PLOG_L);
794           plog("vel_plat:      %d\n",PLOG_L,head->vel_plat);
795           plog("vel_cor:       %d\n",PLOG_L,head->vel_cor);
796           plog("head_plat:     %d\n",PLOG_L,head->head_plat);
797           plog("head_dsp:      %d\n",PLOG_L,head->head_dsp);
798           plog("\n",PLOG_L);
799           plog("set_plat:      %d\n",PLOG_L,head->set_plat);
800           plog("drift_plat:    %d\n",PLOG_L,head->drift_plat);
801           plog("ok_plat:       %d\n",PLOG_L,head->ok_plat);
802           plog("\n",PLOG_L);
803           plog("comments:      %s\n",PLOG_L,head->comments);
804           }
805 #else
806    fprintf(stderr, "You must link with -lplog and compile the toga library with -DUSE_PLOG to get a printout of the header.\n");
807 #endif
808    }