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