Skip to content

Commit bc5d1fa

Browse files
committed
Updating to read ncdf file as input
1 parent 9272f78 commit bc5d1fa

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

demeter/demeter_io/reader.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import numpy as np
1616
import pandas as pd
1717
import gcamreader
18+
import xarray as xr
1819

1920

2021
class ValidationException(Exception):
@@ -390,8 +391,29 @@ def read_base(config, observed_landclasses, sequence_metric_dict, metric_seq, re
390391
:param region_seq: An ordered list of expected region ids
391392
392393
"""
394+
395+
xr_data = xr.open_dataset(config.observed_lu_file)
396+
xr_df = xr_data.to_dataframe().reset_index().dropna()
397+
#print(xr_df.head())
398+
#print(max(xr_df["region_id"]))
399+
400+
name_map = {
401+
var: xr_data[var].attrs.get("long_name", var) # fall back to var name if no long_name
402+
for var in xr_data.data_vars}
403+
404+
xr_df = xr_df.rename(columns=name_map)
405+
406+
407+
colnames_for_rename=(xr_df.drop(["region_id","basin_id","latitude","longitude"],axis=1).columns)
408+
409+
area= 0.00151872768*0.00151872768
410+
411+
xr_df[colnames_for_rename] = xr_df[colnames_for_rename].multiply(area, axis="index")
412+
xr_df= xr_df.dropna()
393413

394-
df = pd.read_csv(config.observed_lu_file, compression='infer')
414+
xr_df["fid"] = range(1, len(xr_df) + 1)
415+
df= xr_df
416+
#df = pd.read_csv(config.observed_lu_file, compression='infer')
395417

396418
# rename columns as lower case
397419
df.columns = [i.lower() for i in df.columns]

0 commit comments

Comments
 (0)