· 6 years ago · Feb 07, 2020, 10:02 PM
1package TransAlta.Weather;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.Iterator;
10import java.util.List;
11import java.util.Map;
12import java.util.Set;
13
14import org.apache.http.HttpResponse;
15import org.apache.http.client.HttpClient;
16import org.apache.http.client.methods.HttpGet;
17import org.apache.http.impl.client.HttpClientBuilder;
18import org.json.JSONArray;
19import org.json.JSONObject;
20
21import TransAlta.EclipseIntegration.TransAltaScript;
22
23
24/**
25 * <pre>
26 * Library for Downloading TWC Wind Forecast Data for the next 15 days {Wind Speed, Wind Direction, and Air Density}.
27 *
28 *
29 * Copyright (C) 2016-2020 Power Costs, Inc. All Rights Reserved.
30 *
31 * Created MM-DD-2020 by First Last (PCI\mlai)
32 * Last modified $Date$
33 *
34 * SVN : $HeadURL$
35 * </pre>
36 *
37 * @author $Author$
38 * @version $Revision$
39 * @scripted false
40 */
41public class TWCEnergyWindForecast15DayV3 extends TransAltaScript {
42 Map<String, JSONArray> attrMap;
43
44 List<Long> timeList;
45
46 double NULL_VALUE = -999.999;
47
48 // https://api.weather.com/v3/wx/forecast/hourly/energywind/15day?geocode=41.89730286,-77.68366863&format=json&units=e&height=100&apiKey=1a46ecc565da4a3d86ecc565dafa3d63
49 String API_KEY = "1a46ecc565da4a3d86ecc565dafa3d63";
50
51
52 public void test() throws Exception {
53 loadWindForecast("41.89730286", "-77.68366863", "e", "100", API_KEY);
54// for (String attr : getAttributeNames()) {
55// logger("=================" + attr + "=================");
56// for (String value : getAttributeValues(attr)) {
57// logger(value);
58// }
59// }
60 logger(getAttributeValuesByIntervals(getIntervals()[10], "windSpeed"));
61 }
62
63
64 /**
65 * Load wind forecast.
66 *
67 * @param latitude latitude
68 * @param longtitude longtitude
69 * @param units units
70 * @param heightNum height num
71 * @param API_KEY api key
72 * @return String
73 * @throws Exception Err
74 */
75 public void loadWindForecast(String latitude, String longtitude, String units, String heightNum, String API_KEY) throws Exception {
76 String sURL = "https://api.weather.com/v3/wx/forecast/hourly/energywind/15day?geocode=" + latitude + "," + longtitude + "&format=json&units=" + units + "&height=" + heightNum + "&apiKey=" + API_KEY;
77 String sResult = null;
78
79 Map<String, Map<String, String>> mapWindData = null;
80 timeList = new ArrayList<>();
81 attrMap = new HashMap<>();
82
83 HttpGet httpGet = new HttpGet(sURL);
84 HttpClient client = HttpClientBuilder.create().build();
85 HttpResponse response = client.execute(httpGet);
86 sResult = convertStreamToString(response.getEntity().getContent());
87
88 JSONObject jo = new JSONObject(sResult);
89 JSONArray validTimeUtcArray = (JSONArray) jo.getJSONObject("forecasts1Hour").get("validTimeUtc");
90
91 /* Map<Time, Order> */
92 for (int i = 0; i < validTimeUtcArray.length(); i++) {
93 long lDateTime = Long.parseLong(validTimeUtcArray.getString(i) + "000");
94 timeList.add(lDateTime);
95 }
96
97 /* Get all attributes */
98 Iterator<String> iterator = jo.getJSONObject("forecasts1Hour").keys();
99 while (iterator.hasNext()) {
100 String key = iterator.next();
101 attrMap.put(key, (JSONArray) jo.getJSONObject("forecasts1Hour").get(key));
102 }
103 }
104
105
106 /**
107 * Gets the intervals.
108 *
109 * @return Long[]
110 */
111 public Long[] getIntervals() {
112 return timeList != null && timeList.size() > 0 ? timeList.toArray(new Long[timeList.size()]) : null;
113 }
114
115
116 /**
117 * Gets the attribute values by intervals.
118 *
119 * @param lDataTime l data time
120 * @param attrName attr name
121 * @return String
122 */
123 public String getAttributeValuesByIntervals(Long lDataTime, String attrName) {
124 int order = timeList.indexOf(lDataTime);
125 String[] values = getAttributeValues(attrName);
126 return values != null ? values[order] : null;
127 }
128
129
130 /**
131 * Gets the attribute names.
132 *
133 * @return String[]
134 */
135 public String[] getAttributeNames() {
136 Set<String> allKeys = null;
137 if (attrMap.isEmpty() != true) {
138 allKeys = attrMap.keySet();
139 }
140 return allKeys != null ? allKeys.toArray(new String[allKeys.size()]) : null;
141 }
142
143
144 /**
145 * Gets the attribute values.
146 *
147 * @param attrName attr name
148 * @return String[]
149 */
150 public String[] getAttributeValues(String attrName) {
151 String[] results = null;
152 if (attrMap.containsKey(attrName)) {
153 JSONArray data = attrMap.get(attrName);
154 if (data != null) {
155 results = getStringArray(data);
156 }
157 }
158 return results;
159 }
160
161
162 /**
163 * Gets the string array.
164 *
165 * @param jsArray js array
166 * @return String[]
167 */
168 private String[] getStringArray(JSONArray jsArray) {
169 String[] sResults = new String[jsArray.length()];
170 for (int i = 0; i < jsArray.length(); i++) {
171 sResults[i] = jsArray.getString(i);
172 }
173 return sResults;
174 }
175
176
177 /**
178 * Convert stream to string.
179 *
180 * @param is is
181 * @return String
182 */
183 private String convertStreamToString(InputStream is) {
184 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
185 StringBuffer sb = new StringBuffer();
186 String line = null;
187 try {
188 while ((line = reader.readLine()) != null) {
189 sb.append(line + "\n");
190 }
191 }
192 catch (IOException e) {
193 e.printStackTrace();
194 }
195 try {
196 is.close();
197 }
198 catch (IOException e) {
199 // TODO Auto-generated catch block
200 e.printStackTrace();
201 }
202 return sb.toString();
203 }
204}