I want python to process the csv file data.

Asked 2 years ago, Updated 2 years ago, 59 views

I'm thinking of reading the following csv files and organizing the data.

Downloaded time: 11/11/2019 16:04:33                  

                staying overnight at a temple
Wind direction (m/s) per hour (month/day)

2016 12 23 55 9.6 South-southeast
2016 12 23 6 9.8 southeast
2016 12 23 7 10.6 East
2016 12 23 8 10.4 Northeast
2016 12 23 9 10.5 West
2016 12 23 10 9.1 West
2016 12 23 11 8.1 West-northwest
2016 12 23 12 7.8 West-northwest
2016 12 23 13 6.8 West-northwest
2016 12 23 14 6.3 Northwest
2016 1223 156.2 Northwest
2016 12 23 166.5 North-West
2016 12 23 17 6.3 North-West
2016 12 23 18 6.3 North
2016 12 23 19 5.4 Southwest
2016 12 23 20 3.9 West-Southwest
2016 12 23 214 South-West
2016 12 23 22 4.6 South
2016 12 23 23 24 South


How do I do this? ➀A value of wind speed satisfying a specific wind direction is squared.
ex) For the above data
The value of wind speed satisfying south-southwest, southwest, west-southwest, west-northwest, northwest, north-northwest, north-northwest in 16 directions is squared. (10.5)^2+(9.1)^2+(8.1)^2+(7.8)^2+(6.8)^2+(5.3)^2+(6.2)^2+...

次 Next, we would like to make angle correction, so depending on the wind direction, cos を is multiplied by the square value of the wind speed
ex) For the above data
·The wind direction is divided into 16 directions, so the interval between the angles of the adjacent directions is 22.5°

with the west facing the front (in this data, the west facing the west) 22.5° for west-northwest and west-southwest, 45° for northwest and southwest, 67.5°

for north-northwest and south-southwest

·Based on the angle based on the west, multiply the square value of the wind speed by cosθ each
(10.5)^2×cos(0)+(9.1)^2×cos(0)+
(8.1)^2×cos(22.5)+(7.8)^2×cos(22.5)+(6.8)^2×cos(22.5)+
(6.3)^2×cos(45)+(6.2)^2×cos(45)+
(6.5)^2×cos(67.5)+(6.3)^2×cos(67.5)+
(5.4)^2×cos(45)+
(3.9)^2×cos(22.5)+
(4)^2×cos (67.5)

Add all values of の 値.

I would like to do this.

Squares the value of the wind speed that currently satisfies a specific wind direction and adds it together
It moved to the point where
➁I'm worried because I can't do the process of applying cos(22.5) to certain wind directions and cos(45) to certain wind directions.
Also, this data is based on the west, but other data have different standards such as east, southeast, and north-northeast, so we would like to create something that can be programmed to match the data we process.

The current program is shown below.
I'm sorry for the long sentence.
Could you tell me how to do this?
I look forward to hearing from you.

import csv

f=open("Terayuki test.csv", "r")
reader=csv.reader(f)
next(reader);next(reader);next(reader);next(reader);next(reader)

Direction = ['South-South-West', 'South-West', 'West-West', 'West-West', 'North-West', 'North-West', 'North-West']

sum = 0
For row in reader:
  if not row[4].strip():# Skip Empty Row
    continue
  colE=float(row[4])
  if(colE>=0) and (row[5] in Direction):
      sum+=power(colE,2)

print(sum)
f.close()

python csv

2022-09-29 22:49

2 Answers

When interpreted as integrating the reference azimuth component value of wind speed (vector), the following ranges are included:

 (reference orientation -(1/2) ,,reference orientation +(1/2) ))

If the reference orientation is "west", it will be from "south-southwest" to "north-northwest" (other orientation, the value of the "west" orientation component will be 0 or less).

Below, we use the Pandas data frame to calculate the integrated values for each reference orientation.

import pandas as pd
from path import cos, pi

Direction_16 = [
  "North", "North-Northeast", "Northeast", "Northeast", "East-Northeast",
  "East", "East-southeast", "Southeast", "South-southeast", "South-southeast",
  "South", "South Southwest", "Southwest", "West Southwest",
  "West", "West-northwest", "North-northwest", "North-northwest"
]
l=len(Direction_16)
Cosine= [0.0 if i%8==4 else cos(i*2*pi/l) for i in range(l)]

defload_csv(f):
  df = pd.read_csv(
    f, skipinitialspace=True, skiprows=4,
    names = ['year', 'month', 'day', 'hour', 'velocity', 'direction',
    converters = {'direction':str.trip}
  )

  return df.assign(
    sq_velocity=df.velocity**2,
    dir_idx=pd.Series(pd.Category(
      df.direction, categories=Direction_16).cat.codes,
    )

def calc(df,src):
  ## Get region: (src-(1/2) ,, src+(1/2) ))
  sidx=Direction_16.index(src)
  beg=(sidx-4)+l)%l
  end=(sidx+4)%l
  op='and'if beg<end else'or'

  return df.query(f'{beg}<dir_idx{op}dir_idx<{end}')\
           .apply(
             lambdar:
               pd.Series(((
                 r.sq_velocity,
                 r.sq_velocity* Cosine [abs(r.dir_idx-sidx)]),
             axis=1)\
           .sum()

if__name__=='__main__': 
  df = load_csv('test.csv')
  print(f'reference azimuth sum squared projection sum')
  for in Direction_16:
    sq,proj=calc(df,d)
    pad='*(4-len(d))
    print(f'{pad+d+pad}{sq:7.2f}{proj:9.2f}')

=>
reference azimuth sum of squares
   480.61 313.21 North
 North-Northeast 420.28 267.43
  Northeast 342.15 247.03
 Tohoku-East 356.255.68
   East 408.72292.02
 East-southeast 445.88 313.32
  southeast 353.72 293.03
 382.88280.69 south-southeast
   285.73 231.44 south
 478.79 263.83 south-southwest
  Southwest 555.44 322.14
 West-southwest 541.41398.06
   586.19480.00 west
 609.88518.28 west-northwest
  580.72483.78 northwest
 North-northwest 673.67428.17


2022-09-29 22:49

Thank you for your reply.
I couldn't move it the way I thought it would be difficult.

So I'm thinking of moving it in this way right now.
Here's how it works:

import csv
import path

cos0=math.cos(math.radians(0))
cos225 = path.cos(math.radians (22.5))
cos45=math.cos(math.radians(45))
cos675 = path.cos(math.radians (67.5))

f = open("test.csv", "r")
reader=csv.reader(f)
next(reader);next(reader);next(reader);next(reader);next(reader)

Direction = ['South-South-West', 'South-West', 'West-West', 'West-West', 'North-West', 'North-West', 'North-West']

sum = 0
For row in reader:
  if not row[4].strip():# Skip Empty Row
    continue
  colE=float(row[4])

After this
➀ All colE squared

D
in seven directions of Direction Multiply colE (squared) with west orientation by cos0
Multiply colE (squared) whose azimuth is west-southwest and west-northwest by cos 225
COLE (squared) whose azimuth is southwest and northwest multiplied by cos45
Multiply colE (squared) in south-southwest and north-northwest by cos675

2 Add the squared value to the corrected value

I'm thinking that I can do it in that way.

However, it is not possible to make corrections to specific wind directions and add all the corrected values.
Please reply.


2022-09-29 22:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.