def csat_no_cal(files, outname, where): v = vcm.VCM(files, verbose=False) cal = v.get_vcm('cal333+cal05+cal20+cal80') csat = v.get_vcm('csat') calcloudy = np.sum(cal, axis=1) csatcloudy = np.sum(csat, axis=1) csatonly = np.where((csatcloudy > 0) & (calcloudy == 0))[0] print 100. * csatonly.shape[0] / calcloudy.shape[0] vcm_csatonly = np.zeros_like(csat) vcm_csatonly[csatonly, :] = csat[csatonly, :] csatcloudy[calcloudy > 0] = 0 dset = da.Dataset() time_axis = da.Axis(v.time, 'tai_time') alt_axis = da.Axis(v.altitude, 'altitude') dset['lon'] = da.DimArray(v.lon, [time_axis]) dset['lat'] = da.DimArray(v.lat, [time_axis]) dset['csat'] = da.DimArray(vcm_csatonly, [time_axis, alt_axis]) dset['cloudpts'] = da.DimArray(csatcloudy, [time_axis]) check_out_dir(where) dset.write_nc(where + outname, 'w', zlib=True, complevel=9)
def vcm_dataset_from_l2_orbit(filename): #print 'Creating vcm dataset from l2 file ' + filename l2 = calipso_l2.Cal2(filename) lon, lat = l2.coords() tai_time = l2.time() nl, base, top = l2.layers() havg = l2.horizontal_averaging() ltype = l2.layer_type() tai_time_min, tai_time_max = l2.time_bounds() tropo = l2.tropopause_height() elevation = l2.dem_surface_elevation()[:,1] l2.close() tropo[lat < -60] = 11. tropo[lat > 60] = 11. dset = da.Dataset() time_axis = da.Axis(tai_time, 'tai_time') alt_axis = da.Axis(vcm_alt, 'altitude') for havg_vcm in havgs_vcm: vcm = vcm_from_layers(nl, base, top, havg, ltype, tropo, only_havg=havg_vcm) vcm_name = 'cal%02d' % (havg_vcm) dset[vcm_name] = da.DimArray(vcm, [time_axis, alt_axis]) dset['lon'] = da.DimArray(lon, [time_axis]) dset['lat'] = da.DimArray(lat, [time_axis]) dset['time_min'] = da.DimArray(tai_time_min, [time_axis]) dset['time_max'] = da.DimArray(tai_time_max, [time_axis]) dset['elevation'] = da.DimArray(elevation, [time_axis]) return dset
def reindex_vcms(vcm, vcm5, vcmc): # vcmc can be None mintime5, maxtime5 = vcm5['time_min'].values, vcm5['time_max'].values nprof5 = mintime5.shape[0] time333 = vcm['time'] nprof333 = time333.shape[0] # first find 333m profiles indexes for a given 5km profile n1, n2 = np.zeros(nprof5, 'int16'), np.zeros(nprof5, 'int16') n = 0 for i in np.r_[0:nprof5]: n1[i] = n while n < nprof333 and time333[n] < maxtime5[i]: n += 1 n2[i] = n # print 'Range for profile5 %d = %d - %d' % (i, n1[i], n2[i]) # reindex all flags on the same 333m coordinates time_axis = da.Axis(vcm['time'], 'tai_time') alt_axis = da.Axis(vcm['altitude'], 'altitude') reindexed = da.Dataset() reindexed['lon'] = da.DimArray(vcm['lon'], [ time_axis, ]) reindexed['lat'] = da.DimArray(vcm['lat'], [ time_axis, ]) reindexed['cal333'] = da.DimArray(vcm['cal333'], [time_axis, alt_axis]) # remap CALIPSO flag for vcm_name in 'cal05', 'cal20', 'cal80': this_vcm = remap_profiles(vcm5[vcm_name].values, n1, n2, nprof333) reindexed[vcm_name] = da.DimArray(this_vcm, labels=reindexed['cal333'].labels, dims=reindexed['cal333'].dims) # remap cloudsat flag if vcmc is None: this_vcm = np.ones_like(vcm['cal333'], 'int8') * -1. else: vcmc.values = remove_ground_clutter(vcmc.values, vcm5['elevation'].values, vcm5['cal05'].altitude) this_vcm = remap_profiles(vcmc.values, n1, n2, nprof333) reindexed['csat'] = da.DimArray(this_vcm, labels=reindexed['cal333'].labels, dims=reindexed['cal333'].dims) # Now we have # cal333, cal05, cal20, cal80, csat in reindexed. return reindexed
def my_read_vcm_nc(f=f): nc = netCDF4.Dataset(f, 'r') time = nc.variables['tai_time'][:] alt = nc.variables['altitude'][:] lon = nc.variables['lon'][:] lat = nc.variables['lat'][:] cal333 = nc.variables['cal333'][:] time_axis = da.Axis(time, 'time') alt_axis = da.Axis(alt, 'alt') lon = da.DimArray(lon, time_axis) lat = da.DimArray(lon, time_axis) cal333 = da.DimArray(cal333, [time_axis, alt_axis])
def cf_from_vcm_orbit(vcm_orbit, layers): print 'Creating vcm from ', vcm_orbit # read data v = vcm.VCM(vcm_orbit, verbose=False) print '%d profiles' % (v.lon.shape[0]) lon_axis = da.Axis(lonbins[:-1], 'lon') lat_axis = da.Axis(latbins[:-1], 'lat') # gridded number of profiles h, xx, yy = np.histogram2d(v.lon, v.lat, bins=(lonbins, latbins)) out = da.Dataset() out['nprof'] = da.DimArray(h * 3, [lon_axis, lat_axis]) out['nprof'].longname = 'Number of measured profiles' for layer in layers: altrange = layers[layer] altidx = np.where((v.altitude >= altrange[0]) & (v.altitude < altrange[1]))[0] for vcm_name in vcm_names: # number of cloudy profiles in grid and altitude range this_vcm = v.get_vcm(vcm_name) assert this_vcm is not None if layer == 'total': t = this_vcm else: t = np.take(this_vcm, altidx, axis=1) cloudy_profile = np.sum(t, axis=1) np.clip(cloudy_profile, 0, 3, out=cloudy_profile) h, xx, yy = np.histogram2d(v.lon, v.lat, bins=(lonbins, latbins), weights=cloudy_profile) outname = vcm_name + '_cprof_%s' % (layer) out[outname] = da.DimArray(h, [lon_axis, lat_axis]) out[outname].longname = 'Number of cloudy profiles from cloud mask = ' + vcm_name + ' at altitudes %5.2f - %5.2f' % ( altrange[0], altrange[1]) return out
def my_read_nc_lon(f=f): nc = netCDF4.Dataset(f, 'r') time = nc.variables['tai_time'][:] lon = nc.variables['lon'][:] nc.close() axis = da.Axis(time, 'time') d1 = da.DimArray(lon, axis) return d1
def zone_vcm_from_vcm_orbit(vcm_orbit, latbins=latbins): # read data #print 'opening ' + vcm_orbit v = vcm.VCM(vcm_orbit, verbose=False) nlat = latbins.shape[0] nalt = v.altitude.shape[0] out = da.Dataset() # ilatbins = vector with nprof indexes containing bin numbers ilatbins = np.digitize(v.lat, latbins) lat_axes = da.Axis(latbins[:-1], 'lat') nprof, xx = np.histogram(v.lat, bins=latbins) out['nprof'] = da.DimArray(nprof * 3, [lat_axes]) alt_axes = da.Axis(v.altitude, 'altitude') for name in vcm_names: this_vcm = v.get_vcm(name) zone_vcm = np.zeros([nlat-1, nalt], dtype='uint16') prof_is_cloudy = np.sum(this_vcm, axis=1) np.clip(prof_is_cloudy, 0, 3, out=prof_is_cloudy) cprof, xx = np.histogram(v.lat, bins=latbins, weights=prof_is_cloudy) out[name + '_cprof'] = da.DimArray(cprof, [lat_axes]) for i,ilatbin in enumerate(ilatbins[:-1]): if prof_is_cloudy[i] > 0: zone_vcm[ilatbin,:] += np.take(this_vcm, i, axis=0) out[name] = da.DimArray(zone_vcm, [lat_axes, alt_axes], longname='Number of cloudy points in lat-z bin, considering ' + name) return out
def cflon(f, altmin, latbounds): # altmin is an array v = vcm.VCM(f, verbose=False) out = da.Dataset() lon_axis = da.Axis(lonbins[:-1], 'lon') # number of profiles per lon bin h, xx = np.histogram(v.lon, bins=lonbins) out['nprof'] = da.DimArray(h, [lon_axis]) out['nprof'].longname = 'Number of measured profiles' for n in names: cv = v.get_vcm(n) assert cv is not None # clip latitudes latidx = (v.lat >= latbounds[0]) & (v.lat < latbounds[1]) cv = np.take(cv, latidx, axis=0) lon = np.take(v.lon, latidx, axis=0) outdict = dict() for a in altmin: idx = np.where(v.altitude >= a)[0] cloudy = np.take(cv, idx, axis=1) cloudy = np.sum(cloudy, axis=1) np.clip(cloudy, 0, 1, out=cloudy) h, xx = np.histogram(v.lon, bins=lonbins, weights=cloudy) outdict[a] = da.DimArray(h, [ lon_axis, ]) outname = n + '_cprof' out[outname] = da.stack(outdict, axis='altmin') out[outname].longname = 'Number of cloudy profiles from cloud mask = ' + n return out