Python插值未测量高度的天气数据

2024-06-16 13:43:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要得到每一个高度在0米到25000米之间的天气数据。对于缺失的高度,最好的插值方法是什么?你知道吗

我试过这个,它看起来很准确,但它看起来不是很干净,依赖于很多模块。你知道吗

import pandas as pd
import numpy as np
import scipy.interpolate

df = pd.read_fwf(data, widths=[7] * 11)
df = df.drop([0, 1])
df = df.drop(columns=['DWPT', 'RELH', 'MIXR', 'THTA', 'THTE', 'THTV']) #Not needed data, only: pressure, temperature, windspeed and wind direction are needed.


HGHT = np.array(df['HGHT']) #Height
PRES = np.array(df['PRES']) #Pressure
TEMP = np.array(df['TEMP']) #Temperature
DRCT = np.array(df['DRCT']) #Wind direction
SKNT = np.array(df['SKNT']) #Wind speed

Pressure = scipy.interpolate.interp1d(HGHT, PRES) #I only did pressure for the test. 

>>> Pressure(203) #Insert height as argument.
array(1000.)

有没有更干净的方法?你知道吗

下面是我正在使用的清理天气表:

data = '''
PRES   HGHT   TEMP   DWPT   RELH   MIXR   DRCT   SKNT   THTA   THTE   THTV


 1022.0     11   26.6   14.6     48  10.32     95      8  297.9  328.2  299.7
 1014.0     81   23.6   12.6     50   9.11    111      9  295.6  322.1  297.2
 1012.0     98   23.5   12.5     50   9.05    115     10  295.6  322.0  297.2
 1000.0    203   22.6   11.6     50   8.65    120     14  295.8  321.0  297.3
  982.0    361   21.4   10.4     49   8.12    134     16  296.1  319.9  297.5
  979.0    387   21.8    8.8     43   7.31    136     16  296.7  318.3  298.1
  968.0    485   21.3    7.4     41   6.70    145     17  297.2  317.1  298.4
  956.0    593   20.8    5.8     38   6.08    146     16  297.8  315.9  298.9
  925.0    876   18.4    5.4     42   6.11    150     14  298.1  316.4  299.2
  850.0   1593   11.8    3.8     58   5.95    145     14  298.5  316.3  299.6
  832.0   1772   10.1    3.6     64   6.01    140     14  298.5  316.5  299.6
  827.0   1822    9.6    3.6     66   6.03    141     13  298.5  316.6  299.6
  814.0   1953    8.8   -0.2     53   4.65    145     12  299.0  313.2  299.9
  802.0   2076    9.8  -13.2     18   1.73    148     12  301.4  307.0  301.7
  775.0   2359    8.7  -18.5     13   1.15    155     10  303.1  306.9  303.3
  751.0   2619    7.6  -23.4      9   0.78    178     10  304.7  307.4  304.8
  728.0   2873    6.1  -19.6     14   1.12    200     10  305.8  309.5  306.0
  718.0   2986    5.4  -17.9     17   1.31    190     10  306.2  310.6  306.5
  700.0   3194    4.2  -14.8     24   1.74    180      8  307.1  312.9  307.4
  657.0   3704    1.6  -16.9     24   1.55    210     12  309.8  315.0  310.1
  616.0   4223   -1.1  -19.1     24   1.38    197     10  312.4  317.2  312.7
  594.0   4510   -3.5  -17.5     33   1.64    190     10  312.9  318.5  313.2
  573.0   4795   -5.9  -15.9     45   1.94    203     10  313.3  319.9  313.7
  561.0   4960   -7.3  -18.9     39   1.54    210     10  313.6  318.8  313.8
  558.0   5002   -7.7  -19.7     38   1.45    209     10  313.6  318.6  313.9
  551.0   5100   -7.3  -35.3      9   0.35    207     10  315.2  316.5  315.3
  550.0   5114   -7.3  -34.3     10   0.38    207     10  315.4  316.8  315.4
  529.0   5416   -8.6  -37.5      8   0.29    200     10  317.3  318.4  317.4
  528.0   5431   -8.7  -37.7      8   0.28    200     10  317.4  318.5  317.4
  500.0   5850  -12.1  -36.1     12   0.35    210     12  318.2  319.6  318.3
  489.0   6019  -13.3  -37.3     11   0.32    215     12  318.8  320.0  318.8
  479.0   6175  -14.4  -38.4     11   0.29    205     10  319.3  320.4  319.4
  456.0   6548  -17.1  -41.1     11   0.23    240     10  320.5  321.4  320.5
  449.0   6665  -17.9  -41.9     10   0.22    245      8  320.9  321.7  320.9
  443.0   6766  -17.9  -30.9     31   0.66    249      7  322.1  324.6  322.2
  427.0   7038  -20.6  -31.0     39   0.68    260      4  322.1  324.6  322.2
  400.0   7520  -25.3  -31.3     57   0.70    260      6  322.0  324.6  322.2
  391.0   7685  -26.9  -31.9     63   0.68    272      7  322.0  324.5  322.2
  390.0   7703  -27.1  -32.1     62   0.67    273      7  322.0  324.5  322.1
  384.0   7815  -27.3  -42.3     23   0.24    281      8  323.2  324.1  323.2
  366.0   8158  -29.3  -47.3     16   0.15    305     10  325.0  325.6  325.1
  355.0   8376  -30.5  -50.5     12   0.11    311      9  326.2  326.6  326.2
  300.0   9550  -39.1  -51.1     27   0.12    345      8  330.1  330.6  330.2
  262.0  10456  -47.0  -56.8     32   0.07    325     10  331.6  331.9  331.6
  250.0  10770  -49.7  -58.7     34   0.06    335     10  332.1  332.3  332.1
  238.0  11089  -52.3  -63.3     25   0.03    328      9  332.8  333.0  332.8
  220.0  11592  -56.5  -62.5     47   0.04    318      7  333.9  334.1  333.9
  208.0  11944  -59.1  -65.1     46   0.03    310      6  335.3  335.4  335.3
  200.0  12190  -60.9  -66.9     45   0.02    290     10  336.2  336.3  336.2
  197.0  12283  -61.5  -67.3     46   0.02    285     12  336.6  336.7  336.6
  188.0  12570  -63.5  -68.4     51   0.02    295     16  338.0  338.1  338.0
  181.0  12805  -61.0  -72.4     21   0.01    290     19  345.6  345.7  345.6
  176.0  12978  -59.2  -75.3     11   0.01    300     10  351.4  351.4  351.4
  174.0  13048  -58.5  -76.5      8   0.01    273      6  353.8  353.8  353.8
  173.0  13085  -58.4  -76.7      8   0.01    260      4  354.5  354.5  354.5
  169.0  13231  -58.2  -77.6      7   0.01    240     10  357.3  357.3  357.3
  162.0  13497  -57.7  -79.2      5   0.00    260     19  362.4  362.4  362.4
  159.0  13614  -57.5  -79.9      4   0.00    270     17  364.7  364.7  364.7
  156.0  13734  -57.3  -80.7      4   0.00    250     10  367.1  367.1  367.1
  151.0  13938  -56.9  -81.9      3   0.00    254     11  371.1  371.2  371.1
  150.0  13980  -56.9  -81.9      3   0.00    255     12  371.8  371.9  371.8
  142.0  14329  -56.7  -82.3      3   0.00    240     16  378.0  378.0  378.0
  135.0  14650  -56.6  -82.6      2   0.00    280     21  383.8  383.8  383.8
  132.0  14793  -56.5  -82.8      2   0.00    280     16  386.4  386.4  386.4
  129.0  14940  -56.5  -82.9      2   0.00    245     16  389.0  389.0  389.0
  127.0  15039  -56.4  -83.0      2   0.00    245     21  390.8  390.9  390.8
  122.0  15295  -56.3  -83.3      2   0.00    275     27  395.6  395.6  395.6
  119.0  15453  -56.2  -83.5      2   0.00    295     21  398.5  398.6  398.5
  117.0  15561  -56.2  -83.6      2   0.00    295     14  400.6  400.6  400.6
  115.0  15671  -56.1  -83.7      2   0.00    255      8  402.6  402.6  402.6
  114.0  15726  -56.1  -83.8      2   0.00    240     10  403.7  403.7  403.7
  109.0  16012  -56.0  -84.1      2   0.00    240     12  409.1  409.2  409.1
  107.0  16129  -55.9  -84.2      2   0.00    200     10  411.4  411.4  411.4
  100.0  16560  -55.7  -84.7      2   0.00    230     17  419.8  419.9  419.8
   98.0  16689  -55.6  -84.7      2   0.00    250     16  422.4  422.4  422.4
   95.0  16888  -55.5  -84.7      2   0.00    235     19  426.4  426.4  426.4
   92.0  17093  -55.4  -84.7      1   0.00    250     23  430.5  430.5  430.5
   87.0  17450  -55.2  -84.6      1   0.00    270     14  437.8  437.8  437.8
   85.0  17599  -55.1  -84.6      1   0.00    245      6  440.9  440.9  440.9
   82.0  17829  -55.0  -84.6      1   0.00    250     10  445.7  445.7  445.7
   78.0  18148  -54.9  -84.6      1   0.00    210     10  452.4  452.5  452.4
   75.0  18399  -54.7  -84.5      1   0.00    240     17  457.8  457.9  457.8
   74.0  18485  -54.7  -84.5      1   0.00    265     12  459.7  459.7  459.7
   70.0  18840  -54.5  -84.5      1   0.00    255     10  467.4  467.5  467.4
   69.0  18932  -54.0  -84.3      1   0.00    265     10  470.4  470.4  470.4
   66.0  19217  -52.5  -83.5      1   0.01      0      0  479.6  479.6  479.6
   65.9  19227  -52.5  -83.5      1   0.01    358      0  479.9  480.0  479.9
   59.0  19937  -53.1  -83.7      1   0.01    185     10  493.9  493.9  493.9
   57.0  20159  -53.3  -83.8      1   0.01    205     17  498.3  498.4  498.3
   55.0  20388  -53.5  -83.9      1   0.01    240     12  503.0  503.0  503.0
   52.0  20748  -53.9  -84.0      1   0.01    200      4  510.3  510.4  510.3
   51.0  20873  -54.0  -84.1      1   0.01    200     10  512.9  513.0  512.9
   50.0  21000  -54.1  -84.1      1   0.01    215     12  515.5  515.6  515.5
   42.6  22025  -53.9  -84.9      1   0.01    100     10  540.2  540.2  540.2
   40.0  22430  -52.1  -83.8      1   0.01     55     10  554.4  554.5  554.4
   39.0  22593  -51.4  -83.4      1   0.01    105     12  560.2  560.3  560.2
   38.8  22626  -51.3  -83.3      1   0.01    109     12  561.4  561.5  561.4
   38.0  22761  -51.4  -83.3      1   0.01    125     14  564.6  564.7  564.6
   37.0  22934  -51.4  -83.4      1   0.01    155     12  568.8  568.9  568.8
   36.0  23111  -51.5  -83.5      1   0.01    165     10  573.1  573.2  573.1
   33.0  23674  -51.7  -83.7      1   0.01    120     10  587.0  587.0  587.0
   32.0  23873  -51.8  -83.8      1   0.01    120     10  591.9  592.0  592.0
   30.0  24290  -51.9  -83.9      1   0.01      5      2  602.5  602.7  602.5
   28.0  24737  -52.0  -83.8      1   0.01     45     10  614.3  614.4  614.3
   27.0  24973  -52.0  -83.7      1   0.01     85     12  620.6  620.7  620.6
   26.0  25217  -52.1  -83.6      1   0.01    100     14  627.1  627.3  627.2
   23.0  26012  -52.2  -83.4      1   0.02    115     10  649.0  649.2  649.0
   22.2  26241  -52.3  -83.3      1   0.02     99      7  655.5  655.7  655.5
   21.0  26603  -51.1  -83.2      1   0.02     75      4  669.5  669.7  669.5
   20.0  26920  -50.1  -83.1      1   0.02     75      6  682.1  682.3  682.1
   19.0  27262  -49.4  -82.8      1   0.02     95     10  694.3  694.6  694.3
   18.0  27622  -48.6  -82.4      1   0.03    125     10  707.5  707.8  707.5
   17.0  28003  -47.9  -82.0      1   0.03     60     10  721.7  722.0  721.7
   16.0  28407  -47.0  -81.6      1   0.03    100     14  737.0  737.4  737.0
   13.0  29791  -44.1  -80.2      1   0.05     65     10  792.0  792.6  792.0
   12.0  30325  -43.0  -79.7      1   0.06    115     14  814.3  815.0  814.3
   11.0  30905  -41.8  -79.1      1   0.07    110      2  839.1  840.0  839.2
   10.0  31540  -40.5  -78.5      1   0.08     80      8  867.2  868.3  867.3
    9.0  32262  -39.1  -78.1      1   0.10     70     10  899.1  900.4  899.2
    8.1  32984  -37.7  -77.7      1   0.12    151     11  932.1  933.8  932.2
    8.0                                       160     12                     

'''


Tags: 方法importdfdata高度asnparray
2条回答

如果只需要线性插值,可以使用np.interp

请尝试一下,看看是否适合您

ind= np.linspace(0,500,500).astype(int)
df.set_index(['HGHT'], inplace=True)
df1=df.reindex(ind)
df1.interpolate(method= 'linear',limit_direction ='both', inplace=True)
df1.reset_index(inplace=True)

细节

ind= np.linspace(0,500,500).astype(int)

我们生成一个数字数组。在本例中,我将0生成到500,要生成的总数是500,因此每一步都是一个。对于你的数据集,你将不得不做ti到25000。所以,np.linspace(0,25000,25000)

df.set_index(['HGHT'], inplace=True)

我们将df的索引设置为生成的数组必须确认的列。你知道吗

df1=df.reindex(ind)

我们重新索引df&输出是一个新的数据帧,您也可以将它设置为相同的数据帧。你知道吗

df1.interpolate(method= 'linear',limit_direction ='both', inplace=True)
df1.reset_index(inplace=True)

我们使用dataframe.interpolate()对其余的值进行交错处理,最后将索引重置为与以前一样的数据帧。你知道吗

相关问题 更多 >