Python writte GRIB file

1.2k views Asked by At

I want to sum two grib files. I am able to create the new variable (newfield), but I don't succeed to write it in a grb file. When I do "cdo sinfo newfile.grb", I get the message: " Unsupported file type" here is my code:

grib_dif = pygrib.open('file1.grb')

grib_dir = pygrib.open('file2.grb')

outfile = open('newfile.grb','wb')

#read grib1:
grib_dif.rewind()
dif_vals=[]
for mes1 in grib_dif:
        print mes1
        if (mes1.shortName == 'SWDIFDS_RAD'):
                fdif=mes1
        dif_vals.append(fdif.values)
        

#read grib2:
grib_dir.rewind()
dir_vals=[]
for mes2 in grib_dir:
        if (mes2.shortName == 'SWDIRS_RAD'):
                fdir=mes2
        dir_vals.append(fdir.values)

#sum up:
newfield=zeros((6,401,561),float)
for t in range(6):
         for i in range(401):
                for j in range(561):
                        newfield[t][i][j]=(dif_vals[t][i][j]+dir_vals[t][i][j])


#write output file:
outfile.write(newfield)
outfile.close()
grib_dif.close()
grib_dir.close()
0

There are 0 answers