Ros*_*ose -1 python arrays python-2.7
我想将数组的经度从 -180 度到 180 度格式更改为 0-360。有没有办法在不使用循环或 if 语句的情况下做到这一点?
lon_04_window = [ 1.39698386e-09 6.53551058e-02 1.30710210e-01 1.96065315e-01 2.61420419e-01 3.26775523e-01 3.92130628e-01 4.57485732e-01 5.22840836e-01 5.88195941e-01 6.53551045e-01 7.18906150e-01 7.84261254e-01 8.49616358e-01 9.14971463e-01 9.80326567e-01 1.04568167e+00 1.11103678e+00 1.17639188e+00 1.24174698e+00 1.30710209e+00 1.37245719e+00 1.43781230e+00 1.50316740e+00 1.56852251e+00 1.63387761e+00 1.69923272e+00 1.76458782e+00 1.82994292e+00 1.89529803e+00 1.96065313e+00 2.02600824e+00 2.09136334e+00 2.15671845e+00 2.22207355e+00 2.28742865e+00 2.35278376e+00 2.41813886e+00 2.48349397e+00 2.54884907e+00 2.61420418e+00 2.67955928e+00 2.74491439e+00 2.81026949e+00]
Run Code Online (Sandbox Code Playgroud)
循环代码:
for i in range(len(lon)):
if lon[i] < 0:
lon[i] = lon[i]+360
Run Code Online (Sandbox Code Playgroud)
假设lon_04_window是一个 numpy 数组,并且你想从 [0,360] 到 [-180,180]:
((lon_04_window - 180) % 360) - 180
Run Code Online (Sandbox Code Playgroud)
或者
np.mod(lon_04_window - 180.0, 360.0) - 180.0
Run Code Online (Sandbox Code Playgroud)
如果你真的想从 [-180,180] 范围到 [0, 360] 如 OP 中所述:
lon_04_window % 360
Run Code Online (Sandbox Code Playgroud)
如果lon_04_window不是numpy数组,请使其:
import numpy as np
lon_04_window = np.asarray(lon_04_window)
Run Code Online (Sandbox Code Playgroud)