|
- #!/usr/bin/python
-
- import nilmtools.filter
- import nilmdb.client
-
- def DecimateException(Exception):
- pass
-
- def main():
- f = nilmtools.filter.Filter()
- parser = f.setup_parser("Decimate a stream")
- group = parser.add_argument_group("Decimate options")
- group.add_argument('-f', '--factor', action='store', default=4, type=int,
- help='Decimation factor (default: %(default)s)')
- # Parse arguments
- try:
- args = f.parse_args()
- except nilmtools.filter.MissingDestination as e:
- # If no destination, suggest how to create it.
- print "Source is %s (%s)" % (e.src, e.layout)
- print "Destination %s doesn't exist" % (e.dest)
- # Figure out a recommended layout
- if "decimate_source" in f.client.stream_get_metadata(e.src):
- rec = e.layout
- elif 'int32' in e.layout_type or 'float64' in e.layout_type:
- rec = 'float64_' + str(e.layout_count * 3)
- else:
- rec = 'float32_' + str(e.layout_count * 3)
- print "You could make it with a command like:"
- print " nilmtool create", e.dest, rec
- raise SystemExit(1)
-
- # See if the metadata jives, and complain if it doesn't
- dest_metadata = f.client.stream_get_metadata(args.destpath)
- try:
- rows = f.destinfo[4] # don't complain unless there's data
- tmp = dest_metadata.get("decimate_source", args.srcpath)
- if tmp != args.srcpath and rows > 0:
- raise DecimateException("storing decimated data from %s" % tmp)
- tmp = int(dest_metadata.get("decimate_factor", args.factor))
- if tmp != args.factor and rows > 0:
- raise DecimateException("storing data decimated at "
- "a different factor (%d)", tmp)
- except DecimateException as e:
- print "The destination seems to already be " + str(e)
- print "Refusing to change it. You can change the stream's"
- print "decimate_* metadata, or remove all data in the stream,"
- print "to prevent this error."
-
- # Fill in the metadata in case it's missing
- f.client.stream_update_metadata(args.destpath,
- { "decimate_source": args.srcpath,
- "decimate_factor": args.factor })
-
- # Process it
-
-
- if __name__ == "__main__":
- main()
|