def do_import(self, manager, p): # self._import_lithologies(manager,p) # self._import_tas(manager, p) # self._import_environment(manager, p) self._import_rock_type(manager, p) return xp = XLSParser() xp.load(p, header_idx=2) overwrite_meta = True overwrite_alt_name = True add_samples = True db = manager.db progress = manager.open_progress(xp.nrows) self._import_lithologies(manager, db, xp) with db.session_ctx(): ellps = xp.get_value(0, 1) zone = xp.get_value(1, 1) convert_coordinates = xp.has_key('E') and xp.has_key('N') self._ref_system = pyproj.Proj(proj='utm', zone=zone, ellps=ellps.upper()) self._wgs84 = pyproj.Proj(init='EPSG:4326') if convert_coordinates: keys = ('sample', 'alt_name', 'project', 'material') else: keys = ('sample', 'alt_name', 'project', 'material', 'lithology', 'lat', 'long', 'elevation', 'location', 'igsn', 'note') for args in xp.itervalues(keys=keys): sample, project, material = args['sample'], args['project'], args['material'] progress.change_message('Setting sample {}'.format(sample)) dbsample = db.get_sample(sample, project, material, verbose=False) # if not dbsample: # self.info('adding sample {} project={} material={}'.format(sample, project, material)) # dbsample=db.add_sample(sample, project, material) alt_name = args['alt_name'] alt_names = None if alt_name: head, tail = alt_name.split('-') try: itail = int(tail) padded_alt_name = '{}-{:03d}'.format(head, itail) non_padded_alt_name = '{}-{:01d}'.format(head, itail) except ValueError: padded_alt_name, non_padded_alt_name = '', '' alt_names = (alt_name, padded_alt_name, non_padded_alt_name) if not dbsample: if alt_names: for a in alt_names: dbsample = db.get_sample(a, project, material, verbose=False) if dbsample: if overwrite_alt_name: self.debug('setting name={} alt_name={}, padded_alt_name={}'.format(sample, a, padded_alt_name)) dbsample.name = sample dbsample.alt_name = padded_alt_name break if not dbsample: if alt_names: for a in alt_names: dbsample = db.get_sample(a, project, material, verbose=False) if dbsample: break if not dbsample: if add_samples: if args['lat'] and args['long']: dbsample = db.add_sample(sample, project, material) if alt_name: dbsample.alt_name = padded_alt_name if not dbsample: continue if overwrite_meta: if convert_coordinates: lon, lat = self._convert_coordinates(args['E'], args['N']) else: lat, lon = args['lat'], args['long'] dbsample.lat = lat dbsample.long = lon dbsample.elevation = args['elevation'] dbsample.location = args['location'] dbsample.igsn = args['igsn'] dbsample.note = args['note'] dbsample.lithology = args['lithology'] progress.close()