首页 > 解决方案 > 在 Python 中从外部文件调用函数

问题描述

我试图从 python 中的不同文件调用函数。我正在尝试以 NetCDF 格式处理来自 Goes 16 的卫星图像。我从保存在名为“remap”的 .py 文件中的函数所需的文件中提取不同的值。我的一段主要代码是这样的:

from remap import remap
# Calculate the image extent required for the reprojection
H = nc.variables['goes_imager_projection'].perspective_point_height
x1 = nc.variables['x_image_bounds'][0] * H
x2 = nc.variables['x_image_bounds'][1] * H
y1 = nc.variables['y_image_bounds'][1] * H
y2 = nc.variables['y_image_bounds'][0] * H

# Projection Prameters 
lat_0 = nc.variables['goes_imager_projection'].latitude_of_projection_origin
lon_0 = nc.variables['goes_imager_projection'].longitude_of_projection_origin
a = nc.variables['goes_imager_projection'].semi_major_axis
b = nc.variables['goes_imager_projection'].semi_minor_axis
f = 1/nc.variables['goes_imager_projection'].inverse_flattening

# Call the reprojection funcion
grid = remap(path, extent, resolution, x1, y1, x2, y2)

在我称为“remap”的 .py 文件中,函数定义为:

# Define KM_PER_DEGREE
KM_PER_DEGREE = 111.32

# GOES-16 Spatial Reference System
sourcePrj = osr.SpatialReference()
sourcePrj.ImportFromProj4('+proj=geos +h=' + H + ' +a=' + a + ' +b=' + b + '  +f=' + f + 'lat_0=' + lat_0 + ' +lon_0=' + lon_0 + ' +sweep=x +no_defs')

# Lat/lon WSG84 Spatial Reference System
targetPrj = osr.SpatialReference()
targetPrj.ImportFromProj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')

def remap(path, extent, resolution, x1, y1, x2, y2):
... (and so on)

现在我有两个不同的问题:

(1) 我的第一个问题是我从系统中得到一个错误说:“remap() 接受 4 个位置参数,但给出了 7 个”,我不明白为什么会这样,因为我已经在函数中定义了这 7 个参数第二个文件名为“remap”

(2) 我的第二个问题是我不知道如何调用从 NetCDF 文件中提取的原始代码中的值,例如:“lat_0、lon_0、a、b、f 和 H”以用于第二个文件,从一开始就需要使用“重新映射”功能。

有什么建议么?

标签: python-3.xmatplotlib-basemapcalling-convention

解决方案


你的第一个问题:

您如何定义 remap() 中所需的路径、范围和分辨率?

和你的第二个问题:

您不需要在重映射文件上调用这些参数,因为从主代码中您正在调用重映射并使用这 7 个参数进行重投影。


推荐阅读