예제 #1
0
    def test_num_headers(self):
        """The number of headers header counts itself as a header."""
        with closing(StringIO()) as buff:
            dfile = DataFile()
            dfile.globals['LONGITUDE'] = '0.000'
            ctdex.write(dfile, buff)

            result = buff.getvalue().split('\n')
            self.assertEqual('2', result[1].split(' = ')[1].lstrip())
예제 #2
0
    def test_write_exchange_decimal_places(self):
        """Decimal places should be kept from the original data."""
        with closing(StringIO()) as buff:
            dfile = DataFile()
            dfile.globals['LONGITUDE'] = _decimal('0.0000000')
            dfile.create_columns(['CTDPRS'])
            dfile['CTDPRS'].values = [_decimal('10.0001'), None]
            ctdex.write(dfile, buff)

            result = buff.getvalue().split('\n')
            # Decimal('0.0000000') is converted to 0E-7 by str. The formatting
            # has to be done manually.
            self.assertEqual('0.0000000', result[2].split(' = ')[1].lstrip())
예제 #3
0
    def test_functional_write(self):
        dfile = DataFile()
        dfile.create_columns(['CTDPRS', 'CTDOXY'])
        dfile['CTDPRS'].parameter.display_order = 0
        dfile['CTDOXY'].parameter.display_order = 1
        dfile['CTDPRS'].values = map(_decimal, ['2.0', '4.0'])
        dfile['CTDOXY'].values = map(_decimal, ['254.0', '253.1'])
        dfile['CTDOXY'].flags_woce = [2, 3]

        with closing(StringIO()) as buff:
            ctdex.write(dfile, buff)
            result = buff.getvalue().split('\n')
            self.assertEqual([u'        2.0', u'      254.0', u'2'],
                             result[4].split(','))
예제 #4
0
파일: tools.py 프로젝트: cberys/libcchdo
def _single_file(reader, files, output, **kwargs):
    d = DataFile()
    reader.read(d, files[0], **kwargs)
    if output is not sys.stdout:
        output = open(output, 'w')
    ctdex.write(d, output)
예제 #5
0
파일: tools.py 프로젝트: cberys/libcchdo
def ctd_polarstern_to_ctd_exchange(args, db):
    import libcchdo.formats.ctd.exchange as ctdex
    import libcchdo.formats.ctd.polarstern as ctd_polarstern

    PARAMETERS = {
        7: "param_depth_water",
        8: "param_press",
        9: "param_sal",
        10: "param_sigma_theta",
        11: "param_temp",
        12: "param_tpot",
        13: "param_cond",
        14: "param_nobs",
        15: "param_atten",
        16: "param_ys_fl",
        17: "param_chl_fluores",
    }

    def unpack_param_meta(meta_param):
        return {
            "units": meta_param[1],
            "pi": meta_param[2],
            "method": meta_param[3],
            "comment": meta_param[4]
        }

    def unpack_citation(meta_cite):
        return {
            "name": meta_cite[1],
            "year": meta_cite[2],
            "description": meta_cite[3]
        }

    def unpack_reference(meta_ref):
        return unpack_citation(meta_ref)

    def unpack_events(meta_events):
        return {
            "name": meta_events[1],
            "latitude": meta_events[2],
            "longitude": meta_events[3],
            "elevation": meta_events[4],
            "date_time": meta_events[5],
            "location": meta_events[6],
            "campaign": meta_events[7],
            "basis": meta_events[8],
            "device": meta_events[9]
        }

    # Make sure this database can read its Unicode entries
    db.text_factory = str

    for input_filename in sys.files:
        log.info(input_filename)

        meta = {}
        meta_cast = db.cursor().execute(
            "select * from ctd_casts where filename = ? limit 1",
            (os.path.basename(input_filename), )).fetchone()

        if not meta_cast:
            log.error(u"no metadata for {0}".format(input_filename))
            continue

        meta["filename"] = meta_cast[1]

        meta["cites"] = unpack_citation(db.cursor().execute(
            "select * from ctd_citations where id = ? limit 1",
            (meta_cast[2], )).fetchone())

        meta["refs"] = unpack_reference(db.cursor().execute(
            "select * from ctd_references where id = ? limit 1",
            (meta_cast[3], )).fetchone())

        meta["events"] = unpack_events(db.cursor().execute(
            "select * from ctd_events where id = ? limit 1",
            (meta_cast[4], )).fetchone())

        meta["min_depth"] = meta_cast[5]

        meta["max_depth"] = meta_cast[6]

        for i in PARAMETERS:
            if meta_cast[i] != 0:
                meta_param = db.cursor().execute(
                    "select * from ctd_%s where id = ? limit 1" %
                    PARAMETERS[i], (meta_cast[i], )).fetchone()
                meta[PARAMETERS[i]] = unpack_param_meta(meta_param)

        output_filename = os.path.basename(input_filename)
        output_filename = output_filename[:output_filename.find('.')] + \
                          "_ct1.csv"

        datafile = ctd_polarstern.read(meta, input_filename)

        if args.commit_to_file:
            with closing(args.ctdex) as out_file:
                try:
                    ctdex.write(datafile, out_file)
                except TypeError:
                    log.error(u'{0} {1!r}'.format(
                        input_filename,
                        map(lambda col: col.parameter.display_order,
                            datafile.columns.values())))
        else:
            print "%sOutput to %s (not written):%s" % ("", output_filename, "")