]> Pileus Git - ~andy/rsl/blobdiff - anyformat_to_radar.c
RSL v1.41
[~andy/rsl] / anyformat_to_radar.c
index 70be908f8f32634f7a1be1285125ca66be696236..146a5e08664a838ffdb43f1c47e8f3ee1601cc61 100644 (file)
-/*
-    NASA/TRMM, Code 910.1.
-    This is the TRMM Office Radar Software Library.
-    Copyright (C) 1996, 1997
-            John H. Merritt
-            Space Applications Corporation
-            Vienna, Virginia
-
-    This library is free software; you can redistribute it and/or
-    modify it under the terms of the GNU Library General Public
-    License as published by the Free Software Foundation; either
-    version 2 of the License, or (at your option) any later version.
-
-    This library is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-    Library General Public License for more details.
-
-    You should have received a copy of the GNU Library General Public
-    License along with this library; if not, write to the Free
-    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-*/
-/*
- * By John H. Merritt
- * Science Applications Corporation, Vienna, VA
- */
-
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <stdlib.h>
-#include "rsl.h"
-void rsl_readflush(FILE *fp);
-/*********************************************************************/
-/*                                                                   */
-/*                   RSL_filetype                                    */
-/*                                                                   */
-/*********************************************************************/
-enum File_type RSL_filetype(char *infile)
-{
-  /* Open the input file and peek at the first few bytes to determine
-   * the type of file.
-   * 
-   * UF     - First two bytes 'UF'
-   *        - or 3,4 bytes 'UF'
-   *        - or 5,6 bytes 'UF'. This is the most common.
-   *
-   * WSR88D - First 8 bytes: 'ARCHIVE2' or 'AR2V0001'
-   *
-   * TOGA   - ??
-   * NSIG   - ??
-   * LASSEN - SUNRISE
-   * RSL    - RSL
-   * MCGILL - P A B
-   * RAPIC  - /IMAGE:
-   * RADTEC - 320      (decimal, in first two bytes)
-   * RAINBOW - First two bytes: decimal 1, followed by 'H'
-   */
-  FILE *fp;
-  char magic[11];
-
-  if ((fp = fopen(infile, "r")) == NULL) {
-       perror(infile);
-       return UNKNOWN;
-  }
-
-  /* Read the magic bytes. */
-  fp = uncompress_pipe(fp); /* If gzip available. */
-  if (fread(magic, sizeof(magic), 1, fp) != 1) {
-       char *magic_str = (char *)calloc(sizeof(magic)+1, sizeof(char));
-       memcpy(magic_str, magic, sizeof(magic));
-       fprintf(stderr,"Error fread: Magic is %s\n", magic_str);
-       free (magic_str);
-       perror("RSL_filetype");
-       fclose(fp);
-       return UNKNOWN;
-  }
-
-  rsl_readflush(fp); /* Fork, read and exit -- eliminates the 'Broken pipe' */
-
-  if (strncmp("ARCHIVE2.", magic, 9) == 0) return WSR88D_FILE;
-  if (strncmp("AR2V000", magic, 7) == 0) return WSR88D_FILE;
-  if (strncmp("UF", magic, 2) == 0) return UF_FILE;
-  if (strncmp("UF", &magic[2], 2) == 0) return UF_FILE;
-  if (strncmp("UF", &magic[4], 2) == 0) return UF_FILE;
-  if ((int)magic[0] == 0x0e &&
-         (int)magic[1] == 0x03 &&
-         (int)magic[2] == 0x13 &&
-         (int)magic[3] == 0x01
-         ) return HDF_FILE;
-  if (strncmp("RSL", magic, 3) == 0) return RSL_FILE;
-  if ((int)magic[0] == 7) return NSIG_FILE_V1;
-  if ((int)magic[1] == 7) return NSIG_FILE_V1;
-  if ((int)magic[0] == 27) return NSIG_FILE_V2;
-  if ((int)magic[1] == 27) return NSIG_FILE_V2;
-  if (strncmp("/IMAGE:", magic, 7) == 0) return RAPIC_FILE;
-  if ((int)magic[0] == 0x40 &&
-         (int)magic[1] == 0x01
-         ) return RADTEC_FILE;
-  if ((int)magic[0] == 0x01 && magic[1] == 'H') return RAINBOW_FILE;
-
-  if (strncmp("SUNRISE", &magic[4], 7) == 0) return LASSEN_FILE;
-/* The 'P A B' is just too specific to be a true magic number, but that's all
- * I've got.
- */
-  if (strncmp("P A B ", magic, 6) == 0) return MCGILL_FILE;
-  /* Byte swapped ? */
-  if (strncmp(" P A B", magic, 6) == 0) return MCGILL_FILE;
-  if (strncmp("Volume", magic, 6) == 0) return EDGE_FILE;
-  if (strncmp("SSWB", magic, 4) == 0) return DORADE_FILE;
-  if (strncmp("VOLD", magic, 4) == 0) return DORADE_FILE;
-
-  return UNKNOWN;
-}
-  
-
-
-
-  
-
-
-/*********************************************************************/
-/*                                                                   */
-/*                   RSL_anyformat_to_radar                          */
-/*                                                                   */
-/*********************************************************************/
-
-Radar *RSL_anyformat_to_radar(char *infile, ...)
-{
-  va_list ap;
-  char *callid_or_file;
-  Radar *radar;
-
-/* If it is detected that the input file is WSR88D, use the second argument
- * as the call id of the site, or the file name of the tape header file.
- *
- * Assumption: Input files are seekable.
- */
-  radar = NULL;
-  switch (RSL_filetype(infile)) {
-  case WSR88D_FILE:
-       callid_or_file = NULL;
-       va_start(ap, infile);
-       callid_or_file = va_arg(ap, char *);
-       va_end(ap);
-       radar = RSL_wsr88d_to_radar(infile, callid_or_file);
-       break;
-  case      UF_FILE: radar = RSL_uf_to_radar(infile);     break;
-  case    TOGA_FILE: radar = RSL_toga_to_radar(infile);   break;
-  case NSIG_FILE_V1: radar = RSL_nsig_to_radar(infile);          break;
-  case NSIG_FILE_V2: radar = RSL_nsig2_to_radar(infile);  break;
-  case   RAPIC_FILE: radar = RSL_rapic_to_radar(infile);  break;
-  case  RADTEC_FILE: radar = RSL_radtec_to_radar(infile); break;
-  case     RSL_FILE: radar = RSL_read_radar(infile);      break;
-#ifdef HAVE_LIBTSDISTK
-  case     HDF_FILE: radar = RSL_hdf_to_radar(infile);    break;
-#endif
-  case RAINBOW_FILE: radar = RSL_rainbow_to_radar(infile); break;
-  case  MCGILL_FILE: radar = RSL_mcgill_to_radar(infile); break;
-  case    EDGE_FILE: radar = RSL_EDGE_to_radar(infile);   break;
-  case  LASSEN_FILE: radar = RSL_lassen_to_radar(infile); break;
-  case  DORADE_FILE: radar = RSL_dorade_to_radar(infile); break;
-
-  default:
-       fprintf(stderr, "Unknown input file type.  File <%s> is not recognized by RSL.\n", infile);
-       return NULL;
-  }
-
-  return radar;
-}
-
-
+/*\r
+    NASA/TRMM, Code 910.1.\r
+    This is the TRMM Office Radar Software Library.\r
+    Copyright (C) 1996, 1997\r
+            John H. Merritt\r
+            Space Applications Corporation\r
+            Vienna, Virginia\r
+\r
+    This library is free software; you can redistribute it and/or\r
+    modify it under the terms of the GNU Library General Public\r
+    License as published by the Free Software Foundation; either\r
+    version 2 of the License, or (at your option) any later version.\r
+\r
+    This library is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+    Library General Public License for more details.\r
+\r
+    You should have received a copy of the GNU Library General Public\r
+    License along with this library; if not, write to the Free\r
+    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
+*/\r
+/*\r
+ * By John H. Merritt\r
+ * Science Applications Corporation, Vienna, VA\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdarg.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+#include "rsl.h"\r
+void rsl_readflush(FILE *fp);\r
+/*********************************************************************/\r
+/*                                                                   */\r
+/*                   RSL_filetype                                    */\r
+/*                                                                   */\r
+/*********************************************************************/\r
+enum File_type RSL_filetype(char *infile)\r
+{\r
+  /* Open the input file and peek at the first few bytes to determine\r
+   * the type of file.\r
+   * \r
+   * UF     - First two bytes 'UF'\r
+   *        - or 3,4 bytes 'UF'\r
+   *        - or 5,6 bytes 'UF'. This is the most common.\r
+   *\r
+   * WSR88D - First 8 bytes: 'ARCHIVE2' or 'AR2V0001'\r
+   *\r
+   * TOGA   - ??\r
+   * NSIG   - ??\r
+   * LASSEN - SUNRISE\r
+   * RSL    - RSL\r
+   * MCGILL - P A B\r
+   * RAPIC  - /IMAGE:\r
+   * RADTEC - 320      (decimal, in first two bytes)\r
+   * RAINBOW - First two bytes: decimal 1, followed by 'H'\r
+   */\r
+  FILE *fp;\r
+  char magic[11];\r
+\r
+  if ((fp = fopen(infile, "r")) == NULL) {\r
+       perror(infile);\r
+       return UNKNOWN;\r
+  }\r
+\r
+  /* Read the magic bytes. */\r
+  fp = uncompress_pipe(fp); /* If gzip available. */\r
+  if (fread(magic, sizeof(magic), 1, fp) != 1) {\r
+       char *magic_str = (char *)calloc(sizeof(magic)+1, sizeof(char));\r
+       memcpy(magic_str, magic, sizeof(magic));\r
+       fprintf(stderr,"Error fread: Magic is %s\n", magic_str);\r
+       free (magic_str);\r
+       perror("RSL_filetype");\r
+        /* Thanks to Thiago Biscaro for fixing defunct process problem. */\r
+        rsl_pclose(fp);\r
+       return UNKNOWN;\r
+  }\r
+\r
+  /*\r
+  Closes the stream and wait for associated processes to terminate\r
+  (just like wait() ) to avoid defunct processes. The old rsl_readflush\r
+  function wasn't doing this, N calls to anyformat_to_radar would\r
+  generate N defunct processes associated with the main program\r
+  --Thiago Biscaro\r
+  */\r
+\r
+  rsl_pclose(fp);\r
+  \r
+  if (strncmp("ARCHIVE2.", magic, 9) == 0) return WSR88D_FILE;\r
+  if (strncmp("AR2V000", magic, 7) == 0) return WSR88D_FILE;\r
+  if (strncmp("UF", magic, 2) == 0) return UF_FILE;\r
+  if (strncmp("UF", &magic[2], 2) == 0) return UF_FILE;\r
+  if (strncmp("UF", &magic[4], 2) == 0) return UF_FILE;\r
+  if ((int)magic[0] == 0x0e &&\r
+         (int)magic[1] == 0x03 &&\r
+         (int)magic[2] == 0x13 &&\r
+         (int)magic[3] == 0x01\r
+         ) return HDF_FILE;\r
+  if (strncmp("RSL", magic, 3) == 0) return RSL_FILE;\r
+  if ((int)magic[0] == 7) return NSIG_FILE_V1;\r
+  if ((int)magic[1] == 7) return NSIG_FILE_V1;\r
+  if ((int)magic[0] == 27) return NSIG_FILE_V2;\r
+  if ((int)magic[1] == 27) return NSIG_FILE_V2;\r
+  if (strncmp("/IMAGE:", magic, 7) == 0) return RAPIC_FILE;\r
+  if ((int)magic[0] == 0x40 &&\r
+         (int)magic[1] == 0x01\r
+         ) return RADTEC_FILE;\r
+  if ((int)magic[0] == 0x01 && magic[1] == 'H') return RAINBOW_FILE;\r
+\r
+  if (strncmp("SUNRISE", &magic[4], 7) == 0) return LASSEN_FILE;\r
+/* The 'P A B' is just too specific to be a true magic number, but that's all\r
+ * I've got.\r
+ */\r
+  if (strncmp("P A B ", magic, 6) == 0) return MCGILL_FILE;\r
+  /* Byte swapped ? */\r
+  if (strncmp(" P A B", magic, 6) == 0) return MCGILL_FILE;\r
+  if (strncmp("Volume", magic, 6) == 0) return EDGE_FILE;\r
+  if (strncmp("SSWB", magic, 4) == 0) return DORADE_FILE;\r
+  if (strncmp("VOLD", magic, 4) == 0) return DORADE_FILE;\r
+\r
+  return UNKNOWN;\r
+}\r
+  \r
+\r
+\r
+\r
+  \r
+\r
+\r
+/*********************************************************************/\r
+/*                                                                   */\r
+/*                   RSL_anyformat_to_radar                          */\r
+/*                                                                   */\r
+/*********************************************************************/\r
+\r
+Radar *RSL_anyformat_to_radar(char *infile, ...)\r
+{\r
+  va_list ap;\r
+  char *callid_or_file;\r
+  Radar *radar;\r
+\r
+/* If it is detected that the input file is WSR88D, use the second argument\r
+ * as the call id of the site, or the file name of the tape header file.\r
+ *\r
+ * Assumption: Input files are seekable.\r
+ */\r
+  radar = NULL;\r
+  switch (RSL_filetype(infile)) {\r
+  case WSR88D_FILE:\r
+       callid_or_file = NULL;\r
+       va_start(ap, infile);\r
+       callid_or_file = va_arg(ap, char *);\r
+       va_end(ap);\r
+       radar = RSL_wsr88d_to_radar(infile, callid_or_file);\r
+       break;\r
+  case      UF_FILE: radar = RSL_uf_to_radar(infile);     break;\r
+  case    TOGA_FILE: radar = RSL_toga_to_radar(infile);   break;\r
+  case NSIG_FILE_V1: radar = RSL_nsig_to_radar(infile);          break;\r
+  case NSIG_FILE_V2: radar = RSL_nsig2_to_radar(infile);  break;\r
+  case   RAPIC_FILE: radar = RSL_rapic_to_radar(infile);  break;\r
+  case  RADTEC_FILE: radar = RSL_radtec_to_radar(infile); break;\r
+  case     RSL_FILE: radar = RSL_read_radar(infile);      break;\r
+#ifdef HAVE_LIBTSDISTK\r
+  case     HDF_FILE: radar = RSL_hdf_to_radar(infile);    break;\r
+#endif\r
+  case RAINBOW_FILE: radar = RSL_rainbow_to_radar(infile); break;\r
+  case  MCGILL_FILE: radar = RSL_mcgill_to_radar(infile); break;\r
+  case    EDGE_FILE: radar = RSL_EDGE_to_radar(infile);   break;\r
+  case  LASSEN_FILE: radar = RSL_lassen_to_radar(infile); break;\r
+  case  DORADE_FILE: radar = RSL_dorade_to_radar(infile); break;\r
+\r
+  default:\r
+       fprintf(stderr, "Unknown input file type.  File <%s> is not recognized by RSL.\n", infile);\r
+       return NULL;\r
+  }\r
+  \r
+  return radar;\r
+}\r
+\r
+\r