#!python

if __name__ == "__main__":
    from sys import argv, path
    from os.path import dirname, join
    path.append(join(dirname(__file__), ".."))
    from py3sac.sac import sac2asc

    if len(argv) < 2 or "-h" in argv:
        print("USAGE: ", argv[0], " <sac_file> [<asc_file> [str_format]]")
        print("Set stdout as asc_file to output to standard output instead of"
              " into a file.")
        print("str_format is in printf style: e.g. 't=%.2f,a=%.2f' (one format for the time, the other for the amplitude).")
        print("Example:", argv[0], " file.sac output.asc 't=%.2f,a=%.2f'")
        exit(0)

    sac_file = argv[1]
    if len(argv) > 2:
        asc_file = argv[2]
        if(asc_file.lower() == 'stdout'):
            asc_file = ".std_output"
    else:
        asc_file = ".std_output"

    str_format = None
    if len(argv) > 3:
        str_format = argv[3]

    sac2asc(sac_file, asc_file, str_format)

    if(asc_file == ".std_output"):
        for line in open(asc_file):
            print(line, end="")
        from os import unlink
        unlink(asc_file)

