none
How to select the same date range from 2001-2015 from a text file RRS feed

  • Question

  • I have a text file that lists weather data from years 1900-2015.  I need to only select
    a specific date range for each year from 2001 to 2015.


    How do I specify a date range, for example, I only want daily data from 5/24/2006 through 10/20/2006?

    Another issue: how to select specify date range that spans multiple years? For example,

    from 11/25/2004 to 5/5/2005?

    So far here's what I'm doing:

    Do While wReader.EndOfStream = False
    		wLine = wReader.ReadLine()
    
                    yr = CInt(Mid(wLine, 1, 6))
                    mo = CInt(Mid(wLine, 7, 4))
                    dy = CInt(Mid(wLine, 11, 4))
    
                    tmax = CSng(Mid(wLine, 21, 6))
                    tmin = CSng(Mid(wLine, 27, 6))
                    tave = (tmax + tmin) / 2
    
    		Dim plant_date = PlantMth + PlantDay / 100  ' format month.day 
                	Dim harvest_date = HarvMth + HarvDay / 100 ' format month.day 
                    mydate = mo + dy / 100 ' format month.day 
                    currDate = New DateTime(yr, mo, dy)
    
                    'Get date range: plant date to harvest date
                    If yr > 2000 And yr <= 2015 Then
                        If (yr = PlantYr4Digit And yr = HarvestYr4Digit) And (mydate >= plant_date And mydate <= harvest_date) Then
    
                            'Calculate daily PHU
                            phu_today = tave - CropMinTemp   'why it's reading at 5/25/2006???
                            If phu_today < 0 Then phu_today = 0 ' negative PHUs not allowed
    
                            phu_oper_cum(i) = phu_today
    
                            If (i = 0) Then
                                phu_oper_running_total(i) = phu_oper_running_total(i) + phu_today
                            Else
                                phu_oper_running_total(i) = phu_oper_running_total(i - 1) + phu_today
                            End If
    
                            sum = dt_oper.AsEnumerable().Sum(Function(row) row.Field(Of Single)("phu"))
                            dt_oper.Rows.Add(phu_today, sum)
    
                            i = i + 1
                            If i > growingDays Then i = growingDays
    
                        End If
    		End If
    	Loop

    Sample text file (Fixed-Width)

      2001  06  28   0.0029.51019.80000.94
      2001  06  29   0.0032.06021.01000.04
      2001  06  30   0.0031.17022.16008.16
      2001  07  01   0.0029.81022.14001.27
      2001  07  02   0.0031.82022.43000.12
      2001  07  03   0.0030.43022.74015.08
      2001  07  04   0.0030.88023.07006.87
      2001  07  05   0.0030.72023.97003.27
      2001  07  06   0.0031.64023.30000.05
      ....etc....
      2006  04  29   0.0026.02017.55000.01
      2006  04  30   0.0024.42020.77004.72
      2006  05  01   0.0025.84020.11001.23
      2006  05  02   0.0026.97017.48000.00
      2006  05  03   0.0027.65016.41000.03
      2006  05  04   0.0028.45018.00000.04
      2006  05  05   0.0028.92019.59000.27
      ....etc...

    My If statements don't seem to hold true for each year.  Can't figure out how to fix it.

    Appreciate any help.



    Marilyn Gambone

    Friday, June 14, 2019 8:50 AM

Answers

  • Hi,

    I made a demo, use the date difference to traverse

    Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim t As DateTime = Convert.ToDateTime("Text file first day date") Dim t1 As DateTime = Convert.ToDateTime("5/25/2006") Dim t2 As DateTime = Convert.ToDateTime("10/20/2006") Dim strlist As String() Dim startdays As Integer = t.Subtract(t1).Duration.Days Dim enddays As Integer = t1.Subtract(t2).Duration().Days Using sr As StreamReader = New StreamReader("D:\test.txt") strlist = sr.ReadToEnd.Split(vbCrLf) End Using For i = startdays To startdays + enddays strlist(i) Next End Sub End Class


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by deskcheck1 Saturday, June 15, 2019 9:14 AM
    Friday, June 14, 2019 9:55 AM
    Moderator
  • Hello,

    You could place data from the text file into a list then query the list. Here is conceptual example.

    1. The Between part could be simple item.TheDate.Date >= #6/29/2001# AndAlso TheDate.Date <= #7/1/2001#

    2. If the data is suspect to bad data then when creating the date use Date.TryParse.


    Public Class WeatherData
        Public Sub New(pYear As String, pMonth As String, pDay As String, pData As String)
            TheDate = New DateTime(CInt(pYear), CInt(pMonth), CInt(pDay))
            Data = pData
        End Sub
        Public Property TheDate() As Date
        Public Property Data() As String
    
        Public Overrides Function ToString() As String
            Return TheDate.ToShortDateString()
        End Function
    End Class

    Extension method

    Public Module ComparableExtensions
        <Runtime.CompilerServices.Extension>
        Public Function Between(Of T)(sender As IComparable(Of T), minimumValue As T, maximumValue As T) As Boolean
            Return sender.CompareTo(minimumValue) >= 0 AndAlso sender.CompareTo(maximumValue) <= 0
        End Function
    End Module

    Form code

    Imports System.IO
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt")
            Dim lines = File.ReadAllLines(fileName)
            Dim weatherList =
                    (
                    From line In lines
                    Let year = line.Substring(0, 4)
                    Let month = line.Substring(6, 2)
                    Let day = line.Substring(10, 2)
                    Let data = line.Substring(15, 21) Select New WeatherData(year, month, day, data)).ToList()
    
            Dim selectedDatesList = weatherList.
                    Where(Function(item) item.TheDate.Date.Between(#6/29/2001#, #7/1/2001#)).
                    ToList()
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by deskcheck1 Saturday, June 15, 2019 9:14 AM
    Friday, June 14, 2019 12:07 PM
    Moderator

All replies

  • Hi,

    I made a demo, use the date difference to traverse

    Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim t As DateTime = Convert.ToDateTime("Text file first day date") Dim t1 As DateTime = Convert.ToDateTime("5/25/2006") Dim t2 As DateTime = Convert.ToDateTime("10/20/2006") Dim strlist As String() Dim startdays As Integer = t.Subtract(t1).Duration.Days Dim enddays As Integer = t1.Subtract(t2).Duration().Days Using sr As StreamReader = New StreamReader("D:\test.txt") strlist = sr.ReadToEnd.Split(vbCrLf) End Using For i = startdays To startdays + enddays strlist(i) Next End Sub End Class


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by deskcheck1 Saturday, June 15, 2019 9:14 AM
    Friday, June 14, 2019 9:55 AM
    Moderator
  • Hello,

    You could place data from the text file into a list then query the list. Here is conceptual example.

    1. The Between part could be simple item.TheDate.Date >= #6/29/2001# AndAlso TheDate.Date <= #7/1/2001#

    2. If the data is suspect to bad data then when creating the date use Date.TryParse.


    Public Class WeatherData
        Public Sub New(pYear As String, pMonth As String, pDay As String, pData As String)
            TheDate = New DateTime(CInt(pYear), CInt(pMonth), CInt(pDay))
            Data = pData
        End Sub
        Public Property TheDate() As Date
        Public Property Data() As String
    
        Public Overrides Function ToString() As String
            Return TheDate.ToShortDateString()
        End Function
    End Class

    Extension method

    Public Module ComparableExtensions
        <Runtime.CompilerServices.Extension>
        Public Function Between(Of T)(sender As IComparable(Of T), minimumValue As T, maximumValue As T) As Boolean
            Return sender.CompareTo(minimumValue) >= 0 AndAlso sender.CompareTo(maximumValue) <= 0
        End Function
    End Module

    Form code

    Imports System.IO
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt")
            Dim lines = File.ReadAllLines(fileName)
            Dim weatherList =
                    (
                    From line In lines
                    Let year = line.Substring(0, 4)
                    Let month = line.Substring(6, 2)
                    Let day = line.Substring(10, 2)
                    Let data = line.Substring(15, 21) Select New WeatherData(year, month, day, data)).ToList()
    
            Dim selectedDatesList = weatherList.
                    Where(Function(item) item.TheDate.Date.Between(#6/29/2001#, #7/1/2001#)).
                    ToList()
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by deskcheck1 Saturday, June 15, 2019 9:14 AM
    Friday, June 14, 2019 12:07 PM
    Moderator
  • Hi,

    Thank you for this.  I realized I wasn't clear about the data range selection.  I followed your codes and it did give me the correct range (e.g., 5/24/2006 to 10/20/2006.

    However, I also need to calculate data for the SAME date range for years 2001, 2002, through 2015.  So how do I make it also get the SAME range for each of the years 2001 through 2015?

    Appreciate your help.



    Marilyn Gambone

    Friday, June 14, 2019 1:30 PM
  • Hi,

    Thanks.  I'm following your suggestion.  The codes Alex provided is what I'm using right now.  Thanks for this, though.  I may use it, too.


    Marilyn Gambone

    Friday, June 14, 2019 1:32 PM
  • Hi to Alex and Karen,

    Thanks for your help.  I'm not quite there yet but I'd like to thank you both for pointing me to the right direction.  I'm using both of your suggestions and still trying to work things out.  I still need to get the same range for each year.  Still, thanks for the big help.


    Marilyn Gambone

    Saturday, June 15, 2019 9:16 AM