def _create_test_data(datafile=DATAFILE, testfile=TESTDATA, start=START, end=END, resize=RESIZE): """ Create test data from downloaded data. Downloaded data from ECMWF for a single day is 3MB. This creates a subset of the downloaded data that is only 100kb. """ import netCDF4 if not os.path.exists(datafile): ecmwf_macc.get_ecmwf_macc(datafile, ("aod550", "tcwv"), start, end) data = netCDF4.Dataset(datafile) testdata = netCDF4.Dataset(testfile, 'w', format="NETCDF3_64BIT_OFFSET") # attributes testdata.Conventions = data.Conventions testdata.history = "intentionally blank" # longitude lon_name = 'longitude' lon_test = data.variables[lon_name][::resize] lon_size = lon_test.size lon = testdata.createDimension(lon_name, lon_size) assert not lon.isunlimited() assert lon_test[0] == LON_BND[0] assert (LON_BND[-1] - lon_test[-1]) == (LON_BND[-1] / lon_size) longitudes = testdata.createVariable(lon_name, "f4", (lon_name, )) longitudes.units = data.variables[lon_name].units longitudes.long_name = lon_name longitudes[:] = lon_test # latitude lat_name = 'latitude' lat_test = data.variables[lat_name][::resize] lat = testdata.createDimension(lat_name, lat_test.size) assert not lat.isunlimited() assert lat_test[0] == LAT_BND[0] assert lat_test[-1] == LAT_BND[-1] latitudes = testdata.createVariable(lat_name, "f4", (lat_name, )) latitudes.units = data.variables[lat_name].units latitudes.long_name = lat_name latitudes[:] = lat_test # time time_name = 'time' time_test = data.variables[time_name][:] time = testdata.createDimension(time_name, None) assert time.isunlimited() times = testdata.createVariable(time_name, 'i4', (time_name, )) times.units = data.variables[time_name].units times.long_name = time_name times.calendar = data.variables[time_name].calendar times[:] = time_test # aod aod_name = 'aod550' aod_vars = data.variables[aod_name] aod_dims = (time_name, lat_name, lon_name) aod_fill_value = getattr(aod_vars, '_FillValue') aods = testdata.createVariable(aod_name, 'i2', aod_dims, fill_value=aod_fill_value) for attr in aod_vars.ncattrs(): if attr.startswith('_'): continue setattr(aods, attr, getattr(aod_vars, attr)) aods[:] = aod_vars[:, ::resize, ::resize] # tcwv tcwv_name = 'tcwv' tcwv_vars = data.variables[tcwv_name] tcwv_dims = (time_name, lat_name, lon_name) tcwv_fill_value = getattr(tcwv_vars, '_FillValue') tcwvs = testdata.createVariable(tcwv_name, 'i2', tcwv_dims, fill_value=tcwv_fill_value) for attr in tcwv_vars.ncattrs(): if attr.startswith('_'): continue setattr(tcwvs, attr, getattr(tcwv_vars, attr)) tcwvs[:] = tcwv_vars[:, ::resize, ::resize] data.close() testdata.close()
def _create_test_data(datafile=DATAFILE, testfile=TESTDATA, start=START, end=END, resize=RESIZE): """ Create test data from downloaded data. Downloaded data from ECMWF for a single day is 3MB. This creates a subset of the downloaded data that is only 100kb. """ import netCDF4 if not os.path.exists(datafile): ecmwf_macc.get_ecmwf_macc(datafile, ("aod550", "tcwv"), start, end) data = netCDF4.Dataset(datafile) testdata = netCDF4.Dataset(testfile, 'w', format="NETCDF3_64BIT_OFFSET") # attributes testdata.Conventions = data.Conventions testdata.history = "intentionally blank" # longitude lon_name = 'longitude' lon_test = data.variables[lon_name][::resize] lon_size = lon_test.size lon = testdata.createDimension(lon_name, lon_size) assert not lon.isunlimited() assert lon_test[0] == LON_BND[0] assert (LON_BND[-1] - lon_test[-1]) == (LON_BND[-1] / lon_size) longitudes = testdata.createVariable(lon_name, "f4", (lon_name,)) longitudes.units = data.variables[lon_name].units longitudes.long_name = lon_name longitudes[:] = lon_test # latitude lat_name = 'latitude' lat_test = data.variables[lat_name][::resize] lat = testdata.createDimension(lat_name, lat_test.size) assert not lat.isunlimited() assert lat_test[0] == LAT_BND[0] assert lat_test[-1] == LAT_BND[-1] latitudes = testdata.createVariable(lat_name, "f4", (lat_name,)) latitudes.units = data.variables[lat_name].units latitudes.long_name = lat_name latitudes[:] = lat_test # time time_name = 'time' time_test = data.variables[time_name][:] time = testdata.createDimension(time_name, None) assert time.isunlimited() times = testdata.createVariable(time_name, 'i4', (time_name,)) times.units = data.variables[time_name].units times.long_name = time_name times.calendar = data.variables[time_name].calendar times[:] = time_test # aod aod_name = 'aod550' aod_vars = data.variables[aod_name] aod_dims = (time_name, lat_name, lon_name) aod_fill_value = getattr(aod_vars, '_FillValue') aods = testdata.createVariable( aod_name, 'i2', aod_dims, fill_value=aod_fill_value) for attr in aod_vars.ncattrs(): if attr.startswith('_'): continue setattr(aods, attr, getattr(aod_vars, attr)) aods[:] = aod_vars[:, ::resize, ::resize] # tcwv tcwv_name = 'tcwv' tcwv_vars = data.variables[tcwv_name] tcwv_dims = (time_name, lat_name, lon_name) tcwv_fill_value = getattr(tcwv_vars, '_FillValue') tcwvs = testdata.createVariable( tcwv_name, 'i2', tcwv_dims, fill_value=tcwv_fill_value) for attr in tcwv_vars.ncattrs(): if attr.startswith('_'): continue setattr(tcwvs, attr, getattr(tcwv_vars, attr)) tcwvs[:] = tcwv_vars[:, ::resize, ::resize] data.close() testdata.close()