]> Pileus Git - ~andy/rsl/blob - anyformat_to_radar.c
70be908f8f32634f7a1be1285125ca66be696236
[~andy/rsl] / anyformat_to_radar.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 1997
5             John H. Merritt
6             Space Applications Corporation
7             Vienna, Virginia
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU Library General Public
11     License as published by the Free Software Foundation; either
12     version 2 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     Library General Public License for more details.
18
19     You should have received a copy of the GNU Library General Public
20     License along with this library; if not, write to the Free
21     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 /*
24  * By John H. Merritt
25  * Science Applications Corporation, Vienna, VA
26  */
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include "rsl.h"
33 void rsl_readflush(FILE *fp);
34 /*********************************************************************/
35 /*                                                                   */
36 /*                   RSL_filetype                                    */
37 /*                                                                   */
38 /*********************************************************************/
39 enum File_type RSL_filetype(char *infile)
40 {
41   /* Open the input file and peek at the first few bytes to determine
42    * the type of file.
43    * 
44    * UF     - First two bytes 'UF'
45    *        - or 3,4 bytes 'UF'
46    *        - or 5,6 bytes 'UF'. This is the most common.
47    *
48    * WSR88D - First 8 bytes: 'ARCHIVE2' or 'AR2V0001'
49    *
50    * TOGA   - ??
51    * NSIG   - ??
52    * LASSEN - SUNRISE
53    * RSL    - RSL
54    * MCGILL - P A B
55    * RAPIC  - /IMAGE:
56    * RADTEC - 320      (decimal, in first two bytes)
57    * RAINBOW - First two bytes: decimal 1, followed by 'H'
58    */
59   FILE *fp;
60   char magic[11];
61
62   if ((fp = fopen(infile, "r")) == NULL) {
63         perror(infile);
64         return UNKNOWN;
65   }
66
67   /* Read the magic bytes. */
68   fp = uncompress_pipe(fp); /* If gzip available. */
69   if (fread(magic, sizeof(magic), 1, fp) != 1) {
70         char *magic_str = (char *)calloc(sizeof(magic)+1, sizeof(char));
71         memcpy(magic_str, magic, sizeof(magic));
72         fprintf(stderr,"Error fread: Magic is %s\n", magic_str);
73         free (magic_str);
74         perror("RSL_filetype");
75         fclose(fp);
76         return UNKNOWN;
77   }
78
79   rsl_readflush(fp); /* Fork, read and exit -- eliminates the 'Broken pipe' */
80
81   if (strncmp("ARCHIVE2.", magic, 9) == 0) return WSR88D_FILE;
82   if (strncmp("AR2V000", magic, 7) == 0) return WSR88D_FILE;
83   if (strncmp("UF", magic, 2) == 0) return UF_FILE;
84   if (strncmp("UF", &magic[2], 2) == 0) return UF_FILE;
85   if (strncmp("UF", &magic[4], 2) == 0) return UF_FILE;
86   if ((int)magic[0] == 0x0e &&
87           (int)magic[1] == 0x03 &&
88           (int)magic[2] == 0x13 &&
89           (int)magic[3] == 0x01
90           ) return HDF_FILE;
91   if (strncmp("RSL", magic, 3) == 0) return RSL_FILE;
92   if ((int)magic[0] == 7) return NSIG_FILE_V1;
93   if ((int)magic[1] == 7) return NSIG_FILE_V1;
94   if ((int)magic[0] == 27) return NSIG_FILE_V2;
95   if ((int)magic[1] == 27) return NSIG_FILE_V2;
96   if (strncmp("/IMAGE:", magic, 7) == 0) return RAPIC_FILE;
97   if ((int)magic[0] == 0x40 &&
98           (int)magic[1] == 0x01
99           ) return RADTEC_FILE;
100   if ((int)magic[0] == 0x01 && magic[1] == 'H') return RAINBOW_FILE;
101
102   if (strncmp("SUNRISE", &magic[4], 7) == 0) return LASSEN_FILE;
103 /* The 'P A B' is just too specific to be a true magic number, but that's all
104  * I've got.
105  */
106   if (strncmp("P A B ", magic, 6) == 0) return MCGILL_FILE;
107   /* Byte swapped ? */
108   if (strncmp(" P A B", magic, 6) == 0) return MCGILL_FILE;
109   if (strncmp("Volume", magic, 6) == 0) return EDGE_FILE;
110   if (strncmp("SSWB", magic, 4) == 0) return DORADE_FILE;
111   if (strncmp("VOLD", magic, 4) == 0) return DORADE_FILE;
112
113   return UNKNOWN;
114 }
115   
116
117
118
119   
120
121
122 /*********************************************************************/
123 /*                                                                   */
124 /*                   RSL_anyformat_to_radar                          */
125 /*                                                                   */
126 /*********************************************************************/
127
128 Radar *RSL_anyformat_to_radar(char *infile, ...)
129 {
130   va_list ap;
131   char *callid_or_file;
132   Radar *radar;
133
134 /* If it is detected that the input file is WSR88D, use the second argument
135  * as the call id of the site, or the file name of the tape header file.
136  *
137  * Assumption: Input files are seekable.
138  */
139   radar = NULL;
140   switch (RSL_filetype(infile)) {
141   case WSR88D_FILE:
142         callid_or_file = NULL;
143         va_start(ap, infile);
144         callid_or_file = va_arg(ap, char *);
145         va_end(ap);
146         radar = RSL_wsr88d_to_radar(infile, callid_or_file);
147         break;
148   case      UF_FILE: radar = RSL_uf_to_radar(infile);     break;
149   case    TOGA_FILE: radar = RSL_toga_to_radar(infile);   break;
150   case NSIG_FILE_V1: radar = RSL_nsig_to_radar(infile);   break;
151   case NSIG_FILE_V2: radar = RSL_nsig2_to_radar(infile);  break;
152   case   RAPIC_FILE: radar = RSL_rapic_to_radar(infile);  break;
153   case  RADTEC_FILE: radar = RSL_radtec_to_radar(infile); break;
154   case     RSL_FILE: radar = RSL_read_radar(infile);      break;
155 #ifdef HAVE_LIBTSDISTK
156   case     HDF_FILE: radar = RSL_hdf_to_radar(infile);    break;
157 #endif
158   case RAINBOW_FILE: radar = RSL_rainbow_to_radar(infile); break;
159   case  MCGILL_FILE: radar = RSL_mcgill_to_radar(infile); break;
160   case    EDGE_FILE: radar = RSL_EDGE_to_radar(infile);   break;
161   case  LASSEN_FILE: radar = RSL_lassen_to_radar(infile); break;
162   case  DORADE_FILE: radar = RSL_dorade_to_radar(infile); break;
163
164   default:
165         fprintf(stderr, "Unknown input file type.  File <%s> is not recognized by RSL.\n", infile);
166         return NULL;
167   }
168
169   return radar;
170 }
171
172