import os
import tempfile

import tables


# default hdf5 file path
HDF5_FILE_PATH = os.path.join(tempfile.gettempdir(), "pyhis.h5")


class NCDCValue(tables.IsDescription):
    date = tables.StringCol(8)
    flag = tables.StringCol(1)
    value = tables.StringCol(20)
    last_modified = tables.StringCol(26)


def init_h5(path=None, mode='w'):
    """creates an hdf5 file an initialized it with relevant tables, etc"""
    if not path:
        path = HDF5_FILE_PATH
    h5file = tables.openFile(path, mode=mode, title="pyHIS data")
def get_stations(update=True, path=None):
    #XXX: we should have a fast pytables version of stations list
    return core.get_stations(update=update)


    ncdc = h5file.createGroup('/', 'ncdc', 'NCDC Data')
    gsod = h5file.createGroup(ncdc, 'gsod', 'Global Summary of the Day')
    h5file.createGroup(gsod, 'values', 'Values')
    h5file.close()



def _get_value_table(h5file, site, variable):
    """returns a value table for a given open h5file (writable), site and
    variable. If the value table already exists, it is returned. If it doesn't,
    it will be created.
    """
    gsod_path = '/ncdc/gsod'
    site_group = site['name']
    site_path = '/'.join((gsod_path, site_group))
    util.get_or_create_group(h5file, site_path, "Site %s" % site['code'])

    value_table_name = variable
    values_path = '/'.join([site_path, value_table_name])

    try:
        value_table = h5file.getNode(values_path)
    except NoSuchNodeError:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            value_table = util.get_or_create_table(
                h5file, values_path, NCDCValue,
                "Values for site: %s, variable: %s" % (site['code'], variable))
            value_table.cols.date.createCSIndex()
            value_table.attrs.USAF = site['USAF']
            value_table.attrs.WBAN = site['WBAN']
            value_table.attrs.name = site['name']

    return value_table
if __name__ == '__main__':
    test_path = '/User/wilsaj/test/pyhis_test.h5'
    _init_h5()
    stations = get_stations(update=False)
    texas_stations = [
        code
        for code, station in stations.iteritems()
        if station['state'] == 'TX']
    update_data(texas_stations, 2012, 2012, path=test_path)
    import pdb; pdb.set_trace()

@@ -309,230 +309,8 @@
 )%0A%0A%0A
-def init_h5(path=None, mode='w'):%0A    %22%22%22creates an hdf5 file an initialized it with relevant tables, etc%22%22%22%0A    if not path:%0A        path = HDF5_FILE_PATH%0A    h5file = tables.openFile(path, mode=mode, title=%22pyHIS data%22)%0A
 def 
@@ -1741,16 +1741,440 @@
 e_table%0A
+%0A%0Adef _init_h5(path=None, mode='w'):%0A    %22%22%22creates an hdf5 file an initialized it with relevant tables, etc%22%22%22%0A    if not path:%0A        path = HDF5_FILE_PATH%0A    with tables.openFile(path, mode=mode, title=%22pyHIS data%22) as h5file:%0A        ncdc = h5file.createGroup('/', 'ncdc', 'NCDC Data')%0A        gsod = h5file.createGroup(ncdc, 'gsod', 'Global Summary of the Day')%0A        h5file.createGroup(gsod, 'values', 'Values')%0A%0A%0A
 if __nam

make init_h5 a private func