|
""" |
|
prep_input.py |
|
|
|
This script reads in MERRA2 SLV,LND data in combination with |
|
flux station data together with HLS chips to produce a CSV with |
|
aggregate data values that can be used to train for GPP flux prediction. |
|
|
|
Author: Besart Mujeci, Srija Chakraborty, Christopher Phillips |
|
|
|
Usage: |
|
python prep_input.py |
|
""" |
|
import rclone |
|
from pathlib import Path |
|
import shutil |
|
import os |
|
import pandas as pd |
|
import netCDF4 as nc |
|
import numpy as np |
|
import pandas as pd |
|
|
|
|
|
|
|
def convert_HLS_date(chip_name): |
|
""" |
|
Extracts date string from HLS tile name and returns date object |
|
|
|
Args: |
|
chip_name (string): name of hls file |
|
|
|
Returns: |
|
datetime: datetime object of time string |
|
""" |
|
hls_date = chip_name.split('.')[3][:7] |
|
year = int(hls_date[:4]) |
|
day = int(hls_date[4:]) |
|
date = datetime(year, 1, 1)+timedelta(days=day-1) |
|
|
|
return date |
|
|
|
|
|
|
|
|
|
rawdir = '' |
|
merradir = '' |
|
cfg = "" |
|
result = rclone.with_config(cfg).run_cmd("ls", extra_args=[f"{rawdir}/"]) |
|
output_lines = result['out'].decode('utf-8').splitlines() |
|
file_list = [line.split(maxsplit=1)[1] for line in output_lines if line] |
|
result = rclone.with_config(cfg).run_cmd("ls", extra_args=[f"{merradir}/"]) |
|
output_lines = result['out'].decode('utf-8').splitlines() |
|
merras = [line.split(maxsplit=1)[1] for line in output_lines if line] |
|
|
|
|
|
|
|
|
|
|
|
station_file = './TILED_filtered_flux_sites_2018_2021.csv' |
|
|
|
spath = './all_inputs.csv' |
|
odir = '' |
|
|
|
|
|
|
|
|
|
stations = {} |
|
fn = open(station_file, 'r') |
|
for line in list(fn)[1:]: |
|
dummy = line.split(',') |
|
stations[dummy[1].strip()] = (dummy[1], float(dummy[9]), float(dummy[8]), dummy[3], dummy[4]) |
|
fn.close() |
|
flux_nets = os.listdir("./fluxnets/flux_sites_2018_2021/") |
|
|
|
|
|
|
|
chips = sorted(file_list) |
|
skipped = [] |
|
|
|
|
|
out_fn = open(spath, 'w') |
|
out_fn.write(f'Chip,Station,T2MIN,T2MAX,T2MEAN,TSMDEWMEAN,GWETROOT,LHLAND,SHLAND,SWLAND,PARDFLAND,PRECTOTLAND,GPP') |
|
|
|
|
|
for chip in chips: |
|
rclone.with_config(cfg).copy(f"{rawdir}/{chip}", f"./{chip}") |
|
|
|
|
|
chip_name = chip.split('/')[-1] |
|
tile = chip_name.split('.')[2][1:] |
|
station_name = chip_name.split('.')[6].split("_")[0] |
|
try: |
|
station = stations[station_name] |
|
except: |
|
print(f"exception - {('station dict indexing', station_name, tile)}") |
|
continue |
|
date = helpers.convert_HLS_date(chip_name) |
|
|
|
|
|
try: |
|
station_file = [fluxnet for fluxnet in flux_nets if station[0] in fluxnet][0] |
|
flux_df = pd.read_csv(f"genai-usra-east/impact/fluxnets/flux_sites_2018_2021/{station_file}") |
|
except: |
|
print(f"exception - {('station exception', station_name, tile)}") |
|
continue |
|
|
|
flux_times = np.array(flux_df.TIMESTAMP, dtype='str') |
|
flux_gpp = np.array(flux_df.GPP_NT_VUT_REF) |
|
try: |
|
quality_flag = np.array(flux_df.NEE_VUT_REF_QC) |
|
if quality_flag[flux_times==date.strftime("%Y%m%d")][0] >= 0.6: |
|
co2 = flux_gpp[flux_times==date.strftime("%Y%m%d")][0] |
|
else: |
|
print(f"co2 quality not met for - {('co2', station_name, tile)}") |
|
continue |
|
except: |
|
print(f"co2 quality not met for - {('co2 exception', station_name, tile)}") |
|
skipped.append(('co2 exception', station_name, tile)) |
|
continue |
|
|
|
|
|
|
|
merra_file = [file for file in merras if "slv" in file and str(date.strftime("%Y%m%d")) in file][0] |
|
rclone.with_config(cfg).copy(f"{merradir}/{merra_file}", f"./merra/") |
|
merra_fn = nc.Dataset(f'./merra/{merra_file}') |
|
|
|
|
|
mlons = merra_fn.variables['lon'][:] |
|
mlats = merra_fn.variables['lat'][:] |
|
xind = np.argmin((mlons-station[1])**2) |
|
yind = np.argmin((mlats-station[2])**2) |
|
|
|
|
|
tmax = np.max(merra_fn.variables['T2M'], keepdims=True, axis=0) |
|
tmin = np.min(merra_fn.variables['T2M'], keepdims=True, axis=0) |
|
tmean = np.nanmean(merra_fn.variables['T2M'][:,yind, xind]) |
|
tmax = tmax[0,yind,xind] |
|
tmin = tmin[0,yind,xind] |
|
tdewmean = np.nanmean(merra_fn.variables['T2MDEW'][:,yind, xind]) |
|
|
|
|
|
shutil.rmtree(Path(f"./merra")) |
|
|
|
|
|
merra_file = [file for file in merras if "lnd" in file and str(date.strftime("%Y%m%d")) in file][0] |
|
rclone.with_config(cfg).copy(f"{merradir}/{merra_file}", f"./merra/") |
|
merra_fn = nc.Dataset(f'./merra/{merra_file}') |
|
|
|
|
|
mlons = merra_fn.variables['lon'][:] |
|
mlats = merra_fn.variables['lat'][:] |
|
xind = np.argmin((mlons-station[1])**2) |
|
yind = np.argmin((mlats-station[2])**2) |
|
|
|
|
|
GWETROOT = np.nanmean(merra_fn.variables['GWETROOT'][:,yind,xind]) |
|
LHLAND = np.nanmean(merra_fn.variables['LHLAND'][:,yind,xind]) |
|
SHLAND = np.nanmean(merra_fn.variables['SHLAND'][:,yind,xind]) |
|
PARDFLAND = np.nanmean(merra_fn.variables['PARDFLAND'][:,yind,xind]) |
|
PRECTOTLAND = np.nanmean(merra_fn.variables['PRECTOTLAND'][:,yind,xind]) |
|
SWLAND = np.nanmean(merra_fn.variables['SWLAND'][:,yind,xind]) |
|
|
|
shutil.rmtree(Path(f"./merra")) |
|
shutil.rmtree(Path(f"./{tile}")) |
|
|
|
out_fn.write(f'\n{chip_name},{station[0]},{tmin:.2f},{tmax:.2f},{tmean:.2f},{tdewmean:.2f},{GWETROOT:.2f},{LHLAND:.2f},{SHLAND:.2f},{SWLAND:.2f},{PARDFLAND:2f},{PRECTOTLAND:2f},{co2}') |
|
|
|
out_fn.close() |
|
rclone.with_config(cfg).copy(f"{spath}", f"{odir}") |
|
print("DONE") |