素淼淼 2022-06-23 00:08 采纳率: 100%
浏览 53
已结题

为啥我的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

  • 写回答

2条回答 默认 最新

  • 素淼淼 2022-06-25 09:24
    关注

    我自己知道了谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月3日
  • 已采纳回答 6月25日
  • 修改了问题 6月23日
  • 创建了问题 6月23日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度