Just 13 months ago, South African President Pieter W. Botha returned from a tour of Western Europe, a visit that symbolized this white-ruled country's bid to return to international respectability.

Botha had sought to convince many in the West, beginning with the Reagan administration, that South Africa was embarked upon a program of real reforms, that it had overcome white intransigence on its right and radical black nationalism on its left and was moving toward a political future that eventually would meet the legitimate aspirations of all its races.

Today the vision Botha offered the West lies shattered as South Africa faces perhaps the worst racial crisis in its history. Nearly 500 persons have died. Some of its black townships have been rendered ungovernable.

The subtle melodies of reconciliation now have been exchanged for the hard cadences of martial law. In just one week, more than 1,000 persons have been jailed, held incommunicado without charge, bail or lawyers. And the West is watching with growing dismay, debating not whether to impose economic sanctions against Pretoria, but when.

The story of what went wrong is a tale of political miscalculation, of a government that fell victim to its own ideology and of a grand scheme that carried within itself the seeds of its own destruction. It is also a story of leaders who now appear trapped in a classic dilemma not dissimilar to the one that befell the shah of Iran in 1979 -- caught between halfhearted reform and halfhearted repression, uncertain which strategy to pursue and unwilling, perhaps even unable, to go all the way with either.

The analogy with Iran can be overdrawn -- there are no analysts here, even among the government's most passionate opponents, who believe revolution is a few days or years away. But the reality of a government ensnared, whose every action seems to feed the appetite for opposition, and of western allies who look on with mixed feelings and thin advice, has a familiar ring.

One week after a state of emergency was declared, South Africa's black townships look sullen but silent, with only occasional spasms of violence. Helmeted police and soldiers, armed with automatic weapons and the power to arrest anyone for any reason, roam the narrow streets in armored vehicles. Each night after midnight, security police working off long lists of names go from house to house, picking up people identified as community activists. More than 100 have been arrested nightly, according to the official count, and opposition groups contend that the real numbers are even higher.

The impact can be seen at the Transvaal headquarters here of the opposition United Democratic Front. The file cabinets are empty, their contents seized in a police raid earlier this week, the offices abandoned by activists who are now either in jail or hiding. Phones that once rang incessantly are silent; a photocopying machine sits unplugged in the middle of a vacant room.

The lone staff member present yesterday spoke of trying to organize a response to the emergency, perhaps a rally in Soweto or a national economic boycott. "But there is no way to organize or plan anything -- everyone is missing," he said. Grass-Roots Activists Targeted

The front, a loose umbrella coalition of more than 700 community-based groups, has been the main target of the crackdown. The operation has been swift and surgical, hitting not at national leaders but at grass-roots activists -- trade unionists, teachers, community organizers.

"They are systematically attempting to dismantle the various organizations, and they are hitting exactly the right people," said political scientist Tom Lodge, a leading academic expert on black politics. "They've done a great deal of intelligence and homework in advance."

But Lodge and other analysts see the government's triumph as only temporary. The grievances that ignited and fueled the township violence remain, as does the anger. "The government may have bought itself a breathing space," said Lodge, "but a breathing space for what?"

One year ago that question might have gotten an easy answer from a government that was confident things were going its way.

Botha had risked splitting the ruling white Afrikaner community by proposing a new constitution that for the first time gave limited national political rights to the country's mixed-race (known here as Coloreds) and Asian minorities. South Africa's black majority was excluded from these rights, but there also were provisions establishing local township councils in black urban areas. It was a first step, Botha's supporters claimed, on the road to self-government for at least urban blacks.

At first the gamble seemed to pay off. Botha won a two-thirds majority in the November 1983 whites-only referendum over the constitution. New houses of Parliament were opened for Coloreds and Asians despite the fact that less than one-third of eligible voters cast ballots in the elections for these bodies.

But it was a short-lived triumph. For one thing, the government, in its attempt to appear more liberal and conciliatory, had allowed new locally based organizations to gain a foothold in black communities. Black opposition to the new constitution helped unify these groups, which in August 1983 formed the United Democratic Front.

From its inception, the front's position was that the government's reform schemes were phony, that Botha was seeking only to maintain Afrikaner control while co-opting Coloreds and middle-class, urban blacks. Government's Verbal Ambivalence

The government's own verbal ambivalence fed these suspicions. While Botha spoke of ridding South Africa of "hurtful discrimination" and of negotiating with "genuine" black leaders, he also insisted that he never would accede to one-man, one-vote democracy. The government appeared intent on creating its own black leadership and setting its own agenda for talks, and "radicals" ranging from Nobel Peace Prize laureate Desmond Tutu to jailed resistance leader Nelson Mandela were not invited.

Looking back, government supporters contend that the problem was one of perception. South Africa was moving step by step away from apartheid, but movement had to be slow and measured to avoid alienating the deeply conservative white constituency.

"If I were black, I would probably also perceive the reforms as part of grand apartheid," said Willie Breytenbach, who until three weeks ago was director of constitutional planning in Pretoria. "As a civil servant, I saw them as an important move away from the structure. We went as fast as was politically possible, and when I look back I am proud of what we achieved."

Breytenbach blames the unrest on "the explosive gap" between the reforms and the black expectations they aroused. The problem was aggravated by the ongoing recession, which drove up unemployment and inflation and left Pretoria with little money to fund the new township councils. The councils felt compelled to raise rents, a move that, given economic hardships in the townships, proved fatal.

The government also fell victim to its own ideology, some analysts argue. Having worked for more than three decades to split South Africa into racially and ethnically distinct communities, the government's lines of communication and knowledge of black politics was sparse. It also believed that blacks were irrevocably divided along tribal and economic lines and that each group could be identified easily and played off against others.

Instead, the black community quickly appeared to unite against the new institutions the government sought to create. Because they were locally based and highly visible, the township councils became targets, first of protests, later of gasoline bombs and rocks.

Heavy-handed police action further aggravated the problem. After residents of townships in the Vaal Triangle south of Johannesburg demonstrated against a big rent increase last September, the government put on a show of force, surrounding the area with the Army while police went door to door, arresting hundreds of persons.

The spiral had begun. Township councilmen and black police officers were singled out for attack. The black deputy mayor of Sharpeville in the Vaal was hacked to death with machetes, his body flung into the family car and set ablaze. Police counterattacked. More than 100 persons died during the first two months of unrest, and the toll has climbed steadily ever since.

The black townships generally are a world apart, located out of white sight across invisible borders in self-contained ghettos. In theory, they can be cordoned off and forgotten, allowed to implode, with no visible impact on the white world of shopping malls, swimming pools and two-car garages.

But some of the townships sit ominously close to white suburbs, and outside Johannesburg there have been a half dozen attacks on whites in recent weeks. That, analysts say, was one reason South Africa's leaders decided to declare a state of emergency. Restlessness Among Whites

Officials also had seen the results of a confidential poll indicating that whites were growing increasingly restless over the violence, according to knowledgeable sources. There were fears that right-wing groups, both within the security forces and outside, were forming vigilante units, even death squads to deal with a problem they perceived the government was not handling.

"I think this [declaration] was intended more for whites than for blacks," said political scientist Hermann Giliomee of the University of Cape Town. Officials, he said, have seen increasing signs of discontent from whites who believe that the government is lost "in some kind of drift . . . that they're not getting things under control."

Then there was Soweto. Until two weeks ago, South Africa's largest and most prosperous black urban center had remained relatively untouched by the violence. But on July 17, a crowd that had gathered to protest the arrests of 107 demonstrators went on a rampage, hijacking buses and burning shops and houses. The rioting continued sporadically for three days. Officials feared that if Soweto, whose population is estimated at 1.5 million, caught fire, all of South Africa might burn.

Botha publicly has offered a simple explanation for the failure of his reforms and the subsequent necessity of martial law. He blames Marxist radicals and "communist-inspired powers" who he says have terrorized the black population into submission and seek the overthrow of the government. He sees the United Democratic Front as an internal wing of the outlawed African National Congress that is carrying out the ANC's campaign to make the townships "no go" areas where "enemy personnel" are under constant threat.

Botha and his ministers see the solution as eliminating this threat first so that ordinary township residents can resume normal life and the process of negotiation and reform can begin anew. But people in the black communities are surprised to hear the government's claims that the emergency is designed to protect them.

"This emergency will only make people angrier," said Caiphus Motsoeneng, a glass-factory worker who lives in the township of KwaThema east of Johannesburg. He says he worries most about his 8-year-old son, who has yet to learn the alphabet because his school has been closed by protests for all but two months this year.

Like many others here, Motsoeneng does not blame the radicals, but rather the government, which he says refuses to lift apartheid. Until it does, he says, the war will go on.

Giliomee believes the government does not hear the voices of ordinary blacks like Motsoeneng. He draws an analogy to the shah, whose regime misunderstood the mood in the mosques of Iran. Pretoria relies on police informers for most of its information from the townships, said Giliomee, and those informers tend to reinforce the conspiracy theories South Africa's leaders cling to.

Most observers believe that the emergency will create at least a brief lull. Pretoria should take advantage of that lull, Giliomee argues, to make the kind of bold, good-will gesture that the shah was incapable of making. But the government appears unwilling to make any concessions under pressure for fear of looking weak. What is more likely, observers say, is that tranquility will be temporary and that the spiral of violence and reaction will begin again soon.