【问题标题】:Now to read multiple files of netcdf with this date directory现在用这个日期目录读取 netcdf 的多个文件
【发布时间】:2017-03-29 19:49:28
【问题描述】:

我以这个文件目录为例,我想读取所有文件作为日期到日期循环 disc2.gesdisc.eosdis.nasa.gov-3B42_Daily.19980101.7.nc4 所以日期 1990101 根据调查日期发生变化的部分 这些文件是 netcdf 文件 我知道如何逐个读取文件,但我无法运行包含多个文件的整个文件 举个例子 1-我们可以读取一个文件夹中的所有文件 2- 读取日期文件(首选)

【问题讨论】:

  • 输入是什么样的?你想要什么输出?
  • 输入是netcdf文件,这个目录在一个文件夹中,输出在这个阶段并不重要,但是它将是一个文本文件
  • “multiaplefiles”是“多个文件”的错字,还是别的什么?
  • 这是一个错字它是多个文件
  • disc2.gesdisc.eosdis.nasa.gov-3B42_Daily.19980101.7.nc4 第一天 disc2.gesdisc.eosdis.nasa.gov-3B42_Daily.19980102.7.nc4 第二天等等

标签: python date for-loop directory


【解决方案1】:
     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

【讨论】:

    【解决方案2】:

    由于文件按天递增,您可以利用 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')
    

    【讨论】:

    • 我是否必须创建一个单独的目录变量作为示例目录 = 'C:/Users/ma570408/Documents/Dr.WAng/Haiti project/TRMM_Haiti/TRMM3B42/' os.chdir(directory) @N1B4
    • 我想在执行一些操作时遍历文件,例如获取某个位置或边界的值,然后将它们保存在每一行的文本文件中。因此,它看起来像 1998.01.01,然后是第一行中 x 和 y 位置的值。然后它对所有选择了特定日期的文件执行此操作。
    • @Mrmr 请提供您目前尝试过的代码,我们可以提供更多反馈和帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2020-03-13
    • 1970-01-01
    • 2020-12-02
    • 2014-04-12
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多