1

I want to create a tiff file starting from a numpy array which contains temperature data points in the format lat,lon,T2

My data array (the columns contain respectively cell number,temperature,lon,lat)

A
Out[117]: 
array([[  1.        ,   4.05535278,  11.17913818,  43.73321152],
       [  2.        ,   4.05043945,  11.19158936,  43.73320389],
       [  3.        ,   4.04885254,  11.20401001,  43.73319244],
       [  4.        ,   4.05535278,  11.21646118,  43.73317719],
       [  5.        ,   4.05422363,  11.22891235,  43.73316574],
       [  6.        ,   4.05349121,  11.24136353,  43.73315048],
       [  7.        ,   4.05535278,  11.2538147 ,  43.73313141],
       [  8.        ,   4.05535278,  11.26626587,  43.73311234],
       [  9.        ,   4.05535278,  11.27871704,  43.73309326],
       [ 10.        ,   4.00774536,  11.29116821,  43.733078  ],
       [ 11.        ,   3.93987427,  11.30361938,  43.7330513 ],
       [ 12.        ,   3.85341797,  11.31604004,  43.73303223],
       [ 13.        ,   3.74407349,  11.32849121,  43.73300171],
       [ 14.        ,   3.61519775,  11.34094238,  43.73297501],
       [ 15.        ,   4.01278076,  11.17913818,  43.74220657],
       [ 16.        ,   4.00777588,  11.19158936,  43.74219513],
       [ 17.        ,   4.00539551,  11.20404053,  43.74217987],
       [ 18.        ,   4.01061401,  11.2164917 ,  43.74216843],
       [ 19.        ,   4.01061401,  11.22894287,  43.7421608 ],
       [ 20.        ,   4.01027832,  11.24139404,  43.74214172],
       [ 21.        ,   4.01070557,  11.25384521,  43.74212646],
       [ 22.        ,   4.01070557,  11.26629639,  43.74210358],
       [ 23.        ,   4.01079712,  11.27874756,  43.74208069],
       [ 24.        ,   3.96340332,  11.29119873,  43.74206924],
       [ 25.        ,   3.89577637,  11.3036499 ,  43.74204636],
       [ 26.        ,   3.80922852,  11.31610107,  43.7420311 ],
       [ 27.        ,   3.7003418 ,  11.32855225,  43.74200058],
       [ 28.        ,   3.57216797,  11.3409729 ,  43.74197006],
       [ 29.        ,   3.96712646,  11.17913818,  43.75120926],
       [ 30.        ,   3.96178589,  11.19158936,  43.751194  ],
       [ 31.        ,   3.95910034,  11.20407104,  43.75118256],
       [ 32.        ,   3.96288452,  11.21652222,  43.75117111],
       [ 33.        ,   3.96437988,  11.22897339,  43.75115585],
       [ 34.        ,   3.96462402,  11.24142456,  43.75114441],
       [ 35.        ,   3.96309814,  11.25387573,  43.75111771],
       [ 36.        ,   3.96309814,  11.2663269 ,  43.75110245],
       [ 37.        ,   3.96322021,  11.27877808,  43.75108337],
       [ 38.        ,   3.91613159,  11.29122925,  43.75106812],
       [ 39.        ,   3.84893188,  11.30368042,  43.75104141],
       [ 40.        ,   3.76265869,  11.31613159,  43.75102234],
       [ 41.        ,   3.65459595,  11.32858276,  43.75099182],
       [ 42.        ,   3.52745972,  11.34103394,  43.75097275],
       [ 43.        ,   3.92229614,  11.1791687 ,  43.76020432],
       [ 44.        ,   3.91524658,  11.19161987,  43.76018524],
       [ 45.        ,   3.91344604,  11.20407104,  43.76018524],
       [ 46.        ,   3.9147583 ,  11.21652222,  43.76015854],
       [ 47.        ,   3.91948853,  11.22897339,  43.76014328],
       [ 48.        ,   3.92107544,  11.24142456,  43.76013184],
       [ 49.        ,   3.91500244,  11.25387573,  43.76012039],
       [ 50.        ,   3.91500244,  11.2663269 ,  43.76009369],
       [ 51.        ,   3.91506348,  11.27880859,  43.7600708 ],
       [ 52.        ,   3.86837158,  11.29125977,  43.76005554],
       [ 53.        ,   3.80166016,  11.30371094,  43.76004028],
       [ 54.        ,   3.71566162,  11.31616211,  43.76000977],
       [ 55.        ,   3.60845337,  11.32861328,  43.75999451],
       [ 56.        ,   3.48250732,  11.34106445,  43.75996017],
       [ 57.        ,   3.86885986,  11.1791687 ,  43.76919174],
       [ 58.        ,   3.86156616,  11.19165039,  43.7691803 ],
       [ 59.        ,   3.85997925,  11.20410156,  43.76917648],
       [ 60.        ,   3.85991821,  11.21655273,  43.76915359],
       [ 61.        ,   3.86611328,  11.22900391,  43.76914215],
       [ 62.        ,   3.86812744,  11.24145508,  43.76912689],
       [ 63.        ,   3.86043701,  11.25390625,  43.76911163],
       [ 64.        ,   3.86043701,  11.26635742,  43.76909256],
       [ 65.        ,   3.86046753,  11.27880859,  43.76906967],
       [ 66.        ,   3.81447754,  11.29129028,  43.7690506 ],
       [ 67.        ,   3.74868164,  11.30374146,  43.76903152],
       [ 68.        ,   3.66365967,  11.31619263,  43.76900482],
       [ 69.        ,   3.55812988,  11.3286438 ,  43.7689743 ],
       [ 70.        ,   3.43413696,  11.34109497,  43.76895523],
       [ 71.        ,   3.81084595,  11.17919922,  43.77819443],
       [ 72.        ,   3.80358276,  11.19165039,  43.77817917],
       [ 73.        ,   3.80223999,  11.20410156,  43.77817154],
       [ 74.        ,   3.80117188,  11.21655273,  43.7781601 ],
       [ 75.        ,   3.80834351,  11.22903442,  43.77814102],
       [ 76.        ,   3.81054077,  11.2414856 ,  43.77812195],
       [ 77.        ,   3.80193481,  11.25393677,  43.77810669],
       [ 78.        ,   3.80193481,  11.26638794,  43.77808762],
       [ 79.        ,   3.80193481,  11.27883911,  43.77806854],
       [ 80.        ,   3.75686035,  11.2913208 ,  43.77804565],
       [ 81.        ,   3.69234619,  11.30377197,  43.77802658],
       [ 82.        ,   3.60875854,  11.31622314,  43.77800369],
       [ 83.        ,   3.50527344,  11.32867432,  43.77797318],
       [ 84.        ,   3.38381348,  11.34112549,  43.77794647],
       [ 85.        ,   3.74840698,  11.17919922,  43.78718567],
       [ 86.        ,   3.74151001,  11.19165039,  43.78717041],
       [ 87.        ,   3.74041138,  11.20413208,  43.7871666 ],
       [ 88.        ,   3.73867188,  11.21658325,  43.78715134],
       [ 89.        ,   3.74630127,  11.22903442,  43.78713608],
       [ 90.        ,   3.74840698,  11.24151611,  43.78712082],
       [ 91.        ,   3.73973999,  11.25396729,  43.78710556],
       [ 92.        ,   3.73973999,  11.26641846,  43.78708649],
       [ 93.        ,   3.73970947,  11.27886963,  43.78705978],
       [ 94.        ,   3.69576416,  11.29135132,  43.78704071],
       [ 95.        ,   3.63277588,  11.30380249,  43.78701019],
       [ 96.        ,   3.5510498 ,  11.31625366,  43.78699493],
       [ 97.        ,   3.45006714,  11.32870483,  43.78697205],
       [ 98.        ,   3.33162842,  11.34118652,  43.78694153],
       [ 99.        ,   3.68200073,  11.17922974,  43.79617691],
       [100.        ,   3.67577515,  11.19168091,  43.79616928],
       [101.        ,   3.67476807,  11.20413208,  43.79615402],
       [102.        ,   3.67281494,  11.21661377,  43.79613876],
       [103.        ,   3.68026123,  11.22906494,  43.79612732],
       [104.        ,   3.68200073,  11.24151611,  43.79610443],
       [105.        ,   3.67415771,  11.2539978 ,  43.79609299],
       [106.        ,   3.67415771,  11.26644897,  43.7960701 ],
       [107.        ,   3.6741272 ,  11.27890015,  43.79605865],
       [108.        ,   3.63149414,  11.29138184,  43.79602814],
       [109.        ,   3.57027588,  11.30383301,  43.79600525],
       [110.        ,   3.49077759,  11.31628418,  43.79598618],
       [111.        ,   3.39275513,  11.32876587,  43.79596329],
       [112.        ,   3.27773438,  11.34121704,  43.79593658],
       [113.        ,   3.6078125 ,  11.17922974,  43.80516815],
       [114.        ,   3.60305176,  11.19168091,  43.80516434],
       [115.        ,   3.60189209,  11.2041626 ,  43.80515289],
       [116.        ,   3.60067139,  11.21661377,  43.80513382],
       [117.        ,   3.60665283,  11.22909546,  43.80512238],
       [118.        ,   3.60720215,  11.24154663,  43.8051033 ],
       [119.        ,   3.60262451,  11.2539978 ,  43.80508804],
       [120.        ,   3.60262451,  11.26647949,  43.80506516],
       [121.        ,   3.60256348,  11.27893066,  43.80505371],
       [122.        ,   3.56148682,  11.29141235,  43.80502701],
       [123.        ,   3.50264893,  11.30386353,  43.80500412],
       [124.        ,   3.42611084,  11.3163147 ,  43.80498505],
       [125.        ,   3.33168945,  11.32879639,  43.80496216],
       [126.        ,   3.22097168,  11.34124756,  43.80493164],
       [127.        ,   3.53673706,  11.17926025,  43.81416702],
       [128.        ,   3.5329834 ,  11.19171143,  43.81415558],
       [129.        ,   3.53191528,  11.2041626 ,  43.81414795],
       [130.        ,   3.53084717,  11.21664429,  43.81413269],
       [131.        ,   3.53600464,  11.22909546,  43.81412125],
       [132.        ,   3.53600464,  11.24157715,  43.81410217],
       [133.        ,   3.53295288,  11.25402832,  43.8140831 ],
       [134.        ,   3.53295288,  11.26651001,  43.81406784],
       [135.        ,   3.53292236,  11.27896118,  43.81404114],
       [136.        ,   3.49331055,  11.29144287,  43.81402206],
       [137.        ,   3.43660889,  11.30389404,  43.81399536],
       [138.        ,   3.36281738,  11.31634521,  43.81397629],
       [139.        ,   3.271875  ,  11.3288269 ,  43.81395721],
       [140.        ,   3.16515503,  11.34127808,  43.81393051]])

Plotting the points I get this grid

points

What I'm trying to do, following this answer, is this

nx = np.unique(np.round(A[:,2],2)).shape[0]
ny = np.unique(np.round(A[:,3],3)).shape[0]
deltax = (np.max(A[:,2]) - np.min(A[:,2]))/(nx-1)
deltay = (np.max(A[:,3]) - np.min(A[:,3]))/(ny-1)

df = pd.DataFrame(A[:,1:])
df.columns = ['T2','longitude','latitude',]

gdf = gpd.GeoDataFrame(
    df,
    geometry=gpd.points_from_xy(df.longitude, df.latitude)
)

gdf.plot() # first image hereunder

geotif_file = "./t0.tif"
out_grd = make_geocube(
    vector_data=gdf,
    measurements=["T2"],
    resolution=(-deltay, deltax)
)

out_grd["T2"].rio.to_raster(geotif_file)

note: I checked that nx and ny correspond to reality given by the image I posted.

Adding the resulting tif to QGis, I see the grid

grid

where the points are the same as in the first picture. I notice that each cell does not have the points as center, while I was expecting to be so. How can I make the grid have the points as cell-centers?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.