Read multiple csv files and write multiple netCDF files
Question: I have the following Python code works perfectly fine for a single .csv file
to convert for a netCDF file.

But, I have multiple files (365), as, 'TRMM_1998_01_02_newntcl.csv',
'TRMM_1998_01_03_newntcl.csv'....upto 'TRMM_1998_12_31_newntcl.csv'.

Can somebody help me to write to loop through all the csv files and create 365
netCDF files using this code.?

Anyhelp is appreciated.

Thanks in advance.

    
    
    import numpy as np
    
    def convert_file(filename):
    data = np.loadtxt(fname=filename, delimiter=',')
    # filename = "TRMM_{}_{}_{}_newntcl.csv".format(d.year,d.month,d.day)
    Lat_data = np.loadtxt('Latitude.csv', delimiter=',')
    Lon_data = np.loadtxt('Longitude.csv', delimiter=',')
    
    # create a netcdf Data object
    
    with netCDF4.Dataset('TEST_file.nc', mode="w", format='NETCDF4') as ds:
        # some file-level meta-data attributes:
        ds.Conventions = "CF-1.6" 
        ds.title = 'precipitation'
        ds.institution = 'Institute'
        ds.author = 'Author'
    
        lat_arr = data[:,0] # the first column 
        lon_arr = data[:,1] # the second column 
        precip_arr = data[:,2] # the third column 
    
        nlat = lat_arr.reshape( (161, 321) )
        nlon = lon_arr.reshape( (161, 321) )  
    
        # ds.createDimension('time', 0)
        ds.createDimension('latitude', 161)
        ds.createDimension('longitude', 321)
    
    
        precip = ds.createVariable('precip', 'f4', ('latitude', 'longitude'))
        precip[:] = data[:,2]
        ## adds some attributes
        precip.units = 'mm'
        precip.long_name = 'Precipitation'
    
    
        lat = ds.createVariable('lat', 'f4', ('latitude'))
        lat[:] = Lat_data[:]
        ## adds some attributes
        lat.units = 'degrees_South'
        lat.long_name = 'Latitude'
    
    
        lon = ds.createVariable('lon', 'f4', ('longitude'))
        lon[:] = Lon_data[:]
        ## adds some attributes
        lon.units = 'degrees_East'
        lon.long_name = 'Longitude'    
    
    
        print ds
    
    
     # print filename
    
    # load the data
    
    path='C:\Users\.spyder2'
    os.chdir(path)
    
    d=datetime.date(1998,01,01)
    while d.year==1998:
        d+=datetime.timedelta(days=1)
        convert_file("TRMM_{}_{}_{}_newntcl.csv".format(d.year,d.month,d.day))
    


Answer: It looks like you can use a
[`datetime.date`](https://docs.python.org/2/library/datetime.html#date-
objects) object to loop through all of the days in a year. First, you should
put the code you have in a function that takes a filename. Then, you can just
make a `date` object and call the function in a loop:

    
    
    import datetime
    d=datetime.date(1998,1,1)
    while d.year==1998:
        d+=datetime.timedelta(days=1)
        convert_file("TRMM_{}_{}_{}_newntcl.csv".format(d.year,d.month,d.day))
    

