All Downloads are FREE. Search and download functionalities are using the official Maven repository.

decodes.consumer.TvaFormatter Maven / Gradle / Ivy

Go to download

A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.

The newest version!
/*
*  $Id$
*/
package decodes.consumer;

import java.util.Iterator;
import java.util.Properties;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;

import ilex.var.TimedVariable;
import ilex.var.IFlags;
import ilex.util.Logger;

import decodes.db.*;
import decodes.decoder.DecodedMessage;
import decodes.decoder.TimeSeries;
import decodes.decoder.Sensor;
import decodes.datasource.RawMessage;
import decodes.datasource.UnknownPlatformException;

/**
  This class formats decoded data as required for TVA Transation files.
*/
public class TvaFormatter extends OutputFormatter
{
	private SimpleDateFormat tvaDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
	public static final String TVA_GAGE_ID = "tva-gage-id";

	private NumberFormat tvaNumberFormat = NumberFormat.getNumberInstance();

	/** default constructor */
	public TvaFormatter()
	{
		super();
		tvaNumberFormat.setMaximumFractionDigits(2);
		tvaNumberFormat.setMinimumFractionDigits(2);
		tvaNumberFormat.setGroupingUsed(false);
		tvaNumberFormat.setMaximumIntegerDigits(4);
		tvaNumberFormat.setMinimumIntegerDigits(4);
	}

	/**
	  Initializes the Formatter. This method is called from the static
	  makeOutputFormatter method in this class. The RoutingSpec does not
	  need to call it explicitly.
	  @param type the type of this output formatter.
	  @param tz the time zone as specified in the routing spec.
	  @param presGrp The presentation group to handle rounding & EU conversions.
	  @param rsProps the routing-spec properties.
	*/
	protected void initFormatter(String type, java.util.TimeZone tz,
		PresentationGroup presGrp, Properties rsProps)
		throws OutputFormatterException
	{
		tvaDateFormat.setTimeZone(tz);
	}

	/** Does nothing. */
	public void shutdown()
	{
	}

	/**
	  Writes the passed DecodedMessage to the passed consumer, using
	  a concrete format.
	  @param msg The message to output.
	  @param consumer The DataConsumer to output to.
	  @throws OutputFormatterException if there was a problem formatting data.
	  @throws DataConsumerException, passed through from consumer methods.
	*/
	public void formatMessage(DecodedMessage msg, DataConsumer consumer)
		throws DataConsumerException, OutputFormatterException
	{
		consumer.startMessage(msg);

		StringBuffer sb = new StringBuffer();
		RawMessage rawmsg = msg.getRawMessage();

		Platform platform = null;
		String tvaGageId = null;

		try
		{
			platform = rawmsg.getPlatform();
		}
		catch(UnknownPlatformException e)
		{
			throw new OutputFormatterException(e.toString());
		}

		SiteName sn = platform.getSite().getName(TVA_GAGE_ID);
		if (sn == null)
		{
			String err = "No name of type '" + TVA_GAGE_ID
				+ "' Defined in site "
				+ platform.getSiteName() + " -- skipped.";
			throw new OutputFormatterException(err);
		}
		tvaGageId = sn.getNameValue();
		if (tvaGageId == null)
		{
			String err = "No TVA Gage ID Defined in site "
				+ platform.getSiteName() + " -- skipped.";
			throw new OutputFormatterException(err);
		}
		if (tvaGageId.length() < 4)
		{
			String err = "Invalid TVA Gage ID '" + tvaGageId 
				+ "' Defined in site "
				+ platform.getSiteName() + " -- skipped.";
			throw new OutputFormatterException(err);
		}

		for(Iterator it = msg.getAllTimeSeries(); it.hasNext(); )
		{
			TimeSeries ts = (TimeSeries)it.next();
			Sensor sensor = ts.getSensor();

			DataType origDt = sensor.getDataType();
			if (origDt == null || origDt.getStandard() == null)
			{
				Logger.instance().log(Logger.E_WARNING,
					"Station '" + platform.getSiteName()
					+ "' No datatype defined for sensor '" + sensor.getName() 
					+ "' -- skipped.");
				continue;
			}
			DataType dt = origDt;
			if (!dt.getStandard().equalsIgnoreCase(Constants.datatype_SHEF))
				dt = dt.findEquivalent(Constants.datatype_SHEF);
			if (dt == null || dt.getStandard() == null
			 || dt.getCode() == null || dt.getCode().length() < 2)
			{
				Logger.instance().log(Logger.E_WARNING,
					"Station '" + platform.getSiteName()
					+ "' Cannot find SHEF datatype for sensor '" 
					+ sensor.getName() + "' -- skipped.");
				continue;
			}

			String tvaDataCode = shef2tvaDataCode(dt.getCode(), sensor);
			if (tvaDataCode == null || tvaDataCode.length() < 2)
			{
				if (!sensor.getName().equalsIgnoreCase("battery"))
					Logger.instance().info(
						"Station '" + platform.getSiteName()
						+ "' Cannot find TVA Data Code for sensor '" 
						+ sensor.getName() + "' -- skipped.");
				continue;
			}

			int sz = ts.size();
			for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy