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



我有以下Python代码,对于将单个.csv文件转换为netCDF文件来说,效果非常好。

但是,我有多个文件(365),如"TRMM_1998_01_02_newntcl.csv"、"TRMM_1 998_01_03_newntclcsv"……至"TRMM_5998_12_31_newntcl.csv。

有人能帮我写循环通过所有csv文件,并使用此代码创建365个netCDF文件吗。?

感谢任何帮助。

提前谢谢。

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

看起来您可以使用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))

如果我没看错你的问题,在这种情况下有一种更容易使用os的方法。你只需要输入文件名并在循环中使用它们:

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

最新更新