Ejemplo n.º 1
0
def conv2snr(year, doy, station, option, orbtype, receiverrate, dec_rate,
             archive, fortran, translator):
    """
    inputs: year and day of year (integers) and station name
    option is for the snr creation ??? integer or character?
    orbtype can be nav or sp3.  if the former, then gpsSNR is used.
    if the later, then gnssSNR
    what are receiverrate and dec_rate defaults?
    this assumes you follow my definitions for where things go,
    i.e. REFL_CODE and ORBITS
    it currently checks Unavco, SOPAC, and SONEL. I should add CDDIS
    author: kristine m. larson
    19may20, added decimation
    19sep12, I got tired of code crashing for files > 20 observables.  I am thus using teqc
    20apr15, xz compression added but also try to streamline it.
    20jul10, added arvchive setting. default is 'all'

    """
    # define directory for the conversion executables
    if not os.path.isdir('logs'):
        subprocess.call(['mkdir', 'logs'])
    logname = 'logs/' + station + '.txt'
    log = open(logname, 'w+')
    log.write("Receiver rate: {0:5s} \n".format(receiverrate))
    log.write("Decimation rate: {0:3.0f} \n".format(dec_rate))
    log.write("Archive: {0:10s} \n".format(archive))
    log.write("Orbits : {0:10s} \n".format(orbtype))
    exedir = os.environ['EXE']
    snrname_full, snrname_compressed, snre = g.define_and_xz_snr(
        station, year, doy, option)
    if (snre == True):
        log.write(
            "The snrfile already exists: {0:50s} \n".format(snrname_full))
        print("The snrfile already exists: ", snrname_full)
    else:
        log.write(
            "The snrfile does not exist: {0:50s} \n".format(snrname_full))
        d = g.doy2ymd(year, doy)
        month = d.month
        day = d.day
        # new function to do the whole orbit thing
        foundit, f, orbdir, snrexe = g.get_orbits_setexe(
            year, month, day, orbtype, fortran)
        # if you have the orbit file, you can get the rinex file
        if foundit:
            # now you can look for a rinex file
            rinexfile, rinexfiled = g.rinex_name(station, year, month, day)
            # This goes to find the rinex file. I am changing it to allow
            # an archive preference
            g.go_get_rinex_flex(station, year, month, day, receiverrate,
                                archive)
            #           define booleans for various files
            oexist = os.path.isfile(orbdir + '/' + f) == True
            rexist = os.path.isfile(rinexfile) == True
            exc = exedir + '/teqc'
            texist = os.path.isfile(exc) == True
            if rexist:
                # decimate using teqc
                if (texist) and (fortran) and (dec_rate > 0):
                    log.write(
                        "Decimating using teqc:  {0:3.0f}  seconds \n".format(
                            dec_rate))
                    log.write(
                        'Unfortunately teqc removes Beidou data. Eventually I will remove this. \n'
                    )
                    rinexout = rinexfile + '.tmp'
                    cdec = str(dec_rate)
                    fout = open(rinexout, 'w')
                    subprocess.call([exc, '-O.dec', cdec, rinexfile],
                                    stdout=fout)
                    fout.close()  # needed?
                    status = subprocess.call(['mv', '-f', rinexout, rinexfile])
            # if orbits and rinexfile exist
            if (oexist) and (rexist):
                snrname = g.snr_name(station, year, month, day, option)
                orbfile = orbdir + '/' + f
                #print('translator',translator)
                if translator == 'hybrid':
                    g.make_snrdir(year,
                                  station)  # make sure output directory exists
                    in1 = g.binary(rinexfile)
                    in2 = g.binary(
                        snrname)  # this file is made locally and moved later
                    in3 = g.binary(orbfile)
                    in4 = g.binary(str(option))
                    if (dec_rate > 0):
                        decr = str(dec_rate)
                    else:
                        decr = '0'
                    in5 = g.binary(
                        decr)  # decimation can be used in hybrid option
                    log.write(
                        'SNR file {0:50s} \n will use hybrid of python and fortran to make \n'
                        .format(snrname))
                    gpssnr.foo(in1, in2, in3, in4, in5)
                else:
                    if (translator == 'fortran'):
                        t1 = time.time()
                        try:
                            #subprocess.call([snrexe, rinexfile, snrname, orbfile, str(option)])
                            log.write(
                                'Using standalone fortran for translation  - separate log is used for stdout \n'
                            )
                            flogname = 'logs/' + station + '_fortran.txt'
                            flog = open(flogname, 'w+')
                            a = subprocess.run([
                                snrexe, rinexfile, snrname, orbfile,
                                str(option)
                            ],
                                               capture_output=True,
                                               text=True)
                            ddd = a.stdout
                            flog.write(ddd)
                            flog.close()
                            status = subprocess.call(['rm', '-f', rinexfile])
                            status = subprocess.call(['xz', orbfile])
                        except:
                            log.write(
                                'Problem with making SNR file, check fortran specific log {0:50s} \n'
                                .format(flogname))
                        t2 = time.time()
#                        print(' Exec time:', '{0:4.2f}'.format(t2-t1) )
# this is for people that want to use slow python code
                    else:
                        log.write(
                            'SNR file {0:50s} \n will use python to make \n'.
                            format(snrname))
                        log.write(
                            'Decimating will be done here instead of using teqc \n'
                        )
                        t1 = time.time()
                        rnx2snr(rinexfile, orbfile, snrname, option, year,
                                month, day, dec_rate, log)
                        t2 = time.time()


#                        print(' Exec time:', '{0:4.2f}'.format(t2-t1) )

# remove the rinex file
                subprocess.call(['rm', '-f', rinexfile])

                if os.path.isfile(snrname):
                    #                make sure it exists and is non-zero size before moving it
                    if (os.stat(snrname).st_size == 0):
                        log.write(
                            'you created a zero file size which could mean a lot of things \n'
                        )
                        log.write(
                            'bad exe, bad snr option, do not really have the orbit file \n'
                        )
                        status = subprocess.call(['rm', '-f', snrname])
                    else:
                        log.write('A SNR file was created: {0:50s}  \n'.format(
                            snrname_full))
                        print('\n')
                        print('SUCCESS: SNR file was created:', snrname_full)
                        g.store_snrfile(snrname, year, station)
                else:
                    print('No SNR file was created')
            else:
                print(
                    'Either the RINEX file or orbit file does not exist, so there is nothing to convert'
                )
                log.write(
                    'Either the RINEX file or orbit file does not exist, so there is nothing to convert \n'
                )
        else:
            print('The orbit file you requested does not exist.')

    # close the log file
    log.close()

    return True
Ejemplo n.º 2
0
def conv2snr(year, doy, station, option, orbtype,receiverrate,dec_rate,archive,fortran):
    """
    inputs: year and day of year (integers) and station name
    option is for the snr creation ??? integer or character?
    orbtype can be nav or sp3.  if the former, then gpsSNR is used.
    if the later, then gnssSNR
    what are receiverrate and dec_rate defaults?
    this assumes you follow my definitions for where things go,
    i.e. REFL_CODE and ORBITS
    it currently checks Unavco, SOPAC, and SONEL. I should add CDDIS
    author: kristine m. larson
    19may20, added decimation
    19sep12, I got tired of code crashing for files > 20 observables.  I am thus using teqc
    20apr15, xz compression added but also try to streamline it.
    20jul10, added arvchive setting. default is 'all'

    """
    # define directory for the conversion executables
    if not os.path.isdir('logs'):
        subprocess.call(['mkdir', 'logs'])
    logname = 'logs/' + station + '.txt' 
    log = open(logname, 'w+')
    log.write("Receiver rate: {0:5s} \n".format(receiverrate))
    log.write("Decimation rate: {0:3.0f} \n".format(dec_rate))
    log.write("Archive: {0:10s} \n".format(archive))
    if fortran:
        log.write("Use Fortran: {0:4s} \n".format('True'))
    else:
        log.write("Use Fortran: {0:5s} \n".format('False'))
    log.write("Orbits : {0:10s} \n".format(orbtype))
    exedir = os.environ['EXE']
    snrname_full, snrname_compressed, snre = g.define_and_xz_snr(station,year,doy,option)
    if (snre == True):
        log.write("The snrfile already exists: {0:50s} \n".format(snrname_full))
    else:
        log.write("The snrfile does not exist: {0:50s} \n".format(snrname_full))
        d = g.doy2ymd(year,doy); 
        month = d.month; day = d.day
        # new function to do the whole orbit thing
        foundit, f, orbdir, snrexe = g.get_orbits_setexe(year,month,day,orbtype,fortran) 
        # if you have the orbit file, you can get the rinex file
        if foundit:
            # now you can look for a rinex file
            rinexfile,rinexfiled = g.rinex_name(station, year, month, day)
            # This goes to find the rinex file. I am changing it to allow 
            # an archive preference 
             
            g.go_get_rinex_flex(station,year,month,day,receiverrate,archive)
# define booleans
            oexist = os.path.isfile(orbdir + '/' + f) == True
            rexist = os.path.isfile(rinexfile) == True
            exc = exedir + '/teqc' 
            texist = os.path.isfile(exc) == True
            if rexist:
                if texist and fortran:
                    # only do this for the older version
                    #print('teqc executable exists, will use to eliminate unnecessary observables')
                    #foutname = 'tmp.' + rinexfile
                    #fout = open(foutname,'w')
                    #subprocess.call([exc, '-O.obs','S1+S2+S5+S6+S7+S8', '-n_GLONASS', '27', rinexfile],stdout=fout)
                    #fout.close()
                    log.write('This option deprecated - no longer use teqc for reducing observables \n')
                # store it in the original rinex filename
                    #subprocess.call(['rm','-f',rinexfile])
                    #subprocess.call(['mv','-f',foutname, rinexfile])
                # decimate this new rinex file
                    if (rexist and dec_rate > 0): 
                        log.write("Decimating using teqc:  {0:3.0f}  seconds \n".format(dec_rate))
                        log.write('Unfortunately teqc removes Beidou data. Eventually I will remove this. \n')
                        rinexout = rinexfile + '.tmp'; cdec = str(dec_rate)
                        fout = open(rinexout,'w')
                        subprocess.call([exc, '-O.dec', cdec, rinexfile],stdout=fout)
                        fout.close() # needed?
                        status = subprocess.call(['mv','-f', rinexout, rinexfile])
            # orbits and rinexfile exist
            if (oexist and rexist):
                snrname = g.snr_name(station, year,month,day,option)
                orbfile = orbdir + '/' + f
                if fortran:
                    try:
                        #subprocess.call([snrexe, rinexfile, snrname, orbfile, str(option)])
                        log.write('Using fortran for translation  - separate log is used for stdout \n')
                        flogname = 'logs/' + station + '_fortran.txt'
                        flog = open(flogname, 'w+')
                        a=subprocess.run([snrexe, rinexfile, snrname, orbfile, str(option)],capture_output=True,text=True)
                        ddd = a.stdout; flog.write(ddd); flog.close()
                        status = subprocess.call(['rm','-f', rinexfile ])
                        status = subprocess.call(['xz', orbfile])
                    except:
                        log.write('Problem with making SNR file, check log {0:50s} \n'.format(flogname))
                else:
                    log.write('SNR file {0:50s} \n will not use fortran to make \n'.format( snrname))
                    log.write('Decimating will be done here instead of using teqc \n')
                    rnx2snr(rinexfile, orbfile,snrname,option,year,month,day,dec_rate,log)

                # remove the rinex file
                subprocess.call(['rm', '-f',rinexfile])

                if os.path.isfile(snrname): 
#                make sure it exists and is non-zero size before moving it
                    if (os.stat(snrname).st_size == 0):
                        log.write('you created a zero file size which could mean a lot of things \n')
                        log.write('bad exe, bad snr option, do not really have the orbit file \n')
                        status = subprocess.call(['rm','-f', snrname ])
                    else:
                        log.write('A SNR file was created: {0:50s}  \n'.format(snrname_full))
                        print('\n')
                        print('A SNR file was created:', snrname_full)
                        g.store_snrfile(snrname,year,station) 
            else:
                print('Either the RINEX file or orbit file does not exist, so there is nothing to convert')
                log.write('Either the RINEX file or orbit file does not exist, so there is nothing to convert \n')

    # close the log file
    log.close()

    return True
Ejemplo n.º 3
0
def conv2snr(year, doy, station, option, orbtype, receiverrate, dec_rate,
             archive, fortran):
    """
    inputs: year and day of year (integers) and station name
    option is for the snr creation ??? integer or character?
    orbtype can be nav or sp3.  if the former, then gpsSNR is used.
    if the later, then gnssSNR
    what are receiverrate and dec_rate defaults?
    this assumes you follow my definitions for where things go,
    i.e. REFL_CODE and ORBITS
    it currently checks Unavco, SOPAC, and SONEL. I should add CDDIS
    author: kristine m. larson
    19may20, added decimation
    19sep12, I got tired of code crashing for files > 20 observables.  I am thus using teqc
    20apr15, xz compression added but also try to streamline it.
    20jul10, added arvchive setting. default is 'all'

    """
    # define directory for the conversion executables
    print('receiver rate:', receiverrate)
    print('decimation rate: ', dec_rate)
    print('archive: ', archive)
    print('use fortran: ', fortran)
    print('orbtype: ', orbtype)
    exedir = os.environ['EXE']
    snrname_full, snrname_compressed, snre = g.define_and_xz_snr(
        station, year, doy, option)
    if (snre == True):
        print('snrfile already exists:', snrname_full)
    else:
        print('the snrfile does not exist ', snrname_full)
        d = g.doy2ymd(year, doy)
        month = d.month
        day = d.day
        # new function to do the whole orbit thing
        foundit, f, orbdir, snrexe = g.get_orbits_setexe(
            year, month, day, orbtype, fortran)
        # if you have the orbit file, you can get the rinex file
        if foundit:
            # now you can look for a rinex file
            rinexfile, rinexfiled = g.rinex_name(station, year, month, day)
            # This goes to find the rinex file. I am changing it to allow
            # an archive preference

            g.go_get_rinex_flex(station, year, month, day, receiverrate,
                                archive)
            # define booleans
            oexist = os.path.isfile(orbdir + '/' + f) == True
            rexist = os.path.isfile(rinexfile) == True
            exc = exedir + '/teqc'
            texist = os.path.isfile(exc) == True
            if rexist:
                if texist and fortran:
                    # only do this for the older version
                    print(
                        'teqc executable exists, will use to eliminate unnecessary observables'
                    )
                    foutname = 'tmp.' + rinexfile
                    fout = open(foutname, 'w')
                    subprocess.call([
                        exc, '-O.obs', 'S1+S2+S5+S6+S7+S8', '-n_GLONASS', '27',
                        rinexfile
                    ],
                                    stdout=fout)
                    fout.close()
                    # store it in the original rinex filename
                    subprocess.call(['rm', '-f', rinexfile])
                    subprocess.call(['mv', '-f', foutname, rinexfile])
                    # decimate this new rinex file
                    if (rexist and dec_rate > 0):
                        print('decimate using teqc ', dec_rate, ' seconds')
                        rinexout = rinexfile + '.tmp'
                        cdec = str(dec_rate)
                        fout = open(rinexout, 'w')
                        subprocess.call([exc, '-O.dec', cdec, rinexfile],
                                        stdout=fout)
                        fout.close()  # needed?
                        status = subprocess.call(
                            ['mv', '-f', rinexout, rinexfile])
            # orbits and rinexfile exist
            if (oexist and rexist):
                snrname = g.snr_name(station, year, month, day, option)
                orbfile = orbdir + '/' + f
                if fortran:
                    print('Using fortran for translation')
                    try:
                        subprocess.call(
                            [snrexe, rinexfile, snrname, orbfile,
                             str(option)])
                        status = subprocess.call(['rm', '-f', rinexfile])
                        status = subprocess.call(['xz', orbfile])
                    except:
                        print('no success making SNR file')
                else:
                    print('Do not use fortran to make ', snrname)
                    print('decimating will be done here instead of using teqc')
                    rnx2snr(rinexfile, orbfile, snrname, option, year, month,
                            day, dec_rate)

                # remove the rinex file
                subprocess.call(['rm', '-f', rinexfile])

                # check to make sure the conversion worked
                if os.path.isfile(snrname):
                    #                make sure it exists and is non-zero size before moving it
                    if (os.stat(snrname).st_size == 0):
                        print(
                            'you created a zero file size which could mean a lot of things'
                        )
                        print(
                            'bad exe, bad snr option, do not really have the orbit file'
                        )
                        status = subprocess.call(['rm', '-f', snrname])
                    else:
                        print(
                            'a SNR file was created and it is non-zero in length'
                        )
                        print(snrname_full)
                        g.store_snrfile(snrname, year, station)
            else:
                print(
                    'Either the rinex file or orbit file does not exist, so there is nothing to convert'
                )

    return True
Ejemplo n.º 4
0
def main():
    """
    command line interface for download_rinex
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("station", help="station name", type=str)
    parser.add_argument("year", help="year", type=int)
    parser.add_argument("month", help="month (or day of year)", type=int)
    parser.add_argument("day",   help="day (zero if you use day of year earlier)", type=int)
# optional arguments
    parser.add_argument("-rate", default='low', metavar='low',type=str, help="sample rate: low or high")
    parser.add_argument("-archive", default=None, metavar='cddis',help="archive (unavco,sopac,cddis,sonel,nz,ga,ngs,bkg,nrcan)", type=str)
    parser.add_argument("-version", default=None, metavar=2,type=int, help="rinex version (2 or 3)")
    parser.add_argument("-doy_end", default=None, type=int, help="last day of year to be downloaded")

    args = parser.parse_args()

#   make sure environment variables exist.  set to current directory if not
    g.check_environ_variables()

#   assign to normal variables
    station = args.station
    year = args.year
    month = args.month
    day = args.day

    if len(str(year)) != 4:
        print('Year must have four characters: ', year)
        sys.exit()

    if (day == 0):
        # then you are using day of year as input
        doy = month
        year,month,day=g.ydoy2ymd(year, doy) 
    else:
        doy,cdoy,cyyyy,cyy = g.ymd2doy(year,month,day)

    # default is low
    rate = args.rate

    # set archive variable
    archive = args.archive


    archive_list = ['sopac', 'unavco','sonel','cddis','nz','ga','bkg','jeff','ngs','nrcan']

    archive_list_high = ['unavco','nrcan','ga']

    if args.version == None:
        version = 2
    else:
        version = args.version

    if args.doy_end == None:
        doy_end = doy
    else:
        doy_end = args.doy_end

    NS = len(station)

    if NS == 9:
        version = 3 # even if you don't choose version 3 .... 
    
    # this is for version 2
    if (version == 2) and (rate == 'low'):
        if (NS != 4):
            print('exiting: RINEX 2.11 station names must have 4 characters, lowercase please')
            sys.exit()
        if args.archive == None:
            archive = 'all'
        else:
            archive = args.archive.lower()
            if archive not in archive_list:
                print('You picked an archive that does not exist')
                print('I am going to check the main ones (unavco,sopac,sonel,cddis)')
                print('For future reference: I allow these archives:')
                print(archive_list)
                archive = 'all'

    #print('archive selected: ' , archive)
    # default archive wil be CDDIS for version 3
    if (version == 3):
        if (args.archive == None):
            archive = 'cddis'
            #print('no archive was specified, so looking for it at CDDIS')
        else:
            archive = args.archive

    # print('data rate', rate)
    if (rate == 'high') and (version == 2):
        if args.archive == None:
            archive = 'unavco'
        else:
            archive = args.archive

        if archive not in archive_list_high:
            print('You picked an archive that is not supported by my code. Exiting')
            sys.exit()

    for d in range(doy, doy_end+1):
        #print('working on year, day of year:', year, d)
        if version == 3:
            version3(station,year,d,NS,archive)
        else: # RINEX VERSION 2
            g.go_get_rinex_flex(station,year,d,0,rate,archive)
            rinexfile,rinexfiled = g.rinex_name(station, year, month, day)
            if os.path.isfile(rinexfile):
                print('SUCCESS: ', rinexfile)