2016-11-15 34 views
0

我有這個文件目錄作爲例子,我想讀取所有文件作爲日期到日期循環 disc2.gesdisc.eosdis.nasa.gov-3B42_Daily.19980101.7 .nc4 所以日期1990101正在改變的部分基於調查日期 這些文件是netcdf文件 我知道如何逐個讀取文件,但我無法運行整個文件,其中包含多個文件 作爲示例 1-我們可以讀取一個文件夾中的所有文件 2-讀取日期文件(首選)現在閱讀netcdf這個日期目錄的多個文件

+0

輸入是什麼樣的?你想要輸出什麼? –

+0

輸入是netcdf文件,這個目錄在一個文件夾中輸出在這個階段並不重要,但是它將作爲一個文本文件 – Mrmr

+0

'multiaplefiles'是'多個文件'的拼寫錯誤還是別的嗎? –

回答

0

由於文件是按天遞增的,所以y你可以利用Python的dateutil rrule function來輕鬆地循環它們。

下面是如何在1998年1月循環每日文件的示例,然後在遍歷日期時讀取它們。

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

fl_pfx = 'disc2.gesdisc.eosdis.nasa.gov-3B42_Daily.' 
fl_sfx = '.7.nc4' 
strt_dt = datetime.date(1998,1,1) 
end_dt = datetime.date(1998,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') 
+0

我必須使作爲一個例子 目錄中的單獨目錄變量= 'C:/Users/ma570408/Documents/Dr.WAng/Haiti項目/ TRMM_Haiti/TRMM3B42 /' os.chdir(目錄) @ N1B4 – Mrmr

+0

我在執行某些操作(例如獲取特定位置或邊界的值)時要遍歷文件,然後將它們保存在每行中的文本文件中。所以,它看起來像1998.01.01,然後是第一行中x和y位置的值。然後它對所有選定具體日期的文件執行此操作。 – Mrmr

+0

@Mrmr請提供您到目前爲止所嘗試的代碼,然後我們可以提供更多的反饋和幫助。 – N1B4

0
 from mpl_toolkits.basemap import Basemap,cm 
    import matplotlib.pyplot as plt 
    import numpy as np 
    from netCDF4 import Dataset 
    import arcpy 
    import os 
    import datetime 
    from dateutil.rrule import rrule, DAILY 
    import netCDF4 


    directory = 'C:/Users/ma570408/Documents/Dr.WAng/Haiti project/TRMM_Haiti/TRMM3B42/' 
    os.chdir(directory) 
    result_array = np.array((0,28)) 
    fl_pfx = 'disc2.gesdisc.eosdis.nasa.gov-3B42_Daily.' 
    fl_sfx = '.7.nc4' 
    strt_dt = datetime.date(1998,1,1) 
    end_dt = datetime.date(1998,1,3) 

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) 
precip = netCDF4.Dataset(src_fl, 'r') 

precip = precip.variables['precipitation'] 
precip = precip[:] 

precip = np.transpose(precip) 


#precip.close() 

theLats = np.arange(-49.875,50,0.25) 
theLons = np.arange(-179.875,180,0.25) 

# Set all the missing values less than 0 to NaNs 
np.putmask(precip,precip<0,np.nan) 
# Plot the figure, define the geographic bounds 
fig = plt.figure(dpi=300) 
latcorners = ([17.25,21.25]) 
loncorners = ([-75.25,-67.57]) 
m = Basemap(projection='cyl',llcrnrlat=latcorners[0],urcrnrlat=latcorners[1],llcrnrlon=loncorners[0],urcrnrlon=loncorners[1]) 
# Draw coastlines, state and country boundaries, edge of map. 
m.drawcoastlines() 
m.drawstates() 
m.drawcountries() 
# Draw filled contours. 
clevs = np.arange(0,100,0.5) 
# Define the latitude and longitude data 
x, y = np.float32(np.meshgrid(theLons, theLats)) 
cs = m.contourf(x,y,precip,clevs,cmap=cm.GMT_drywet,latlon=True) 

parallels = np.arange(-50.,51,25.) 
m.drawparallels(parallels,labels=[True,False,True,False]) 
meridians = np.arange(-180.,180.,60.) 
m.drawmeridians(meridians,labels=[False,False,False,True]) 

# Set the title and fonts 
plt.title('01 Jan 1998 UTC Rain Rate') 
font = {'family' : 'normal', 'weight' : 'bold', 'size' : 4} 
plt.rc('font', **font) 

# Add colorbar 
cbar = m.colorbar(cs,location='right',pad="5%") 
cbar.set_label('mm/day') 
#plt.show() 


#plt.savefig('testTRMMmap.jpg',dpi=300) 
lons=[-72.357027,-72.459848,-72.49067 ,-72.682285,-72.338742,-72.483468,-72.375638,-72.625579 ,-72.621485,-72.689335,-72.701488,-72.334749,-72.081942 ,-72.019022, -71.992917 ,-71.950678 ,-71.841423 ,-72.237008 ,-72.15214,-72.178977,-71.825884,-72.12135,-72.271279,-72.192902,-72.107022,-72.126704,-72.397491,-72.529367] 
lats=[19.07039,19.218378,19.025554,19.098092,19.34189,19.490445,19.525487,19.493374,19.355849 ,19.310919,19.258246,18.909316,18.785043,19.144316,18.999588,18.866657,18.746084,18.830828,18.952298,19.188401,19.075994,19.464433,19.5202,19.402877,19.324003,18.66134,18.774135,18.88551] 

lons=np.array(lons) 
lats=np.array(lats) 

py = (lons +180) /0.25 
px = (lats +50) /0.25 
py=py.tolist() 
px=px.tolist() 

pixel_value=precip[px,py] 

pixel_value=np.array(pixel_value).reshape(1,28) 
pixel_value=str(pixel_value) 
print (pixel_value) 
thefile=open('test.txt','w') 
thefile.write(pixel_value) 
thefile.close() 

在這一點上,我努力我的輸出保存到每一行都有每個日期 唯一的問題一行和28點值的文本文件我只有最後打印出來的僅限reslut。 @ N1B4