简体   繁体   English

Numpy 形状不匹配

[英]Numpy shape mismatch

Good day,再会,

Sorry for the long detail.., I have 258 input files which includes 15 years of data.对不起,详细信息..,我有 258 个输入文件,其中包括 15 年的数据。 The purpose of this script is extract a particular column assigned as a variable within a user entered start and end year, The script was originally structured to extract the whole column in that time period, but now I am further filtering the desired values to be extracted as I am only interested in values between 0-1 in that column.此脚本的目的是在用户输入的开始和结束年份中提取指定为变量的特定列,该脚本最初的结构是提取该时间段内的整个列,但现在我正在进一步过滤要提取的所需值因为我只对该列中 0-1 之间的值感兴趣。 But, since the original script was designed to handle 731 samples (365 days + 365 days + 1).但是,由于原始脚本旨在处理 731 个样本(365 天 + 365 天 + 1)。 the script automatically looks for 731 sample values, And since I am not experienced with the Netcdf function in Scipy and Numpy modules that much.该脚本会自动查找 731 个样本值,并且由于我对 Scipy 和 Numpy 模块中的 Netcdf function 没有太多经验。 I don't know how to restructure this part of the script according to the filtered days having the desired values, As each year will have different number of values between 0-1, I am having trouble assigning the initial sample size according to the filtered days found by the script, I did found a way around it by giving dummy values of -1 to avoid broadcasting issues and to meet the requirement of 731 sample size.我不知道如何根据具有所需值的过滤天数来重构这部分脚本,由于每年在 0-1 之间的值数量不同,因此我无法根据过滤后的值分配初始样本大小脚本找到的天数,我确实找到了一种解决方法,通过给出 -1 的虚拟值来避免广播问题并满足 731 样本大小的要求。 but when I open the Netcdf file in ArcGIS I see this.但是当我在 ArcGIS 中打开 Netcdf 文件时,我看到了这一点。 which is okay as the max and min values are 1 and -1 respectively?哪个可以,因为最大值和最小值分别为 1 和 -1? so I am not able to see my desired values in the raster properly as ArcGIS only shows most of the -1s values in the display, I hope I am able to explain my problem, Can anyone please help me with structuring the sample size according to the filtered values found by the script.所以我无法在栅格中正确看到我想要的值,因为 ArcGIS 仅在显示中显示大部分 -1s 值,我希望我能够解释我的问题,任何人都可以帮助我根据脚本找到的过滤值。 Although, I did find NetCDF4Excel.虽然,我确实找到了 NetCDF4Excel。 an Excel add-in to just delete the dummy values stored in the,nc file in Excel.一个 Excel 加载项仅删除存储在 Excel 的 nc 文件中的虚拟值。 but it seems like this add-in doesn't work on 64-bit systems, I know the problem lies with days.但似乎这个加载项在 64 位系统上不起作用,我知道问题出在天上。 but I don't know how to fix it.但我不知道如何解决它。 Oh just forgot to mention that it's a 3D array containing the X coordinate, Y coordinate and date/time.哦刚刚忘了提到它是一个包含 X 坐标、Y 坐标和日期/时间的 3D 数组。 enter image description here在此处输入图像描述

# for what date?
start_year = input("Enter start year:")
end_year = input("End year:")

inidate = datetime.date(start_year,1,1)
enddate = datetime.date(end_year,12,31)

days = enddate.toordinal() - inidate.toordinal()+1 

This is the part of the script I use for filtering values in a particular column:这是我用于过滤特定列中的值的脚本的一部分:

for l in lixo:
        if int(l.split("\t")[0]) in range(inidate.year, enddate.year+1):
            if var==3:
                if previousValue==-10:                    
                    previousValue=float(l.split("\t")[var])
                    dado.append(-1)
                else:
                    currentValue=float(l.split("\t")[var])
                    if currentValue==0 and previousValue>0:
                        dado[-1]=previousValue
                        dado.append(currentValue)
                    else:
                        dado.append(-1)
                    previousValue=currentValue
            else:
                dado.append(float(l.split("\t")[var]))
        # putting data inside array.
        # Since data has lat & lon fixed uses dimension [:,lat_index,lon_index]

    print dado

The whole script is as follows:整个脚本如下:

import os
import sys
# handle dates...
import datetime
# SciPy netCDF and NumPy
from scipy.io.netcdf import *
from numpy import *

skip_lines = 6

# building file list and sorted lat lon list
file_list = os.listdir(sys.argv[1])

lat_t = []
lon_t = []
lat = []
lon = []

for f in file_list:
    lat_t.append(float(f.split("_")[1]))
    lon_t.append(float(f.split("_")[2]))

for i in lat_t:
    if i not in lat:
        lat.append(i)

for i in lon_t:
    if i not in lon:
        lon.append(i)
# putting in order. Lat should be from top to bottom
# lon from left to right 
lon.sort()
lat.sort()
lat.reverse()

del(lat_t)
del(lon_t)

#determining the parameter to use
print "Choose output parameter"
varini = input('Choose output (1 a 8)>')

#getting the column right
if int (varini) < 8:
    var = varini + 2
#set name of out_file. Named after parameter choice
if var == 3:
    var_txt = "ABCD"
    var_name = "ABCD"
# for what date?
start_year = input("Enter start year:")
end_year = input("End year:")

inidate = datetime.date(start_year,1,1)
enddate = datetime.date(end_year,12,31)

days = enddate.toordinal() - inidate.toordinal()+1

print "Go grab a coffee, this could take a while..."

#
# create array containig all data
# This is going to be huge. Create an array with -9999 (NoData)
# Then populate the array by reading each input file
#

all_data = zeros([days,len(lat),len(lon)], float)-9999

c = len(file_list)

# for each file in list
for f in file_list:
    # get lat & lon and it's index
    latitude = float(f.split("_")[1])
    longitude = float(f.split("_")[2])
    lat_id = lat.index(latitude)
    lon_id = lon.index(longitude)

    print "%i files to write." % c
    c = c -1

    infile = open(sys.argv[1]+f, "r")
    # here we skip the number of header lines
    # variable set at the beginning of the code
    lixo = infile.readlines()[skip_lines:]
    infile.close()
    dado = []
    previousValue = -10

    for l in lixo:
        if int(l.split("\t")[0]) in range(inidate.year, enddate.year+1):
            if var==3:
                if previousValue==-10:                    
                    previousValue=float(l.split("\t")[var])
                    dado.append(-1)
                else:
                    currentValue=float(l.split("\t")[var])
                    if currentValue==0 and previousValue>0:
                        dado[-1]=previousValue
                        dado.append(currentValue)
                    else:
                        dado.append(-1)
                    previousValue=currentValue
            else:
                dado.append(float(l.split("\t")[var]))

        # putting data inside array.
        # Since data has lat & lon fixed uses dimension [:,lat_index,lon_index]

    print dado
all_data[:,lat_id,lon_id] = dado

# writing NetCDF
#

ncfile = netcdf_file(var_txt+".nc", "w")

ncfile.Conventions = "COARDS"
ncfile.history = "Created using flux2cdf.py. " + datetime.date.today().isoformat()
ncfile.production = "ABCD output"

ncfile.start_date = inidate.isoformat()
ncfile.end_date = enddate.isoformat()

#create dimensions
ncfile.createDimension("X", len(lon))
ncfile.createDimension("Y", len(lat))
ncfile.createDimension("T", days)

#create variables
latvar = ncfile.createVariable("Y", "f4", ("Y",))
latvar.long_name = "Latitude"
latvar.units = "degrees_north"
latvar[:] = lat

lonvar = ncfile.createVariable("X", "f4", ("X",))
lonvar.long_name = "Longitude"
lonvar.units = "degrees_east"
lonvar[:] = lon

timevar = ncfile.createVariable("T", "f4", ("T",))
timevar.long_name = "Time"
timevar.units = "days since " + inidate.isoformat()
timevar[:] = range(0, days)

data_var = ncfile.createVariable(var_txt, "f4", ("T","Y","X"))
data_var.long_name = var_name+" calculated by ABCD"
data_var.missing_value = -9999.0
data_var.units = "milimeters"
data_var[:] = all_data

ncfile.close()

Sample data that's written in the.nc file with the required filtered values have been highlighted in yellow shown below:使用所需过滤值写入 .nc 文件中的示例数据已以黄色突出显示,如下所示:

enter image description here在此处输入图像描述

Sample data:样本数据:

245 files to write.
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0.0978, 0.0, -1, -1, -1, -1, -1, -1, -1, -1, 0.3112, 0.0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0.3112, 0.0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0.0978, 0.0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0.1334, 0.0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0.0978, 0.0, -1, -1, -1, -1, -1, 0.0978, 0.0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
244 files to write.

It turns out there is a much easier way to extract particular days of interest containing those days respective values by simply using this time slice tool in ArcGIS https://support.esri.com/en/technical-article/000011318 .事实证明,通过简单地使用 ArcGIS https://support.esri.com/en/technical-article/000011318中的时间切片工具,有一种更简单的方法可以提取包含这些日期各自值的特定感兴趣日期。 Now, I am able to extract individual raster files of each day of interest.现在,我能够提取感兴趣的每一天的单个光栅文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM