wangyi111 commited on
Commit
1981005
·
verified ·
1 Parent(s): d103dba

upload airquality_s5p dataset

Browse files
airquality_s5p/airquality_s5p.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:96d82c0e7b8678cd9d8168324a7f2fae3a3f7ed03e97911a5c8b5738eecb6880
3
+ size 272559465
airquality_s5p/dataset_airquality_s5p.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.utils.data import DataLoader, Dataset
3
+ import cv2
4
+ import os
5
+ import rasterio
6
+ import numpy as np
7
+ from pyproj import Transformer
8
+ from datetime import date
9
+
10
+ class S5P_EEAAirQualityDataset(Dataset):
11
+ '''
12
+ 1973/494 train/test air quality dataset for NO2 and O3, measure from S5P, label from EEA
13
+ annual: 1x56x56x1 annual avg
14
+ seasonal: 4x56x56x1 seasonal avg
15
+ s5p nodata: -inf
16
+ label nodata: -3.4e38 # this needs to be masked out for loss and metric calculation
17
+ '''
18
+
19
+ def __init__(self, root_dir, modality='no2', mode='annual', split='train', meta=False):
20
+ self.root_dir = root_dir
21
+ self.mode = mode
22
+ self.modality = modality
23
+
24
+ if self.mode == 'annual':
25
+ mode_dir = 's5p_annual'
26
+ elif self.mode == 'seasonal':
27
+ mode_dir = 's5p_seasonal'
28
+
29
+ self.img_dir = os.path.join(root_dir, modality, split, mode_dir)
30
+ self.label_dir = os.path.join(root_dir, modality, split, 'label_annual')
31
+
32
+ self.fnames = sorted(os.listdir(self.label_dir))
33
+
34
+ self.meta = meta
35
+ if self.meta:
36
+ self.reference_date = date(1970, 1, 1)
37
+
38
+ def __len__(self):
39
+ return len(self.fnames)
40
+
41
+ def __getitem__(self, idx):
42
+ fname = self.fnames[idx] # label filename
43
+ label_path = os.path.join(self.label_dir, fname)
44
+ img_path = os.path.join(self.img_dir, fname.replace('.tif', ''))
45
+ img_fnames = os.listdir(img_path)
46
+ img_paths = []
47
+ for img_fname in img_fnames:
48
+ img_paths.append(os.path.join(img_path, img_fname))
49
+
50
+ # img
51
+ imgs = []
52
+ meta_infos = []
53
+ for img_path in img_paths:
54
+ with rasterio.open(img_path) as src:
55
+ img = src.read(1)
56
+ img = cv2.resize(img, (56,56), interpolation=cv2.INTER_CUBIC)
57
+ img[np.isnan(img)] = 0
58
+ img = torch.from_numpy(img).float()
59
+ img = img.unsqueeze(0)
60
+
61
+ if self.meta:
62
+ cx,cy = src.xy(src.height // 2, src.width // 2)
63
+ crs_transformer = Transformer.from_crs(src.crs, 'epsg:4326')
64
+ lon, lat = crs_transformer.transform(cx,cy)
65
+ #lon, lat = cx, cy
66
+ img_fname = os.path.basename(img_path)
67
+ date_str = img_fname.split('_')[0][:10]
68
+ date_obj = date(int(date_str[:4]), int(date_str[5:7]), int(date_str[8:10]))
69
+ delta = (date_obj - self.reference_date).days
70
+ meta_info = np.array([lon, lat, delta, 0]).astype(np.float32)
71
+ else:
72
+ meta_info = np.array([np.nan,np.nan,np.nan,np.nan]).astype(np.float32)
73
+
74
+ imgs.append(img)
75
+ meta_infos.append(meta_info)
76
+ if self.mode == 'seasonal':
77
+ # pad to 4 images if less than 4
78
+ while len(imgs) < 4:
79
+ imgs.append(img)
80
+ img_paths.append(img_path)
81
+ meta_infos.append(meta_info)
82
+ # label
83
+ with rasterio.open(label_path) as src:
84
+ label = src.read(1)
85
+ label = cv2.resize(label, (56,56), interpolation=cv2.INTER_CUBIC) # 0-650
86
+ label[label<-1e10] = np.nan
87
+ label[label>1e10] = np.nan
88
+ #label[np.isnan(label)] = -1e10
89
+ label = torch.from_numpy(label.astype('float32'))
90
+ #nan_mask = (label > -1e10)
91
+
92
+ if self.mode == 'annual':
93
+ return imgs[0], meta_infos[0], label#, nan_mask #,label_path
94
+ elif self.mode == 'seasonal':
95
+ return imgs[0], imgs[1], imgs[2], imgs[3], meta_infos[0], meta_infos[1], meta_infos[2], meta_infos[3], label#, nan_mask #,label_path
96
+
97
+ if __name__ == '__main__':
98
+ dataset = S5P_EEAAirQualityDataset(root_dir='./airquality_s5p', modality='no2', mode='annual', split='train')
99
+ dataloader = DataLoader(dataset, batch_size=1, shuffle=False)
100
+ for i, data in enumerate(dataloader):
101
+ print(data[0].shape, data[1].shape, data[2].shape, data[3].shape)
102
+ break