Пример #1
0
print "Sa: ", cov.shape

priorCDF_filename = 'Xa_Sa_datafile.55_levels.' + yyyymmdd + '.' + hh + '.' + type + '.cdf'
print priorCDF_filename
stop

data = Dataset(priorCDF_filename, 'w')

data.Date_created = datetime.strftime(datetime.now(), '%a %b %d %H:%M:%S %Y')
data.Version = 'get_RUCRAP_prior.py'
data.Machine_used = 'N/A'
data.LBL_HOME = climo.LBL_HOME
data.Standard_atmos = climo.Standard_atmos
data.QC_limits_T = climo.QC_limits_T
data.QC_limits_q = climo.QC_limits_q
data.Comment = 'Thermodynamic profiles from the ' + type + ' analyses.'
#Need to include the web links to the data used to produce these files
#Need to include the times used to produce this file.
data.Nsonde = str(
    priors.shape[0]
) + ' profiles were included in the computation of this prior dataset.'
data.lat = aeri_lat
data.lon = aeri_lon
data.domain_size = str(2 * size) + "x" + str(2 * size)
data.grid_spacing = '13 km'

print data.Date_created

data.createDimension('height', len(all_temps.T))
data.createDimension('wnum', len(climo.variables['wnum'][:]))
data.createDimension('height2', len(mean))
def WriteNCCF(FileName,Dates,Latitudes,Longitudes,ClimPoints,DataObject,DimObject,AttrObject,GlobAttrObject):
    ''' Sort out the date/times to write out and time bounds '''
    ''' Sort out clim bounds '''
    ''' Sort out lat and long bounds '''
    ''' Convert variables using the obtained scale_factor and add_offset: stored_var=int((var-offset)/scale) '''
    ''' Write to file, set up given dimensions, looping through all potential variables and their attributes, and then the provided dictionary of global attributes '''
    
    # Sort out date/times to write out
    print(Dates)
    TimPoints,TimBounds = MakeDaysSince(Dates['StYr'],Dates['StMon'],Dates['EdYr'],Dates['EdMon'])
    nTims = len(TimPoints)
	
    # Sort out clim bounds - paired strings
    ClimBounds = np.empty((12,2),dtype='|S10')
    for mm in range(12):
	ClimBounds[mm,0] = str(ClimPoints[0])+'-'+str(mm+1)+'-'+str(1)
	ClimBounds[mm,1] = str(ClimPoints[1])+'-'+str(mm+1)+'-'+str(MonthDays[mm])
		
    # Sort out LatBounds and LonBounds
    LatBounds = np.empty((len(Latitudes),2),dtype='float')
    LonBounds = np.empty((len(Longitudes),2),dtype='float')
	
    LatBounds[:,0] = Latitudes - ((Latitudes[1]-Latitudes[0])/2.)
    LatBounds[:,1] = Latitudes + ((Latitudes[1]-Latitudes[0])/2.)

    LonBounds[:,0] = Longitudes - ((Longitudes[1]-Longitudes[0])/2.)
    LonBounds[:,1] = Longitudes + ((Longitudes[1]-Longitudes[0])/2.)	
	
    #pdb.set_trace()
    
    # No need to convert float data using given scale_factor and add_offset to integers - done within writing program (packV = (V-offset)/scale
    # Not sure what this does to float precision though...
    # Change mdi into an integer -999 because these are stored as integers
    for vv in range(len(DataObject)):
        DataObject[vv][np.where(DataObject[vv] == OLDMDI)] = MDI

    # Create a new netCDF file - have tried zlib=True,least_significant_digit=3 (and 1) - no difference
    ncfw=Dataset(FileName,'w',format='NETCDF4_CLASSIC') # need to try NETCDF4 and also play with compression but test this first
    
    # Write out the global attributes
    if ('description' in GlobAttrObject):
        ncfw.description = GlobAttrObject['description']
	#print(GlobAttrObject['description'])
	
    if ('File_created' in GlobAttrObject):
        ncfw.File_created = GlobAttrObject['File_created']

    if ('Title' in GlobAttrObject):
        ncfw.Title = GlobAttrObject['Title']

    if ('Institution' in GlobAttrObject):
        ncfw.Institution = GlobAttrObject['Institution']

    if ('History' in GlobAttrObject):
        ncfw.History = GlobAttrObject['History']

    if ('Licence' in GlobAttrObject):
        ncfw.Licence = GlobAttrObject['Licence']

    if ('Project' in GlobAttrObject):
        ncfw.Project = GlobAttrObject['Project']

    if ('Processing_level' in GlobAttrObject):
        ncfw.Processing_level = GlobAttrObject['Processing_level']

    if ('Acknowledgement' in GlobAttrObject):
        ncfw.Acknowledgement = GlobAttrObject['Acknowledgement']

    if ('Source' in GlobAttrObject):
        ncfw.Source = GlobAttrObject['Source']

    if ('Comment' in GlobAttrObject):
        ncfw.Comment = GlobAttrObject['Comment']

    if ('References' in GlobAttrObject):
        ncfw.References = GlobAttrObject['References']

    if ('Creator_name' in GlobAttrObject):
        ncfw.Creator_name = GlobAttrObject['Creator_name']

    if ('Creator_email' in GlobAttrObject):
        ncfw.Creator_email = GlobAttrObject['Creator_email']

    if ('Version' in GlobAttrObject):
        ncfw.Version = GlobAttrObject['Version']

    if ('doi' in GlobAttrObject):
        ncfw.doi = GlobAttrObject['doi']

    if ('Conventions' in GlobAttrObject):
        ncfw.Conventions = GlobAttrObject['Conventions']

    if ('netcdf_type' in GlobAttrObject):
        ncfw.netcdf_type = GlobAttrObject['netcdf_type']
	
    # Loop through and set up the dimension names and quantities
    for vv in range(len(DimObject[0])):
        ncfw.createDimension(DimObject[0][vv],DimObject[1][vv])
	
    # Go through each dimension and set up the variable and attributes for that dimension if needed
    for vv in range(len(DimObject)-2): # ignore first two elements of the list but count all other dictionaries
        print(DimObject[vv+2]['var_name'])
	
	# NOt 100% sure this works in a loop with overwriting
	# initiate variable with name, type and dimensions
	MyVar = ncfw.createVariable(DimObject[vv+2]['var_name'],DimObject[vv+2]['var_type'],DimObject[vv+2]['var_dims'])
        
	# Apply any other attributes
        if ('standard_name' in DimObject[vv+2]):
	    MyVar.standard_name = DimObject[vv+2]['standard_name']
	    
        if ('long_name' in DimObject[vv+2]):
	    MyVar.long_name = DimObject[vv+2]['long_name']
	    
        if ('units' in DimObject[vv+2]):
	    MyVar.units = DimObject[vv+2]['units']
		   	 
        if ('axis' in DimObject[vv+2]):
	    MyVar.axis = DimObject[vv+2]['axis']

        if ('calendar' in DimObject[vv+2]):
	    MyVar.calendar = DimObject[vv+2]['calendar']

        if ('start_year' in DimObject[vv+2]):
	    MyVar.start_year = DimObject[vv+2]['start_year']

        if ('end_year' in DimObject[vv+2]):
	    MyVar.end_year = DimObject[vv+2]['end_year']

        if ('start_month' in DimObject[vv+2]):
	    MyVar.start_month = DimObject[vv+2]['start_month']

        if ('end_month' in DimObject[vv+2]):
	    MyVar.end_month = DimObject[vv+2]['end_month']

        if ('bounds' in DimObject[vv+2]):
	    MyVar.bounds = DimObject[vv+2]['bounds']

        if ('climatology' in DimObject[vv+2]):
	    MyVar.climatology = DimObject[vv+2]['climatology']

        if ('point_spacing' in DimObject[vv+2]):
	    MyVar.point_spacing = DimObject[vv+2]['point_spacing']
	
	# Provide the data to the variable
        if (DimObject[vv+2]['var_name'] == 'time'):
	    MyVar[:] = TimPoints

        if (DimObject[vv+2]['var_name'] == 'bounds_time'):
	    MyVar[:,:] = TimBounds

        if (DimObject[vv+2]['var_name'] == 'month'):
	    for mm in range(12):
	        MyVar[mm,:] = stringtoarr(MonthName[mm],10)

        if (DimObject[vv+2]['var_name'] == 'climbounds'):
	    for mm in range(12):
	        MyVar[mm,0,:] = stringtoarr(ClimBounds[mm,0],10)
	        MyVar[mm,1,:] = stringtoarr(ClimBounds[mm,1],10)

        if (DimObject[vv+2]['var_name'] == 'latitude'):
	    MyVar[:] = Latitudes

        if (DimObject[vv+2]['var_name'] == 'bounds_lat'):
	    MyVar[:,:] = LatBounds

        if (DimObject[vv+2]['var_name'] == 'longitude'):
	    MyVar[:] = Longitudes

        if (DimObject[vv+2]['var_name'] == 'bounds_lon'):
	    MyVar[:,:] = LonBounds

    # Go through each variable and set up the variable attributes
    for vv in range(len(AttrObject)): # ignore first two elements of the list but count all other dictionaries

        print(AttrObject[vv]['var_name'])

        # NOt 100% sure this works in a loop with overwriting
	# initiate variable with name, type and dimensions
	MyVar = ncfw.createVariable(AttrObject[vv]['var_name'],AttrObject[vv]['var_type'],AttrObject[vv]['var_dims'],fill_value = AttrObject[vv]['_FillValue'])
        
	# Apply any other attributes
        if ('standard_name' in AttrObject[vv]):
	    MyVar.standard_name = AttrObject[vv]['standard_name']
	    
        if ('long_name' in AttrObject[vv]):
	    MyVar.long_name = AttrObject[vv]['long_name']
	    
        if ('cell_methods' in AttrObject[vv]):
	    MyVar.cell_methods = AttrObject[vv]['cell_methods']
	    
        if ('comment' in AttrObject[vv]):
	    MyVar.comment = AttrObject[vv]['comment']
	    
        if ('units' in AttrObject[vv]):
	    MyVar.units = AttrObject[vv]['units']
		   	 
        if ('axis' in AttrObject[vv]):
	    MyVar.axis = AttrObject[vv]['axis']

        if ('add_offset' in AttrObject[vv]):
	    MyVar.add_offset = AttrObject[vv]['add_offset']

        if ('scale_factor' in AttrObject[vv]):
	    MyVar.scale_factor = AttrObject[vv]['scale_factor']

        if ('valid_min' in AttrObject[vv]):
	    MyVar.valid_min = AttrObject[vv]['valid_min']

        if ('valid_max' in AttrObject[vv]):
	    MyVar.valid_max = AttrObject[vv]['valid_max']

        if ('missing_value' in AttrObject[vv]):
	    MyVar.missing_value = AttrObject[vv]['missing_value']

#        if ('_FillValue' in AttrObject[vv]):
#	    MyVar._FillValue = AttrObject[vv]['_FillValue']

        if ('reference_period' in AttrObject[vv]):
	    MyVar.reference_period = AttrObject[vv]['reference_period']

        if ('ancillary_variables' in AttrObject[vv]):
	    MyVar.ancillary_variables = AttrObject[vv]['ancillary_variables']
	
	# Provide the data to the variable - depending on howmany dimensions there are
        if (len(AttrObject[vv]['var_dims']) == 1):
	    MyVar[:] = DataObject[vv]
	    
        if (len(AttrObject[vv]['var_dims']) == 2):
	    MyVar[:,:] = DataObject[vv]
	    
        if (len(AttrObject[vv]['var_dims']) == 3):
	    MyVar[:,:,:] = DataObject[vv]
	    
	    
    ncfw.close()
   
    return # WriteNCCF
def WriteNCCF(FileName, Dates, Latitudes, Longitudes, ClimPoints, DataObject,
              DimObject, AttrObject, GlobAttrObject):
    ''' Sort out the date/times to write out and time bounds '''
    ''' Sort out clim bounds '''
    ''' Sort out lat and long bounds '''
    ''' Convert variables using the obtained scale_factor and add_offset: stored_var=int((var-offset)/scale) '''
    ''' Write to file, set up given dimensions, looping through all potential variables and their attributes, and then the provided dictionary of global attributes '''

    # Sort out date/times to write out
    print(Dates)
    TimPoints, TimBounds = MakeDaysSince(Dates['StYr'], Dates['StMon'],
                                         Dates['EdYr'], Dates['EdMon'])
    nTims = len(TimPoints)

    # Sort out clim bounds - paired strings
    ClimBounds = np.empty((12, 2), dtype='|S10')
    for mm in range(12):
        ClimBounds[mm,
                   0] = str(ClimPoints[0]) + '-' + str(mm + 1) + '-' + str(1)
        ClimBounds[mm, 1] = str(ClimPoints[1]) + '-' + str(mm + 1) + '-' + str(
            MonthDays[mm])

    # Sort out LatBounds and LonBounds
    LatBounds = np.empty((len(Latitudes), 2), dtype='float')
    LonBounds = np.empty((len(Longitudes), 2), dtype='float')

    LatBounds[:, 0] = Latitudes - ((Latitudes[1] - Latitudes[0]) / 2.)
    LatBounds[:, 1] = Latitudes + ((Latitudes[1] - Latitudes[0]) / 2.)

    LonBounds[:, 0] = Longitudes - ((Longitudes[1] - Longitudes[0]) / 2.)
    LonBounds[:, 1] = Longitudes + ((Longitudes[1] - Longitudes[0]) / 2.)

    #pdb.set_trace()

    #    # No need to convert float data using given scale_factor and add_offset to integers - done within writing program (packV = (V-offset)/scale
    #    # Not sure what this does to float precision though...
    #    # Change mdi into an integer -999 because these are stored as integers
    #    for vv in range(len(DataObject)):
    #        DataObject[vv][np.where(DataObject[vv] == OLDMDI)] = MDI

    # Create a new netCDF file - have tried zlib=True,least_significant_digit=3 (and 1) - no difference
    ncfw = Dataset(
        FileName, 'w', format='NETCDF4_CLASSIC'
    )  # need to try NETCDF4 and also play with compression but test this first

    # Write out the global attributes
    if ('description' in GlobAttrObject):
        ncfw.description = GlobAttrObject['description']
#print(GlobAttrObject['description'])

    if ('File_created' in GlobAttrObject):
        ncfw.File_created = GlobAttrObject['File_created']

    if ('Title' in GlobAttrObject):
        ncfw.Title = GlobAttrObject['Title']

    if ('Institution' in GlobAttrObject):
        ncfw.Institution = GlobAttrObject['Institution']

    if ('History' in GlobAttrObject):
        ncfw.History = GlobAttrObject['History']

    if ('Licence' in GlobAttrObject):
        ncfw.Licence = GlobAttrObject['Licence']

    if ('Project' in GlobAttrObject):
        ncfw.Project = GlobAttrObject['Project']

    if ('Processing_level' in GlobAttrObject):
        ncfw.Processing_level = GlobAttrObject['Processing_level']

    if ('Acknowledgement' in GlobAttrObject):
        ncfw.Acknowledgement = GlobAttrObject['Acknowledgement']

    if ('Source' in GlobAttrObject):
        ncfw.Source = GlobAttrObject['Source']

    if ('Comment' in GlobAttrObject):
        ncfw.Comment = GlobAttrObject['Comment']

    if ('References' in GlobAttrObject):
        ncfw.References = GlobAttrObject['References']

    if ('Creator_name' in GlobAttrObject):
        ncfw.Creator_name = GlobAttrObject['Creator_name']

    if ('Creator_email' in GlobAttrObject):
        ncfw.Creator_email = GlobAttrObject['Creator_email']

    if ('Version' in GlobAttrObject):
        ncfw.Version = GlobAttrObject['Version']

    if ('doi' in GlobAttrObject):
        ncfw.doi = GlobAttrObject['doi']

    if ('Conventions' in GlobAttrObject):
        ncfw.Conventions = GlobAttrObject['Conventions']

    if ('netcdf_type' in GlobAttrObject):
        ncfw.netcdf_type = GlobAttrObject['netcdf_type']

    # Loop through and set up the dimension names and quantities
    for vv in range(len(DimObject[0])):
        ncfw.createDimension(DimObject[0][vv], DimObject[1][vv])

    # Go through each dimension and set up the variable and attributes for that dimension if needed
    for vv in range(
            len(DimObject) - 2
    ):  # ignore first two elements of the list but count all other dictionaries
        print(DimObject[vv + 2]['var_name'])

        # NOt 100% sure this works in a loop with overwriting
        # initiate variable with name, type and dimensions
        MyVar = ncfw.createVariable(DimObject[vv + 2]['var_name'],
                                    DimObject[vv + 2]['var_type'],
                                    DimObject[vv + 2]['var_dims'])

        # Apply any other attributes
        if ('standard_name' in DimObject[vv + 2]):
            MyVar.standard_name = DimObject[vv + 2]['standard_name']

        if ('long_name' in DimObject[vv + 2]):
            MyVar.long_name = DimObject[vv + 2]['long_name']

        if ('units' in DimObject[vv + 2]):
            MyVar.units = DimObject[vv + 2]['units']

        if ('axis' in DimObject[vv + 2]):
            MyVar.axis = DimObject[vv + 2]['axis']

        if ('calendar' in DimObject[vv + 2]):
            MyVar.calendar = DimObject[vv + 2]['calendar']

        if ('start_year' in DimObject[vv + 2]):
            MyVar.start_year = DimObject[vv + 2]['start_year']

        if ('end_year' in DimObject[vv + 2]):
            MyVar.end_year = DimObject[vv + 2]['end_year']

        if ('start_month' in DimObject[vv + 2]):
            MyVar.start_month = DimObject[vv + 2]['start_month']

        if ('end_month' in DimObject[vv + 2]):
            MyVar.end_month = DimObject[vv + 2]['end_month']

        if ('bounds' in DimObject[vv + 2]):
            MyVar.bounds = DimObject[vv + 2]['bounds']

        if ('climatology' in DimObject[vv + 2]):
            MyVar.climatology = DimObject[vv + 2]['climatology']

        if ('point_spacing' in DimObject[vv + 2]):
            MyVar.point_spacing = DimObject[vv + 2]['point_spacing']

# Provide the data to the variable
        if (DimObject[vv + 2]['var_name'] == 'time'):
            MyVar[:] = TimPoints

        if (DimObject[vv + 2]['var_name'] == 'bounds_time'):
            MyVar[:, :] = TimBounds

        if (DimObject[vv + 2]['var_name'] == 'month'):
            for mm in range(12):
                MyVar[mm, :] = stringtoarr(MonthName[mm], 10)

        if (DimObject[vv + 2]['var_name'] == 'climbounds'):
            for mm in range(12):
                MyVar[mm, 0, :] = stringtoarr(ClimBounds[mm, 0], 10)
                MyVar[mm, 1, :] = stringtoarr(ClimBounds[mm, 1], 10)

        if (DimObject[vv + 2]['var_name'] == 'latitude'):
            MyVar[:] = Latitudes

        if (DimObject[vv + 2]['var_name'] == 'bounds_lat'):
            MyVar[:, :] = LatBounds

        if (DimObject[vv + 2]['var_name'] == 'longitude'):
            MyVar[:] = Longitudes

        if (DimObject[vv + 2]['var_name'] == 'bounds_lon'):
            MyVar[:, :] = LonBounds

    # Go through each variable and set up the variable attributes
    for vv in range(
            len(AttrObject)
    ):  # ignore first two elements of the list but count all other dictionaries

        print(AttrObject[vv]['var_name'])

        # NOt 100% sure this works in a loop with overwriting
        # initiate variable with name, type and dimensions
        MyVar = ncfw.createVariable(AttrObject[vv]['var_name'],
                                    AttrObject[vv]['var_type'],
                                    AttrObject[vv]['var_dims'],
                                    zlib=True,
                                    fill_value=AttrObject[vv]['_FillValue'])

        # Apply any other attributes
        if ('standard_name' in AttrObject[vv]):
            MyVar.standard_name = AttrObject[vv]['standard_name']

        if ('long_name' in AttrObject[vv]):
            MyVar.long_name = AttrObject[vv]['long_name']

# Too many issues with CF compliance
#        if ('cell_methods' in AttrObject[vv]):
#            MyVar.cell_methods = AttrObject[vv]['cell_methods']

        if ('comment' in AttrObject[vv]):
            MyVar.comment = AttrObject[vv]['comment']

        if ('units' in AttrObject[vv]):
            MyVar.units = AttrObject[vv]['units']

        if ('axis' in AttrObject[vv]):
            MyVar.axis = AttrObject[vv]['axis']

#        if ('add_offset' in AttrObject[vv]):
#            MyVar.add_offset = AttrObject[vv]['add_offset']
#
#        if ('scale_factor' in AttrObject[vv]):
#            MyVar.scale_factor = AttrObject[vv]['scale_factor']

#        if ('valid_min' in AttrObject[vv]):
#            MyVar.valid_min = AttrObject[vv]['valid_min']#
#
#        if ('valid_max' in AttrObject[vv]):
#            MyVar.valid_max = AttrObject[vv]['valid_max']

#        if ('missing_value' in AttrObject[vv]):
#            MyVar.missing_value = AttrObject[vv]['missing_value']

#        if ('_FillValue' in AttrObject[vv]):
#	    MyVar._FillValue = AttrObject[vv]['_FillValue']

        if ('reference_period' in AttrObject[vv]):
            MyVar.reference_period = AttrObject[vv]['reference_period']

        if ('ancillary_variables' in AttrObject[vv]):
            MyVar.ancillary_variables = AttrObject[vv]['ancillary_variables']

# Provide the data to the variable - depending on howmany dimensions there are
        if (len(AttrObject[vv]['var_dims']) == 1):
            MyVar[:] = DataObject[vv]

        if (len(AttrObject[vv]['var_dims']) == 2):
            MyVar[:, :] = DataObject[vv]

        if (len(AttrObject[vv]['var_dims']) == 3):
            MyVar[:, :, :] = DataObject[vv]

    ncfw.close()

    return  # WriteNCCF
Пример #4
0
def makeFile(mean, cov, dir, types, yyyymmdd, hh, paths, climo, size, t_size, n, aeri_lat, aeri_lon):
    priorCDF_filename = dir.strip() + '/Xa_Sa_datafile.55_levels.' + yyyymmdd + '.' + hh + '.' + types[0] + '.' + str(aeri_lat) + '.' + str(aeri_lon) + '.cdf'
    print "Saving prior file as: " + priorCDF_filename

    data = Dataset(priorCDF_filename, 'w', 'NETCDF3_CLASSIC')

    data.Date_created = datetime.strftime(datetime.now(), '%a %b %d %H:%M:%S %Y')
    data.Version = 'get_model_prior.py'
    data.Machine_used = platform.platform()
    data.model = "RUC/RAP"
    data.LBL_HOME = climo.LBL_HOME
    data.Standard_atmos = climo.Standard_atmos
    data.QC_limits_T = climo.QC_limits_T
    data.QC_limits_q = climo.QC_limits_q
    data.Comment = "Prior generated using model (" + types[0] + ") data."
    #Need to include the web links to the data used to produce these files
    #Need to include the times used to produce this file.
    data.Nsonde = str(n) + ' profiles were included in the computation of this prior dataset.'
    data.lat = aeri_lat
    data.lon = aeri_lon
    data.paths = '; '.join(paths)
    data.model_types = '; '.join(types)
    data.domain_size = str(2*size) + "x" + str(2*size)
    data.temporal_size = t_size
    data.grid_spacing = '13 km'

    print "Prior generation took place at: ", data.Date_created

    data.createDimension('height', len(mean)/2)
    data.createDimension('wnum', len(climo.variables['wnum'][:]))
    data.createDimension('height2', len(mean))

    var = data.createVariable('mean_pressure', 'f4', ('height',))
    var[:] = climo.variables['mean_pressure'][:]
    var.units = climo.variables['mean_pressure'].units
    var.long_name = climo.variables['mean_pressure'].long_name

    var = data.createVariable('height', 'f4', ('height',))
    var[:] = climo.variables['height'][:]
    var.units = climo.variables['height'].units
    var.long_name = climo.variables['height'].long_name

    var = data.createVariable('mean_temperature', 'f4', ('height',))
    var[:] = mean[:55]
    var.units = climo.variables['mean_temperature'].units
    var.long_name = climo.variables['mean_temperature'].long_name

    var = data.createVariable('mean_mixingratio', 'f4', ('height',))
    var[:] = mean[55:]
    var.units = climo.variables['mean_mixingratio'].units
    var.long_name = climo.variables['mean_mixingratio'].long_name

    var = data.createVariable('height2', 'f4', ('height2',))
    var[:] = climo.variables['height2'][:]
    var.units = climo.variables['height2'].units
    var.long_name = climo.variables['height2'].long_name

    var = data.createVariable('wnum', 'f4', ('wnum',))
    var[:] = climo.variables['wnum'][:]
    var.units = climo.variables['wnum'].units
    var.long_name = climo.variables['wnum'].long_name

    var = data.createVariable('delta_od', 'f4', ('wnum',))
    var[:] = climo.variables['delta_od'][:]
    var.units = climo.variables['delta_od'].units
    var.long_name = climo.variables['delta_od'].long_name

    var = data.createVariable('radiance_true', 'f4', ('wnum',))
    var[:] = climo.variables['radiance_true'][:]
    var.units = climo.variables['radiance_true'].units
    var.long_name = climo.variables['radiance_true'].long_name

    var = data.createVariable('radiance_fast', 'f4', ('wnum',))
    var[:] = climo.variables['radiance_fast'][:]
    var.units = climo.variables['radiance_fast'].units
    var.long_name = climo.variables['radiance_fast'].long_name

    var = data.createVariable('mean_prior', 'f4', ('height2',))
    var[:] = mean
    var.units = climo.variables['mean_prior'].units
    var.long_name = climo.variables['mean_prior'].long_name

    var = data.createVariable('covariance_prior', 'f4', ('height2','height2',))
    var[:] = cov
    var.units = climo.variables['covariance_prior'].units
    var.long_name = climo.variables['covariance_prior'].long_name

    climo.close()
    
    return priorCDF_filename
Пример #5
0
def write_CRTM_GOCART(dimensions,
                      variables,
                      output_gocart,
                      Release=3,
                      Version=1):
    [
        n_Wavelengths, n_Radii, n_Sigma, n_Types, n_RH, n_Legendre_Terms,
        n_Phase_Elements, tnsl
    ] = dimensions

    [
        Aerosol_Type, Aerosol_Type_Name, Wavelength, Reff, Reff_RH, Rsig, RH,
        ke, w, g, pcoeff, growthf
    ] = variables

    # Unit conversion
    Reff = Reff * 1e6
    Reff_RH = Reff_RH * 1e6
    Wavelength = Wavelength * 1e6

    # OUTPUT interpolated data to a nc4 file
    print('write to: ' + output_gocart)

    w_nc_fid = Dataset(output_gocart, 'w', format='NETCDF4')
    # write dimensions
    w_nc_fid.createDimension('n_Wavelengths', n_Wavelengths)
    w_nc_fid.createDimension('n_Radii', n_Radii)
    w_nc_fid.createDimension('n_Sigma', n_Sigma)
    w_nc_fid.createDimension('n_Types', n_Types)
    w_nc_fid.createDimension('n_RH', n_RH)
    w_nc_fid.createDimension('n_Legendre_Terms', n_Legendre_Terms)
    w_nc_fid.createDimension('n_Phase_Elements', n_Phase_Elements)
    w_nc_fid.createDimension('tnsl', tnsl)

    # create varaibles
    Aerosol_Type_nc = w_nc_fid.createVariable('Aerosol_Type', 'i4',
                                              ('n_Types', ))
    Aerosol_Type_Name_nc = w_nc_fid.createVariable('Aerosol_Type_Name', 'S1',
                                                   ('n_Types', 'tnsl'))
    Wavelength_nc = w_nc_fid.createVariable('Wavelength', 'f8',
                                            ('n_Wavelengths', ))
    Reff_nc = w_nc_fid.createVariable('Reff',
                                      'f8', ('n_Types', 'n_Radii'),
                                      fill_value=0.0)
    Rsig_nc = w_nc_fid.createVariable('Rsig', 'f8', ('n_Types', 'n_Sigma'))
    RH_nc = w_nc_fid.createVariable('RH', 'f8', ('n_RH', ))
    ke_nc = w_nc_fid.createVariable(
        'ke', 'f8', ('n_Types', 'n_Sigma', 'n_Radii', 'n_RH', 'n_Wavelengths'))
    w_nc = w_nc_fid.createVariable(
        'w', 'f8', ('n_Types', 'n_Sigma', 'n_Radii', 'n_RH', 'n_Wavelengths'))
    g_nc = w_nc_fid.createVariable(
        'g', 'f8', ('n_Types', 'n_Sigma', 'n_Radii', 'n_RH', 'n_Wavelengths'))
    pcoeff_nc = w_nc_fid.createVariable(
        'pcoeff', 'f8', ('n_Phase_Elements', 'n_Legendre_Terms', 'n_Types',
                         'n_Sigma', 'n_Radii', 'n_RH', 'n_Wavelengths'))
    Reff_RH_nc = w_nc_fid.createVariable('Reff_RH',
                                         'f8', ('n_Types', 'n_Radii', 'n_RH'),
                                         fill_value=0.0)
    growth_factor_nc = w_nc_fid.createVariable('growth_factor',
                                               'f8',
                                               ('n_Types', 'n_Radii', 'n_RH'),
                                               fill_value=0.0)

    # write attributes
    nc_fid = Dataset('AerosolCoeff.nc4', 'r')
    for ncattr in nc_fid.variables['Aerosol_Type'].ncattrs():
        Aerosol_Type_nc.setncattr(
            ncattr, nc_fid.variables['Aerosol_Type'].getncattr(ncattr))
    for ncattr in nc_fid.variables['Aerosol_Type_Name'].ncattrs():
        Aerosol_Type_Name_nc.setncattr(
            ncattr, nc_fid.variables['Aerosol_Type_Name'].getncattr(ncattr))
    for ncattr in nc_fid.variables['Wavelength'].ncattrs():
        Wavelength_nc.setncattr(
            ncattr, nc_fid.variables['Wavelength'].getncattr(ncattr))
    for ncattr in nc_fid.variables['Reff'].ncattrs():
        Reff_nc.setncattr(ncattr, nc_fid.variables['Reff'].getncattr(ncattr))
    for ncattr in nc_fid.variables['Rsig'].ncattrs():
        Rsig_nc.setncattr(ncattr, nc_fid.variables['Rsig'].getncattr(ncattr))
    for ncattr in nc_fid.variables['RH'].ncattrs():
        RH_nc.setncattr(ncattr, nc_fid.variables['RH'].getncattr(ncattr))
    for ncattr in nc_fid.variables['ke'].ncattrs():
        ke_nc.setncattr(ncattr, nc_fid.variables['ke'].getncattr(ncattr))
    for ncattr in nc_fid.variables['w'].ncattrs():
        w_nc.setncattr(ncattr, nc_fid.variables['w'].getncattr(ncattr))
    for ncattr in nc_fid.variables['g'].ncattrs():
        g_nc.setncattr(ncattr, nc_fid.variables['g'].getncattr(ncattr))
    for ncattr in nc_fid.variables['pcoeff'].ncattrs():
        pcoeff_nc.setncattr(ncattr,
                            nc_fid.variables['pcoeff'].getncattr(ncattr))
    w_nc_fid.variables[
        'Reff'].long_name = 'dry particle effective radius of sizebin'
    w_nc_fid.variables['Reff'].units = 'Microns (um)'
    w_nc_fid.variables['Reff_RH'].long_name = 'effective radius of size bin'
    w_nc_fid.variables['Reff_RH'].units = 'Microns (um)'
    w_nc_fid.variables[
        'growth_factor'].long_name = 'growth factor = ratio of wet to dry particle radius'
    w_nc_fid.variables['growth_factor'].units = "fraction"
    nc_fid.close()

    # write variables
    w_nc_fid.variables['Aerosol_Type'][:] = Aerosol_Type
    w_nc_fid.variables['Aerosol_Type_Name'][:] = Aerosol_Type_Name
    w_nc_fid.variables['Wavelength'][:] = Wavelength
    w_nc_fid.variables['Reff'][:] = Reff
    w_nc_fid.variables['Reff_RH'][:] = Reff_RH
    w_nc_fid.variables['Rsig'][:] = Rsig
    w_nc_fid.variables['RH'][:] = RH
    w_nc_fid.variables['ke'][:] = ke
    w_nc_fid.variables['w'][:] = w
    w_nc_fid.variables['ke'][:] = ke
    w_nc_fid.variables['g'][:] = g
    w_nc_fid.variables['pcoeff'][:] = pcoeff
    w_nc_fid.variables['growth_factor'][:] = growthf

    # write global variables
    w_nc_fid.Release = np.int32(Release)
    w_nc_fid.Version = np.int32(Version)
    w_nc_fid.Data_Source = "GOCART-GEOS5"
    w_nc_fid.Title = "Aerosol Optical Properties in the infrared and visible spectral region."
    w_nc_fid.History = "Combine GOCART LUTs (Cheng DAND, 202011)"
    w_nc_fid.Comment = "Based on GOCART LUTs: optics_DU.v15_6.nc, optics_DU.v15_6.nc, optics_NI.v2_6.nc,                         optics_OC.v1_6.nc, optics_SS.v3_6.nc, optics_SU.v1_6.nc, with delta fit applied"

    w_nc_fid.close()
Пример #6
0
type = "ARM.RUCRAP"
priorCDF_filename = 'Xa_Sa_datafile.55_levels.' + yyyymmdd + '.' + hh + '.' + type + '.cdf'
print priorCDF_filename
stop

data = Dataset(priorCDF_filename, 'w')

data.Date_created = datetime.strftime(datetime.now(), '%a %b %d %H:%M:%S %Y')
data.Version = 'get_RUCRAP_prior.py'
data.Machine_used = 'N/A'
data.LBL_HOME = climo.LBL_HOME
data.Standard_atmos = climo.Standard_atmos
data.QC_limits_T = climo.QC_limits_T
data.QC_limits_q = climo.QC_limits_q
data.Comment = 'Thermodynamic profiles from the ' + type + ' analyses.'
#Need to include the web links to the data used to produce these files
#Need to include the times used to produce this file.
data.Nsonde = str(priors.shape[0]) + ' profiles were included in the computation of this prior dataset.'
data.lat = aeri_lat
data.lon = aeri_lon
data.domain_size = str(2*size) + "x" + str(2*size)
data.grid_spacing = '13 km'

print data.Date_created

data.createDimension('height', len(all_temps.T))
data.createDimension('wnum', len(climo.variables['wnum'][:]))
data.createDimension('height2', len(mean))

var = data.createVariable('mean_pressure', 'f4', ('height',))
Пример #7
0
def makeFile(mean, cov, dir, types, yyyymmdd, hh, paths, climo, size, t_size,
             n, aeri_lat, aeri_lon):
    priorCDF_filename = dir.strip(
    ) + '/Xa_Sa_datafile.55_levels.' + yyyymmdd + '.' + hh + '.' + types[
        0] + '.' + str(aeri_lat) + '.' + str(aeri_lon) + '.cdf'
    print "Saving prior file as: " + priorCDF_filename

    data = Dataset(priorCDF_filename, 'w', 'NETCDF3_CLASSIC')

    data.Date_created = datetime.strftime(datetime.now(),
                                          '%a %b %d %H:%M:%S %Y')
    data.Version = 'get_model_prior.py'
    data.Machine_used = platform.platform()
    data.model = "RUC/RAP"
    data.LBL_HOME = climo.LBL_HOME
    data.Standard_atmos = climo.Standard_atmos
    data.QC_limits_T = climo.QC_limits_T
    data.QC_limits_q = climo.QC_limits_q
    data.Comment = "Prior generated using model (" + types[0] + ") data."
    #Need to include the web links to the data used to produce these files
    #Need to include the times used to produce this file.
    data.Nsonde = str(
        n
    ) + ' profiles were included in the computation of this prior dataset.'
    data.lat = aeri_lat
    data.lon = aeri_lon
    data.paths = '; '.join(paths)
    data.model_types = '; '.join(types)
    data.domain_size = str(2 * size) + "x" + str(2 * size)
    data.temporal_size = t_size
    data.grid_spacing = '13 km'

    print "Prior generation took place at: ", data.Date_created

    data.createDimension('height', len(mean) / 2)
    data.createDimension('wnum', len(climo.variables['wnum'][:]))
    data.createDimension('height2', len(mean))

    var = data.createVariable('mean_pressure', 'f4', ('height', ))
    var[:] = climo.variables['mean_pressure'][:]
    var.units = climo.variables['mean_pressure'].units
    var.long_name = climo.variables['mean_pressure'].long_name

    var = data.createVariable('height', 'f4', ('height', ))
    var[:] = climo.variables['height'][:]
    var.units = climo.variables['height'].units
    var.long_name = climo.variables['height'].long_name

    var = data.createVariable('mean_temperature', 'f4', ('height', ))
    var[:] = mean[:55]
    var.units = climo.variables['mean_temperature'].units
    var.long_name = climo.variables['mean_temperature'].long_name

    var = data.createVariable('mean_mixingratio', 'f4', ('height', ))
    var[:] = mean[55:]
    var.units = climo.variables['mean_mixingratio'].units
    var.long_name = climo.variables['mean_mixingratio'].long_name

    var = data.createVariable('height2', 'f4', ('height2', ))
    var[:] = climo.variables['height2'][:]
    var.units = climo.variables['height2'].units
    var.long_name = climo.variables['height2'].long_name

    var = data.createVariable('wnum', 'f4', ('wnum', ))
    var[:] = climo.variables['wnum'][:]
    var.units = climo.variables['wnum'].units
    var.long_name = climo.variables['wnum'].long_name

    var = data.createVariable('delta_od', 'f4', ('wnum', ))
    var[:] = climo.variables['delta_od'][:]
    var.units = climo.variables['delta_od'].units
    var.long_name = climo.variables['delta_od'].long_name

    var = data.createVariable('radiance_true', 'f4', ('wnum', ))
    var[:] = climo.variables['radiance_true'][:]
    var.units = climo.variables['radiance_true'].units
    var.long_name = climo.variables['radiance_true'].long_name

    var = data.createVariable('radiance_fast', 'f4', ('wnum', ))
    var[:] = climo.variables['radiance_fast'][:]
    var.units = climo.variables['radiance_fast'].units
    var.long_name = climo.variables['radiance_fast'].long_name

    var = data.createVariable('mean_prior', 'f4', ('height2', ))
    var[:] = mean
    var.units = climo.variables['mean_prior'].units
    var.long_name = climo.variables['mean_prior'].long_name

    var = data.createVariable('covariance_prior', 'f4', (
        'height2',
        'height2',
    ))
    var[:] = cov
    var.units = climo.variables['covariance_prior'].units
    var.long_name = climo.variables['covariance_prior'].long_name

    climo.close()

    return priorCDF_filename