Thread: A question about calling sac from a perl script

Started: 2013-04-14 20:38:28
Last activity: 2013-04-18 23:17:17
Topics: SAC Help
Dongdong Tian
2013-04-14 20:38:28
Hi,

I am running SAC v101.5c under CentOS 6.4 (Final) x86_64.

I want to read a sac file, plot it, and visually inspect the waveform.
If the waveform is good, just go on; if not, just quit.
I want to do it in a script, but not a SAC macro file.

A simple script is like below.

#!/usr/bin/perl -w
open(SAC,"|sac");
print SAC "fg seis\n";
print SAC "p\n";
print SAC "q\n";
close(SAC);

Then I run this script, the waveform appear in a window and disappear
quickly, so I can see nothing
in that short time.

A modified version of last example is like below.

#!/usr/bin/perl -w
open(SAC,"|sac");
print SAC "fg seis\n";
print SAC "p\n";
print SAC "pause\n";
print SAC "q\n";
close(SAC);

The waveform appear and disappear in a short time again, and there is an
error:

SAC Error: EOF/Quit
SAC executed from a script: quit command missing
Please add a quit to the script to avoid this message
If you think you got this message in error,
please report it to: sac-help<at>iris.washington.edu

Is there any solution to visually inspect waveforms in a script ?
ps: I know that SAC macro file can do it, but it is not convenient to me.

Thanks in advance.


Dongdong Tian

  • Satish_iitb
    2013-04-14 16:49:24
    I am doing little-bit similar like in shell (may be help you)
    its read each file one by one if click "y" (click y) it will go to good
    else in bad with visualization of each seismogram.

    ####################################
    #! /bin/sh
    rm -f -r GOOD_events BAD_events
    mkdir GOOD_events
    mkdir BAD_events
    rm -f filename
    adv="Verify events"
    for cdf in *.SAC
    do
    ls=`basename $cdf .SAC `
    echo $ls >> filename
    sac << !
    r $ls.SAC
    qdp off
    message '$adv'
    ppk bell off
    p
    q
    !
    read -p "Continue? [y/n]" CONT
    if [ "$CONT" = "y" ]; then
    cp $ls* GOOD_events/
    else
    cp $ls* BAD_events/
    fi
    done
    ########################################




    On Sun, Apr 14, 2013 at 7:38 AM, Dongdong Tian <seisman.ustc<at>gmail.com>wrote:

    Hi,

    I am running SAC v101.5c under CentOS 6.4 (Final) x86_64.

    I want to read a sac file, plot it, and visually inspect the waveform.
    If the waveform is good, just go on; if not, just quit.
    I want to do it in a script, but not a SAC macro file.

    A simple script is like below.

    #!/usr/bin/perl -w
    open(SAC,"|sac");
    print SAC "fg seis\n";
    print SAC "p\n";
    print SAC "q\n";
    close(SAC);

    Then I run this script, the waveform appear in a window and disappear
    quickly, so I can see nothing
    in that short time.

    A modified version of last example is like below.

    #!/usr/bin/perl -w
    open(SAC,"|sac");
    print SAC "fg seis\n";
    print SAC "p\n";
    print SAC "pause\n";
    print SAC "q\n";
    close(SAC);

    The waveform appear and disappear in a short time again, and there is an
    error:

    SAC Error: EOF/Quit
    SAC executed from a script: quit command missing
    Please add a quit to the script to avoid this message
    If you think you got this message in error,
    please report it to: sac-help<at>iris.washington.edu

    Is there any solution to visually inspect waveforms in a script ?
    ps: I know that SAC macro file can do it, but it is not convenient to me.

    Thanks in advance.


    Dongdong Tian

    _______________________________________________
    sac-help mailing list
    sac-help<at>iris.washington.edu
    http://www.iris.washington.edu/mailman/listinfo/sac-help




    --
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    *Satish Maurya*
    * Ph.D. student
    Dept. of Seismology
    *
    **
    **
    *IPGP, France*
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    • Dongdong Tian
      2013-04-14 23:02:47
      It works, it's a nice solution.
      Thanks for your help.


      2013/4/14 satish maurya <maurya2278satish<at>gmail.com>

      I am doing little-bit similar like in shell (may be help you)
      its read each file one by one if click "y" (click y) it will go to good
      else in bad with visualization of each seismogram.

      ####################################
      #! /bin/sh
      rm -f -r GOOD_events BAD_events
      mkdir GOOD_events
      mkdir BAD_events
      rm -f filename
      adv="Verify events"
      for cdf in *.SAC
      do
      ls=`basename $cdf .SAC `
      echo $ls >> filename
      sac << !
      r $ls.SAC
      qdp off
      message '$adv'
      ppk bell off
      p
      q
      !
      read -p "Continue? [y/n]" CONT
      if [ "$CONT" = "y" ]; then
      cp $ls* GOOD_events/
      else
      cp $ls* BAD_events/
      fi
      done
      ########################################




      On Sun, Apr 14, 2013 at 7:38 AM, Dongdong Tian <seisman.ustc<at>gmail.com>wrote:

      Hi,

      I am running SAC v101.5c under CentOS 6.4 (Final) x86_64.

      I want to read a sac file, plot it, and visually inspect the waveform.
      If the waveform is good, just go on; if not, just quit.
      I want to do it in a script, but not a SAC macro file.

      A simple script is like below.

      #!/usr/bin/perl -w
      open(SAC,"|sac");
      print SAC "fg seis\n";
      print SAC "p\n";
      print SAC "q\n";
      close(SAC);

      Then I run this script, the waveform appear in a window and disappear
      quickly, so I can see nothing
      in that short time.

      A modified version of last example is like below.

      #!/usr/bin/perl -w
      open(SAC,"|sac");
      print SAC "fg seis\n";
      print SAC "p\n";
      print SAC "pause\n";
      print SAC "q\n";
      close(SAC);

      The waveform appear and disappear in a short time again, and there is an
      error:

      SAC Error: EOF/Quit
      SAC executed from a script: quit command missing
      Please add a quit to the script to avoid this message
      If you think you got this message in error,
      please report it to: sac-help<at>iris.washington.edu

      Is there any solution to visually inspect waveforms in a script ?
      ps: I know that SAC macro file can do it, but it is not convenient to me.

      Thanks in advance.


      Dongdong Tian

      _______________________________________________
      sac-help mailing list
      sac-help<at>iris.washington.edu
      http://www.iris.washington.edu/mailman/listinfo/sac-help




      --
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      *Satish Maurya*
      * Ph.D. student
      Dept. of Seismology
      *
      **
      **
      *IPGP, France*
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    • Arthur Snoke
      2013-04-18 23:17:17
      I have done something similar to what you do except that it is completely
      done in SAC. It might be of interest to you and others.

      When I download an event for surace-wave analysis, my processing changes
      all waveforms to origin-time reference and instrument corrects them.
      Typically, a few percent are obviously bad based on their waveforms, and
      my macro culls out the bad ones putting them a a bad-files subdirectory.

      The only tricky thing is that when a waveform is displayed (via p1), that
      window becomes acrive so to get the one with SAC> prompts acrive, one
      needs to click on it. What works for me is to separate the two windown on
      the screen an put pauses in the macro.

      The switching between okay and bad is done using the REPLY inline
      function. In v101.5c the default does not work, so one has to enter a
      charaxter (not x in this script) to get it to work.

      Here is my macro. Let me know if anaything is unclear.

      * Macro plotone.m Displays all waveforms in directory
      * with a chosen ending string (such as .sac) one at a time.
      * Syntax: m plotone.m .sac called from within SAC.
      * Pauses after each waveform and then queries if the waveform
      * iis okay or bad. If bad, respond with an x. In v101.6
      * just entering a carriage return returns okay. Prior
      * versions require a keyed resopnse.
      * The window with the responses should not overlap the plot
      * window. The pausisng is necessary to allow control to
      * move from the plot just displayed to the SAC> window.
      * If the directory bad-files does not exist, it is created.
      *
      bd x
      qdp 3000;xdiv power off
      sc test -e bad-files || mkdir bad-files
      do file wild *$1
      r $file
      rmean
      p1
      pause
      SETBB RESPONSE (REPLY "Enter x if bad [okay] ")
      IF %RESPONSE EQ "x"
      sc mv $file bad-files/.
      endif
      enddo



08:43:25 v.22510d55