I have weather data that was recorded every hour, and location data (X,Y) that
was recorded every 4 hours. I want to know what the temperature was at
location X,Y. The weather data isn't exactly at the same time. So, I
have
written this loop for every location to scan through the weather data looking
for the "closest" in Date/TIME and extracting the data from that
time. The
problem is the way Ive written it, for location #2, it scans through the
weather data but will not allow the closest time information to be assigned
that was assigned for location#1. Say location #1 & 2 are taken within 10
minutes at 6pm and 6:10pm, the closest weather time is 6pm. I can't get it
to
allow the weather data at 6pm as an option for location #2. I kind of set it
up like this because 200 locations into my location data set (say 3 months into
it), I do not want it starting at time 1 from the weather data, when I know
that the closest weather data was just calculated for the last location and
that happens to be 3 months into that data set too. Below is some sample data
and my code. I don't know if this makes sense. I am also concerned about
the
search if
the location dates extend past the weather data
Location data
X Y DateTime
1 2 4/2/2003 18:01:01
3 2 4/4/2003 17:01:33
2 3 4/6/2003 16:03:07
5 6 4/8/2003 15:03:08
3 7 4/10/2003 14:03:06
4 5 4/2/2003 13:02:00
4 5 4/4/2003 12:14:43
4 3 4/6/2003 11:00:56
3 5 4/8/2003 10:02:06
2 4 4/10/2003 9:02:19
Weather Data
DateTime WndSp WndDir Hgt
4/2/2003 17:41:00 8.17 102.86 3462.43
4/2/2003 20:00:00 6.70 106.00 17661.00
4/2/2003 10:41:00 6.18 106.00 22000.00
4/2/2003 11:41:00 5.78 106.00 22000.00
4/2/2003 12:41:00 5.48 104.00 22000.00
4/4/2003 17:53:00 7.96 104.29 6541.00
4/4/2003 20:53:00 6.60 106.00 22000.00
4/4/2003 19:41:00 7.82 105.00 7555.00
4/4/2003 7:41:00 6.62 105.00 14767.50
4/4/2003 8:41:00 6.70 106.00 17661.00
4/4/2003 9:41:00 6.60 106.00 22000.00
4/5/2003 20:41:00 7.38 106.67 11156.67
4/6/2003 18:07:00 7.82 105.00 7555.00
4/6/2003 21:53:00 6.18 106.00 22000.00
4/6/2003 21:41:00 6.62 105.00 14767.50
4/6/2003 4:41:00 7.96 104.29 6541.00
4/6/2003 5:41:00 7.82 105.00 7555.00
4/6/2003 6:41:00 7.38 106.67 11156.67
4/8/2003 18:53:00 7.38 106.67 11156.67
4/8/2003 22:53:00 5.78 106.00 22000.00
4/8/2003 1:41:00 5.78 106.00 22000.00
4/8/2003 2:41:00 5.48 104.00 22000.00
4/8/2003 3:41:00 8.17 102.86 3462.43
4/10/2003 19:53:00 6.62 105.00 14767.50
4/10/2003 23:53:00 5.48 104.00 22000.00
4/10/2003 22:41:00 6.70 106.00 17661.00
4/10/2003 23:41:00 6.60 106.00 22000.00
4/10/2003 0:41:00 6.18 106.00 22000.00
4/11/2003 17:41:00 8.17 102.86 3462.43
4/12/2003 18:41:00 7.96 104.29 6541.0
#####code:
SortLoc = loc[order(loc$DateTime),] #sorting the location data
SortWeath = weather[order(weather$DateTime),] #sorting the weather data
####
weathrow =1
for (i in 1:nrow(SortLoc)) {
t = 0
while (t < 1){
timedif1 = difftime(SortLoc$DateTime[i], SortWeath$DateTime[weathrow],
units="auto")
timedif2 = difftime(SortLoc$DateTime[i],
SortWeath$DateTime[weathrow+1], units="auto")
if (timedif2<0){
if (abs(timedif1)
} else {
SortLoc$WndSp[i]=SortWeath$WndSp[weathrow+1]
SortLoc$WndDir[i]=SortWeath$WndDir[weathrow+1]
SortLoc$Hgt[i]=SortWeath$Hgt[weathrow+1]
}
t = 1
}
###THIS IS MY ATTEMPT TO GET IT TO USE THE PREVIOUS WEATHER INFORMATION OR TO GO
ONTO THE NEXT DATA POINT
#if (abs(SortLoc$DateTime[i] - SortLoc$DateTime[i+1] < 50)){
# weathrow=weathrow
#} else {
#weathrow = weathrow+1
#if(weathrow = nrow(SortWeath)){t=1} HOW TO GET IT TO STOP????
}
} #end while
}
}
[[alternative HTML version deleted]]