示例#1
0
def from_igra(ident, var=['p', 't', 'r', 'dpd', 'wd', 'ws'], filename=None, attach=None, save=False, verbose=0, **kwargs):
    """Read IGRA data from text

    Parameters
    ----------
    ident       Radiosonde ID (WMO or IGRA ID)
    wmo         convert WMO ID to IGRA
    variables   select variables
    filename    filename of txt.gz
    attach      radiosonde object to attach with
    verbose     Verbosity

    Returns
    -------
    radiosonde object / save to HDF5
    """

    wmoid = None
    igraid = wmo2igra(ident)  # try to convert WMO to IGRA ?
    if igraid is not None:
        wmoid = ident   # WMO ID
        ident = igraid  # found IGRA ID
        print_verbose("[IGRA] WMO %s to IGRA %s"%(wmoid, ident), verbose)

    data = from_igra_to_dataframe(ident, filename=filename, verbose=verbose)

    lon = data.ix[-1, 'lon'] / 10000.  # IGRA specific
    lat = data.ix[-1, 'lat'] / 10000.
    data = data[var]
    #
    if isinstance(attach, radiosonde):
        attach.add_data('igra', data)
        attach.add_attr('igraid', ident)
        if 'lon' not in attach.attrs:
            attach.add_attr('lon', lon)
        if 'lat' not in attach.attrs:
            attach.add_attr('lat', lat)

        journal("[IGRA] data read with %s" % ident, attach.history, verbose)

    else:
        # try to convert to WMO ID
        if wmoid is None:
            wmoid = igra2wmo(ident)

        if wmoid is None:
            out = radiosonde(id=ident)
        else:
            out = radiosonde(id=wmoid)
            out.add_attr('igraid', ident)

        out.add_data('igra', data)
        out.add_attr('lon', lon)
        out.add_attr('lat', lat)  # convert to float
        out.history.append(now() + " IGRA Data read")
        if save:
            out.save()
        else:
            return out
示例#2
0
def from_store_deprecated(ident, filename=None, variables=None, directory=None, attributes=True, verbose=0, **kwargs):
    """Read previously saved Radiosonde from default directory
    pandas HDFStore HDF5

    Parameters
    ----------
    ident       Radiosonde Station Number
    filename    filename of HDFStore
    variables   select only given variables
    directory   directory to search for raso_######.h5 with station number

    Returns
    -------
    radiosonde class object
    """
    from raso.sonde.radiosonde import radiosonde
    from raso.config import rasodir, wkdir
    #
    if isinstance(ident, (float, int)):
        ident = "%06d" % ident
    #
    if filename is not None:
        if 'h5' not in filename:
            raise ValueError("This routine can only read HDF5 files from pandas!")
        default = filename

    elif directory is None:
        default = rasodir + '/raso_%s.h5' % ident

    else:
        default = directory + '/raso_%s.h5' % ident

    print_verbose("[STORE] %s" % default, verbose)
    #
    if not os.path.isfile(default):
        raise IOError("File %s not found" % default.replace(wkdir, '.'))
    #
    out = radiosonde(id=ident)
    metadata = False
    if variables is not None:
        if isinstance(variables, str):
            variables = [variables]  # convert to list
    #
    with pd.HDFStore(default, complevel=9, complib='blosc') as store:
        for key in store.keys():
            # drop /
            if key[1:] == 'metadata':
                metadata = True
                continue

            elif variables is not None:
                if key[1:] in variables:
                    out.add_data(key[1:], store[key])  # only certain variables

            else:
                out.add_data(key[1:], store[key])  # default

            print_verbose("[STORE] %s " % key, verbose)
        #
        if metadata:
            print_verbose("[STORE] METADATA ", verbose)
            imeta = store.get_storer('metadata')
            #
            # Attributes
            if 'metadata' in imeta.attrs:
                for ikey, ival in imeta.attrs['metadata'].items():
                    if ikey in ['metadata']:
                        continue  # ? do we need that?

                    if attributes:
                        out.add_attr(ikey, ival)
                    elif ikey in ['lon', 'lat', 'alt']:
                        out.add_attr(ikey, ival)
                    else:
                        pass
            #
            # Infos (id history, saved)
            if 'iparams' in imeta.attrs:
                for ikey, ival in imeta.attrs['iparams'].items():
                    if ikey in ['infos']:
                        continue

                    setattr(out, ikey, ival)

    out.filename = default
    return out
示例#3
0
def from_store(ident, variables=None, directory=None, verbose=0):
    """ Read Radiosonde from Store

    Skips any directory or file with extract in their name
    As well as mars_dump.h5

    Parameters
    ----------
    ident           str
    variables       list
    directory       str
    verbose         int

    Returns
    -------
    radiosonde

    Examples
    --------
    >>>import raso
    >>>raso.read.from_store('011035')

    """
    from raso.sonde.radiosonde import radiosonde
    from raso.config import rasodir

    if directory is None:
        directory = rasodir

    default = directory + '/%s/' % ident
    if not os.path.isdir(default):
        raise IOError("[from_store] Requires a valid directory: %s" % default)

    if variables is not None:
        if isinstance(variables, str):
            variables = [variables]


    out = radiosonde(id=ident)
    for ifile in os.listdir(default):
        if ifile == 'attributes.pickle':
            attrs = pickle.load(open(default + 'attributes.pickle'))
            for ikey, ival in attrs.items():
                out.add_attr(ikey, ival)

        elif ifile == 'radiosonde.pickle':
            attrs = pickle.load(open(default + 'radiosonde.pickle'))
            for ikey, ival in attrs.items():
                setattr(out, ikey, ival)

        elif ifile == 'history.txt':
            f = open(default + 'history.txt')
            setattr(out, 'history', f.read().splitlines())
            f.close()

        elif ifile == 'notes.txt':
            f = open(default + 'notes.txt')
            setattr(out, 'notes', f.read())
            f.close()

        elif ifile == 'mars_dump.h5':
            print_verbose("%20s Skipped" % ifile, verbose)
            continue

        elif os.path.isdir(default+ifile):
            continue  # any directory is ignored

        elif 'extract' in ifile:
            print_verbose("%20s Skipped" % ifile, verbose)  # extract is a codename for temp files
            continue

        else:
            varname = ifile.split('.')[0]
            if variables is not None:
                if varname not in variables:
                    print_verbose("%20s [%5s]" % (varname, color_boolean(False)), verbose)
                    continue
            # TODO add file timestamp as info
            # print varname, "created: %s" % time.ctime(os.path.getctime(default + ifile)), time.ctime(os.path.getmtime(default + ifile))
            if 'h5' in ifile:
                out.add_data(varname, pd.read_hdf(default + ifile, varname), verbose=-1)  # not reported

            else:
                out.add_data(varname, pickle.load(open(default + ifile)), verbose=-1)  # not reported
            out.var_saved[varname] = True
            print_verbose("%20s [%5s]" % (varname, color_boolean(True),), verbose)

    out.directory = directory
    out.is_saved = True
    return out