Skip to content

Commit 492eee3

Browse files
author
Ayush Patnaik
authored
Merge pull request #41 from ayushpatnaikgit/readnl
Add function to read monthly nl files
2 parents 5fe700d + e0398ad commit 492eee3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/Karmana.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ include("geom_utils.jl")
3838

3939
# the below code is mostly stuff internal to xKDR
4040
include("data.jl")
41+
include("readnl.jl")
42+
export readnl
4143
export deltares_url
4244

4345
############################################################

src/readnl.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Define a function that sorts the files in a folder by date
2+
function sort_files_by_date(folder_path, start_date=Date(0), end_date=Date(today()))
3+
function extract_date(filename)
4+
# Extract the date range using regex
5+
m = match(r"_(\d{8})-\d{8}_", filename)
6+
return m == nothing ? nothing : DateTime(Date(m[1], "yyyymmdd"))
7+
end
8+
# Get the list of file names in the directory
9+
files = readdir(folder_path)
10+
# Filter out files that we couldn't extract a date from and are not within the date range
11+
files = filter(x -> (d = extract_date(x)) != nothing && start_date <= d <= end_date, files)
12+
# Sort files based on the date they contain
13+
sort!(files, by=extract_date)
14+
# Extract the list of dates
15+
dates = map(extract_date, files)
16+
# Return the sorted files and their corresponding dates
17+
return files, dates
18+
end
19+
"""
20+
readnl(xlims = X(Rasters.Between(65.39, 99.94)), ylims = Y(Rasters.Between(5.34, 39.27)), start_date = Date(2012, 04), end_date = Date(2023, 01))
21+
22+
Read nighttime lights data from a specific directory and return two raster series representing radiance and coverage.
23+
24+
# Arguments
25+
- `xlims`: An instance of `X(Rasters.Between(min, max))`, defining the X-coordinate limits. Default is `X(Rasters.Between(65.39, 99.94))`.
26+
- `ylims`: An instance of `Y(Rasters.Between(min, max))`, defining the Y-coordinate limits. Default is `Y(Rasters.Between(5.34, 39.27))`.
27+
- `start_date`: The start date (inclusive) of the period for which to load data. Should be an instance of `Date`. Default is `Date(2012, 04)`.
28+
- `end_date`: The end date (inclusive) of the period for which to load data. Should be an instance of `Date`. Default is `Date(2023, 01)`.
29+
30+
# Returns
31+
Two data cubes. The first contains the radiance data, and the second contains the coverage data. Each data cube includes data from the start_date to the end_date, sorted in ascending order.
32+
# Examples
33+
```julia
34+
using Rasters
35+
xlims = X(Rasters.Between(65.39, 75.39))
36+
ylims = Y(Rasters.Between(5.34, 15.34))
37+
start_date = Date(2015, 01)
38+
end_date = Date(2020, 12)
39+
rad_dc, cf_dc = readnl(xlims, ylims, start_date, end_date)
40+
"""
41+
function readnl(xlims = X(Rasters.Between(65.39, 99.94)), ylims = Y(Rasters.Between(5.34, 39.27)), start_date = Date(2012, 04), end_date = Date(2023, 01))
42+
lims = xlims, ylims
43+
rad_path = "/mnt/giant-disk/nighttimelights/monthly/rad/"
44+
rad_files, sorted_dates = sort_files_by_date(rad_path, start_date, end_date)
45+
cf_path = "/mnt/giant-disk/nighttimelights/monthly/cf/"
46+
cf_files, sorted_dates = sort_files_by_date(cf_path, start_date, end_date)
47+
rad_raster_list = [Raster(i, lazy = true)[lims...] for i in rad_path .* rad_files]
48+
cf_raster_list = [Raster(i, lazy = true)[lims...] for i in cf_path .* cf_files]
49+
rad_series = RasterSeries(rad_raster_list, Ti(sorted_dates))
50+
cf_series = RasterSeries(cf_raster_list, Ti(sorted_dates))
51+
rad_datacube = Rasters.combine(rad_series, Ti)
52+
cf_datacube = Rasters.combine(cf_series, Ti)
53+
return rad_datacube, cf_datacube
54+
end
55+
56+
"""
57+
readnl(geom, start_date = Date(2012, 04), end_date = Date(2023, 01))
58+
59+
Read nighttime lights data from a specific directory and return two raster data cubes representing radiance and coverage. This function also crops the rasters based on the given geometry.
60+
61+
# Arguments
62+
- `geom`: A geometry object, which will be used to crop the rasters. This could be an instance of `Geometry`, `Polygon`, `MultiPolygon`, etc. from a shapefile.
63+
- `start_date`: The start date (inclusive) of the period for which to load data. Should be an instance of `Date`. Default is `Date(2012, 04)`.
64+
- `end_date`: The end date (inclusive) of the period for which to load data. Should be an instance of `Date`. Default is `Date(2023, 01)`.
65+
66+
# Returns
67+
Two `RasterDataCube` instances. The first contains the cropped radiance data, and the second contains the cropped coverage data. Each `RasterDataCube` includes data from the `start_date` to the `end_date`, sorted in ascending order.
68+
69+
# Examples
70+
```julia
71+
using Geometries
72+
geom = Geometries.read("path_to_your_shapefile.shp") # replace this with your actual shapefile
73+
start_date = Date(2015, 01)
74+
end_date = Date(2020, 12)
75+
rad_dc, cf_dc = readnl(geom, start_date, end_date)
76+
```
77+
"""
78+
function readnl(geom, start_date = Date(2012, 04), end_date = Date(2023, 01))
79+
rad_path = "/mnt/giant-disk/nighttimelights/monthly/rad/"
80+
rad_files, sorted_dates = sort_files_by_date(rad_path, start_date, end_date)
81+
cf_path = "/mnt/giant-disk/nighttimelights/monthly/cf/"
82+
cf_files, sorted_dates = sort_files_by_date(cf_path, start_date, end_date)
83+
rad_raster_list = [crop(Raster(i, lazy = true), to = geom) for i in rad_path .* rad_files]
84+
cf_raster_list = [crop(Raster(i, lazy = true), to = geom) for i in cf_path .* cf_files]
85+
rad_series = RasterSeries(rad_raster_list, Ti(sorted_dates))
86+
cf_series = RasterSeries(cf_raster_list, Ti(sorted_dates))
87+
rad_datacube = Rasters.combine(rad_series, Ti)
88+
cf_datacube = Rasters.combine(cf_series, Ti)
89+
return rad_datacube, cf_datacube
90+
end

0 commit comments

Comments
 (0)