我有以下代码,这些代码可以读取csv文件并为我运行的某些模拟创建文本(ied)文件。在输出文本(IED文件)中。时间和流量数据被写入。每个站点的标题也被写入。站点标题是从csv文件获得的。因此,例如,读取Kelwell Drain,25小时storm.csv的ReFH2输出,并生成文件ReFH Total flow Kelwell Drain,25小时storm.csv。然后用时间和流量数据写入一个ied文本文件
QTBDY
Kelwell Drain
106 0.000 0.000 HOURS EXTEND LINEAR 0.000
0.369 0.000
0.362 1.000
0.359 2.000
0.359 3.000
0.364 4.000
0.374 5.000
0.392 6.000
0.418 7.000
0.453 8.000
0.496 9.000
0.552 10.000
0.622 11.000
0.710 12.000
0.819 13.000
0.945 14.000
1.083 15.000
1.222 16.000
1.352 17.000
1.463 18.000
1.544 19.000
1.589 20.000
1.599 21.000
1.582 22.000
1.542 23.000
1.486 24.000
1.420 25.000
1.349 26.000
1.273 27.000
1.194 28.000
1.114 29.000
1.034 30.000
0.953 31.000
0.874 32.000
0.801 33.000
0.733 34.000
0.672 35.000
0.619 36.000
0.574 37.000
0.538 38.000
0.507 39.000
0.481 40.000
0.459 41.000
0.440 42.000
0.424 43.000
0.410 44.000
0.398 45.000
0.387 46.000
0.377 47.000
0.369 48.000
0.000 49.000
0.000 50.000
0.000 51.000
0.000 52.000
0.000 53.000
0.000 54.000
0.000 55.000
0.000 56.000
0.000 57.000
0.000 58.000
0.000 59.000
0.000 60.000
0.000 61.000
0.000 62.000
0.000 63.000
0.000 64.000
0.000 65.000
0.000 66.000
0.000 67.000
0.000 68.000
0.000 69.000
0.000 70.000
0.000 71.000
0.000 72.000
0.000 73.000
0.000 74.000
0.000 75.000
0.000 76.000
0.000 77.000
0.000 78.000
0.000 79.000
0.000 80.000
0.000 81.000
0.000 82.000
0.000 83.000
0.000 84.000
0.000 85.000
0.000 86.000
0.000 87.000
0.000 88.000
0.000 89.000
0.000 90.000
0.000 91.000
0.000 92.000
0.000 93.000
0.000 94.000
0.000 95.000
0.000 96.000
0.000 97.000
0.000 98.000
0.000 99.000
0.000 100.000
0.000 101.000
0.000 102.000
0.000 103.000
0.000 104.000
0.000 105.000
在这里可以看到Kelwell Drain一词之间有一个空格。我不要这个空间。我想说的是KelwellDrain。我不确定如何尝试以下代码
station_name = Replace(station_name, " ", "")
但它不起作用。
完整代码在这里
Option Explicit
Dim ReFH_files() As Variant
Dim num_ReFH_files As Integer
Dim Stations() As Variant
Dim num_stations As Integer
Dim Storm_Lengths() As Variant
Dim num_storm_lengths As Integer
Dim Total_Flow_files() As Variant
Dim WorkingFolder As String
Dim Return_Periods() As Integer
Dim num_return_periods As Integer
Dim root_name_ReFH As String
Dim root_name_TotalFlow As String
Dim root_name_StormLength As String
Dim header_text_to_match As String
Dim start_row As Integer
Dim start_col As Integer
Sub ProcessReFH_Files()
' Get the data and write it on the sheet for info
GetSummaryReFHData
' Create the Total Flow files from the refh files
ExtractTotalFlows
' Now create the ied files
CreateIEDFiles
End Sub
Sub SetWorkingFolder()
WorkingFolder = GetFolder
ActiveSheet.Cells(4, 11).Value = WorkingFolder
End Sub
Sub CreateIEDFiles()
Dim output_filename As String
Dim input_filename As String
Dim total_flow_filename As String
Dim fileOut As Integer
Dim fileIn As Integer
Dim station_name As String
Dim storm_length As String
Dim return_period As Integer
Dim i As Integer, j As Integer, k As Integer
Dim dataLine As String, outputLine As String
Dim dataArray As Variant
Dim time As Double, strTime As String
Dim flow As Double, strFlow As String
Dim num_data As Integer
' For each storm length create one ied file per return period
For i = 0 To num_storm_lengths - 1
storm_length = Storm_Lengths(i)
Debug.Print storm_length
For j = 0 To num_return_periods - 1
return_period = Return_Periods(j + 1)
Debug.Print return_period
' output file name
output_filename = root_name_StormLength & storm_length & "_Return_Period_" & return_period & ".ied"
Debug.Print output_filename
fileOut = FreeFile()
' Open the output file
output_filename = WorkingFolder & "\" & output_filename
Open output_filename For Output As #fileOut
' loop through the station files
For k = 0 To num_stations - 1
station_name = Stations(k)
' write the station standard station data
Print #fileOut, "QTBDY"
station_name = Replace(station_name, " ", "")
Print #fileOut, Trim(station_name)
total_flow_filename = GetTotalFlowFileName(station_name, storm_length)
Debug.Print total_flow_filename
input_filename = total_flow_filename
fileIn = FreeFile()
' Open the station file for this storm length and read the flow for this return period
input_filename = WorkingFolder & "\" & input_filename
Open input_filename For Input As #fileIn
' count the number of lines (this is very inefficient!
num_data = 0
While Not EOF(fileIn)
Line Input #fileIn, dataLine ' read in data 1 line at a time
If (Len(dataLine) > 1) Then
num_data = num_data + 1
End If
Wend
num_data = num_data - 1
Close #fileIn
' write the data header
'num_data = 120
outputLine = Right(Space(10) & num_data, 10)
outputLine = outputLine & " 0.000 0.000 HOURS EXTEND LINEAR 0.000"
Print #fileOut, outputLine
' open the file again
Open input_filename For Input As #fileIn
' Read the first line
Line Input #fileIn, dataLine
' read the rest of the lines, extracting the time and the flow for this return period
While Not EOF(fileIn)
Line Input #fileIn, dataLine ' read in data 1 line at a time
dataArray = Split(dataLine, ",")
flow = CDbl(dataArray(j + 1))
strTime = dataArray(0)
time = GetDecimalTime(strTime)
' make the numbers 3dp and string 10 characters wide
strFlow = Right(Space(10) & Format(flow, "0.000"), 10)
strTime = Right(Space(10) & Format(time, "0.000"), 10)
outputLine = strFlow & strTime
Print #fileOut, outputLine
Wend
Close #fileIn
Next k
' close the ied file
Close #fileOut
Next j
Next i
End Sub
Sub ExtractTotalFlows()
Dim i As Integer, j As Integer
Dim filename As String, output_filename As String
Dim fileNum As Integer, output_fileNum As Integer
Dim dataLine As String
Dim dataArray As Variant
Dim dataCols() As Integer
Dim num_cols As Integer
Dim outputLine As String
Dim i1 As Integer, i2 As Integer
Dim return_period As String
For i = 0 To num_ReFH_files - 1
filename = WorkingFolder & "\" & ReFH_files(i)
output_filename = WorkingFolder & "\" & Total_Flow_files(i)
fileNum = FreeFile()
Open filename For Input As #fileNum
output_fileNum = FreeFile()
Open output_filename For Output As #output_fileNum
' read the first line
Line Input #fileNum, dataLine ' read in data 1 line at a time
dataArray = Split(dataLine, ",")
' Get the columns where we want data
num_cols = 1
ReDim dataCols(0 To UBound(dataArray))
ReDim Return_Periods(0 To UBound(dataArray))
dataCols(0) = 0 ' time
For j = 0 To UBound(dataArray)
If (InStr(1, dataArray(j), header_text_to_match) > 0) Then
dataCols(num_cols) = j
' Take this opportunity to get the return period of this flow
' Find the first (
i1 = InStr(1, dataArray(j), "(")
' Find the next " "
i2 = InStr(i1 + 1, dataArray(j), " ")
' get the return period integer
return_period = Mid(dataArray(j), i1 + 1, i2 - i1)
Return_Periods(num_cols) = CInt(return_period)
' write it to the summary sheet
ActiveSheet.Cells(start_row + num_cols, start_col + 4).Value = return_period
num_cols = num_cols + 1
End If
Next j
ReDim Preserve dataCols(0 To num_cols - 1)
ReDim Preserve Return_Periods(0 To num_cols - 1)
num_return_periods = num_cols - 1
' Write out this data
outputLine = ""
For j = 0 To num_cols - 1
outputLine = outputLine & """" & dataArray(dataCols(j)) & """"
If (j <> num_cols - 1) Then
outputLine = outputLine & ","
End If
Next j
'Debug.Print outputLine
Print #output_fileNum, outputLine
' Now read and write the data
While Not EOF(fileNum)
Line Input #fileNum, dataLine ' read in data 1 line at a time
dataArray = Split(dataLine, ",")
outputLine = ""
For j = 0 To num_cols - 1
outputLine = outputLine & dataArray(dataCols(j))
If (j <> num_cols - 1) Then
outputLine = outputLine & ","
End If
Next j
Print #output_fileNum, outputLine
Wend
Close #fileNum
Close #output_fileNum
Next i
End Sub
Function GetSummaryReFHData()
Dim i As Integer
Dim i1 As Integer, i2 As Integer
Dim file_name As String
Dim station_name As String
Dim storm_length As String
Dim total_flow_file_name As String
'get the root names
root_name_ReFH = ActiveSheet.Cells(17, 4).Value
root_name_TotalFlow = ActiveSheet.Cells(18, 4).Value
root_name_StormLength = ActiveSheet.Cells(19, 4).Value
header_text_to_match = ActiveSheet.Cells(20, 4).Value
WorkingFolder = ActiveSheet.Cells(4, 11).Value
' read all the names of the files
ReFH_files = listfiles(WorkingFolder, root_name_ReFH)
num_ReFH_files = UBound(ReFH_files) + 1
' extract all of the station names
ReDim Total_Flow_files(0 To num_ReFH_files - 1)
ReDim Stations(0 To num_ReFH_files - 1)
ReDim Storm_Lengths(0 To num_ReFH_files - 1)
For i = 0 To num_ReFH_files - 1
file_name = ReFH_files(i)
' extract the station
' It is the characters from the end of the root name to the comma
i1 = Len(root_name_ReFH) + 1
i2 = InStr(1, file_name, ",")
station_name = Mid(file_name, i1, i2 - i1)
Stations(i) = station_name
' Make the total flow filename from this data
total_flow_file_name = root_name_TotalFlow & Mid(file_name, i1)
Total_Flow_files(i) = total_flow_file_name
' extract the storm length
' It is the characters from the comma to the -hr
i1 = InStr(1, file_name, ",") + 1
i2 = InStr(1, file_name, "-hr")
storm_length = Mid(file_name, i1, i2 - i1)
Storm_Lengths(i) = CInt(storm_length)
Next i
' Get the unique entries from these lists
Stations = ArrayUnique(Stations)
num_stations = UBound(Stations) + 1
Storm_Lengths = ArrayUnique(Storm_Lengths)
num_storm_lengths = UBound(Storm_Lengths) + 1
' Write the info found
Dim myrow As Integer, mycol As Integer
start_row = 7
start_col = 11 ' col K
' Clear any existing data
ActiveSheet.Range(Cells(start_row, start_col), Cells(start_row + 1000, start_col + 50)).Clear
myrow = start_row
ActiveSheet.Cells(myrow, start_col).Value = "Station names"
ActiveSheet.Cells(myrow, start_col).Font.Bold = True
ActiveSheet.Cells(myrow, start_col + 4).Value = "Return Periods"
ActiveSheet.Cells(myrow, start_col + 4).Font.Bold = True
For i = 0 To num_stations - 1
myrow = myrow + 1
ActiveSheet.Cells(myrow, start_col).Value = Stations(i)
Next i
myrow = myrow + 2
ActiveSheet.Cells(myrow, start_col).Value = "Storm lengths"
ActiveSheet.Cells(myrow, start_col).Font.Bold = True
For i = 0 To num_storm_lengths - 1
myrow = myrow + 1
ActiveSheet.Cells(myrow, start_col).Value = Storm_Lengths(i)
ActiveSheet.Cells(myrow, start_col + 1).Value = "hours"
Next i
myrow = myrow + 2
ActiveSheet.Cells(myrow, start_col).Value = "ReFH files"
ActiveSheet.Cells(myrow, start_col).Font.Bold = True
ActiveSheet.Cells(myrow, start_col + 5).Value = "Total Flowfiles"
ActiveSheet.Cells(myrow, start_col + 5).Font.Bold = True
For i = 0 To num_ReFH_files - 1
myrow = myrow + 1
ActiveSheet.Cells(myrow, start_col).Value = ReFH_files(i)
ActiveSheet.Cells(myrow, start_col + 5).Value = Total_Flow_files(i)
Next i
End Function
Function GetTotalFlowFileName(station_name As String, storm_length As String) As String
' returns the Total_Flow filename searching the Total_Flow_files array to match the station name and storm length
On Error GoTo err:
Dim i As Integer
Dim i2 As Integer
Dim station_storm_length_str As String
GetTotalFlowFileName = ""
For i = 0 To num_ReFH_files - 1
station_storm_length_str = station_name & ", " & storm_length
i2 = InStr(1, Total_Flow_files(i), station_storm_length_str)
If (i2 > 0) Then
GetTotalFlowFileName = Total_Flow_files(i)
Exit Function
End If
Next i
err:
i = 0
End Function
答案 0 :(得分:0)
所以我设法解决了这个问题
' loop through the station files
For k = 0 To num_stations - 1
station_name = Stations(k)
' write the station standard station data
Print #fileOut, "QTBDY"
Corr_station_name = Replace(station_name, " ", "")
Print #fileOut, Trim(Corr_station_name)
我刚刚创建了另一个名为corr_station_name的Dim。