MATSIM
Count.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Count.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.counts;
22 
23 import it.unimi.dsi.fastutil.ints.Int2DoubleMap;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.matsim.api.core.v01.Coord;
27 import org.matsim.api.core.v01.Id;
30 
31 import java.util.HashMap;
32 import java.util.OptionalDouble;
33 
39 @Deprecated
40 public class Count<T> implements Identifiable<T> {
41 
42 
43  private static final Logger log = LogManager.getLogger(Counts.class);
44 
46  private final Measurable volume;
47 
49  this.location = v;
50 
51  String mode;
53  mode = Measurable.ANY_MODE;
54  else
55  mode = TransportMode.car;
56 
57  // Counts only support hourly volumes
58  this.volume = location.createVolume(mode, Measurable.HOURLY);
59 
60  if (!this.volume.supportsHourlyAggregate()) {
61  log.warn("Unsupported counts interval '" + this.volume.getInterval() + "' for analysis functionality. " +
62  "Interval should be able to aggregate hourly, otherwise counts will be empty");
63  }
64  }
65 
76  public final Volume createVolume(final int h, final double val) {
77  if (h < 1) {
78  throw new RuntimeException("counts start at 1, not at 0. If you have a use case where you need to go below one, "
79  + "let us know and we think about it, but so far we had numerous debugging sessions because someone inserted counts at 0.");
80  }
81 
82  Volume v = new Volume(h, val);
83 
84  // Only hourly volumes can be set
85  if (this.volume.getInterval() == Measurable.HOURLY)
86  this.volume.setAtHour(h - 1, val);
87  else
88  log.warn("Unsupported counts '" + this.volume.getInterval() + "' for setting values. Resolution must be hourly.");
89 
90  return v;
91  }
92 
93  public final void setCsId(final String cs_id) {
94  location.setStationName(cs_id);
95  }
96 
97  @Override
98  public final Id<T> getId() {
99  return location.getRefId();
100  }
101 
102  public final String getCsLabel() {
103  return location.getStationName();
104  }
105 
106  public final Volume getMaxVolume() {
107  int hour = -1;
108  double max = -1.0;
109  for (Int2DoubleMap.Entry e : this.volume) {
110  if (e.getDoubleValue() > max) {
111  max = e.getDoubleValue();
112  hour = e.getIntKey() / Measurable.HOURLY;
113  }
114  }
115  return hour >= 0 ? new Volume(hour + 1, max) : null;
116  }
117 
118  public final Volume getVolume(final int h) {
119  if (this.volume.supportsHourlyAggregate()) {
120  // Uses old format where hours starts at 1
121  OptionalDouble v = this.volume.aggregateAtHour(h - 1);
122  return v.isPresent() ? new Volume(h, v.getAsDouble()) : null;
123  }
124 
125  return null;
126  }
127 
128  public final HashMap<Integer, Volume> getVolumes() {
129  HashMap<Integer, Volume> res = new HashMap<>();
130  for (Int2DoubleMap.Entry e : volume) {
131  // Offset for old API
132  int h = (e.getIntKey() / Measurable.HOURLY) + 1;
133  res.put(h, new Volume(h, e.getDoubleValue()));
134  }
135 
136  return res;
137  }
138 
143  public Coord getCoord() {
144  return location.getCoordinates();
145  }
146 
147  public void setCoord(final Coord coord) {
148  location.setCoordinates(coord);
149  }
150 
151  @Override
152  public final String toString() {
153  return "[Loc_id=" + this.location.getId() + "]" +
154  "[cs_id=" + this.location.getStationName() + "]" +
155  "[nof_volumes=" + this.volume.size() + "]";
156  }
157 
158 }
final Volume getMaxVolume()
Definition: Count.java:106
final Volume getVolume(final int h)
Definition: Count.java:118
static final String ANY_MODE
Definition: Measurable.java:21
final String getCsLabel()
Definition: Count.java:102
boolean hasMeasurableForMode(String measurableType, String mode)
void setCoord(final Coord coord)
Definition: Count.java:147
final Volume createVolume(final int h, final double val)
Definition: Count.java:76
final Id< T > getId()
Definition: Count.java:98
static final Logger log
Definition: Count.java:43
Count(MeasurementLocation< T > v)
Definition: Count.java:48
final void setCsId(final String cs_id)
Definition: Count.java:93
final Measurable volume
Definition: Count.java:46
void setAtHour(int hour, double value)
Definition: Measurable.java:64
final HashMap< Integer, Volume > getVolumes()
Definition: Count.java:128
OptionalDouble aggregateAtHour(int hour)
final String toString()
Definition: Count.java:152
final MeasurementLocation< T > location
Definition: Count.java:45