问题遇到的现象和发生背景
读取栅格数据转换为了数组,然后传入该数组调用函数的时候,发出警告并一直在重复运行
Warning (from warnings module):
File "C: \Python27\AreGIS10. 7\lib\site-packages\scipy\stats_stats_mstats_ common.py", line97
sterrest=np.sqrt((1 - r**2) * ssym 1/ssxm / df)
RunitmeWarning:invalid value encountered in double_scalars
问题相关代码,请勿粘贴截图
-- coding:utf-8 --
import glob, os, sys
import numpy as np
import numpy.ma as ma
import time
import datetime
from osgeo import gdal #导入osgeo包的gdal模块,GDAL用于读栅格数据,函数返回Dataset对象
from scipy import stats, linalg
from scipy.stats import mstats
import pandas as pd
import matplotlib.pyplot as plt
def Calculate_trend(inFol, outFol, factor='p', inFormat = ".tif"):
factor_finList = []
for files in os.listdir(inFol): #listdir(path):列举目录下的所有文件
for year in range(1988, 1990):
if (factor in files) and (inFormat== files[-4:]) and (str(year) in files):
fileIn = os.path.join(inFol, files) #加入目录下的所有栅格文件
dataset = gdal.Open(fileIn, gdal.GA_ReadOnly) #读取栅格数据
#print"fileIn是:",fileIn
#print"dataset是:",dataset
if dataset is None:
print ('Could not open raster file'), fileIn
sys.exit(1) #exit(1):有错误退出
factor_array = dataset.ReadAsArray().astype(np.float32) # 将这个数组转化为 float32 位的数组
print"factor_array是:",factor_array
factor_array = factor_array.astype('float')
factor_finList.append(factor_array) #append()方法用于在列表末尾添加新的对象
print"factor_finList是:",factor_finList
factor_array = np.array(factor_finList) #创建数组
#print"factor_array是:",factor_array
kwargs = {"fillvalue": -9999.0, "plot": False} #
outArray= np.apply_along_axis(Func_single_linear_reg, 0, factor_array, **kwargs) #np.apply_along_axis将一个函数沿一个轴作用到数组中 调用“Func_single_linear_reg”
#export_array_trend(outFol, outArray, geoTran, geoProj, cols, rows, variable = 'lineareg', factor = factor, fillvalue= -9999.0, driverName='GTiff') #调用“export_array_trend”
inFol = r"D:\py" #读入的文件夹路径
outFol = r"D:\py\r" #输出的新文件的路径
if not os.path.exists(outFol): #os.path.exists()函数用来检验给出的路径是否真地存在 返回bool
os.makedirs(outFol) #makedirs(path):递归式的创建文件夹,注:创建已存在的文件夹将异常
get raster tiff infomation(GeoTransform, Projection)
global geoTran, geoProj, cols, rows, nodatav
for allRasters in os.listdir(inFol): #listdir(path):列举目录下的所有文件
print"第一个栅格数据:",allRasters
if allRasters.endswith(".tif"):
firstRasPath = os.path.join(inFol, allRasters) #加入目录下的所有栅格文件
break
print"第一个栅格数据的路径:",firstRasPath
firstdataset = gdal.Open(firstRasPath, gdal.GA_ReadOnly) #读取栅格数据
print"栅格数据:",firstdataset
cols= firstdataset.RasterXSize #读取列数
print"列数:",cols
rows= firstdataset.RasterYSize #读取行数
print"行数:",rows
bandnum = firstdataset.RasterCount #读取波段数
print"波段数:",bandnum
driver = firstdataset.GetDriver() #读取驱动(返回当前的磁盘驱动器?)
print"驱动器:",driver
geoTran = firstdataset.GetGeoTransform() #读取坐标转换参数
print"坐标转换参数:",geoTran
geoProj= firstdataset.GetProjection() #读取空间参照系
print"空间坐标系:",geoProj
nodatav = firstdataset.GetRasterBand(1).GetNoDataValue() #栅格数值替换???
print"nodata值:",nodatav
Calculate_trend(inFol, outFol, factor='p', inFormat = ".tif")
运行结果及报错内容
我的解答思路和尝试过的方法
是因为数据精度的问题嘛
我想要达到的结果
不报错