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