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


Messages In This Thread
Convert grib files to text files - by tuxman - Sep-12-2017, 04:36 PM
RE: Convert grib files to text files - by snippsat - Sep-12-2017, 06:37 PM
RE: Convert grib files to text files - by nilamo - Sep-12-2017, 06:39 PM
RE: Convert grib files to text files - by tuxman - Sep-13-2017, 02:50 PM
RE: Convert grib files to text files - by snippsat - Sep-14-2017, 06:38 PM
RE: Convert grib files to text files - by tuxman - Sep-15-2017, 01:58 PM
RE: Convert grib files to text files - by nilamo - Sep-15-2017, 03:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Correct/proper way to create save files snakes 0 463 Mar-11-2025, 06:58 PM
Last Post: snakes
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,350 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  how to download large files faster? kucingkembar 3 821 Feb-20-2025, 06:57 PM
Last Post: snippsat
  Inserting Python Buttons into KV Files edand19941 3 511 Feb-19-2025, 07:44 PM
Last Post: buran
Question [SOLVED] Right way to open files with different encodings? Winfried 3 4,178 Jan-18-2025, 02:19 PM
Last Post: Winfried
  Applications config files / Best practices aecordoba 2 1,990 Oct-23-2024, 12:56 PM
Last Post: aecordoba
  Compare 2 files for duplicates and save the differences cubangt 2 959 Sep-12-2024, 03:55 PM
Last Post: cubangt
  Convert Xls files into Csv in on premises sharpoint Andrew_andy9642 3 1,022 Aug-30-2024, 06:41 PM
Last Post: deanhystad
  deleting files in program files directory RRADC 6 3,005 Aug-21-2024, 06:11 PM
Last Post: snippsat
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 947 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362

Forum Jump:

User Panel Messages

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