 Hey folks! Welcome back for another episode of Code Club. Hopefully you've been following along in the recent episodes where we've been building out this webpage that indicates the relative level of droughtiness across the world using data that's collected from NOAA, which is a U.S.-based agency that collects all sorts of climate data from around the world, but as you can tell from this figure, most intensively from the United States. This figure is indicating the Z scores, so basically the amount of precipitation over the past 30 days relative to all other years within that same window. And the Z score is the amount of precipitation minus the average precipitation for that window over the years divided by the standard deviation. And so if it's red then it's more droughty, and if it's green then it's more wet. And so as you can see from this figure, we are in the midst of a drought here in the Midwest of the United States, and we've certainly seen that over the past summer across Europe and the United States as well. So we're at the stage of this project where I'm noticing little things that just kind of annoy me that I want to go back and fix. In this episode I'm going to pay attention to a couple things that have to do with the date. So the first thing is that you'll notice that we're going from September 28th to October 28th, and the site was last updated on October 28th. So that might seem, you know, all good and well and make sense, but the reality is that the data were posted to the NOAA website on the 27th and the data were collected, or, you know, the data were generated, so to speak, on the 26th, right? So we're not really looking at data from the 28th to 28th. We're more looking at the data from like the 26th to the 26th. But the site was updated on the 28th. So what I want to do in this episode is go back and adjust those dates to better reflect what we're actually dealing with for the data. The second thing that I would like to address is the formatting of the title, right? So September 28th, October 28th. I'd like to include the year, but we might get into a situation where we have, say, December 20th of this year of 2022 to, say, January 10th of 2023, right? And so we'll want to be careful about not putting the year in too many times, but perhaps also not, you know, putting in the month too often. So say it goes from October 1st to October 31st. That doesn't seem necessary. Why not go from October 1st to 31st of 2022. So to make that adjustment to the title, we're going to make use of a great function called case when that's a lot like an if else statement, but has greater flexibility to allow for more different types of logical comparisons that you might want to make with your data. As I've been doing in the past series of episodes, I'm working in Visual Studio Code, VS Code, because I'm using more tools than our studio, I'm using a lot of stuff from the command line. I'm using Conda, Mamba, all sorts of great stuff. And so the file that I want to go back to is the read split DLY files. So in the read split DLY files, I'm reading in the DLY files out of the massive GHD and D all files, a tar ball, and we're reading it in kind of in a special way. I'd encourage you to go back and look at how we did that earlier in the series. But one of the things I do is I look at today's Julian day. So basically, what day of the year is it? And I do that based on today. But again, like I said, it's really two days ago. And if I wanted to be safe, maybe I'd make it five days ago. So I think what I'll do is let's go ahead and make it five days ago, because I don't know that all weather stations are able to get all their data in immediately. So let's go ahead and do today minus five. And we'll use that five as a kind of buffer to help protect us. But we need to remember that we are using this five day buffer in the other scripts we're using. I want to double check that I'm not using the today function anywhere else in the script. Great. So that's the only place that we're using today. The other file I want to look at then is plot drought by region. And then if I look at today, I see that I've got four instances of the today function here. And so I'm going to be doing a fair amount of work in this script. So what I'd like to do is go ahead and load all this stuff into our you'll see that I'm into my conda environment drought. I'll go ahead and fire up our and then load these different packages as well as the font stuff we did in the last episode. I think I'll also get rid of these anti joints because they're not really useful. I'm now going to create a variable for today. I'll call it buffered today. And that will be the today function minus five. And so if I look at today, so if I look at today, I see that it's October 28. But if I look at buffered today, it should be the 23rd. And sure enough, it is right. So now I'm going to use this buffered today. Basically, wherever else I use today in this figure, right? So I'll go ahead and copy this and then paste it in here, as well as here. And I've got an extra T there. And we'll remove this there as well. And so now if I look at the end and the start and October 23rd and start being September 23rd, right? So we went back from September 23rd to October 23rd. That's all good. And then I think here we also did a year on today. So let's put in here buffered today as well. So let's go ahead and run this and make sure that we've got the 23rd as our starting and ending point. And it looks like I forgot a closing parentheses here. So we'll get that in there. So that updates the date in the title. Again, the data haven't changed because I haven't rerun all of the preceding steps, right? I want to focus on the formatting and then we'll go back and make sure everything works together. So back up here in end and start, you'll recall that we have end and start, you'll recall that we use the format function to output the month and the day as a string, right? And so there's a couple different situations that I want to be mindful of, right? And so we could have a different month, same year, we could have same month, and then obviously the same year. And we could have different month, different year. And so if I think about a 30 day window, I think those are the three different combinations I want to be able to account for as I'm building out a title. Currently, we're not showing the year. And if I wanted to include the year, I might do something like percent Y. And so then that would give me an end of October 23, 2022, right? So we're going to modify these two steps. And I think I'm going to make a variable that I'll call date range. And so down here, instead of start to end, I'll do date range. So then I'll do date range here. And that will be glue on. We'll put in curly braces, start to end. Good. And so now let's regenerate our figure. Again, we've got that date range there as well. And it's got the year because I left the year on for the end. So that's what we're going to use for different month and same year. Good. And then I'll go ahead and comment this out for now. And so let's then bring these comments down. And we can kind of build out the date range value that we would use for each, right? So if we have the same month and the same year, then I'm going to want to alter the values for end and start. So let's come back and think about this perhaps a little bit differently. Maybe what I'll do instead of building out a new date range for each of the three conditions is build out a different start and end value for the three conditions, right? So let's go ahead and try that. And again, this is kind of a work in progress. I haven't done this before. And so I'm thinking this through with you as I'm writing this code. All right, different month and same year. And again, that is going to be end and start, right? And so we've already seen that that works for different month and same year. For now, I'm going to comment this out. And then same month and same year, let's go ahead and copy this down. And again, to test this out, we'll leave it uncommented. So the end is going to be again the same year. So here, maybe what I'll do is D, what does that look like for the end now? So that's going to be 23 comma 22. I think that's good. And then the start, if that is the same month, right, that will be the month and the day. Yeah, that should work, right? So that would be basically start. And then September 23 to 23, we know that this doesn't make any sense. But it'll work, it'll work out. So I'm going to go ahead and comment this out for now. And then if we have a different month and different year, then we're going to want the capital B, or the percent B, d y. And then here, we're going to want the same thing, right? So it's going to be a long title for, you know, those ranges between December and January, spanning a year. Okay, so if we do that, then we do date range. We'll then see, yeah, let's go from it's basically the full thing, right? So I'm not going to comment and uncomment the code as the date changes, right? Instead, what we're going to do is we're going to use a case when statement to modify the values of end and start. So let's go ahead and uncomment this code, as well as this code. Okay. And let's get everything up onto the same screen here and I'll drop this down. And what we'll do is I will create a variable that I'll call end, and we'll then do case when is the name of the function, right? So in case the buffered today versus buffered today minus 30. So so I don't have to do minus 30 minus 30 minus 30. I think what I'll do instead will be buffered. Start. And that will be buffered today. minus 30. And let's get that loaded. Great. And so the way this works is that we have a logic. And then if it's true, then we return a value. If it's false, then we go to the next line. So logic return. And if it's true, there's another logic and then return, right? So again, the idea is that it'll run each line or each slot of the case when statement. And if it's true, it'll return that value and then kick it out of the case when statement. So you want to be mindful of how you write these logic statements and how you order them, because you generally want to go from much more specific to much more broad. In general, these will end with a true statement. And then I generally will return an NA value. So I'll do NA underscore character. So again, true is the most vague result that you could get because that is always true, right? It's the least specific. So the first logic statement will be for different month and same year. So again, the months will be different. So we'll do month on buffered start. And again, what is that? That should be 10. No, it's nine. So because September is the start, right? So if it's different than the end, so it'd be not equal to month buffered end, right? And so for currently, that should be true, right? Because we're in October, that's the buffered end, going back to the start September. And I should put in today. You know what, maybe I think buffered end is a much better name. So let's go ahead and do buffered end instead of buffered today. I don't know why I didn't do that originally. So maybe what I'll do is I'll go ahead and do a find and replace for today. And we'll want to replace that with buffered end and I misspelled today. So that needs to be an a and I'll go ahead and replace all those instances. And we'll do buffered end and start get that loaded. Okay, now we should be good. So now we have buffered end and start. So yeah, buffered end and start. So this should be true, right? Great. So what is that going to return that then we'll go ahead and return this statement. And good. And this also will take care of the different month and different year, because obviously in different years, they're in different months. I'll go ahead and comment that out. I guess if somebody came along and they made the window a year, rather than 30 days, then that wouldn't work. But that's not the condition I currently have. Alright, so now we have the condition of the same month and the same year. So now I'll go ahead and grab that. And again, if they're in the same month, they're obviously going to be in the same year. Unless again, you're looking at a year long window. And so then we will return this and we'll put that in there and I can remove this third option. Good. And so now we have all of the end values set. So I'm going to replace this buffered end minus 30 with buffered start. And we'll do this as well. Great. And so now we're going to create a case when statement for start, we'll do case when. And then we'll do the again different month, same year, I'm going to grab this same logic. But we're going to have a different format, right? So this is going to output the month and the day. But there's going to be a problem if it's different month, but the same year, I probably need to say different year. So again, like I said, we go from specific to more general. And what I could do is I could put different month, different year first. So I could say year on buffered start, not equal to year on buffered year. And so again, as I said earlier, in that case, the months are obviously going to be different. So now I'll go ahead and grab this and pump that in there. And then we'll need a comma. So that gets us those two conditions, right? That gets us the different month and the same year and different month, different year. And so the third condition we need is the same month and the same year. And so now, if we have month equals month, so I'll go ahead and copy this down. So if their months are the same, then we're going to replace this, then we're going to actually leave the same thing. And so I think the logic would be simpler if we said, if the years are the same, then the start formatting will be the same, right? And so we'll we'll go ahead and do that. Again, to make our code simpler and easier to maintain. And then we'll include the true tilde and a character. I don't know that we'd ever need that. But just in case we'll leave that in there, it's a good practice to always have true as the last statement in your case when. And I think I forgot a underscore here and underscore here. All right. So we'll go ahead and load those and buffered year that should be buffered and not year. All right. Cool. So now what do we have for start and end? And so I've got an extra parentheses there. All right. So now for start, I have September 23. And I have October 23 22. And then date range, we'll be putting that all together with our glue statement, right? If we can look at date range, we get the right formatting. Cool. All right. So let's try this with a couple different options for testing purposes, I'm going to grab the buffered end and start. And we'll go ahead and put it here. And I'm going to format something to be a date that will get us different month and same year, we've already done that, right? That's where we're currently at. So I'll move this down. So same month, same year. So let's go ahead and do as dot date. And then I'll do 222 hyphen 10 hyphen 31. And then we'll do buffered and minus 30. So let's make sure these work. We get October one to 31. So that kind of works. But I've got this zero one for the date. And one of the things that I've read in my research on this is that if I put a hyphen before the D the lowercase D, it will remove that leading zero. So let's go ahead and put that there. This works on Mac and Linux. If you're on Windows, you'll want to use a pound sign instead. So let's go ahead and give this all another run and see if that doesn't clean up our date range. Good. Now we have October 1 to 31. Good. That's formatted, right? Again, that's the same month, same year. So let's now try for a January date for the start. So we'll go ahead and do zero one, zero five. And so now our buffered start got to run the whole line buffered start will be December six 2021. Good. So that's different years, right? And so now if we run our end and start and date range, we now see that we've got both years on the dates. Cool. So that should account for everything. Now let's see what it does to our title when we run that. And so having both years is going to give us the longest possible title. And so we see that our title actually goes off the right side of the screen, which isn't really what I want. So there's a couple options. I could force this to wrap. I'm not totally sold on that though, because I think the only thing that would wrap here would be the year 2022. And that would look weird having that at the end. I think the option that I'm going to go for is to shrink the font just a smidge to see if we can't get those final two digits on the year. Again, to do that, we'll come back here to plot title and let's go to 18. I think in the last episode, I increased it to 20. So that brings it in. Let's see if we can't just get as much out of that as we can. I'm going to go up to 19. I think I will bring that down to 18. And maybe I'll shrink the size of my subtitle just a smidge. So let's go here and do 18. And then we had plot subtitle and we'll do size equals 12. I'm not really sure what the default size is. So we'll start there and adjust. So I think that may have made it bigger. Let's go down to eight, maybe a little too small. And let's let's try 10. And that's probably where we'll stop. I like that aspect ratio in size between the title and the subtitle. I think this is the longest possible title that we'd ever have. So I think that looks pretty good. Let's go back and put it back to the real dates that we have, which again, I can achieve by removing all of these lines. And let's clean this all up. And we'll go ahead and run everything and see what our title looks like with the current dates. So I think that looks great. So the final place that we had a date was in our index.rmd. And so that's last updated on today. That is perfectly fine because that's the day that we ran this analysis on. I'm happy with that. I'll go ahead and save everything and bop out of our do it get status to see what all we have changed. And we've changed those to our script as well as the final visual. I'll go ahead and add everything and commit hyphen m to add buffered time lag and reformat title. And we'll go ahead and I'll push this up. And we'll see what the final version looks like with the lagged data. So the dates won't match the date in the lower left corner. Once we've rerun everything. And I think that will look great. So now as we can see it ran through GitHub actions and has been updated to the website, we can see that we have that time lag in the plot. And we also have the current date today's date in the lower left corner. I'm really happy with the way this came together. Let me know what you think of this figure and this whole process down below in the comments. I would love it if you told your friends about what we've been doing here in this series of episodes, you can watch the full series in this window over here over my shoulder. I really hope that you do please leave feedback if you like this type of content where we work on a single project over multiple episodes. All right, we'll keep practicing with these concepts. The case when statement is really powerful. And I really hope you got something out of seeing how I work through the logic testing things and implementing things with a case one statement. All right, take care and we'll see you next time for another episode of code club.