python-3.x - 在 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”以用于第二个文件,从一开始就需要使用“重新映射”功能。
有什么建议么?
解决方案
你的第一个问题:
您如何定义 remap() 中所需的路径、范围和分辨率?
和你的第二个问题:
您不需要在重映射文件上调用这些参数,因为从主代码中您正在调用重映射并使用这 7 个参数进行重投影。
推荐阅读
- vue.js - 将一个字段的值链接到另一个字段
- javascript - 创建一个 div 并给它一个 CSS 条目?
- c# - 无法转换类型的 COM 对象(不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)))
- javascript - 如何在 iOS 11.3 Safari 中禁用视口缩放?
- corda - 计划的活动从不运行
- sql-server - Microsoft SQL Server 区分大小写的查询
- c# - 为什么抛出异常时应用程序不会崩溃
- calendar - ui timepicker 中的“pi pi-chevron-up”没有显示任何图标来更改时间间隔。Insted 应该显示“fa fa-angle-up”
- css - Alt-Background 用于透明背景图像
- anylogic - AnyLogic - 存储为多个位置的资源池