-1

long story short, I am plotting climate data from a netCDF file. My only problem, I have to plot data from tens of these files, each having over a hundred data points. Luckily, they are all identically formatted, and their names are in rising order (for example: file1.nc, file2.nc...). This is my code (unfinished as I have to change the markers and colors of the markers):

Anyways, I want to plot more than that one file (about 20 to begin with). Is there a way to do that? Also, if you guys have an idea how how to set up the colorbar based on variable 'data' that would be great.

Thanks!

xyzman
  • 11
  • 7

2 Answers2

0

Make an empty array :

data =[] 

Make a list of filenames :

flist=["001.dat","002.dat",...] 

then iterate through that list:

for fn in flist: 
        data.append( netcdf_file(fn,'r'))

Now you can refer to your data sets like:

data[0]
data[1]

etc.

roadrunner66
  • 7,772
  • 4
  • 32
  • 38
  • 1
    if the files are all in the same folder, you can use glob to find them all: `filelist = glob.glob('/Users/epsuser/Dropbox/Argo/Data/2/*.nc')` – story645 Jul 22 '16 at 02:46
0

at the least, plt.savefig("some unique name") means you can generate them in a loop without having to save the plots/close them individually.

I also suggest getting comfortable with the object oriented interface:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
map = Basemap(projection='robin',lon_0=0,resolution='l',ax=ax)
#keep all your code
cs = map.scatter(x,y,data)
fig.savefig("{}".format(some unique identifier))

Eta: And you can find all the files using glob if they're in the same folder:

import glob
filelist = glob.glob('/Users/epsuser/Dropbox/Argo/Data/*.nc')
for fl in filelist:
    ncfile = netcdf_file(fname,'r')
    #the rest of your reading code
    fig = plt.figure()
    #etc...
story645
  • 566
  • 3
  • 13