Overview
Prerequisites
Users must have a NASA Earthdata account to download or, in this case, stream data from a remote NASA Earthdata OPeNDAP server.
Users must also have an access to IDL, at least version 8.8.
Procedure
Described below is a simple IDL recipe intended to give an idea how to exploit remote OPeNDAP access and concatenate many Orbiting Carbon Observatory (OCO) Lite daily files into one data object, without downloading individual Lite files. In effect, the result is an ultra-light OCO-2 data object, containing concatenated vectors of XCO2, and the geolocation and quality flags, for a time period set by the user.
Users must have a NASA Earthdata account to download or, in this case, stream data from a remote OPeNDAP NASA Earthdata server.
The recipe takes advantage of the ability of the newest (4.7 and newer) netCDF-4 libraries in IDL to perform authentication with the Earthdata authenticator (urs.earthdata.nasa.gov); we'll call this process simply "Earthdata Login" (EDL). This requires:
- Earthdata account
- ".netrc" file
- ".dodsrc" file
The newer netCDF-4 libraries, if challenged for credentials on an attempt to open a file on a remote OPeNDAP server, know how to parse the ".dodsrc" and then ".netrc" files, get credentials from the latter, and send them to the registration system for authentication that the credentials are valid. The ".dodsrc" file must be familiar to those users who are aware of GrADS (Grid Analysis and Display System) tool. This recipe does not need GrADS installed. The ".netrc" file is familiar to all users who download files with "wget" or "curl", from any data service that requires authentication of credentials (user id and password).
By "newer" netCDF-4 libraries, we mean at least version 4.7.1. This should be checked from the IDL command line as follows, which will return the netCDF-4 version your IDL is packaged with:
help,/dlm,'ncdf'
A side benefit of this recipe is that it also gives an example of how to use the NASA Earthdata "Common Metadata Repository" (CMR). The latter is in effect a database that can be queried with specially formatted URLs. In our case, the expected response of the CMR query is a list of OPeNDAP URLs that are used to access the data. The response from CMR contains a lot of extra information that is not needed in this application, so we have to parse it to get the OPeNDAP URLs only. To make things simple, we wrap both the query and the parsing in one system call, and employ the "awk" shell utility which (while in general is standard on Linux-based platforms), can be downloaded for Windows as well.
Note that CMR limits its return to 2000 data file links per query. This is equivalent to ~5 years of OCO daily Lite files. We don't recommend aggregating more than ONE year of Lite files at a time, however. Aggregating one year will take about 20 minutes, and if some timeout or other disruption occurs, you'll have to start over.
The recipe is written in IDL (Interactive Data Language).
1. Earthdata authentication (EDL)
We will start with the necessary ".dodsrc" and ".netrc" files, which are simple text files. If you work on a Linux-like multi-user platform, the simplest procedure is to ensure that the two files are in your "home" directory. It may be something like "/Users/your_username/"
Here is an example of a bare-bones ".dodsrc" file content:
COOKIE_JAR=/Users/your_username/.urs_cookiesHTTP.NETRC=/Users/your_username/.netrc
It is not critical if .urs_cookies is empty, or doesn't exist. But it is in general beneficial to have it. It may be populated by previous data downloads using "wget" or "curl", correspondingly using command-line options "--save-cookies ~/.urs_cookies" with wget, and "-c ~/.urs_cookies" with curl, on linux-like platforms. (Details on generic data access and using cookies.)
Note that ".dodsrc" points to the file with your Earthdata credentials, ".netrc". The latter must contain your Earthdata credentials:
machine urs.earthdata.nasa.gov login your_EDL_user_id password your_EDL_password
It is critical that you have your EDL credentials accurate in the .netrc file! Only the strings in bold italics have to be substituted for the particular user's credentials (user id and password). The rest of the strings on that line are literals (have to be taken literally). If you mistype your credentials or something else in your .netrc, or if netCDF libraries can't find the .netrc file, you should receive an error like:
syntax error, unexpected WORD_WORD, expecting SCAN_ATTR or SCAN_DATASET or SCAN_ERROR context: HTTP^ Basic: Access denied.
Let's summarize the .dodsrc and .netrc files.
Contents of .dodsrc :COOKIE_JAR=/Users/your_username/.urs_cookiesHTTP.NETRC=/Users/your_username/.netrc
Contents of .netrc :machine urs.earthdata.nasa.gov login your_EDL_user_id password your_EDL_password
2. IDL program
With the Earthdata Login part completed, the following is the IDL code snippet. If stored in, say, "oco2.pro" text file, it can be executed as an IDL "main" program, as described.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; At the IDL prompt, execute:
; .rnew oco2.pro
; Set the time period to concatenate.
; Note the ISO 8601 time format. It is critical to adhere to it.
; There are no checks to ensure the format is correct.
; These time strings are used in the CMR query, and if the format is not correct, null
; result will be received.
t1 = '2019-10-01T00:01:00Z'
t2 = '2019-10-31T23:59:00Z'
t1_t2 = t1 + ',' + t2
; Earthdata dataset short name, and dataset version strings.
; Make sure to use the currently available dataset version.
short_name = 'OCO2_L2_Lite_FP' & vid='11.1r'
; Variables names to access and concatenate.
sdname = ['longitude','latitude','time','xco2','xco2_qf_simple_bitflag','xco2_quality_flag']
Nsd = N_ELEMENTS(sdname)
; Here we build a one-line command that executes the CMR URL query
; and parses the response from the CMR.
; The command should look like this one-liner string:
; curl -sS "https://cmr.earthdata.nasa.gov/search/granules.native?short_name=OCO2_L2_Lite_FP&version=11.1r&temporal[]=2020-03-01T00:01:00Z,2020-03-31T00:59:00Z&pretty=true&page_size=2000" | awk -F">|<" '/<URL>http.*opendap/ {print $3}'
;
; You can try from your shell, but remember - It is a one-line command, so no line
; breaks in the above string! Make sure the OCO2_L2_Lite_FP version is right!
; Depending on the shell, the square brackets may need to be escaped, like this: \[ \]
; Note the page_size=2000 in the query URL.
; This is the CMR limit on the number of data files links
; to return per query, and is equivalent to ~5 years of Lite files.
; We don't recommend setting the time period (t1,t2, above)
; to more than ONE year, though.
; Aggregating one year will be a process about 20 minutes long, and if
; some timeout or other disruption occurs, you'll have to start all over.
;
; The CMR query used here results in a response in XML format.
; If the query is successful, the response contains a lot of extra
; information, so we need to parse only the opendap URLs by simple means.
; Here we use "awk".
; The CMR query and awk parsing are stored as one
; string, "cmd", which will be used as an argument in the IDL system call "SPAWN".
cmr = 'curl -sS ' + '"https://cmr.earthdata.nasa.gov/search/granules.native?short_name=' + short_name + '&version=' + vid + '&temporal[]=' + t1_t2 + '&pretty=true&page_size=2000"'
cmd = cmr + " | awk -F'>|<' '/<URL>http.*opendap/ {print $3}' > temp.txt"
; Here we spawn a child shell process, which is the actual query of CMR and awk parsing
; of the response.
SPAWN, cmd
Nf = FILE_LINES('temp.txt')
; If CMR returns null results, there is no sense to continue.
IF Nf EQ 0 THEN BEGIN
PRINT, "No granules found!"
PRINT, "Query and parse: ", cmd
PRINT, "Check: Begin/End Dates; Dataset name and version"
STOP
ENDIF
; If we pass the above check, CMR response contains at least one OPeNDAP URL.
; The list of all discovered OPeNDAP URL within the set time period are
; stored in text file "temp.txt"
flnm=STRARR(Nf)
OPENR,1,'temp.txt'
READF,1,flnm
CLOSE,1
; This is only preparation of data objects in the name space.
; No storage is allocated here.
lon = !null
lat = !null
time = !null
xco2 = !null
qfsmpl = !null
qual = !null
; This is the loop where OPeNDAP URLs are accessed by the netCDF,
; and where the concatenation takes place.
FOR i=0, Nf-1 DO BEGIN
PRINT,flnm[i]
fid = NCDF_OPEN(flnm[i])
vid0 = NCDF_VARID(fid,sdname[0])
vid1 = NCDF_VARID(fid,sdname[1])
vid2 = NCDF_VARID(fid,sdname[2])
vid3 = NCDF_VARID(fid,sdname[3])
vid4 = NCDF_VARID(fid,sdname[4])
vid5 = NCDF_VARID(fid,sdname[5])
; Read data from one file, flnm[i]
NCDF_VARGET, fid, vid0, tmp0
NCDF_VARGET, fid, vid1, tmp1
NCDF_VARGET, fid, vid2, tmp2
NCDF_VARGET, fid, vid3, tmp3
NCDF_VARGET, fid, vid4, tmp4
NCDF_VARGET, fid, vid5, tmp5
; This is the concatenation. The final concatenated data are
; in variables lon, lat, time, xco2, qfsmpl and qual
lon = [lon, tmp0]
lat = [lat, tmp1]
time = [time, tmp2]
xco2 = [xco2, tmp3]
qfsmpl = [qfsmpl, tmp4]
qual = [qual, tmp5]
; Close the current file handle after getting the data from the file.
; If you don't close, you may run out of allowed number of opened handles.
NCDF_CLOSE, fid
ENDFOR
; At this point, all the variables have been concatenated as 1D vector variables
; in the computer memory.
; They can be manipulated by all IDL mathematical functions, or saved.
; An example quality screening, based on variable "xco2_quality_flag"
; from the Lite Full-Physics files. This quality flag is stored as an IDL variable "qual".
; The best quality is when qual=0.
best = WHERE(qual EQ 0, /null)
xco2_best = xco2[best]
lon_best = lon[best]
lat_best = lat[best]
; The following section is just an example of saving the concatenated data into a netCDF-4 file.
; It is simplified to the bare minimum to write into your current working directory.
; Make sure you have enough space.
; It will create or overwrite netCDF-4 data file "test.nc"
; Uncomment the following lines:
; cnt = N_ELEMENTS(xco2)
; fout_id = NCDF_CREATE('test.nc', /NETCDF4_FORMAT,/clobber)
; dimid = NCDF_DIMDEF(fout_id, 'soundingid', cnt)
; varid1 = NCDF_VARDEF(fout_id,'xco2',[dimid],gzip=5,/float)
; varid2 = NCDF_VARDEF(fout_id,'longitude',[dimid],gzip=5,/float)
; varid3 = NCDF_VARDEF(fout_id,'latitude',[dimid],gzip=5,/float)
; varid4 = NCDF_VARDEF(fout_id,'time',[dimid],gzip=5,/double)
; varid5 = NCDF_VARDEF(fout_id,'xco2_quality_flag',[dimid],gzip=5,/short)
; NCDF_CONTROL, fout_id, /ENDEF ; Put the file into data mode.
; NCDF_VARPUT, fout_id, varid1, xco2
; NCDF_VARPUT, fout_id, varid2, lon
; NCDF_VARPUT, fout_id, varid3, lat
; NCDF_VARPUT, fout_id, varid4, time
; NCDF_VARPUT, fout_id, varid5, qual
; NCDF_CLOSE, fout_id
END ; This line is critical, don't comment it , don't miss it!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;