Sep-12-2017, 04:36 PM
Hi,
I want to read many grib files from a folder, and then save as text files. The name of the text file will be same name as grib file.
My code:
Cheers !
I want to read many grib files from a folder, and then save as text files. The name of the text file will be same name as grib file.
My code:
import pygrib, os, glob LAT1 = None LAT2 = None LON1 = None LON2 = None def process_message(grb, outfile): with open(outfile, 'w') as output: tmps = "%s" % grb wtitle = tmps.split(':')[1] wtime = tmps.split(':')[len(tmps.split(':'))-1].split(' ')[1] data, lat, lons = grb.data(LAT1, LAT2, LON1, LON2) for i in xrange(len(data)): tdata = data[i] tlat = lat[i] tlon = lons[1] for j in xrange(len(tdata)): wdata = tdata[j] wlat = tlat[j] wlon = tlon[j] output.write("%s, %s, %s, %s, %s\n" % (wtitle, wtime, wlat, wlon, wdata)) os.chdir('/home/george/data') filenames = glob.glob('*.grb') for f in filenames: #print f grbs = pygrib.open(f) grbs.seek(0) for grb in grbs: grb process_message(grb, f) os.rename(f, f + '.txt') grbs.close()When I run the code, nothing happens...no errors, no text files was created.
Cheers !