Mrmr Mrmr - 1 year ago 69
Python Question

Now to read multiple files of netcdf with this date directory

I have this file directory as an example and I would like to read all files as a date to date loop
so the part where the date 1990101 is changing based on the investigated dates
these files are netcdf files
I know how to read one by one file but I could not make to run the whole file that includes multiple files
as an example
1- we can read as all files in one folder
2- read date to date files (preferred)

Answer Source

Since the files are incremented by day, you can take advantage of Python's dateutil rrule function to loop over them easily.

Below is an example of how to loop over daily files in Jan. 1998 and then read them in as you iterate through the days.

import netCDF4
import datetime
from dateutil.rrule import rrule, DAILY

fl_pfx = ''
fl_sfx = '.7.nc4'
strt_dt =,1,1)
end_dt =,1,31)

for day in rrule(DAILY, dtstart=strt_dt, until=end_dt):
    day_fmt = datetime.datetime.strftime(day, '%Y%m%d')
    src_fl = '{0}{1}{2}'.format(fl_pfx, day_fmt, fl_sfx)
    ncfile = netCDF4.Dataset(src_fl, 'r')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download