forked from ACMG-CH4/CH4_TROPOMI_INV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_gridded_posterior.py
61 lines (47 loc) · 2.11 KB
/
make_gridded_posterior.py
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
import datetime
import xarray as xr
import os
import numpy as np
def make_gridded_posterior(posterior_SF_path, clusters_path, save_path):
'''
Lu's inversion code outputs the posterior scaling factors as a vector (.nc file)
HEMCO wants the scaling factors as a gridded product, by latitude/longitude
This script uses the posterior vector file and the clusters file to generate a gridded
version of the posterior scaling factors
Arguments
posterior_SF_path [str] : path to the posterior scaling factors from an inversion
clusters_path [str] : path to the cluster file, from which we will take coords information
save_path [str] : path where the gridded posterior should be saved
'''
# Load clusters and scaling factor data
clust = xr.load_dataset(clusters_path)
scfac = xr.load_dataset(posterior_SF_path)
# Map the scaling factors to the cluster grid
nlat = len(clust['lat'])
nlon = len(clust['lon'])
scfac_arr = np.zeros(clust['Clusters'].shape)
for ilat in range(nlat):
for ilon in range(nlon):
cluster_id = int(clust['Clusters'].values[ilat,ilon])
scfac_arr[ilat,ilon] = scfac['xhat'].values[cluster_id-1]
# Convert to data array
lat = clust['lat'].values
lon = clust['lon'].values
scfac_arr = xr.DataArray(scfac_arr, [("lat", list(lat)), ("lon", list(lon))], attrs={'units': "none"})
# Create dataset
ds_scfac = xr.Dataset({"SF_Nonwetland": (["lat", "lon"], scfac_arr)},
coords={"lon": ("lon", lon), "lat": ("lat", lat)})
# Add attribute metadata
ds_scfac.lat.attrs['units'] = 'degrees_north'
ds_scfac.lat.attrs['long_name'] = 'Latitude'
ds_scfac.lon.attrs['units'] = 'degrees_east'
ds_scfac.lon.attrs['long_name'] = 'Longitude'
ds_scfac.SF_Nonwetland.attrs['units'] = '1'
# Create netcdf
ds_scfac.to_netcdf(save_path)
if __name__ == '__main__':
import sys
posterior_SF_path = sys.argv[1]
clusters_path = sys.argv[2]
save_path = sys.argv[3]
make_gridded_posterior(posterior_SF_path, clusters_path, save_path)