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

decodes.util.PdtEntry 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.util;

import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.text.ParseException;

import ilex.util.TextUtil;
import ilex.util.IDateFormat;
import lrgs.common.DcpAddress;

/**
 * This class contains a PDT entry.
 */
public class PdtEntry
{
	public static final int fieldWidths[] = { 6, 8, 1, 3, 1, 3, 6, 6, 4, 4, 1,
	    2, 1, 1, 31, 7, 8, 1, 14, 16, 1, 1, 6, 24, 20, 20, 30, 11, 1, 1 };
	public static final SimpleDateFormat sdf = new SimpleDateFormat(
	    "yyyyDDDHHmm");
	public static int lineLength = 0;
	static
	{
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		for(int i=0; i 0)
				line.setCharAt(position++, ' ');
			for(int i=0; i 0)
				line.setCharAt(position++, '0');
			for(int i=0; i 0)
				line.setCharAt(position++, ' ');
		}
	}

//	private String printWhiteSpace(String line, String insert, int index)
//	{
//		if (fieldWidths[index] <= insert.length())
//		{
//			return line;
//		}
//		for (int pos = 0; pos < fieldWidths[index] - insert.length(); pos++)
//		{
//			line = line + " ";
//		}
//		return line;
//	}

	public void assignFromLine(String line)
	    throws BadPdtEntryException
	{
		String[] f = TextUtil.getFixedFields(line, fieldWidths);
		int len = f.length;
		if (len < 11)
			throw new BadPdtEntryException("Only " + len + " fields in line.");

		agency = new String(f[0].trim());
		try
		{
			dcpAddress = new DcpAddress(new String(f[1]));
		}
		catch (NumberFormatException ex)
		{
			throw new BadPdtEntryException("Bad DCP address '" + f[1] + "'");
		}

		char c = f[2].charAt(0);
		if (c == 'S' || c == 'D')
		{
			try
			{
				st_channel = Integer.parseInt(f[3].trim());
				c = f[4].charAt(0);
				if (c == 'R')
					rd_channel = Integer.parseInt(f[5].trim());

				st_first_xmit_sod = IDateFormat.getSecondOfDay(f[6]);
				st_xmit_interval = IDateFormat.getSecondOfDay(f[7]);
				st_xmit_window = IDateFormat.getSecondOfDay(f[8]) / 60;
			}
			catch (NumberFormatException ex)
			{
				throw new BadPdtEntryException("Bad chan params");
			}
			catch (IllegalArgumentException ex)
			{
				throw new BadPdtEntryException("time params");
			}
		}
		else if (c == 'R')
		{
			try
			{
				rd_channel = Integer.parseInt(f[3].trim());
			}
			catch (NumberFormatException ex)
			{
				throw new BadPdtEntryException("Bad chan Params");
			}
		}

		try
		{
			baud = Integer.parseInt(f[9].trim());
		}
		catch (NumberFormatException ex)
		{
			throw new BadPdtEntryException("Bad baud");
		}

		data_format = f[10].charAt(0);

		if (len < 12)
			return;
		state_abbr = new String(f[11]);

		if (len < 14)
			return;
		location_code = f[13].charAt(0);

		if (len < 15)
			return;
		description = new String(f[14].trim());

		try
		{
			if (len < 16)
				return;
			latitude = cvtloc(f[15]);

			if (len < 17)
				return;
			longitude = cvtloc(f[16]);
		}
		catch (NumberFormatException ex)
		{
		}

		if (len < 18)
			return;
		location_type = f[17].charAt(0);

		if (len < 19)
			return;
		manufacturer = new String(f[18].trim());

		if (len < 20)
			return;
		model = new String(f[19].trim());

		if (len < 21)
			return;
		unknown_flag1 = f[20].charAt(0);

		if (len < 22)
			return;
		nmc_flag = f[21].charAt(0);

		if (len < 23)
			return;
		nmc_descriptor = new String(f[22].trim());

		if (len < 24)
			return;
		maintainer = new String(f[23].trim());

		if (len < 25)
			return;
		telnum1 = new String(f[24].trim());

		if (len < 26)
			return;
		telnum2 = new String(f[25].trim());

		if (len < 27)
			return;
		shefcodes = new String(f[26].trim());

		if (len < 28)
			return;
		try
		{
			lastmodified = sdf.parse(f[27]);
		}
		catch (ParseException ex)
		{
			throw new BadPdtEntryException("Bad LMT");
		}

		if (len < 29)
			return;
		c = f[28].charAt(0);
		if (Character.isDigit(c))
			num_failures = (int) c - (int) '0';

		if (len < 30)
			return;
		
// No: this is not an active flag.		
//		active_flag = f[29].charAt(0);
	}

	private double cvtloc(String s)
	    throws NumberFormatException
	{
		int sign = 1;
		if (s.charAt(0) == '-')
			sign = -1;
		int deg = 0, min = 0, sec = 0;
		if (s.length() == 8)
		{
			deg = Integer.parseInt(s.substring(1, 4));
			min = Integer.parseInt(s.substring(4, 6));
			sec = Integer.parseInt(s.substring(6));
		}
		else if (s.length() == 7)
		{
			deg = Integer.parseInt(s.substring(1, 3));
			min = Integer.parseInt(s.substring(3, 5));
			sec = Integer.parseInt(s.substring(5));
		}
		return ((double) deg + (double) min / 60. + (double) sec / 3600.)
		    * sign;
	}
	
	public static String hhmmss(int sod) 
	{
		int h = sod / (60*60);
		sod -= (h*60*60);
		int m = sod / 60;
		int s = sod % 60;

		StringBuilder sb = new StringBuilder();
		if (h < 10) sb.append('0');
		sb.append(h);
		if (m < 10) sb.append('0');
		sb.append(m);
		if (s < 10) sb.append('0');
		sb.append(s);
		return sb.toString();
	}
	public static String mmss(int sod) 
	{
		int h = sod / (60*60);
		sod -= (h*60*60);
		int m = sod / 60;
		int s = sod % 60;

		StringBuilder sb = new StringBuilder();
//		if (h < 10) sb.append('0');
//		sb.append(h);
		if (m < 10) sb.append('0');
		sb.append(m);
		if (s < 10) sb.append('0');
		sb.append(s);
		return sb.toString();
	}
	
	public static String latitude2str(double value)
	{
		byte sb[] = new byte[7];
		if (value < 0)
		{
			sb[0] = (byte)'-';
			value = -value;
		}
		else
			sb[0] = (byte)' ';
		
		int deg = (int)value;
		sb[1] = (byte)((int)'0' + (deg/10));
		sb[2] = (byte)((int)'0' + (deg%10));
		
		value = (value - (double)deg) * 60;
		int min = (int)value;
		sb[3] = (byte)((int)'0' + (min/10));
		sb[4] = (byte)((int)'0' + (min%10));
		
		value = (value - (double)min) * 60;
		int sec = (int)(value+.5);
		sb[5] = (byte)((int)'0' + (sec/10));
		sb[6] = (byte)((int)'0' + (sec%10));
	
		return new String(sb);
	}
	
	public static String longitude2str(double value)
	{
		byte sb[] = new byte[8];
		if (value < 0)
		{
			sb[0] = (byte)'-';
			value = -value;
		}
		else
			sb[0] = (byte)' ';
		
		int deg = (int)value;
		sb[1] = (byte)((int)'0' + (deg/100));
		sb[2] = (byte)((int)'0' + ((deg%100)/10));
		sb[3] = (byte)((int)'0' + (deg%10));
		
		value = (value - (double)deg) * 60;
		int min = (int)value;
		sb[4] = (byte)((int)'0' + (min/10));
		sb[5] = (byte)((int)'0' + (min%10));
		
		value = (value - (double)min) * 60;
		int sec = (int)(value+.5);
		sb[6] = (byte)((int)'0' + (sec/10));
		sb[7] = (byte)((int)'0' + (sec%10));
	
		return new String(sb);
	}
		
	public String toString()
	{
		return agency + ":" + dcpAddress + ":" + st_channel + ":"
		    + rd_channel + ":" + st_first_xmit_sod + ":" + st_xmit_interval
		    + ":" + st_xmit_window + ":" + baud + ":" + data_format + ":"
		    + state_abbr + ":" + location_code + ":" + description + ":"
		    + latitude + ":" + longitude + ":" + location_type + ":"
		    + manufacturer + ":" + model + ":" + unknown_flag1 + ":" + nmc_flag
		    + ":" + nmc_descriptor + ":" + maintainer + ":" + telnum1 + ":"
		    + telnum2 + ":" + shefcodes + ":" + lastmodified + ":"
		    + num_failures + ":" + active_flag;
	}
	
	public String getDescription()
	{
		return nwsXrefEntry != null ? nwsXrefEntry.getLocationName() : description;
	}

	public NwsXrefEntry getNwsXrefEntry()
	{
		if (!xrefed)
		{
			NwsXref nx = NwsXref.instance();
			if (nx.isLoaded())
				nwsXrefEntry = nx.getByAddr(dcpAddress);
			xrefed = true;
		}
		return nwsXrefEntry;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy