Python Forum
Convert grib files to text files
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert grib files to text files
#1
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 !
Reply
#2
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))
Output:
/home/george/data/foo.grb
Remember that you needs to this for both src and dst parameter in os.rename().
Reply
#3
Quote:
for f in filenames:
        #print f

If you uncomment that, do you get output?
Reply
#4
Yes, I get the output from line 'print f': the grib files from /data folder.
Reply
#5
Maybe my answer work Think
/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])))
Output:
/foo cd1.txt cd2.txt cd3.txt
You have to think of this process_message(grb, f) dos it do an in place change of f and return f.
Reply
#6
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()
Error:
20150219_083013_.grb Traceback (most recent call last): File "/home/george/mona_modificat/Grib.py", line 35, in <module> os.rename(join(root, f), '{}.txt'.format(join(root, splitext(f)[0]))) OSError: [Errno 2] No such file or directory
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to generating multiple json files using python script dzgn989 4 244 May-10-2024, 03:09 PM
Last Post: deanhystad
  Filer and sort files by modification time in a directory tester_V 5 378 May-02-2024, 05:39 PM
Last Post: tester_V
  [SOLVED] Loop through directories and files one level down? Winfried 3 306 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 570 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
Question Right way to open files with different encodings? Winfried 2 322 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 403 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Using zipfile module - finding folders not files darter1010 2 340 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,151 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 956 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 502 Feb-07-2024, 12:24 PM
Last Post: Viento

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020