-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path01_getNLDAS.R
More file actions
166 lines (137 loc) · 7.97 KB
/
01_getNLDAS.R
File metadata and controls
166 lines (137 loc) · 7.97 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
###########################################################
### Downloading NLDAS2 data for meteorological hourly forcing
### http://ldas.gsfc.nasa.gov/nldas/NLDAS2forcing.php
### Author: Hilary Dugan hilarydugan@gmail.com
### Date: 2019-09-30
###########################################################
library(RCurl)
library(lubridate)
library(raster)
library(ncdf4)
library(sf)
library(httr)
library(curl)
your_username <- ''
your_password <- ''
shapefile_link <- 'ShapeFiles/LakeMendota.shp'
output_folder <- '~/Documents/DSI/Paul_PModel_NLDAS/MendotaRawData/'
###########################################################
### Enter password information
###########################################################
#https://urs.earthdata.nasa.gov/profile <-- GET A EARTHDATA LOGIN
username = your_username
password = your_password
###########################################################
### Use shapefile of lake to set bounding box
###########################################################
# read in lake file to get bounding box
# Mendota Example
lakeShape = st_read(shapefile_link)
# lakeShape = st_read("~/Downloads/ME_805400/")
## Download the shapefile I sent you on Slack and write the path to it here:
extent = as.numeric(st_bbox(lakeShape))
###########################################################
### Set timeframe
###########################################################
out.ts = seq.POSIXt(as.POSIXct('2017-01-01 00:00:00',tz = 'GMT'),as.POSIXct('2022-12-31 23:00',tz='GMT'),by = 'hour')
vars = c('PEVAPsfc_110_SFC_acc1h', 'DLWRFsfc_110_SFC', 'DSWRFsfc_110_SFC', 'CAPE180_0mb_110_SPDY',
'CONVfracsfc_110_SFC_acc1h', 'APCPsfc_110_SFC_acc1h', 'SPFH2m_110_HTGL',
'VGRD10m_110_HTGL', 'UGRD10m_110_HTGL', 'TMP2m_110_HTGL', 'PRESsfc_110_SFC')
# Create output list of tables
output = list()
###########################################################
### Need to know how many cells your lake falls within
### Can download one instance of data and see how many columns there are
###########################################################
###########################################################
### Run hourly loop
###########################################################
# Start the clock!
ptm <- proc.time()
for (i in 1:length(out.ts)) {
print(out.ts[i])
yearOut = year(out.ts[i])
monthOut = format(out.ts[i], "%m")
dayOut = format(out.ts[i], "%d")
hourOut = format(out.ts[i], "%H%M")
doyOut = format(out.ts[i],'%j')
ydayOut = sprintf("%03d", yday(out.ts[i]))
filename = format(out.ts[i], "%Y%m%d%H%M")
# URL3 = paste('http://',username,':',password,'@hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?',
# 'FILENAME=%2Fdata%2FNLDAS%2FNLDAS_FORA0125_H.002%2F',yearOut,'%2F',doyOut,'%2FNLDAS_FORA0125_H.A',yearOut,monthOut,dayOut,'.',hourOut,'.002.grb&',
# 'FORMAT=bmV0Q0RGLw&BBOX=',extent[2],'%2C',extent[1],'%2C',extent[4],'%2C',extent[3],'&',
# 'LABEL=NLDAS_FORA0125_H.A',yearOut,monthOut,dayOut,'.',hourOut,'.002.2017013163409.pss.nc&',
# 'SHORTNAME=NLDAS_FORA0125_H&SERVICE=SUBSET_GRIB&VERSION=1.02&DATASET_VERSION=002',sep='')
#
# URL <- paste('http://hydro1.sci.gsfc.nasa.gov/daac-bin/OTF/HTTP_services.cgi?',
# 'FILENAME=%2Fdata%2Fs4pa%2FNLDAS%2FNLDAS_FORA0125_H.002%2F',yearOut,
# '%2F',doyOut,
# '%2FNLDAS_FORA0125_H.A',yearOut,monthOut,dayOut,'.',
# hourOut,'.002.grb&',
# 'FORMAT=bmV0Q0RGLw&BBOX=',
# extent[2],'%2C',
# extent[1],'%2C',
# extent[4],'%2C',
# extent[3],'&',
# 'LABEL=NLDAS_FORA0125_H.A',yearOut,monthOut,dayOut,'.',
# hourOut,'.002.2016116144611.pss.nc&',
# 'SHORTNAME=NLDAS_FORA0125_H&SERVICE=SUBSET_GRIB&VERSION=1.02&DATASET_VERSION=002',sep='')
## Patricia Tran note (2020-12-03 : I updated the link the webpage)
## Robert Ladwig note (2023-01-09 : I updated the link the webpage)
# This is an example of a link:
#
# https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FNLDAS%2FNLDAS_FORA0125_H.002%2F2017%2F001%2FNLDAS_FORA0125_H.A20170101.0000.002.grb&BBOX=43.07%2C-89.48%2C43.14%2C-89.36&FORMAT=bmM0Lw&DATASET_VERSION=002&SHORTNAME=NLDAS_FORA0125_H&LABEL=NLDAS_FORA0125_H.A20170101.0000.002.grb.SUB.nc4&VERSION=1.02&SERVICE=L34RS_LDAS
# https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FNLDAS%2FNLDAS_FORA0125_H.002%2F2017%2F004%2FNLDAS_FORA0125_H.A20170104.0200.002.grb&BBOX=43.07%2C-89.48%2C43.14%2C-89.36&FORMAT=bmM0Lw&DATASET_VERSION=002&SHORTNAME=NLDAS_FORA0125_H&LABEL=NLDAS_FORA0125_H.A20170104.0200.002.grb.SUB.nc4&VERSION=1.02&SERVICE=L34RS_LDAS
# https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FNLDAS%2FNLDAS_FORA0125_H.002%2F'
URL <- paste('https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FNLDAS%2FNLDAS_FORA0125_H.002%2F',
yearOut, '%2F',ydayOut,'%2FNLDAS_FORA0125_H.A',
# 20170104.0200.002.grb&BBOX=43.07%2C-89.48%2C43.14%2C-89.36&FORMAT=bmM0Lw&DATASET_VERSION=002&SHORTNAME=NLDAS_FORA0125_H&LABEL=NLDAS_FORA0125_H.A20170104.0200.002.grb.SUB.nc4&VERSION=1.02&SERVICE=L34RS_LDAS
yearOut, monthOut, dayOut, '.',
hourOut, '.002.grb&BBOX=',
round(extent[2], 2),'%2C',
round(extent[1], 2),'%2C',
round(extent[4], 2),'%2C',
round(extent[3], 2),
# &FORMAT=bmM0Lw&DATASET_VERSION=002&SHORTNAME=NLDAS_FORA0125_H&LABEL=NLDAS_FORA0125_H.A20170104.0200.002.grb.SUB.nc4&VERSION=1.02&SERVICE=L34RS_LDAS
'&FORMAT=bmM0Lw&DATASET_VERSION=002&SHORTNAME=NLDAS_FORA0125_H&LABEL=NLDAS_FORA0125_H.A',
yearOut,monthOut,dayOut,'.',
hourOut,
'.002.grb.SUB.nc4&VERSION=1.02&SERVICE=L34RS_LDAS',
sep='')
# URL <- paste('https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FNLDAS%2FNLDAS_FORA0125_H.002%2F',
# yearOut, '%2F001%2FNLDAS_FORA0125_H.A',
# yearOut, monthOut, dayOut, '.',
# hourOut, '.002.grb&FORMAT=bmM0Lw&BBOX=',
# round(extent[2], 2),'%2C',
# round(extent[1], 2),'%2C',
# round(extent[4], 2),'%2C',
# round(extent[3], 2),
# '&LABEL=NLDAS_FORA0125_H.A',
# yearOut,monthOut,dayOut,'.',
# hourOut,
# '.002.grb.SUB.nc4&SHORTNAME=NLDAS_FORA0125_H&SERVICE=L34RS_LDAS&VERSION=1.02&DATASET_VERSION=002',
# sep='')
# IMPORTANT MESSAGE Dec 05, 2016 The GES DISC will be migrating from http to https throughout December
# As part of our ongoing migration to HTTPS, the GES DISC will begin redirecting all HTTP traffic to HTTPS.
# We expect to have all GES DISC sites redirecting traffic by January 4th. For most access methods, the redirect will be transparent to the user.
# However, users with locally developed scripts or utilities that do not support an HTTP code 301 redirect may find that the scripts will fail.
# If you access our servers non-interactively (i.e. via a mechanism other than a modern web browser), you will want to modify your scripts to
# point to the HTTPS addresses to avoid the enforced redirect.
# x = download.file(URL3,destfile = paste(filename,'.nc',sep=''),mode = 'wb',quiet = T)
# x = download.file(URL,destfile = paste(filename,'.nc',sep=''),mode = 'wb',quiet = T)
lk <- URL
# or this with curl
h <- curl::new_handle()
curl::handle_setopt(
handle = h,
httpauth = 1,
userpwd = paste0(your_username,"@wisc.edu:",your_password)
)
# resp <- curl::curl_fetch_memory(lk, handle = h)
resp <- curl::curl_fetch_disk(url = lk,
path = paste(output_folder, filename,'.nc',sep=''),
handle = h)
#Sys.sleep(2)
}
# Stop the clock
proc.time() - ptm