def write_3d_netcdf(infile, var, varname, description, source, \ var_units, lons, lats, sdate): """write netcdf files""" rootgrp = nc4_dataset(infile, 'w', format='NETCDF4') longitude = rootgrp.createDimension('lon', len(lons)) latitude = rootgrp.createDimension('lat', len(lats)) time = rootgrp.createDimension('time', None) longitudes = rootgrp.createVariable('lon', 'f4', ('lon', )) latitudes = rootgrp.createVariable('lat', 'f4', ('lat', )) times = rootgrp.createVariable('time', 'f8', ('time', )) # two dimensions unlimited. varname = rootgrp.createVariable(varname, 'f4', \ ('time', 'lat', 'lon'), \ fill_value=-9999., zlib=True) #import time rootgrp.description = description rootgrp.history = 'Created ' + t_ctime(t_time()) rootgrp.source = source latitudes.units = 'degrees_north' longitudes.units = 'degrees_east' varname.units = var_units string_date = datetime.strftime(sdate, "%Y-%m-%d") times.units = 'days since ' + string_date times.calendar = 'gregorian' latitudes[:] = lats longitudes[:] = lons varname[:, :, :] = var times[:] = nc4_date2num(sdate, units=times.units, calendar=times.calendar) rootgrp.close()
def write_bc_netcdf(outfile, var, varname, description, source, var_units, \ var_standard_name, lons, lats, sdate, dates, sig_digit, north_east_corner_lat, \ north_east_corner_lon, south_west_corner_lat, south_west_corner_lon, \ resolution_x, resolution_y, time_increment): """write netcdf""" rootgrp = nc4_dataset(outfile, 'w', format='NETCDF4_CLASSIC') time = rootgrp.createDimension('time', None) longitude = rootgrp.createDimension('lon', len(lons)) latitude = rootgrp.createDimension('lat', len(lats)) longitudes = rootgrp.createVariable('lon', 'f4', ('lon', )) latitudes = rootgrp.createVariable('lat', 'f4', ('lat', )) times = rootgrp.createVariable('time', 'f4', ('time', )) # two dimensions unlimited. varname = rootgrp.createVariable(varname, 'f4', ('time', 'lat', \ 'lon',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) rootgrp.missing_value = -9999 rootgrp.description = description rootgrp.zenith_interp = "true,false," rootgrp.MAP_PROJECTION = "EQUIDISTANT CYLINDRICAL" rootgrp.conventions = "CF-1.6" rootgrp.south_west_corner_lat = float(south_west_corner_lat) rootgrp.south_west_corner_lon = float(south_west_corner_lon) rootgrp.north_east_corner_lat = float(north_east_corner_lat) rootgrp.north_east_corner_lon = float(north_east_corner_lon) rootgrp.DX = resolution_x rootgrp.DY = resolution_y rootgrp.history = 'Created ' + t_ctime(t_time()) rootgrp.source = source latitudes.units = 'degrees_north' longitudes.units = 'degrees_east' varname.units = var_units varname.standard_name = var_standard_name string_date = datetime.strftime(sdate, "%Y-%m-%d %H:%M:%S") times.units = 'minutes since ' + string_date times.time_increment = time_increment times.begin_date = datetime.strftime(sdate, "%Y%m%d") times.begin_time = '000000' times.calendar = 'gregorian' latitudes[:] = lats longitudes[:] = lons varname[:, :, :] = var times[:] = nc4_date2num(dates, units=times.units, calendar=times.calendar) rootgrp.close()
def write_bc_netcdf(outfile, var, varname, description, source, var_units, \ var_standard_name, lons, lats, sdate, dates, sig_digit, north_east_corner_lat, \ north_east_corner_lon, south_west_corner_lat, south_west_corner_lon, \ resolution_x, resolution_y, time_increment): """write netcdf""" rootgrp = nc4_dataset(outfile, 'w', format='NETCDF4_CLASSIC') time = rootgrp.createDimension('time', None) longitude = rootgrp.createDimension('longitude', len(lons)) latitude = rootgrp.createDimension('latitude', len(lats)) longitudes = rootgrp.createVariable('longitude', 'f4', ('longitude', )) latitudes = rootgrp.createVariable('latitude', 'f4', ('latitude', )) times = rootgrp.createVariable('time', 'f4', ('time', )) # two dimensions unlimited. varname1 = rootgrp.createVariable(varname[0], 'f4', ('time', \ 'latitude', 'longitude',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) varname2 = rootgrp.createVariable(varname[1], 'f4', ('time', \ 'latitude', 'longitude',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) varname3 = rootgrp.createVariable(varname[2], 'f4', ('time', \ 'latitude', 'longitude',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) varname4 = rootgrp.createVariable(varname[3], 'f4', ('time', \ 'latitude', 'longitude',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) varname5 = rootgrp.createVariable(varname[4], 'f4', ('time', \ 'latitude', 'longitude',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) varname6 = rootgrp.createVariable(varname[5], 'f4', ('time', \ 'latitude', 'longitude',), fill_value=-9999, zlib=True, \ least_significant_digit=sig_digit) rootgrp.missing_value = -9999 rootgrp.description = description rootgrp.zenith_interp = "true,false," rootgrp.MAP_PROJECTION = "EQUIDISTANT CYLINDRICAL" rootgrp.conventions = "CF-1.6" rootgrp.SOUTH_WEST_CORNER_LAT = float(south_west_corner_lat) rootgrp.SOUTH_WEST_CORNER_LON = float(south_west_corner_lon) rootgrp.NORTH_EAST_CORNER_LAT = float(north_east_corner_lat) rootgrp.NORTH_EAST_CORNER_LON = float(north_east_corner_lon) rootgrp.DX = resolution_x rootgrp.DY = resolution_y #rootgrp.history = 'Created ' + time.ctime(time.time()) rootgrp.history = 'Created ' + t_ctime(t_time()) rootgrp.source = source latitudes.units = 'degrees_north' longitudes.units = 'degrees_east' ### Assigning units for each variables varname1.units = var_units[0] varname2.units = var_units[1] varname3.units = var_units[2] varname4.units = var_units[3] varname5.units = var_units[4] varname6.units = var_units[5] ### Assigning standard names for each variables varname1.standard_name = var_standard_name[0] varname2.standard_name = var_standard_name[1] varname3.standard_name = var_standard_name[2] varname4.standard_name = var_standard_name[3] varname5.standard_name = var_standard_name[4] varname6.standard_name = var_standard_name[5] string_date = datetime.strftime(sdate, "%Y-%m-%d %H:%M:%S") times.units = 'minutes since ' + string_date times.time_increment = time_increment times.begin_date = datetime.strftime(sdate, "%Y%m%d") times.begin_time = '000000' times.calendar = 'gregorian' latitudes[:] = lats longitudes[:] = lons ## Passing on values varname1[:, :, :] = var[0, ] varname2[:, :, :] = var[1, ] varname3[:, :, :] = var[2, ] varname4[:, :, :] = var[3, ] varname5[:, :, :] = var[4, ] varname6[:, :, :] = var[5, ] times[:] = nc4_date2num(dates, units=times.units, calendar=times.calendar) rootgrp.close()