0

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()
user5276228
  • 187
  • 1
  • 3
  • 14
  • Please revise the question. Is this a Python or CDO question? CDO is telling you that it does not support the file type. Please check CDO's documentation to see if this is true. What does `cdo sinfo newfile.grb` tell you? – Robert Wilson Oct 22 '20 at 08:54
  • That's a python question. I don't create a correct grib file and cdo sinfo proves it, cdo sinfo gives information on the file ( file format, grid coordinate, time coordinate...) – user5276228 Oct 22 '20 at 09:44

0 Answers0