⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sac2mseed.c

📁 C编写的格式转换程序
💻 C
📖 第 1 页 / 共 3 页
字号:
 *   Component Incident Angle (cmpinc), degrees from vertical *   Instrument Name (kinst) * * Returns 0 on sucess and -1 on failure. ***************************************************************************/static intwritemetadata (struct SACHeader *sh){  static flag wroteheader = 0;  char network[9];  char station[9];  char location[9];  char channel[9];    if ( ! sh || ! mfp )    return -1;    if ( ! wroteheader )    {      wroteheader = 1;      if ( ! fprintf (mfp, "Net,Sta,Loc,Chan,Scaling,Lat,Lon,Elev,Depth,Az,Inc,Inst\n") )	{	  fprintf (stderr, "Error writing to metadata output file\n");	  return -1;	}    }  if ( strncmp (SUNDEF, sh->knetwk, 2) ) ms_strncpclean (network, sh->knetwk, 2);  else network[0] = '\0';  if ( strncmp (SUNDEF, sh->kstnm, 2) ) ms_strncpclean (station, sh->kstnm, 5);  else station[0] = '\0';  if ( strncmp (SUNDEF, sh->khole, 2) ) ms_strncpclean (location, sh->khole, 2);  else location[0] = '\0';  if ( strncmp (SUNDEF, sh->kcmpnm, 2) ) ms_strncpclean (channel, sh->kcmpnm, 3);  else channel[0] = '\0';    /* LINE: Net,Sta,Loc,Chan,Scale,Lat,Lon,Elev,Dep,Az,Inc,Inst */    /* Write the source parameters */  if ( ! fprintf (mfp, "%s,%s,%s,%s,", network, station, location, channel) )    {      fprintf (stderr, "Error writing to metadata output file\n");      return -1;    }    /* Write scale, lat, lon, elev, depth, azimuth and incident */  if ( sh->scale != FUNDEF ) fprintf (mfp, "%.g,", sh->scale);  else fprintf (mfp, ",");  if ( sh->stla != FUNDEF ) fprintf (mfp, "%.5f,", sh->stla);  else fprintf (mfp, ",");  if ( sh->stlo != FUNDEF ) fprintf (mfp, "%.5f,", sh->stlo);  else fprintf (mfp, ",");  if ( sh->stel != FUNDEF ) fprintf (mfp, "%g,", sh->stel);  else fprintf (mfp, ",");  if ( sh->stdp != FUNDEF ) fprintf (mfp, "%g,", sh->stdp);  else fprintf (mfp, ",");  if ( sh->cmpaz != FUNDEF ) fprintf (mfp, "%g,", sh->cmpaz);  else fprintf (mfp, ",");  if ( sh->cmpinc != FUNDEF ) fprintf (mfp, "%g,", sh->cmpinc);  else fprintf (mfp, ",");    /* Write the instrument name and newline */  if ( strncmp (SUNDEF, sh->kinst, 2) ) fprintf (mfp, "%.8s\n", sh->kinst);  else fprintf (mfp, "\n");    return 0;}  /* End of writemetadata() *//*************************************************************************** * parameter_proc: * Process the command line parameters. * * Returns 0 on success, and -1 on failure. ***************************************************************************/static intparameter_proc (int argcount, char **argvec){  int optind;  /* Process all command line arguments */  for (optind = 1; optind < argcount; optind++)    {      if (strcmp (argvec[optind], "-V") == 0)	{	  fprintf (stderr, "%s version: %s\n", PACKAGE, VERSION);	  exit (0);	}      else if (strcmp (argvec[optind], "-h") == 0)	{	  usage();	  exit (0);	}      else if (strncmp (argvec[optind], "-v", 2) == 0)	{	  verbose += strspn (&argvec[optind][1], "v");	}      else if (strcmp (argvec[optind], "-S") == 0)	{	  srateblkt = 1;	}      else if (strcmp (argvec[optind], "-n") == 0)	{	  forcenet = getoptval(argcount, argvec, optind++);	}      else if (strcmp (argvec[optind], "-l") == 0)	{	  forceloc = getoptval(argcount, argvec, optind++);	}      else if (strcmp (argvec[optind], "-r") == 0)	{	  packreclen = strtoul (getoptval(argcount, argvec, optind++), NULL, 10);	}      else if (strcmp (argvec[optind], "-e") == 0)	{	  encoding = strtoul (getoptval(argcount, argvec, optind++), NULL, 10);	}      else if (strcmp (argvec[optind], "-b") == 0)	{	  byteorder = strtoul (getoptval(argcount, argvec, optind++), NULL, 10);	}      else if (strcmp (argvec[optind], "-o") == 0)	{	  outputfile = getoptval(argcount, argvec, optind++);	}      else if (strcmp (argvec[optind], "-m") == 0)	{	  metafile = getoptval(argcount, argvec, optind++);	}      else if (strcmp (argvec[optind], "-s") == 0)	{	  datascaling = strtoull (getoptval(argcount, argvec, optind++), NULL, 10);	}      else if (strcmp (argvec[optind], "-f") == 0)	{	  sacformat = strtoul (getoptval(argcount, argvec, optind++), NULL, 10);	}      else if (strncmp (argvec[optind], "-", 1) == 0 &&	       strlen (argvec[optind]) > 1 )	{	  fprintf(stderr, "Unknown option: %s\n", argvec[optind]);	  exit (1);	}      else	{	  addnode (&filelist, NULL, argvec[optind]);	}    }  /* Make sure an input files were specified */  if ( filelist == 0 )    {      fprintf (stderr, "No input files were specified\n\n");      fprintf (stderr, "%s version %s\n\n", PACKAGE, VERSION);      fprintf (stderr, "Try %s -h for usage\n", PACKAGE);      exit (1);    }  /* Report the program version */  if ( verbose )    fprintf (stderr, "%s version: %s\n", PACKAGE, VERSION);  /* Check the input files for any list files, if any are found   * remove them from the list and add the contained list */  if ( filelist )    {      struct listnode *prevln, *ln;      char *lfname;            prevln = ln = filelist;      while ( ln != 0 )	{	  lfname = ln->data;	  	  if ( *lfname == '@' )	    {	      /* Remove this node from the list */	      if ( ln == filelist )		filelist = ln->next;	      else		prevln->next = ln->next;	      	      /* Skip the '@' first character */	      if ( *lfname == '@' )		lfname++;	      /* Read list file */	      readlistfile (lfname);	      	      /* Free memory for this node */	      if ( ln->key )		free (ln->key);	      free (ln->data);	      free (ln);	    }	  else	    {	      prevln = ln;	    }	  	  ln = ln->next;	}    }  return 0;}  /* End of parameter_proc() *//*************************************************************************** * getoptval: * Return the value to a command line option; checking that the value is  * itself not an option (starting with '-') and is not past the end of * the argument list. * * argcount: total arguments in argvec * argvec: argument list * argopt: index of option to process, value is expected to be at argopt+1 * * Returns value on success and exits with error message on failure ***************************************************************************/static char *getoptval (int argcount, char **argvec, int argopt){  if ( argvec == NULL || argvec[argopt] == NULL ) {    fprintf (stderr, "getoptval(): NULL option requested\n");    exit (1);    return 0;  }    /* Special case of '-o -' usage */  if ( (argopt+1) < argcount && strcmp (argvec[argopt], "-o") == 0 )    if ( strcmp (argvec[argopt+1], "-") == 0 )      return argvec[argopt+1];    if ( (argopt+1) < argcount && *argvec[argopt+1] != '-' )    return argvec[argopt+1];    fprintf (stderr, "Option %s requires a value\n", argvec[argopt]);  exit (1);  return 0;}  /* End of getoptval() *//*************************************************************************** * readlistfile: * * Read a list of files from a file and add them to the filelist for * input data.  The filename is expected to be the last * space-separated field on the line. * * Returns the number of file names parsed from the list or -1 on error. ***************************************************************************/static intreadlistfile (char *listfile){  FILE *fp;  char  line[1024];  char *ptr;  int   filecnt = 0;    char  filename[1024];  char *lastfield = 0;  int   fields = 0;  int   wspace;    /* Open the list file */  if ( (fp = fopen (listfile, "rb")) == NULL )    {      if (errno == ENOENT)        {          fprintf (stderr, "Could not find list file %s\n", listfile);          return -1;        }      else        {          fprintf (stderr, "Error opening list file %s: %s\n",		   listfile, strerror (errno));          return -1;        }    }    if ( verbose )    fprintf (stderr, "Reading list of input files from %s\n", listfile);    while ( (fgets (line, sizeof(line), fp)) !=  NULL)    {      /* Truncate line at first \r or \n, count space-separated fields       * and track last field */      fields = 0;      wspace = 0;      ptr = line;      while ( *ptr )	{	  if ( *ptr == '\r' || *ptr == '\n' || *ptr == '\0' )	    {	      *ptr = '\0';	      break;	    }	  else if ( *ptr != ' ' )	    {	      if ( wspace || ptr == line )		{		  fields++; lastfield = ptr;		}	      wspace = 0;	    }	  else	    {	      wspace = 1;	    }	  	  ptr++;	}            /* Skip empty lines */      if ( ! lastfield )	continue;            if ( fields >= 1 && fields <= 3 )	{	  fields = sscanf (lastfield, "%s", filename);	  	  if ( fields != 1 )	    {	      fprintf (stderr, "Error parsing file name from: %s\n", line);	      continue;	    }	  	  if ( verbose > 1 )	    fprintf (stderr, "Adding '%s' to input file list\n", filename);	  	  addnode (&filelist, NULL, filename);	  filecnt++;	  	  continue;	}    }    fclose (fp);    return filecnt;}  /* End readlistfile() *//*************************************************************************** * addnode: * * Add node to the specified list. ***************************************************************************/static voidaddnode (struct listnode **listroot, char *key, char *data){  struct listnode *lastlp, *newlp;    if ( data == NULL )    {      fprintf (stderr, "addnode(): No file name specified\n");      return;    }    lastlp = *listroot;  while ( lastlp != 0 )    {      if ( lastlp->next == 0 )        break;            lastlp = lastlp->next;    }    newlp = (struct listnode *) malloc (sizeof (struct listnode));  memset (newlp, 0, sizeof (struct listnode));  if ( key ) newlp->key = strdup(key);  else newlp->key = key;  if ( data) newlp->data = strdup(data);  else newlp->data = data;  newlp->next = 0;    if ( lastlp == 0 )    *listroot = newlp;  else    lastlp->next = newlp;  }  /* End of addnode() *//*************************************************************************** * record_handler: * Saves passed records to the output file. ***************************************************************************/static voidrecord_handler (char *record, int reclen, void *handlerdata){  if ( fwrite(record, reclen, 1, ofp) != 1 )    {      fprintf (stderr, "Error writing to output file\n");    }}  /* End of record_handler() *//*************************************************************************** * usage: * Print the usage message and exit. ***************************************************************************/static voidusage (void){  fprintf (stderr, "%s version: %s\n\n", PACKAGE, VERSION);  fprintf (stderr, "Convert SAC waveform data to Mini-SEED.\n\n");  fprintf (stderr, "Usage: %s [options] file1 [file2 file3 ...]\n\n", PACKAGE);  fprintf (stderr,	   " ## Options ##\n"	   " -V             Report program version\n"	   " -h             Show this usage message\n"	   " -v             Be more verbose, multiple flags can be used\n"	   " -S             Include SEED blockette 100 for very irrational sample rates\n"	   " -n netcode     Specify the SEED network code, default is blank\n"	   " -l loccode     Specify the SEED location code, default is blank\n"	   " -r bytes       Specify record length in bytes for packing, default: 4096\n"	   " -e encoding    Specify SEED encoding format for packing, default: 11 (Steim2)\n"	   " -b byteorder   Specify byte order for packing, MSBF: 1 (default), LSBF: 0\n"	   " -o outfile     Specify the output file, default is <inputfile>.mseed\n"	   " -m metafile    Specify the metadata output file\n"	   " -s factor      Specify scaling factor for sample values, default is autoscale\n"	   " -f format      Specify input SAC file format (default is autodetect):\n"	   "                  0=autodetect, 1=alpha, 2=binary (detect byte order),\n"	   "                  3=binary (little-endian), 4=binary (big-endian)\n"	   "\n"	   " file(s)        File(s) of SAC input data\n"	   "                  If a file is prefixed with an '@' it is assumed to contain\n"	   "                  a list of data files to be read\n"	   "\n"	   "Supported Mini-SEED encoding formats:\n"           " 3  : 32-bit integers, scaled\n"           " 4  : 32-bit floats (C float)\n"           " 10 : Steim 1 compression of scaled 32-bit integers\n"           " 11 : Steim 2 compression of scaled 32-bit integers\n"           "\n"           "For any of the non-floating point encoding formats the data samples\n"           "will be scaled either by the specified scaling factor or autoscaling\n"	   "where the magnitude of the maximum sample will be 6 digits.\n"	   "\n");}  /* End of usage() */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -