For the past few weeks Spanish secret police and military intelligence have been attempting to identify and infiltrate Latin American rightist groups using Spain as a haven for funding and controlling terrorists elsewhere, including the United States.

Informed sources said that CIA and FBI agents are engaged in similar efforts, fearing that eventual changes in U.S. policy toward leftist Cuba and rightist Chile may turn American diplomats and businessmen into targets for Latin American rightwing terrorists.

While neither U.S. nor Spanish officials will speak about the activities of DINA, the Chilean secret police, in Spain, it is estimated that 30 agents of the Chilean dictatorship are based here and operate throughout Western Europe. An official source said he "presumed" that the agents are controlled by the Chilean military mission here.

Headed by Col. Oscar Coddou, the mission represents the Chilean regime of Gen. Augusto Pinochet not just in Spain but in Western Europe. Chilean officials deny that DINA exists in Europe but leftist exiles here have received telephoned and written threats. Most of them have left Spain because they felt unsafe.

Spain's watch on Latin American rightists - particularly Cubans and Chileans - is a reversal of policy. The degree of collaboration between the United States and Spain could not be determined. It is clear, however, that the Spanish government considers Latin American and European rightwing extremists a threat to Spain's democratization program and to the lives of its leaders.

Throughout the long Francisco Franco anti-Communist dictatorship, now being dismantled by King Juan Carlos and Premier Adolfo Suarez, Latin American rightists and European fascists and Nazis were welcome. But their recent involvement with Spanish rightists attempting to subvert the government's move toward democracy has sparked a major reassessment.

Following a recent shakeup in the police and military hierarchy, foreign right-wing extremists have been arrested and many deported. Among them were Argentines and anti-Castro Cubans who served as police informers in Franco's days, sources said.

Spanish officials are reluctant to provide figures on the number of Latin Americans residing in Spain, but it is estimated that there are 50,000 Argentines, 7,000 Cubans, and 3,000 Chileans.

About 70 per cent of the Chileans are students with government grants. Among the rest are many of the businessmen and professionals who filed to Spain after the election of the late Marxist President Salvador Allende. A few established large and successful enterprises here and are loath to return to Chile, where the business climate is not propitious. But analysts allege that they may be financing Chilean rightists abroad.

A Chilean leftist said, "There are about 100 of us in Spain - mostly writers, painters, film people." They mingle with Spanish leftists, go to cafes like the Gijon, eat in "tascas," the old Madrid restaurants favored by intellectuals, and try to promote the playing and recording of Chilean folk music.

Spanish officials refuse to characterize "political exiles" as such, claiming that Spain does not grant "political asylum." Spain lets most Latin Americans with a valid passport enter Spain without a tourist visa. After three months they must either register with police or leave.

It is the registration process that frightens leftists. "We have to answer all sorts of questions, and we don't know who ends up with the information," said a Chilean writer. "A year and a half ago police tried to deport a Chilean novelist for not reason at all. He was considered dangerous, that's all."

Argentines flocked to Madrid following the fall of Gen. Juan Domingo Peron in 1956. The number increased when Peron took up residence in Madrid. Many of them - including Peron's financial adviser, Jorge Antonio - invested heavily in Spain and profited from the Spanish boom.

Others found jobs and became a part of the Spanish community. A few, however, became involved in the country's fiercely anti-Communist rightist politics and ended up in such police-protected groups of the Warriors of Christ of King.

Spain has also become the haven of rich South Americans who fear kidnaping by leftists. The most prominent such exile is Mario Hirsch, head of the Argentine Bunge-Borne Corp. one of the world's biggest grain companies.

The exodus from Cuba to Madrid, as to Miami, began when Fidel Castro came to power in 1959. Jobs were hard to get, but U.S. companies were quick to hire Cubans. American executives who had lived and worked in Cuba helped to finance the Spanish capital's Centro Cubano, which still serves a fine frozen daiquiri.

Prodded by the United States, however, Spain required visas of Cubans. Many underwent security checks. According to Spanish sources, the reports went to the CIA station in the U.S. embassy. A Spanish psychiatrist recalled seeing Cuban exiles undergoing lie detector tests in the psychiatric ward of a Madrid hospital in the 1960s.

"This was at the height of America's anti-Castro phobia," said a Spanish official. "Madrid was a center of operations against Castro. There was one plane a week from Havana, and there was ship traffic. I don't know the degree of collaboration between the CIA and Spanish police, but it was big."

Castro agents were active here at the time, and the Cuban embassy in Madrid is still considered a center of espionage and agitation by many Spaniards. The Cubans move not only among leftist exiles, but among Eastern Europeans, Africans and Arabs. The Cuban consulate here was bombed once, and so was the Cuban airlines office. A Spanish right-wing organization claimed responsibility for an explosion in the Cuban embassy in Lisbon in 1975.

At one time there were thousands of Cuban exiles in Spain but the number has dwindled. Between 1972 and 1976, 35,830 wen t to the United States, 8,466 as immigrants, the remainder on parole. At the moment 2,800 Cubans are awaiting replies to applications to enter the United States. Many were sponsored either by the Catholic Committee for European Migration or by the International Rescue Committee.

SPaniards were not altogether anti-Castro, however. Post-Castro Cuban performers had successful tours. The latest Cuban making a hit in Madrid sings "ballads" to Castro's revolution with titles such as "the 10-million-ton sugar harvest."

A box-office success for months was a political play by leftist Argentine dramatists. Latin American journalists are adding a measure of controversy to the new crop of Spanish magazines and newspapers. Latin American leftists are also active in cafe theaters, centers of protest. The Chilean embassy counters with a booklet trying to show that Pinochet loves his family and children.

"It's better than being in Miami, which is a rightist Cuban city," said a Brazilian photographer. "There's no money here but at least there is the excitement of seeing the end of a dictatorship - if they don't change their minds and throw you out."