def update_time_in_NetCDF2save(ds, convert_time2dt=False): """ Update time of monthly output to be in NetCDF saveable format Parameters ------- convert_time2dt (bool): convert the time into a datetime.datetime format """ # Climate model time sdate = datetime.datetime(1985, 1, 1) # Convert / setup time dim? if convert_time2dt: months = np.arange(1, 13) ds['time'] = [AC.add_months(sdate, i - 1) for i in months] # Update to hours since X hours = [(AC.dt64_2_dt([i])[0] - sdate).days * 24. for i in ds['time'].values] ds['time'] = hours attrs_dict = {'units': 'hours since 1985-01-01 00:00:00'} ds['time'].attrs = attrs_dict return ds
def mk_NetCDF_from_productivity_data(): """ Convert productivity .csv file (Behrenfeld and Falkowski, 1997) into a NetCDF file """ # Location of data (update to use public facing host) folder = utils.get_file_locations('data_root') + '/Productivity/' # Which file to use? filename = 'productivity_behrenfeld_and_falkowski_1997_extrapolated.csv' # Setup coordinates lon = np.arange(-180, 180, 1/6.) lat = np.arange(-90, 90, 1/6.) lat = np.append(lat, [90]) # Setup time varname = 'vgpm' months = np.arange(1, 13) # Extract data df = pd.read_csv(folder+filename, header=None) print(df.shape) # Extract data by month da_l = [] for n in range(12): # Assume the data is in blocks by longitude? arr = df.values[:, n*1081: (n+1)*1081].T[None, ...] print(arr.shape) da_l += [xr.Dataset( data_vars={varname: (['time', 'lat', 'lon', ], arr)}, coords={'lat': lat, 'lon': lon, 'time': [n]})] # Concatenate to data xr.Dataset ds = xr.concat(da_l, dim='time') # Update time ... sdate = datetime.datetime(1985, 1, 1) # Climate model tiem ds['time'] = [AC.add_months(sdate, i-1) for i in months] # Update to hours since X hours = [(AC.dt64_2_dt([i])[0] - sdate).days * 24. for i in ds['time'].values] ds['time'] = hours # Add units attrs_dict = {'units': 'hours since 1985-01-01 00:00:00'} ds['time'].attrs = attrs_dict # Add attributes for variable attrs_dict = { 'long_name': "net primary production", 'units': "mg C / m**2 / day", } ds[varname].attrs = attrs_dict # For latitude... attrs_dict = { 'long_name': "latitude", 'units': "degrees_north", "standard_name": "latitude", "axis": "Y", } ds['lat'].attrs = attrs_dict # And longitude... attrs_dict = { 'long_name': "longitude", 'units': "degrees_east", "standard_name": "longitude", "axis": "X", } ds['lon'].attrs = attrs_dict # Add extra global attributes global_attribute_dictionary = { 'Title': 'Sea-surface productivity (Behrenfeld and Falkowski, 1997)', 'Author': 'Tomas Sherwen ([email protected])', 'Notes': "Data extracted from OCRA and extrapolated to poles by Martin Wadley. NetCDF contructed using xarray (xarray.pydata.org) by Tomas Sherwen. \n NOTES from oringal site (http://orca.science.oregonstate.edu/) from 'based on the standard vgpm algorithm. npp is based on the standard vgpm, using modis chl, sst4, and par as input; clouds have been filled in the input data using our own gap-filling software. For citation, please reference the original vgpm paper by Behrenfeld and Falkowski, 1997a as well as the Ocean Productivity site for the data.' ", 'History': 'Last Modified on:' + strftime("%B %d %Y", gmtime()), 'Conventions': "COARDS", } ds.attrs = global_attribute_dictionary # Save to NetCDF filename = 'productivity_behrenfeld_and_falkowski_1997_extrapolated.nc' ds.to_netcdf(filename, unlimited_dims={'time': True})