001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import java.util.Date;
007    import java.util.HashMap;
008    import java.util.Iterator;
009    import java.util.Map;
010    import java.util.TimeZone;
011    import java.util.Map.Entry;
012    
013    /**
014     * Base class of all the data types that can be displayed.
015     * 
016     * @author Lakshman
017     * @version $Id: DataType.java,v 1.10 2007/11/20 21:03:59 lakshman Exp $
018     */
019    public class DataType {
020            protected Location originLocation;
021    
022            protected Date startTime;
023    
024            protected String typeName;
025    
026            protected Map<String, String> attributes = new HashMap<String, String>();
027    
028            public static final float MissingData = -99900;
029    
030            public static final float RangeFolded = -99901;
031    
032            public static final float DataUnavailable = -99902;
033            
034            public DataType(Location originLoc, Date startTime, String typeName) {
035                    this.originLocation = originLoc;
036                    this.startTime = startTime;
037                    this.typeName = typeName;
038            }
039    
040            /**
041             * copies all the attributes, etc. from the master. The master can change
042             * without affecting this object
043             */
044            public DataType(DataType master) {
045                    this.originLocation = master.originLocation;
046                    this.startTime = master.startTime;
047                    this.typeName = master.typeName;
048                    Iterator<Entry<String, String>> entries = master.attributes.entrySet()
049                                    .iterator();
050                    while (entries.hasNext()) {
051                            Entry<String, String> entry = entries.next();
052                            this.setAttribute(entry.getKey(), entry.getValue());
053                    }
054            }
055    
056            /** copies all the attributes over */
057            public void addAttributes(DataType example){
058                    Iterator<Entry<String,String>> entries = example.attributes.entrySet().iterator();
059                    while (entries.hasNext()){
060                            Entry<String,String> entry = entries.next();
061                            setAttribute(entry.getKey(), entry.getValue());
062                    }
063            }
064            
065            public void setAttributes(Map<String, String> attr) {
066                    attributes = attr;
067            }
068    
069            public void setAttribute(String name, String value) {
070                    attributes.put(name, value);
071            }
072    
073            public Map<String, String> getAttributes() {
074                    return attributes;
075            }
076    
077            public Object getAttribute(String name) {
078                    return attributes.get(name);
079            }
080    
081            public Date getTime() {
082                    return startTime;
083            }
084    
085            /** of the origin. */
086            public Location getLocation() {
087                    return originLocation;
088            }
089    
090            public String getTypeName() {
091                    return typeName;
092            }
093    
094            public void setTypeName(String typeName){
095                    this.typeName = typeName;
096            }
097            
098            public String getUnit(){
099                    String unit = attributes.get("Unit");
100                    if (unit == null){
101                            return "dimensionless";
102                    } else {
103                            return unit;
104                    }
105            }
106            
107            public String getUTC() {
108                    java.text.DateFormat df = java.text.DateFormat.getDateTimeInstance();
109                    df.setTimeZone(TimeZone.getTimeZone("GMT"));
110                    return df.format(startTime) + " UTC";
111            }
112    
113            public String toStringDB() {
114                    String s = "datatype: at " + getUTC() + " for " + originLocation + " ";
115                    Iterator<Map.Entry<String,String>> entries = getAttributes().entrySet().iterator();
116                    while (entries.hasNext()) {
117                            Map.Entry<String,String> entry = entries.next();
118                            s += entry.getKey() + "=" + entry.getValue() + " ";
119                    }
120                    return s;
121            }
122            
123            /** Makes a copy of 2D arrays */
124            static float[][] copyOf(float[][] master){
125                    float[][] values = new float[ master.length ][ master[0].length ];
126                    for (int i=0; i < values.length; ++i){
127                            for (int j=0; j < values[0].length; ++j){
128                                    values[i][j] = master[i][j];
129                            }
130                    }
131                    return values;
132            }
133    }