def test_diff(self): aaa = Column('aaa') bbb = Column('aaa') # Make sure can diff on Nones, results in no diff. aaa.flags_woce = [None] bbb.flags_woce = [None] diff = aaa.diff(bbb) self.assertFalse(diff['diff'])
def _read_oliver_sun(dfc, fileobj, cfg): """Read HRP2 format from Oliver Sun.""" mat = loadmat(fileobj) filekey = mat.keys()[0] casts = mat[filekey][0] for cast in casts: dfile = DataFile() dfc.append(dfile) dfile.globals['EXPOCODE'] = cfg['expocode'] # TODO dfile.globals['DEPTH'] = 0 for key, item in zip(cast.dtype.names, cast): if item.shape == (1, 1): key = cfg['global_mapping'].get(key, None) if key: dfile.globals[key] = item[0, 0] else: try: dfile[key] = Column(key) dfile[key].values = list(item.flatten()) # Act as if all files had QC and assign it to OceanSITES 1. # Assuming that someone has already gone through level 0 # data and we are receiving level 1 or higher. dfile[key].flags_woce = [2] * len(dfile[key].values) except KeyError: pass try: dfile.globals['STNNBR'] except KeyError: dfile.globals['STNNBR'] = '999' woce.fuse_datetime(dfile)
def australian_navy_ctd(args): """Download and convert Australian Navy CTD data.""" from pydap.client import open_url from libcchdo.thredds import crawl from libcchdo.formats.ctd.zip import exchange as ctdzipex from libcchdo.formats.zip import write as zwrite dfcs = [] cf_param_to_cchdo_param = { 'sea_water_pressure': 'CTDPRS', 'sea_water_temperature': 'CTDTMP', 'sea_water_practical_salinity': 'CTDSAL', } ignored_qc_flags = [ 'time_qc_flag', 'position_qc_flag', ] qc_conventions = { 'Proposed IODE qc scheme March 2012': { 1: 2, # good 2: 5, # not_evaluated_or_unknown 3: 3, # suspect 4: 4, # bad 9: 9, # missing }, } dfc = DataFileCollection() catalog = "http://www.metoc.gov.au/thredds/catalog/RAN_CTD_DATA/catalog.xml" for url in crawl(catalog): df = DataFile() log.info(u'Reading %s', url) dset = open_url(url) vars = dset.keys() for vname in vars: var = dset[vname] attrs = var.attributes if 'standard_name' in attrs: std_name = attrs['standard_name'] if std_name == 'time': df.globals['_DATETIME'] = \ datetime(1950, 1, 1) + timedelta(var[:]) elif std_name == 'latitude': df.globals['LATITUDE'] = var[:] elif std_name == 'longitude': df.globals['LONGITUDE'] = var[:] elif std_name in cf_param_to_cchdo_param: cparam = cf_param_to_cchdo_param[std_name] if '_FillValue' in attrs: fill_value = attrs['_FillValue'] values = [] for x in var[:]: if equal_with_epsilon(x, fill_value): values.append(None) else: values.append(x) else: values = var[:] try: df[cparam].values = values except KeyError: df[cparam] = Column(cparam) df[cparam].values = values elif 'status_flag' in std_name: flagged_param = std_name.replace('status_flag', '').strip() cparam = cf_param_to_cchdo_param[flagged_param] qc_convention = attrs['quality_control_convention'] if qc_convention in qc_conventions: qc_map = qc_conventions[qc_convention] df[cparam].flags_woce = [qc_map[x] for x in var[:]] else: log.debug('unhandled standard_name %s', std_name) elif ('long_name' in attrs and attrs['long_name'] == 'profile identifier'): profile_id = var[:] cruise_id = profile_id / 10**4 profile_id = profile_id - cruise_id * 10**4 df.globals['EXPOCODE'] = str(cruise_id) df.globals['STNNBR'] = str(profile_id) df.globals['CASTNO'] = str(1) elif vname in ignored_qc_flags: df.globals['_' + vname] = var[:] elif (vname.endswith('whole_profile_flag') or vname.endswith('sd_test')): pass else: log.debug('unhandled variable %s', vname) # attach new file to appropriate collection if dfc.files: if dfc.files[0].globals['EXPOCODE'] != df.globals['EXPOCODE']: dfcs.append(dfc) dfc = DataFileCollection() dfc.append(df) with closing(args.output) as out_file: next_id = 0 def get_filename(dfc): try: return '{0}_ct1.zip'.format(dfc.files[0].globals['EXPOCODE']) except IndexError: next_id += 1 return '{0}_ct1.zip'.format(next_id) zwrite(dfcs, out_file, ctdzipex, get_filename)