11using NCDatasets
22using DataStructures
33using ClimaArtifactsHelper
4+ import Dates
45
56"""
67 parse_year_season(path)
@@ -12,12 +13,25 @@ The seasons MAM, JJA, SON, and DJF map to 1, 2, 3, and 4.
1213function parse_year_season (path)
1314 # File name should look like "2008-MAM-..."
1415 filename = last (splitpath (path))
15- s = first (filename, 8 )
16+ s = split (filename, " _ " )[ 1 ]
1617 year, season = split (s, " -" )
1718 SEASON_ORDER = Dict (" MAM" => 1 , " JJA" => 2 , " SON" => 3 , " DJF" => 4 )
1819 return (parse (Int, year), SEASON_ORDER[season])
1920end
2021
22+ """
23+ parse_year_month(path)
24+
25+ Given a file `path`, return the year and month as integers.
26+ """
27+ function parse_year_month (path)
28+ # File name should look like "2008-MAM-..."
29+ filename = last (splitpath (path))
30+ s = split (filename, " _" )[1 ]
31+ year, month = split (s, " -" )
32+ return parse .(Int, (year, month))
33+ end
34+
2135"""
2236 get_and_sort_files(dir)
2337
@@ -31,7 +45,13 @@ function get_and_sort_files(dir)
3145 # For each year, the order of the seasons are "MAM", "JJA", "SON", "DJF"
3246 # Note that the first month is used, so 2006-DJF refers to Dec 2006, Jan
3347 # 2007, and Feb 2007
34- sort! (files, by = parse_year_season)
48+ if occursin (" seasonal" , dir)
49+ sort! (files, by = parse_year_season)
50+ elseif occursin (" monthly" , dir)
51+ sort! (files, by = parse_year_month)
52+ else
53+ error (" Only support files with monthly or seasonal periods" )
54+ end
3555 return files
3656end
3757
@@ -81,6 +101,16 @@ function stitch_cloud_data(files, output_filepath)
81101 defVar (ds, varname, data, dimnames (mfds[varname]), attrib = mfds[varname]. attrib)
82102 end
83103
104+ # Seasonal files have a time dimension, but monthly files doesn't
105+ if occursin (" monthly" , first (files))
106+ @info " Adding time dimension for monthly dataset"
107+ year_and_month = parse_year_month .(files)
108+ data = [Dates. DateTime (year, month) for (year, month) in year_and_month]
109+ attrib = Dict (" units" => " days since 2006-06-01 00:00:00" , " calendar" => " proleptic_gregorian" )
110+ first (data) == Dates. DateTime (2006 , 6 ) || error (" The first date is not 2006-06-01" )
111+ defVar (ds, " time" , data, (" time" , ), attrib = attrib)
112+ end
113+
84114 # Close datasets
85115 close (ds)
86116 close (mfds)
@@ -179,7 +209,7 @@ function make_artifact(filepath_names)
179209 end
180210
181211 for filepath_name in filepath_names
182- if endswith (filepath_name, " radarlidar_seasonal_2 .5x2.5.nc" )
212+ if endswith (filepath_name, " _2 .5x2.5.nc" )
183213 filename = last (splitpath (filepath_name))
184214 mv (filepath_name, joinpath (output_dir, filename), force = true )
185215 end
@@ -196,7 +226,7 @@ function make_artifact(filepath_names)
196226 end
197227
198228 for filepath_name in filepath_names
199- if endswith (filepath_name, " radarlidar_seasonal_10x10 .nc" )
229+ if endswith (filepath_name, " _10x10 .nc" )
200230 filename = last (splitpath (filepath_name))
201231 mv (filepath_name, joinpath (output_dir_lowres, filename), force = true )
202232 end
@@ -208,13 +238,15 @@ function make_artifact(filepath_names)
208238 )
209239end
210240
211- radarlidar_dir_2_5 =
212- joinpath (dirname (@__FILE__ ), " radarlidar_seasonal_data" , " radarlidar_seasonal_2.5x2.5" )
213- radarlidar_dir_10 =
214- joinpath (dirname (@__FILE__ ), " radarlidar_seasonal_data" , " radarlidar_seasonal_10x10" )
241+ radarlidar_dirs = [
242+ joinpath (dirname (@__FILE__ ), " radarlidar_data" , " radarlidar_seasonal_2.5x2.5" )
243+ joinpath (dirname (@__FILE__ ), " radarlidar_data" , " radarlidar_seasonal_10x10" )
244+ joinpath (dirname (@__FILE__ ), " radarlidar_data" , " radarlidar_monthly_2.5x2.5" )
245+ joinpath (dirname (@__FILE__ ), " radarlidar_data" , " radarlidar_monthly_10x10" )
246+ ]
215247
216248# Check if the directory exists
217- for directory in (radarlidar_dir_10, radarlidar_dir_2_5)
249+ for directory in radarlidar_dirs
218250 if ! isdir (directory)
219251 error (" $directory is not a directory. Check the file path passed in" )
220252 end
223255# Loop over the directories containing the NetCDF files and create a netcdf file
224256# from them
225257filepath_names = []
226- for directory in (radarlidar_dir_10, radarlidar_dir_2_5)
258+ for directory in radarlidar_dirs
227259 files = get_and_sort_files (directory)
228260 filename = last (splitpath (directory)) * " .nc"
229261 filepath_name = joinpath (dirname (directory), filename)
0 commit comments