Four years after masterminding the peaceful takeover of the Spanish Sahara, King Hassan II's often shaky throne is again threatened by the ensuing desert war that refuses to go away.

Paradoxically, the recent miltary conduct of the war is vaguely encourging. Rarely has it been so in the past, since the king has managed to rally all party support behind the throne as never before and obtain Saudi Arabian financing of the fighting.

Yet even the recent military improvement -- largely the result of the intervention of new French-built Mirage F1 aircraft -- does not change the basic evaluation that Morocco is caught in a long, sapping conflict. So, too, are the Polisario guerrillas, a regional independence group, and their military financial and diplomatic backers: Algeria and Libya.

Even the palace mood has changed. Gone is the pretension that the Sahara problem was solved when Spain, in Gen. Francisco Franco's dying days, agreed to give the California-sized territory to Mauritania and Morocco in November 1976.

The high economic cost of the subsequent war with Polisario guerrillas for control of the Western Sahara forced Amuritania to renounce its claim to the southern third of the territory in August and sign a peace treaty with the Polisario. As a result, Morocco annexed Mauritania's portion and its forces have been stretched thinner than ever in trying to maintain control of the territory.

"Yes the situation is grave, serious," a palace insider remarked recently, "but we are not in a trap."

Other observers are not so sanguine, even if they do not necessarily share a leading Socialist opposition leader's private complaint that "everything is unraveling, everything is falling to pieces. The king is wasting valuable time."

The Socialists, still politically suspect because of earlier plotting against the king, have tended to support rightist opposition demands for "hot pursuit" of Polisario guerrillas inside Algeria, a people's militia to fight alongside the regular armed forces and annexationist land claims.

Diminishing options confront the king, who at 50 remains very much the same man who wrote his political science dissertation on Machiavelli and repeatedly has proved to be one of contemporary history's great survivors.

Politically, opposition parties find the king too moderate in his prosecution of the war. They are pushing him toward a potential conflict with Algeria, which he neither wants nor stands much chance of winning.

He is still too traumatized by military involvement in assassination attempts in 1971 and 1972 -- plus an Algerian-backed abortive coup in 1973 -- to appoint a defense minister or chief of staff. Most domestic and foreign analysts believe such a move is necessary to improve the armed forces' performance.

Diplomatically, analysts say, Morocco has mishandled massively the defense of its claims to the disputed Western Sahara. It has argued that its case was self-evident and required no explanation, much less justification. i

Such hubris has cost Morocco dearly and allowed the Polisario and its Algerian backers to make diplomatic gains.

At last count 35 countries have recognized the Polisario's political arm, the Saharan Arab Democratic Republic. The king's refusal to attend the Organization of African Unity summit in Monrovia, Liberia, last July was crucial in providing the Polisario with another easy victory. The king's fellow chiefs of state were irked that he had not bothered to appear -- and showed it by voting in support of self-determination for the Western Sahara.

Equally self-defeating is the government information policy which for years denied any problems -- much less fighting in the Western Sahara. The Polisario has this attitude to its advantage. From the start of the war in early 1976 it has issued frequent, if often exaggerated, reports and has taken foreign journalists on its hit-and-run operations in the Sahara.

Only in January, when Polisario troops attacked Tan Tan, a city in southern Morocco, did the government begin admitting there was a war on. Nevertheless, access to Moroccan troops is all but impossible.

Even when Morocco earlier this month announced a major victory over the Polisario at Smara -- the king's Mirages chewed up major guerrilla units as they fled from the holy city they claimed to have captured -- 11 days passed before the press was flown down for a brief two-hour visit. No detailed questions were answered, and while diplomats and journalists visited the airfield and adjoining town and barracks, they did not see the battlefield several miles away.

Economically, Morocco is beset with common Third World Problems that have nothing to do directly with the war. However, pessimists are convinced it is only a matter of time before Moroccans start blaming their plight on a war costing about $1.5 million a day.

A rash of strikes last spring gave the king a whiff of what could be in store in the coming months.

The government is paying the price for overly ambitious development budgets -- based on post-1973 sky-high phosphate prices -- that were not scaled back after the market skidded for Morocco's biggest export. Inflation is running at more than 20 percent annually while the population grows at more than 3 percent a year. Unemployment is rampant.

A mediocre harvest means importing 2 million tons of wheat worth $400 million. Tourism receipts are down. Oil, which before 1973 accounted for 4 percent of Morocco's imports, now eats up 25 percent of the total and exceeds phosphate export revenues.

Foreign investment is sluggish. Economists predict two or three more MOROCCO, From A10> years of enforced government restraint before Morocco gets out from under a 22 percent debt ratio.

Relieving the gloom, however, are large loans underwritten by the Saudis -- the exact amount is not public but is believed to involve several hundred million dollars -- and a $250 million undertaking by European banks.

Saudi largesse, interrupted last year apparently to signal displeasure with Hassan's spirited defense of the Camp David accords, thus has been restorted. But the price is toeing the line on the Arab boycott of Egypt.

Diplomats are convinced that the king can do no more than accept limited Egyptian arms shipments despite his temptation both to break his encirclement and please the United States.

Domestically, the king outwardly appears to be basking in the warm embrace of 20 million nationalist Moroccans who, with the exception of a minority within the university student union, seem to support him solidly.

But the slight improvement in Morocco's military posture is not deemed sufficient to provide anything approaching a military victory for the king.

Indeed his greatest nightmare is said to be the possibility of a border strike led by some frustrated and disgruntled colonel against either Tindouf, the southern, Algerian base area for the Polisario, or the Mediterranean port of Arzew, where much of Algeria's high-technology industry is located.

Not only would the Algerian riposte almost certainly be massive -- and potentially catastrophic, since Algeria's armed forces vastly outgun Morocco's -- but the king would probably lose whatever remaining international credit he still enjoys.

The king has sought to control any such temptation to the "right of hot pursuit" by first announcing he could not rule out such a possibility, then trying to make sure no such action could take place. After the Polisario's Tan Tan raid, the king sidestepped the issue by naming a Higher Defense Council, which included members of all major political parties, but not a single military man.

Morocco's diplomatic failures, and the Polisario's concomitant successes, have made it all but impossible to envisage an honorable way out for any of the interested parties, diplomats say. It is too late for the king to peaceably accept Saharan self-determination. And in light of the wide recognition of the Polisario, it would seem equally difficult for the organization's Algerian backers to sell the Polisario down the river.

President Carter justified his controversial decision to provide new weapons to Morocco on grounds that the king would thus be encouraged to enter negotiations. So far, however, mediation efforts by Saudi Arabia's King Khalid, Tunisian President Habib Bourguiba and Yasser Arafat of the Palestine Liberation Organization have proved fruitless, and diplomats here hold out little hope of any quick negotiated settlement.

The king seemed to pine his hopes for an end of the war on a change of Algerian heart after Chadli Benjedid replaced Houari Boumediene as president of Algeria in January. But there has been no visible change in the Algerian policy of supporting the Polisario.

The main problem for Morocco is how to make Algeria and Libya feel the cost of escalating Polisario attacks. That is no easy task with the king's desire to avoid a wider war, Libya's willingness to supply the Polisario from its bulging Soviet-supplied arsenal and Algeria and Libya both able to use their oil to dissuade meaningful third-party diplomacy.

So far the king has made do with strings and mirrors. He moved boldly to prevent a Polisario takeover of Mauritania's third of the Western Sahara in early August after Mauritania pulled out of the conflict.

Similarly, improved air and ground coordination in a series of major battles since August has restored public faith in the armed forces and crystalized anti-Algerian nationalism after the Polisario got the best of several clashes.

On the other hand, all parties to the dispute now seem trapped. Neither Algerians nor Moroccans can back down, and as an opposition politician said, "The history of the Moroccan monarchy is linked to defense of our territory."

"We will succeed if the war becomes unpopular in Algeria before it does in Morocco" he added. But even if the king were overthrown for losing the Sahara, many observers -- including, it is said, some Polisario leaders -- are convinced an even more nationalistic government would take his place and continue the war.