forked from DSSAT/NASAP_CHIRPS_WTH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NP.py
105 lines (89 loc) · 4.13 KB
/
NP.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
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
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# Name: NP.py
# Purpose: To get the NASAPOWER weather variables using the OPeNDAP server.
# Author: Oscar Castillo
# Range_Days, DataRequest, and nasap_point functions were adapted
# from the original version provided by the NASAPOWER developers team.
# Created: 06/04/2020
# Runs in Python 3.8.5
# Example:
#Columns of the CSV file:
#ID,Latitude,Longitude, nasapid, LatNP, LonNP
#nasap_gen(in_file, out_dir, sy, sm, sd, ey, em, ed, NASAP_ID = "nasapid")
#nasap_gen("C:\\Work\\Test\\XYpoints.csv", "C:\\Work\\Test\\Output", 2020, 10, 29, 2020, 10, 31)
#-------------------------------------------------------------------------------
from concurrent.futures import ThreadPoolExecutor, wait
from datetime import datetime, timedelta
import os, netCDF4
import pandas as pd
import xarray as xr
import numpy as np
import joblib
from datetime import datetime
import urllib.request
def Range_Days(Start_Date, End_Date):
Days = []
for each in range((End_Date - Start_Date).days + 1):
Days.append(Start_Date + timedelta(days=each))
return Days
def DataRequest(Collection):
URL, Parameter, Latitude, Longitude, Times = Collection
df = xr.open_dataset(URL).sel(lon=Longitude, lat=Latitude, time=Times, method='nearest')[Parameter]
return df
def nasap_point(ycoord, xcoord, sy, sm, sd, ey, em, ed):
List = [
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_t2mdew_lst.nc", "T2MDEW"),
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_t2m_lst.nc", "T2M_MIN"),
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_t2m_lst.nc", "T2M_MAX"),
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_rh2m_lst.nc", "RH2M"),
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_prectotcorr_lst.nc", "PRECTOTCORR"),
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_ws2m_lst.nc", "WS2M"),
("https://opendap.larc.nasa.gov/opendap/hyrax/POWER/daily/power_801_daily_allsky_sfc_sw_dwn_lst.nc", "ALLSKY_SFC_SW_DWN"),
]
Latitude = np.array([ycoord])
Longitude = np.array([xcoord])
Times = Range_Days(datetime(sy, sm, sd), datetime(ey, em, ed))
pool = ThreadPoolExecutor(None)
Futures = []
for URL, Parameter in List:
Collection = (URL, Parameter, Latitude, Longitude, Times)
Futures.append(pool.submit(DataRequest, Collection))
wait(Futures);
df = xr.merge([Future.result() for Future in Futures])
df1 = df.to_dataframe()
df2 = df1.reset_index()
ylat = df2.loc[0,"lat"]
xlon = df2.loc[0,"lon"]
return df2, ylat, xlon
def nasap_gen(in_file, out_dir, sy, sm, sd, ey, em, ed, NASAP_ID = "nasapid"):
ndays = (datetime(ey, em, ed) - datetime(sy, sm, sd)).days + 1
df_out = out_dir + "\\dfNASAP.pkl"
if os.path.isfile(df_out):
print("The", df_out, "file already exists. This file will be used.")
return
pt = pd.read_csv(in_file)
pt = pt.drop_duplicates(subset=[NASAP_ID])
df1 =[]
if pt.ndim == 1:
Id = pt.loc[NASAP_ID].astype(int)
lat = pt.loc["LatNP"]
lon = pt.loc["LonNP"]
df, y, x = nasap_point(lat, lon, sy, sm, sd, ey, em, ed)
id_vec = [Id] * ndays
df.insert(loc=0, column=NASAP_ID, value=id_vec)
df.to_csv(out_dir + "\\" + "{}.txt".format(Id), index=False, sep='\t')
else:
Id = pt.loc[:, NASAP_ID]
lat = pt.loc[:,"LatNP"]
lon = pt.loc[:,"LonNP"]
for n, i, j in zip(Id, lat, lon):
df, y, x = nasap_point(i, j, sy, sm, sd, ey, em, ed)
id_vec = [n] * ndays
df.insert(loc=0, column='ID', value=id_vec)
df1.append(df)
print(n)
result = pd.concat(df1)
joblib.dump(result, df_out)
print("The", df_out, "file was successfully created.")