Convert grib files to text files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Convert grib files to text files (/thread-4907.html) |
Convert grib files to text files - tuxman - Sep-12-2017 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: 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 ! RE: Convert grib files to text files - snippsat - Sep-12-2017 You are not running script in same folder as files,as you us chdir(). Then have to give absolute path to rename. Example: import os root = '/home/george/data/' f = 'foo.grb' print(os.path.join(root, f)) Remember that you needs to this for both src and dst parameter in os.rename().
RE: Convert grib files to text files - nilamo - Sep-12-2017 Quote:for f in filenames: #print f If you uncomment that, do you get output? RE: Convert grib files to text files - tuxman - Sep-13-2017 Yes, I get the output from line 'print f': the grib files from /data folder. RE: Convert grib files to text files - snippsat - Sep-14-2017 Maybe my answer work /foo cd1.rgb cd2.rgb cd3.rgb import glob, os from os.path import join, splitext root= '/foo' for f in glob.glob('*.grb'): os.rename(join(root, f), '{}.txt'.format(join(root, splitext(f)[0]))) You have to think of this process_message(grb, f) dos it do an in place change of f and return f .
RE: Convert grib files to text files - tuxman - Sep-15-2017 snippsat, thanks very much for your reply. My new code: import pygrib, os, glob from os.path import join, splitext 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)) root = 'data/' os.chdir(root) for f in glob.glob('*.grb'): print f grbs = pygrib.open(f) grbs.seek(0) for grb in grbs: grb process_message(grb, f) os.rename(join(root, f), '{}.txt'.format(join(root, splitext(f)[0]))) grbs.close()
RE: Convert grib files to text files - nilamo - Sep-15-2017 Stop using os.chdir(), and just use absolute paths for everything. Or at least print the old/new paths, since one of them is clearly pointing to nowhere. |