读取多个csv文件并写入多个netCDF文件

时间:2015-03-19 23:56:17

标签: python loops csv

我有以下Python代码非常适合单个.csv文件转换为netCDF文件。

但是,我有多个文件(365),如'TRMM_1998_01_02_newntcl.csv','TRMM_1998_01_03_newntcl.csv'.... upto'TRMM_1998_12_31_newntcl.csv'。

有人可以帮我编写循环遍历所有csv文件并使用此代码创建365个netCDF文件。?

Anyhelp表示赞赏。

提前致谢。

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))

2 个答案:

答案 0 :(得分:0)

看起来你可以使用datetime.date对象来循环一年中的所有日子。首先,您应该将您拥有的代码放在一个带文件名的函数中。然后,您可以创建一个date对象并循环调用该函数:

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))

答案 1 :(得分:0)

如果我正确地阅读了您的问题,在这种情况下使用操作系统会更简单。您只需接收文件名并在循环中使用它们:

import os

main_fp = "C:\\Users\\spyder2" 
path, dirs, files = os.walk(main_fp).next()

for f_path in files:

    data = np.loadtxt(f_path, delimiter=',')
    Lat_data = np.loadtxt('Latitude.csv', delimiter=',') #put lat and long csv's in separate folder, so you don't read them into the loop
    Lon_data = np.loadtxt('Longitude.csv', delimiter=',')

    #strip csv extentions
    new_fname = f_path.strip('.csv')


    with netCDF4.Dataset(new_fname+'.nc', mode="w", format='NETCDF4') as ds:
            # some file-level meta-data attributes:
            ds.Conventions = "CF-1.6" 
            ds.title = 'Non TC precipitation'
            ds.institution = 'AIR-Worldwide'
            ds.author = 'Dr. Dumindu Jayasekera'

        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('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