-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclimate.py
More file actions
125 lines (85 loc) · 2.54 KB
/
climate.py
File metadata and controls
125 lines (85 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 20 13:41:08 2023
@author: HIDRAULICA-Dani
"""
import os
import pandas as pd
import numpy as np
import netCDF4 as nc
import matplotlib.pyplot as plt
import rasterio
from rasterio.plot import show
from osgeo import gdal
os.chdir('D:/DANI/2023/TEMA_TORMENTAS/DATOS/CLIMAS/Map_KG-Global/')
os.listdir()
coord = pd.read_csv('D:/DANI/2023/TEMA_TORMENTAS/DATOS/CLIMAS/Map_KG-Global/KG_1986-2010_5m.csv')
Lon = coord['lon'].unique()
Lat = coord['lat'].unique()
# lons = []
# lats = []
# kgs = [[],[]]
# for row, i in coord.iterrows():
# print(row, i)
# lon = i[0]
# lat = i[1]
# kg = i[2]
# if (~np.isin(lon,lons)):
# lons.append(lon)
# if (~np.isin(lat,lats)):
# lats.append(lat)
# kgs.append(kg)
KG_grid = coord.pivot(index='lat', columns='lon', values='KG')
KG_grid.index.name = None
KG_grid.columns.name = None
KG_grid = KG_grid.sort_index(ascending=False)
KG_arr = KG_grid.values
# KG_arr[np.isnan(KG_arr)] = -9999
rows = KG_grid.index.values
cols = KG_grid.columns.values
KG_arr.shape
N = KG_grid.index[0]
# KG_grid.index[-1]
W = KG_grid.columns[0]
len(KG_grid.columns)
KG_grid.columns[1]-KG_grid.columns[2]
KG_grid.index[1]-KG_grid.index[2]
ras_filename = './KG_climate.tif'
new_transform = rasterio.transform.from_origin(west=W, north=N, xsize=0.08333333, ysize=0.08333333)
driver = gdal.GetDriverByName('GTiff')
driver.Register()
outds = driver.Create(ras_filename, xsize=KG_arr.shape[1], ysize=KG_arr.shape[0],
bands=1, eType=gdal.GDT_Float64)
outds.SetGeoTransform(new_transform)
driver = None
outds = None
raster = None
new_transform = rasterio.transform.from_origin(west=W, north=N, xsize=KG_arr.shape[1], ysize=KG_arr.shape[0])
new_transform
new_transform = rasterio.transform.from_origin(west=W, north=N, xsize=0.08333333, ysize=0.08333333)
rasterio.transform.xy(new_transform, rows, cols, zs=KG_arr, offset='center') #, **rpc_options)
plt.imshow(KG_arr)
KG = rasterio.open(
'./KG_climate.tif', 'w',
driver = 'GTiff',
height = KG_arr.shape[0],
width = KG_arr.shape[1],
count = 1,
nodata = -9999,
dtype = KG_arr.dtype,
crs = 4326,
transform = new_transform,
compress='lzw'
)
KG.close()
show(KG_arr)
KG_grid.values.dtype
show(KG_grid)
plt.scatter(coord['lon'], coord['lat'], s=1, marker=',')
lat.min()
lat.max()
lon.min()
lon.max()
len(coord)
len(lat)
len(lon)