The goal of my code is to download GFS data from the date specified (whether user inputted or just grabbing today's data) and have it downloaded and read using netCDF4. I need to download the data package so that when my code runs, it isn't taking more than 15 minutes to run and then being shut down by the DOS server since it is accessing so much data. This is what I have so far:
def accessGFS():
baseURL = 'http://nomads.ncep.noaa.gov:9090/dods/gfs_0p25/'
GFSDate = int(time.strftime("%Y%m%d"))
currentHour = time.gmtime()[3]
gfsTimeHeader = 'gfs_0p25_'
if currentHour > 22:
timeURL = gfsTimeHeader + '18z'
GFSTime = 18
elif currentHour > 16:
timeURL = gfsTimeHeader + '12z'
GFSTime = 12
elif currentHour > 10:
timeURL = gfsTimeHeader + '06z'
GFSTime = 6
elif currentHour > 4:
timeURL = gfsTimeHeader + '00z'
GFSTime = 0
else:
timeURL = gfsTimeHeader + '18z'
GFSTime = 18
GFSDate -= 1
GFSDate = str(GFSDate)
GFSDateTime = datetime.datetime(int(GFSDate[:4]),int(GFSDate[4:6]),int(GFSDate[6:]),GFSTime, 0, 0)
dateURL = 'gfs' + GFSDate + '/'
url = baseURL + dateURL + timeURL
values = {}
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
gfs_download = urllib.request.urlopen(req)
gfsData = gfs_download.read()
saveFile = open('GFS%sdata.nc' %GFSDate, 'w')
saveFile.write(str(gfsData))
saveFile.close()
gfs = Dataset(gfsData)
return GFSDateTime, gfs
Which is then called upon the line of code:
gfs, gfsDate = GFSReader.accessGFS()
When I run the code it does access the GFS server and downloads the file into the right folder, but it throws me the error:
FileNotFoundError: [Errno 2] No such file or directory: b'b\'<html>\\n<head>\\n
There is way more to that error though. It basically copies and pastes the entire '.nc' file I created in accessGFS() and throws that in the error code. These are the trackbacks:
File "C:/Users/Desktop/Predictions/GFSDriver.py", line 65 in <module>
gfs, gfsDate = GFSReader.accessGFS()
File "C:\Users\Desktop\Predictions\GFSReader.py", line 53. in accessGFS
gfs = Dataset(gfsData)
File "netCDF4\_netCDF4.pyx", line 2111, in netCDF4._netCDF4.Dataset.__init__
File "netCDF4\_netCDF4.pyx", line 1731, in netCDF4._ensure_nc_success
So I know it has something to do with the way I downloaded the file or the way it is being read through netCDF4, but I'm not sure what it is. The code has worked without downloading the data at all, and just getting the Dataset every time it was called on. So that's what makes me think that for some reason the function within netCDF4, Dataset, isn't reading the file I am downloading properly.
Any suggestions?