|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import cftime as cf |
| 4 | +import earthaccess |
| 5 | +import numpy as np |
| 6 | +import xarray as xr |
| 7 | + |
| 8 | +from ilamb3_data import ( |
| 9 | + create_output_filename, |
| 10 | + gen_trackingid, |
| 11 | + gen_utc_timestamp, |
| 12 | + get_cmip6_variable_info, |
| 13 | + set_coord_bounds, |
| 14 | + set_lat_attrs, |
| 15 | + set_lon_attrs, |
| 16 | + set_ods26_global_attrs, |
| 17 | + set_time_attrs, |
| 18 | + set_var_attrs, |
| 19 | + standardize_dim_order, |
| 20 | +) |
| 21 | + |
| 22 | +# Download CERES EBAF TOA Edition4.2.1 data from Earthdata |
| 23 | +earthaccess.login() # You must create an account at https://urs.earthdata.nasa.gov/ |
| 24 | +granules = earthaccess.search_data( |
| 25 | + short_name="CERES_EBAF-TOA", |
| 26 | + granule_name="*200003-202511.nc", |
| 27 | +) |
| 28 | +files = earthaccess.download(granules, "_raw") |
| 29 | + |
| 30 | +# Set timestamps and tracking id |
| 31 | +download_stamp = gen_utc_timestamp(files[0].stat().st_mtime) |
| 32 | +creation_stamp = gen_utc_timestamp() |
| 33 | +today_stamp = datetime.now().strftime("%Y%m%d") |
| 34 | +tracking_id = gen_trackingid() |
| 35 | + |
| 36 | +# Open and rename vars |
| 37 | +time_coder = xr.coders.CFDatetimeCoder(use_cftime=True) |
| 38 | +ds = xr.open_dataset(files[0], decode_times=time_coder, mask_and_scale=True) |
| 39 | +renaming_dict = { |
| 40 | + "toa_sw_all_mon": "rsut", |
| 41 | + "toa_lw_all_mon": "rlut", |
| 42 | + "toa_net_all_mon": "rtmt", |
| 43 | + "solar_mon": "rsdt", |
| 44 | +} |
| 45 | +ds = ds.rename(renaming_dict) |
| 46 | + |
| 47 | +# Subset to only variables we care about |
| 48 | +vars = list(renaming_dict.values()) |
| 49 | +ds = ds[vars] |
| 50 | + |
| 51 | +# Get variable attribute info via ESGF CMIP variable information |
| 52 | +for var in vars: |
| 53 | + var_info = get_cmip6_variable_info(var, variable_id=var) |
| 54 | + ds = set_var_attrs( |
| 55 | + ds, |
| 56 | + var, |
| 57 | + ds[var].attrs["units"], |
| 58 | + var_info["cf_standard_name"], |
| 59 | + var_info["variable_long_name"], |
| 60 | + target_dtype=np.float32, |
| 61 | + ) |
| 62 | + |
| 63 | + # Remove some straggling var attrs |
| 64 | + for attr in ["CF_name", "comment"]: |
| 65 | + ds[var].attrs.pop(attr, None) |
| 66 | + |
| 67 | +# Clean up attrs |
| 68 | +ds = set_time_attrs(ds, bounds_frequency="M", ref_date=cf.DatetimeGregorian(2000, 3, 1)) |
| 69 | +ds = set_lat_attrs(ds) |
| 70 | +ds = set_lon_attrs(ds) |
| 71 | +ds = set_coord_bounds(ds, "lat") |
| 72 | +ds = set_coord_bounds(ds, "lon") |
| 73 | +ds = standardize_dim_order(ds) |
| 74 | + |
| 75 | +# Set global attributes and export |
| 76 | +for var in vars: |
| 77 | + # Create one ds per variable |
| 78 | + to_drop = [ |
| 79 | + v |
| 80 | + for v in ds.data_vars |
| 81 | + if (var not in v) and ("time" not in v) and (not v.endswith("_bnds")) |
| 82 | + ] |
| 83 | + var_ds = ds.drop_vars(to_drop) |
| 84 | + |
| 85 | + # Set global attributes |
| 86 | + out_ds = set_ods26_global_attrs( |
| 87 | + var_ds, |
| 88 | + contact="Norman Loeb (norman.g.loeb@nasa.gov)", |
| 89 | + creation_date=creation_stamp, |
| 90 | + dataset_contributor="Morgan Steckler", |
| 91 | + doi="https://doi.org/10.5067/TERRA-AQUA-NOAA20/CERES/EBAF-TOA_L3B004.2.1", |
| 92 | + frequency="mon", |
| 93 | + grid="1x1 degree latitude x longitude", |
| 94 | + grid_label="gn", |
| 95 | + has_aux_unc="FALSE", |
| 96 | + history=f""" |
| 97 | +{download_stamp}: downloaded {files[0].name} from Earthdata; |
| 98 | +{creation_stamp}: formatted attrs according to obs4MIPs conventions |
| 99 | +""", |
| 100 | + institution="NASA-LaRC (Langley Research Center) Hampton, Va", |
| 101 | + institution_id="NASA-LaRC", |
| 102 | + license="Data in this file produced by ILAMB is licensed under a Creative Commons Attribution - 4.0 International (CC BY - 4.0) License (https://creativecommons.org/licenses/).", |
| 103 | + nominal_resolution="100 km", |
| 104 | + processing_code_location="https://github.com/rubisco-sfa/ilamb3-data/tree/main/data/CERES-4-2-1/convert.py", |
| 105 | + product="derived", |
| 106 | + realm="atmos", |
| 107 | + references="Loeb, N. G., D. R. Doelling, H. Wang, W. Su, C. Nguyen, J. G. Corbett, L. Liang, C. Mitrescu, F. G. Rose, and S. Kato, 2018: Clouds and the Earth's Radiant Energy System (CERES) Energy Balanced and Filled (EBAF) Top-of-Atmosphere (TOA) Edition-4.0 Data Product. J. Climate, 31, 895-918, doi: 10.1175/JCLI-D-17-0208.1.", |
| 108 | + region="global", |
| 109 | + source="Data are collected on Terra, Aqua, Suomi National Polar-Orbiting Partnership (SNPP), and NOAA-20 satellites, then an objective constrainment algorithm is applied to adjust SW and LW TOA fluxes within their ranges of uncertainty", |
| 110 | + source_data_retrieval_date=today_stamp, |
| 111 | + source_data_url="https://asdc.larc.nasa.gov/data/CERES/EBAF/TOA_Edition4.2.1/", |
| 112 | + source_id="CERES-EBAF-TOA-4-2-1", |
| 113 | + source_label="CERES_EBAF-TOA", |
| 114 | + source_type="satellite_blended", |
| 115 | + source_version_number="4.2.1", |
| 116 | + title=f"CERES EBAF TOA Edition 4.2.1 {var} monthly mean data", |
| 117 | + tracking_id=tracking_id, |
| 118 | + variable_id=var, |
| 119 | + variant_label="ILAMB", |
| 120 | + variant_info="CMORized product prepared by ILAMB", |
| 121 | + version=f"v{today_stamp}", |
| 122 | + ) |
| 123 | + |
| 124 | + # Prep for export |
| 125 | + out_path = create_output_filename(out_ds.attrs) |
| 126 | + out_ds.to_netcdf(out_path, format="NETCDF4") |
0 commit comments