|
| 1 | +# plot_helper |
| 2 | + |
| 3 | +""" |
| 4 | + get_nc_data_all(var, red, DATA_DIR) |
| 5 | +
|
| 6 | +Reads the netcdf file for the variable `var` and reduction `red` from the directory `DATA_DIR` and returns the variable, lat, lon, z, and time. |
| 7 | +""" |
| 8 | +get_nc_data_all = (var, red, DATA_DIR) -> begin |
| 9 | + ds = NCDataset("$DATA_DIR/$(var)_$red.nc") |
| 10 | + var = ds["$var"][:, :, :, :] |
| 11 | + lat = ds["lat"][:] |
| 12 | + lon = ds["lon"][:] |
| 13 | + z = ds["z"][:] |
| 14 | + time = ds["time"][:] |
| 15 | + close(ds) |
| 16 | + return var, lat, lon, z, time |
| 17 | +end |
| 18 | + |
| 19 | +""" |
| 20 | + mean_climate_data(varname, reduction, DATA_DIR; lev_i = 1, spinup=1) |
| 21 | +
|
| 22 | +Postprocesses the climate data for the variable `varname` and `reduction` from the directory `DATA_DIR`. Returns the zonal mean and horizontal surface slice mean of the variable. |
| 23 | +""" |
| 24 | +mean_climate_data = |
| 25 | + (varname, reduction, DATA_DIR; lev_i = 1, spinup = 1) -> begin |
| 26 | + |
| 27 | + var, lat, lon, z, time = get_nc_data_all(varname, reduction, DATA_DIR) |
| 28 | + @assert spinup < size(var, 1) |
| 29 | + |
| 30 | + var_time_zonal_mean = mean(var[spinup:end, :, :, :], dims = (1, 2))[1, 1, :, :] |
| 31 | + var_time_mean_sfc = mean(var[spinup:end, :, :, :], dims = (1))[1, :, :, lev_i] |
| 32 | + |
| 33 | + return var_time_zonal_mean, var_time_mean_sfc, lat, lon, z |
| 34 | + end |
| 35 | + |
| 36 | +""" |
| 37 | + point_timeseries_data(variable, lon_i, lat_i, lev_i) |
| 38 | +
|
| 39 | +Returns the time series data for the variable `variable` at the indices `lon_i`, `lat_i`, and `lev_i`. |
| 40 | +""" |
| 41 | +point_timeseries_data = |
| 42 | + (variable, lon_i, lat_i, lev_i) -> begin |
| 43 | + |
| 44 | + variable_time_mean = mean(variable[:, lon_i[1]:lon_i[2], lat_i[1]:lat_i[2], lev_i], dims = (2, 3))[:, 1, 1] |
| 45 | + |
| 46 | + return variable_time_mean |
| 47 | + end |
| 48 | + |
| 49 | +""" |
| 50 | + plot_climate(var, DATA_DIR, PLOT_DIR, job_id; reduction = "inst", interpolate_to_pressure = false) |
| 51 | +
|
| 52 | +Plots the zonal mean and horizontal surface slice mean of the variable `var` from the directory `DATA_DIR` and saves the plots in the directory `PLOT_DIR`. |
| 53 | +""" |
| 54 | +function plot_climate(var, DATA_DIR, PLOT_DIR, job_id; reduction = "inst", interpolate_to_pressure = false) |
| 55 | + strf_zm, strf_sfc, lat, lon, z = mean_climate_data(var, reduction, DATA_DIR) |
| 56 | + strf_zm, strf_upper, lat, lon, z = mean_climate_data(var, reduction, DATA_DIR, lev_i = 10) |
| 57 | + |
| 58 | + # vertical-lat plot of zonal and time mean |
| 59 | + if interpolate_to_pressure |
| 60 | + pa_zm, ~, ~, ~, ~ = mean_climate_data("pfull", reduction, DATA_DIR) |
| 61 | + pa_zm = pa_zm ./ 100 # convert to hPa |
| 62 | + pa_grid = [950, 800, 700, 600, 500, 400, 300, 200, 50] |
| 63 | + strf_zm = interpolate_to_pressure_coord_2d(strf_zm, pa_zm, pa_grid) |
| 64 | + Plots.contourf( |
| 65 | + lat, |
| 66 | + -pa_grid, |
| 67 | + strf_zm', |
| 68 | + xlabel = "Latitude (deg N)", |
| 69 | + ylabel = "Pressure (hPa)", |
| 70 | + title = "$var", |
| 71 | + color = :viridis, |
| 72 | + ylims = (-pa_grid[1], -pa_grid[end]), |
| 73 | + yticks = (-pa_grid, pa_grid), |
| 74 | + )# , clims=(-1e10, 1e10)) |
| 75 | + png(joinpath(PLOT_DIR, "$(job_id)_$(var)_pa.png")) |
| 76 | + else |
| 77 | + Plots.contourf( |
| 78 | + lat, |
| 79 | + z, |
| 80 | + strf_zm', |
| 81 | + xlabel = "Latitude", |
| 82 | + ylabel = "Height (km)", |
| 83 | + title = "$var", |
| 84 | + color = :viridis, |
| 85 | + ylims = (0, 3e4), |
| 86 | + yscale = :log10, |
| 87 | + yticks = ([1e3, 5e3, 10e3, 20e3, 30e3], ["1", "5", "10", "20", "30"]), |
| 88 | + )# , clims=(-1e10, 1e10) |
| 89 | + png(joinpath(PLOT_DIR, "$(job_id)_$var.png")) |
| 90 | + end |
| 91 | + |
| 92 | + # horizontal slices |
| 93 | + Plots.contourf(lon, lat, strf_sfc', xlabel = "Longitude", ylabel = "Latitude", title = "$var", color = :viridis)#, clims=(-1e10, 1e10)) |
| 94 | + png(joinpath(PLOT_DIR, "$(job_id)_$(var)_sfc.png")) |
| 95 | + |
| 96 | + Plots.contourf(lon, lat, strf_upper', xlabel = "Longitude", ylabel = "Latitude", title = "$var", color = :viridis)#, clims=(-1e10, 1e10)) |
| 97 | + png(joinpath(PLOT_DIR, "$(job_id)_$(var)_10km.png")) |
| 98 | +end |
| 99 | + |
| 100 | +""" |
| 101 | + interpolate_to_pressure_coord_2d(var_zm, pa, pa_grid) |
| 102 | +
|
| 103 | +Interpolates the 2D variable `var_zm` to the pressure grid `pa_grid` using the pressure values `pa`. |
| 104 | +""" |
| 105 | +function interpolate_to_pressure_coord_2d(var_zm, pa, pa_grid) |
| 106 | + var_on_pa = zeros(size(var_zm, 1), length(pa_grid)) |
| 107 | + for lat_i in collect(1:size(var_zm, 1)) |
| 108 | + # Extract ua and corresponding ta values |
| 109 | + var_values = var_zm[lat_i, :] |
| 110 | + pa_values = pa[lat_i, :] |
| 111 | + |
| 112 | + # Interpolate ua onto ta_grid |
| 113 | + for (pa_j, pa_val) in enumerate(pa_grid) |
| 114 | + itp_var = LinearInterpolation(-pa_values, var_values) |
| 115 | + var_on_pa[lat_i, pa_j] = itp_var(-pa_val) |
| 116 | + end |
| 117 | + end |
| 118 | + return var_on_pa |
| 119 | +end |
0 commit comments