为啥我的j和lat_index和lon-index都是0呀

extract variable(given region by coord) from .nc4 file

import numpy as np
from netCDF4 import Dataset
import os
import pandas as pd

import time

import re

def extract_nc(path, coord_path, variable_name, precision=2):
"""extract variable(given region by coord)from.nc file
input:
path: path of the source nc file
coord_path: path of the coord extracted by fishnet:OID,lat,lon
variable_name: name of the variable need to read
precision: the minimum precision of lat/lon,to match the lat/lon of source nc file

output:
    {variable_name}.txt [i,j]:i(file number) j(grid point number)
    lat_index.txt/lon_index.txt
    coord.txt
"""
print(f"variable:{variable_name}")
coord = pd.read_csv(coord_path, sep=",")  # read coord(extract by fishnet)
print(f"grid point number:{len(coord)}")
coord = coord.round(precision)  # coord precision correlating with .nc file lat/lon
result = [path + "/" + d for d in os.listdir(path) if d[-4:] == ".nc4"]
print(f"file number:{len(result)}")
variable = np.zeros((len(result), len(coord) + 1))  # save the path correlated with read order

# calculate the index of lat/lon in coord from source nc file
f1 = Dataset(result[0], 'r')
Dataset.set_auto_mask(f1, False)
lat_index = []
lon_index = []
lat = f1.variables["lat"][:]
lon = f1.variables["lon"][:]
count = len(coord)
indexs = range(count)
for j in indexs:
    lat_index.append(np.where(lat == coord["lat"][j])[0][0])
    lon_index.append(np.where(lon == coord["lon"][j])[0][0])
f1.close()

到这一步运行完显示错误:
lat_index.append(np.where(lat == coord["lat"][j])[0][0])
IndexError: index 0 is out of bounds for axis 0 with size 0

Process finished with exit code 1

一直到for j in indexs之前的语句都能正常运行,这一句理论上j应该是跟count一样是92671,但是就显示是0

我的coord是四列92671行,四列分别是FID_ OID lat lon

我自己知道了谢谢

代码和数据贴全点,帮你一起分析分析