简体   繁体   中英

Loop saving netcdf images pycdf and numpy change time slice

I have a netcdf file which shows how a variable changes at different time slices. I want to save a .png of this time slice and then save a separate .png of the next time slice until all time slices have been saved. I would like to have each time slice saved as a number.

My attempt at this is below, but I can't get the loop of saving different files to work.

    nc=pycdf.CDF("netcdf1.nc",pcdf.NC.NOWRITE)
    nc.automode()
    nc2=pycdf.CDF("netcdf2.nc",pcdf.NC.NOWRITE)
    nc.automode()
    for i in range(5):
        time = i
        lons  = nc2.var('lon')[0,1:382,1:440]
        lats  = nc2.var('lat')[0,1:382,1:440]
        thk=nc.var('thk')[time,1:382,1:440]
        m = Basemap(width=2200000,height=1910000,resolution='l',projection='aea',lat_0=lats.mean(),lon_0=lons.mean())
        x,y = m(*(lons,lats))
        cs=m.pcolor(x,y,thk,norm=colors.LogNorm())
        plt.savefig('/home/jeremy/Desktop/Figures_presentation/Hagdorn_SIA/Vel/value{i}.png')
        nc.close()

You must change the plt.savefig command to: plt.savefig('/home/jeremy/Desktop/Figures_presentation/Hagdorn_SIA/Vel/value'+i+'.png')

or if You want numbers like 001, 002, 003, then plt.savefig('/home/jeremy/Desktop/Figures_presentation/Hagdorn_SIA/Vel/value{0:03d}.png'.format(i))

Also, take the basemap command out from the loop, will be a bit faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM