質問編集履歴

5

プログラム変更

2017/01/28 17:46

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -190,44 +190,78 @@
190
190
 
191
191
  return wind
192
192
 
193
-
194
-
195
- def exchange(rad):
193
+ def exchange_cos(rad):
196
194
 
197
195
  test=float(rad)
198
196
 
199
197
  test=math.radians(test)
200
198
 
201
- an=math.sin(test)
199
+ hi=math.cos(test)
202
-
200
+
203
- return an
201
+ return hi
204
-
205
-
206
-
202
+
203
+
204
+
207
- def wind_exchange_sin(wind_sd,wind_s):
205
+ def wind_exchange_cos(wind_s,wind):
208
-
209
-
210
206
 
211
207
  for k,v in sincos_codes:
212
208
 
213
- wind = re.sub('^%s$'%k,v,wind_s.strip())
209
+ wind = re.sub('^%s$'%k,v,wind.strip())
214
-
215
- wind_1 = wind
210
+
216
-
217
- #print wind_1
218
-
219
- wind_1=exchange(wind_1)
211
+ wind=exchange_cos(wind)
212
+
220
-
213
+ #test=math.radians(wind)
214
+
215
+ #hi=math.sin(test)
216
+
221
- wind_1=wind_sd*wind_1
217
+ wind=wind_s*wind
222
-
218
+
223
- #print str(wind_1).encode('utf-8')
219
+ #print str(wind).encode('utf-8')
224
220
 
225
221
  return str(wind).encode('utf-8')
226
222
 
227
223
 
228
224
 
225
+ def exchange_sin(rad):
226
+
227
+ test=float(rad)
228
+
229
+ test=math.radians(test)
230
+
231
+ hi=math.sin(test)
232
+
233
+ return hi
234
+
235
+
236
+
237
+ def wind_exchange_sin(wind_s,wind):
238
+
239
+ for k,v in sincos_codes:
240
+
241
+ wind = re.sub('^%s$'%k,v,wind.strip())
242
+
243
+ wind=exchange_sin(wind)
244
+
245
+ #test=math.radians(wind)
246
+
247
+ #hi=math.sin(test)
248
+
249
+ wind=wind_s*wind
250
+
251
+ #print str(wind).encode('utf-8')
252
+
253
+ return str(wind).encode('utf-8')
254
+
255
+
256
+
229
257
  def encode_weather(weather):
230
258
 
259
+ # 日本語があると何かと面倒なのでコード値にしてしまう。彦根市は上記のルールだけで大丈夫だったが、他の地域や期間に対応するにはパターンを増やす必要があるかも。
260
+
261
+ # 「晴れ一時雨後曇り」とか後ろについてくるやつは面倒くさいので除去する。
262
+
263
+
264
+
231
265
  weather=ame_yuki(weather)
232
266
 
233
267
  wcode = re.sub( u'(一時|時々|後|、).*','',weather.strip())
@@ -320,14 +354,30 @@
320
354
 
321
355
 
322
356
 
323
- out_format = ['date','temp_max','temp_min','total_rainfall','avg_windspeed','avg_atom','avg_humid','avg_temp', 'hours_of_sunshine','highest_snow_depth',(wind_exchange_sin(max_windspeed,max_wind_direction)),('max_wind_direction', wind_direction),'max_instantaneous_windspeed',('max_instantaneous_wind_direction',wind_direction),'avg_vapor_pressure','min_atom','avg_humid','min_humid','10min_avg_cloudcover','total_snow_depth','total_solar_insolation','1hour_max_rainfall',('weather', encode_weather ),]
324
-
325
-
357
+
358
+
359
+ out_format = ['date','temp_max','temp_min','total_rainfall',
360
+
361
+ 'avg_windspeed','avg_atom','avg_humid','avg_temp', 'hours_of_sunshine',
362
+
363
+ 'highest_snow_depth','max_windspeed',
364
+
365
+ (wind_exchange_sin(max_windspeed,max_wind_direction)),(wind_exchange_cos(max_windspeed,max_wind_direction)),
366
+
367
+ 'max_instantaneous_windspeed',('max_instantaneous_wind_direction',wind_exchange_sin),('max_instantaneous_wind_direction',wind_exchange_cos),
368
+
369
+ 'avg_vapor_pressure','min_atom','avg_humid','min_humid','10min_avg_cloudcover',
370
+
371
+ 'total_snow_depth','total_solar_insolation','1hour_max_rainfall',('weather', encode_weather ),]
372
+
373
+
374
+
375
+
376
+
377
+
326
378
 
327
379
  for file in sys.argv[1:]:
328
380
 
329
381
  format_file_main(file,in_format,out_format)
330
382
 
331
-
332
-
333
383
  ```

4

修正

2017/01/28 17:46

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -320,7 +320,7 @@
320
320
 
321
321
 
322
322
 
323
- out_format = ['date','temp_max','temp_min','total_rainfall','avg_windspeed','avg_atom','avg_humid','avg_temp', 'hours_of_sunshine','highest_snow_depth',(wind_exchange_sin('max_windspeed','max_wind_direction')),('max_wind_direction', wind_direction),'max_instantaneous_windspeed',('max_instantaneous_wind_direction',wind_direction),'avg_vapor_pressure','min_atom','avg_humid','min_humid','10min_avg_cloudcover','total_snow_depth','total_solar_insolation','1hour_max_rainfall',('weather', encode_weather ),]
323
+ out_format = ['date','temp_max','temp_min','total_rainfall','avg_windspeed','avg_atom','avg_humid','avg_temp', 'hours_of_sunshine','highest_snow_depth',(wind_exchange_sin(max_windspeed,max_wind_direction)),('max_wind_direction', wind_direction),'max_instantaneous_windspeed',('max_instantaneous_wind_direction',wind_direction),'avg_vapor_pressure','min_atom','avg_humid','min_humid','10min_avg_cloudcover','total_snow_depth','total_solar_insolation','1hour_max_rainfall',('weather', encode_weather ),]
324
324
 
325
325
 
326
326
 

3

プログラム修正

2017/01/28 16:02

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -214,11 +214,11 @@
214
214
 
215
215
  wind_1 = wind
216
216
 
217
- print wind_1
217
+ print wind_1
218
-
218
+
219
- #wind_1=exchange(wind_1)
219
+ wind_1=exchange(wind_1)
220
-
220
+
221
- #wind_1=wind_sd*wind_1
221
+ wind_1=wind_sd*wind_1
222
222
 
223
223
  #print str(wind_1).encode('utf-8')
224
224
 

2

プログラム追加

2017/01/28 15:52

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
@@ -14,8 +14,68 @@
14
14
 
15
15
  ```python
16
16
 
17
+ # coding: utf-8
18
+
19
+ import codecs
20
+
17
21
  import math
18
22
 
23
+
24
+
25
+ class JMACSVReader(object):
26
+
27
+ def __init__(self, filename,format,encoding='shift_jisx0213', header_ofs=6):
28
+
29
+ self.format = format
30
+
31
+ self._istream = codecs.open(filename, 'r', encoding)
32
+
33
+ for i in range(header_ofs):
34
+
35
+ self._istream.readline() # ヘッダを読み飛ばす
36
+
37
+
38
+
39
+ def _parse_line(self,line):
40
+
41
+ record = {}
42
+
43
+ for k,v in zip(self.format, line.split(',')):
44
+
45
+ if k is not None:
46
+
47
+ record[k] = v
48
+
49
+ return record
50
+
51
+
52
+
53
+ def __iter__(self):
54
+
55
+ for line in self._istream:
56
+
57
+ yield(self._parse_line(line))
58
+
59
+
60
+
61
+ weather_codes = [
62
+
63
+ (u'薄', 'Usu'),(u'大', 'Oo'),
64
+
65
+ (u'暴風雨', 'Bofu'), (u'快晴', 'Kaisei'),
66
+
67
+ (u'雨', 'Ame'),(u'晴', 'Hare'),
68
+
69
+ (u'雪', 'Yuki'), (u'曇', 'Kumori'),
70
+
71
+ (u'みぞれ', 'Mizore'), (u'ひょう', 'Hyo'),
72
+
73
+ (u'霧', 'Kiri'),
74
+
75
+ ]
76
+
77
+
78
+
19
79
  sincos_codes = [
20
80
 
21
81
  (u'北','0'),(u'東','90'),
@@ -38,15 +98,109 @@
38
98
 
39
99
 
40
100
 
101
+ wind_direction_codes = [
102
+
103
+ (u'北', 'N'),(u'東','S'),
104
+
105
+ (u'南','S'),(u'西','N'),
106
+
107
+ (u'北北東','N'),(u'北東','N'),
108
+
109
+ (u'東北東','N'),(u'東南東','S'),
110
+
111
+ (u'南東','S'),(u'南南東','S'),
112
+
113
+ (u'南南西','S'),(u'南西','S'),
114
+
115
+ (u'西南西','E'),(u'西北西','N'),
116
+
117
+ (u'北西','N'),(u'北北西','N'),
118
+
119
+ ]
120
+
121
+
122
+
123
+ def ame(weather):
124
+
125
+ yuki_length=weather.find(u'雪')
126
+
127
+ ame_length=weather.find(u'雨')
128
+
129
+ if(yuki_length==-1):
130
+
131
+ weather= u'雨'
132
+
133
+ return weather
134
+
135
+ if(ame_length<yuki_length):
136
+
137
+ weather= u'雨'
138
+
139
+ return weather
140
+
141
+ else:
142
+
143
+ weather= u'雪'
144
+
145
+ return weather
146
+
147
+
148
+
149
+ def ame_yuki(weather):
150
+
151
+ if u'雨' in weather:
152
+
153
+ weather=ame(weather)
154
+
155
+ return weather
156
+
157
+ elif u'雪' in weather:
158
+
159
+ weather= u'雪'
160
+
161
+ return weather
162
+
163
+ elif u'みぞれ' in weather:
164
+
165
+ weather= u'雪'
166
+
167
+ return weather
168
+
169
+ elif u'ひょう' in weather:
170
+
171
+ weather= u'雪'
172
+
173
+ return weather
174
+
175
+ else:
176
+
177
+ return weather
178
+
179
+
180
+
181
+
182
+
183
+ def wind_direction(wind):
184
+
185
+
186
+
187
+ for k,v in wind_direction_codes:
188
+
189
+ wind = re.sub('^%s$'%k,v,wind.strip())
190
+
191
+ return wind
192
+
193
+
194
+
41
195
  def exchange(rad):
42
196
 
43
197
  test=float(rad)
44
198
 
45
199
  test=math.radians(test)
46
200
 
47
- hi=math.sin(test)
201
+ an=math.sin(test)
48
-
202
+
49
- return hi
203
+ return an
50
204
 
51
205
 
52
206
 
@@ -58,23 +212,121 @@
58
212
 
59
213
  wind = re.sub('^%s$'%k,v,wind_s.strip())
60
214
 
215
+ wind_1 = wind
216
+
217
+ print wind_1
218
+
61
- wind=exchange(wind)
219
+ #wind_1=exchange(wind_1)
62
-
63
- #test=math.radians(wind)
220
+
64
-
65
- #hi=math.sin(test)
66
-
67
- wind=wind_sd*wind
221
+ #wind_1=wind_sd*wind_1
68
-
222
+
69
- print str(wind).encode('utf-8')
223
+ #print str(wind_1).encode('utf-8')
70
224
 
71
225
  return str(wind).encode('utf-8')
72
226
 
73
227
 
74
228
 
75
- out_format =['date',(wind_exchange_sin('max_windspeed','max_wind_direction')),]
76
-
77
-
229
+ def encode_weather(weather):
230
+
231
+ weather=ame_yuki(weather)
232
+
233
+ wcode = re.sub( u'(一時|時々|後|、).*','',weather.strip())
234
+
235
+
236
+
237
+ for k,v in weather_codes:
238
+
239
+ wcode = wcode.replace(k,v)
240
+
241
+ return wcode
242
+
243
+
244
+
245
+ def format_file_main(filename, in_format, out_format):
246
+
247
+ jmacsv = JMACSVReader(filename, in_format)
248
+
249
+ for record in jmacsv:
250
+
251
+ line = []
252
+
253
+ for of in out_format:
254
+
255
+ if isinstance(of,tuple):
256
+
257
+ line.append(of[1](record[of[0]]))
258
+
259
+ else:
260
+
261
+ line.append(record[of])
262
+
263
+ print(u'\t'.join(line).encode('utf-8'))
264
+
265
+
266
+
267
+ if __name__=='__main__':
268
+
269
+ # usage: jmaformat.py data.csv
270
+
271
+ import sys
272
+
273
+ import re
274
+
275
+ in_format = [
276
+
277
+ 'date',
278
+
279
+ 'temp_max',None,None,
280
+
281
+ 'temp_min',None,None,
282
+
283
+ 'weather',None,None,
284
+
285
+ 'total_rainfall',None,None,None,
286
+
287
+ 'avg_windspeed',None,None,
288
+
289
+ 'avg_atom',None,None,
290
+
291
+ 'avg_humid',None,None,
292
+
293
+ 'avg_temp',None,None,
294
+
295
+ 'hours_of_sunshine',None,None,None,
296
+
297
+ 'highest_snow_depth',None,None,None,
298
+
299
+ 'max_windspeed',None,'max_wind_direction',None,None,
300
+
301
+ 'max_instantaneous_windspeed',None,'max_instantaneous_wind_direction',None,None,None,None,None,
302
+
303
+ 'avg_vapor_pressure',None,None,
304
+
305
+ 'min_atom',None,None,
306
+
307
+ 'avg_humid',None,None,
308
+
309
+ 'min_humid',None,None,None,
310
+
311
+ '10min_avg_cloudcover',None,None,
312
+
313
+ 'total_snow_depth',None,None,None,
314
+
315
+ 'total_solar_insolation',None,None,
316
+
317
+ '1hour_max_rainfall',None,None,None,
318
+
319
+ ]
320
+
321
+
322
+
323
+ out_format = ['date','temp_max','temp_min','total_rainfall','avg_windspeed','avg_atom','avg_humid','avg_temp', 'hours_of_sunshine','highest_snow_depth',(wind_exchange_sin('max_windspeed','max_wind_direction')),('max_wind_direction', wind_direction),'max_instantaneous_windspeed',('max_instantaneous_wind_direction',wind_direction),'avg_vapor_pressure','min_atom','avg_humid','min_humid','10min_avg_cloudcover','total_snow_depth','total_solar_insolation','1hour_max_rainfall',('weather', encode_weather ),]
324
+
325
+
326
+
327
+ for file in sys.argv[1:]:
328
+
329
+ format_file_main(file,in_format,out_format)
78
330
 
79
331
 
80
332
 

1

タグ追加

2017/01/28 15:49

投稿

usuallyi
usuallyi

スコア41

test CHANGED
File without changes
test CHANGED
File without changes