[Tutor] directory within directory
questions anon
questions.anon at gmail.com
Wed Aug 17 00:46:44 CEST 2011
Thank you, that does create the directories in the new place but when I
process the data it does not put the outputs in the correct directory they
all end up in the last directory created.
Below is the code of what I am trying to do.
Any feedback will be greatly appreciated.
from netCDF4 import Dataset
import numpy as N
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from netcdftime import utime
from datetime import datetime
import os
inputpath=r'E:/temp_samples2/'
outputpath=r'E:/figureoutputs/'
for (path, dirs, files) in os.walk(inputpath):
for dir in dirs:
print dir
sourcepath=os.path.join(path,dir)
relativepath=os.path.relpath(sourcepath,inputpath)
newdir=os.path.join(outputpath,relativepath)
if not os.path.exists(newdir):
os.makedirs(newdir)
for ncfile in files:
if ncfile[-3:]=='.nc':
ncfile=os.path.join(sourcepath,ncfile)
ncfile=Dataset(ncfile, 'r+', 'NETCDF4')
TSFC=ncfile.variables['T_SFC'][:,:,:]
LAT=ncfile.variables['latitude'][:]
LON=ncfile.variables['longitude'][:]
TIME=ncfile.variables['time'][:]
fillvalue=ncfile.variables['T_SFC']._FillValue
TSFC=MA.masked_values(TSFC, fillvalue)
ncfile.close()
for TSFC, TIME in zip((TSFC[4::24]),(TIME[4::24])):
print TSFC, TIME
#convert time from numbers to date and prepare it to have no
symbols for saving to filename
cdftime=utime('seconds since 1970-01-01 00:00:00')
ncfiletime=cdftime.num2date(TIME)
print ncfiletime
timestr=str(ncfiletime)
d = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S')
date_string = d.strftime('%Y%m%d_%H%M')
print date_string
#Set up basemap using mercator projection
map = Basemap(projection='merc',llcrnrlat=-40,urcrnrlat=-33,
llcrnrlon=139.0,urcrnrlon=151.0,lat_ts=0,resolution='i')
# compute map projection coordinates for lat/lon grid.
x,y=map(*N.meshgrid(LON,LAT))
map.drawcoastlines(linewidth=0.5)
map.drawstates()
plt.title('Surface temperature at %s UTC'%ncfiletime)
ticks=[-5,0,5,10,15,20,25,30,35,40,45,50]
CS = map.contourf(x,y,TSFC, ticks, cmap=plt.cm.jet)
l,b,w,h =0.1,0.1,0.8,0.8
cax = plt.axes([l+w+0.025, b, 0.025, h], )
cbar=plt.colorbar(CS, cax=cax, drawedges=True)
#save map as *.png and plot netcdf file
plt.savefig((os.path.join(newdir,'TSFC'+date_string+'UTC.png')))
On Tue, Aug 16, 2011 at 6:21 PM, Peter Otten <__peter__ at web.de> wrote:
> questions anon wrote:
>
> > I would like to open up a bunch of files within a folder within a folder
> > and then process them and output them in another location but with the
> > same folder structure. I seem to having trouble with the folder within
> > folder section.
> > I have a separate folder for each year and then within a year I have a
> > separate folder for each month but when I try to make a directory in a
> new
> > location it does not place the month folders within the year folders,
> > instead they are all places in the outputpath together
> > any help will be greatly appreciated
> >
> > import os
> >
> > inputpath=r'E:/temp_samples2/'
> > outputpath=r'E:/figureoutputs/'
> >
> > for (path, dirs, files) in os.walk(inputpath):
> > for dir in dirs:
> > print path, dir
> > newfolders=outputpath+dir
>
> Using string concatenation to produce file paths is errorprone.
> Have a look at the path manipulation functions that the os.path module has
> to offer.
>
> > if not os.path.exists(newfolders):
> > os.makedirs(newfolders)
> > print newfolders
>
> dir is just the directory name. You get the source directory with
>
> sourcepath = os.path.join(path, dir)
>
> Now you have to remove the start of the path with
>
> relativepath = os.path.relpath(sourcepath, inputpath)
>
> Finally add the outputpath:
>
> newdir = os.path.join(outputpath, relativepath)
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110817/22721f0f/attachment.html>
More information about the Tutor
mailing list